Huggingface
DataCollatorForLSR
¶
Bases: SentenceTransformerDataCollator
, _DataCollatorForLSRAttributes
A HuggingFace DataCollator for LM-Supervised Retrieval (LSR).
This class extends SentenceTransformerDataCollator
with additional attributes and
mechanisms specific to LSR. It processes dataset features for retrieval and
language model scores used during fine-tuning tasks. The data collator interfaces
directly with a RAGSystem for retrieval and scoring operations.
Attributes:
Name | Type | Description |
---|---|---|
rag_system |
RAGSystem
|
The RAG system used for retrieval and generation. |
prompt_template |
str
|
Template for generating prompts (default: DEFAULT_PROMPT_TEMPLATE). |
target_template |
str
|
Template for generating targets (default: DEFAULT_TARGET_TEMPLATE). |
default_return_tensors |
str
|
Default tensor type (e.g., "pt"). |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rag_system
|
RAGSystem
|
Retrieval-augmented generation system. |
required |
prompt_template
|
str
|
Template string for prompts (default: system default). |
None
|
target_template
|
str
|
Template string for targets (default: system default). |
None
|
default_return_tensors
|
str
|
Tensor type for return values, defaults to "pt". |
'pt'
|
**kwargs
|
Any
|
Additional keyword arguments passed to the base class. |
{}
|
Raises:
Type | Description |
---|---|
MissingExtraError
|
If required dependencies (e.g., HuggingFace) are missing. |
FedRAGError
|
If an unsupported return_tensors type is passed. |
Source code in src/fed_rag/data_collators/huggingface/lsr.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
__call__
¶
Use the features of the dataset in order to get the retrieval and lm-scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
list[Any]
|
Should contain a 'query' and 'reponse' field. |
required |
return_tensors
|
_type_
|
supports right now only 'pt' |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: a dictionary of ~torch.Tensors with keys 'retrieval_scores' and 'lm_scores' |
dict[str, Any]
|
Note that each ('query', 'response') pair generates one fine-tuning instance for LSR. |
Source code in src/fed_rag/data_collators/huggingface/lsr.py
DataCollatorForRALT
¶
Bases: DataCollatorMixin
, BaseDataCollator
Data collator class for Retrieval-Augmented Language Tuning (RALT).
This class is responsible for processing dataset features to create proper inputs and labels for the fine-tuning of a retrieval-augmented language model. It uses an example template and a RAG (Retrieval-Augmented Generation) system to build and encode fine-tuning instances, and applies padding to align the training data.
Attributes:
Name | Type | Description |
---|---|---|
example_template(str) |
A string template used to format fine-tuning instances. |
|
default_return_tensors(str) |
The default framework type for returned tensors ('pt'). |
|
model_dtype(torch.dtype|None) |
The model's data type (e.g., torch.float32). Initialized from the generator model in the RAG system, if available. |
|
rag_system(RAGSystem|NoEncodeRAGSystem) |
An instance of a RAG system supporting the retrieval and in-context augmentation for fine-tuning. |
Source code in src/fed_rag/data_collators/huggingface/ralt.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
__init__
¶
Initialize an instance with a RAG system, example template, and optional parameters.
Validates the RAG system and ensures required dependencies are installed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rag_system
|
RAGSystem | NoEncodeRAGSystem
|
The RAG system to be used, which can be either a standard RAGSystem or a NoEncodeRAGSystem. |
required |
example_template
|
str | None
|
Template for examples. Defaults to None; if not specified, a predefined template is used. |
None
|
default_return_tensors
|
str
|
Default tensor format (e.g., "pt" for PyTorch). Defaults to "pt". |
'pt'
|
**kwargs
|
Any
|
Additional keyword arguments passed to the superclass or used during initialization. |
{}
|
Raises:
Type | Description |
---|---|
MissingExtraError
|
If required Hugging Face dependencies are missing. |
Source code in src/fed_rag/data_collators/huggingface/ralt.py
__call__
¶
Prepare input tensors for fine-tuning using RAG system features.
Converts a list of features into input tensors suitable for retrieval-augmented
language model (RALT) fine-tuning. Each ('query', 'response') pair generates
rag_system.config.top_k
fine-tuning instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
list[dict[str, Any]]
|
List of examples, each containing 'query' and 'response' fields. |
required |
return_tensors
|
str | None
|
Tensor framework to use. Only 'pt'
is supported. Defaults to None (uses |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: Dictionary of PyTorch tensors with keys: - 'input_ids': Token IDs. - 'labels': Target IDs. |
Raises:
Type | Description |
---|---|
DataCollatorError
|
If |
Note
Applies left-padding to all sequences to ensure uniform length.
Source code in src/fed_rag/data_collators/huggingface/ralt.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|