GradientTrainingDataset
Modality-agnostic dataset for GRADIEND gradient creation and batching.
Wraps a training dataset that provides items with 'factual' and 'alternative' inputs. Batches items, optionally pads variable-length tensors (via get_padding_value), runs gradient_creator on factual/alternative, and returns source/target tensors (factual, alternative, or diff). When cache_dir and use_cached_gradients are set, cache_key_fields must be provided: list of batch dict keys whose values are included in the cache hash (e.g. ['input_text', 'label']). All listed keys must be present in every batch when caching is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training_data
|
Any
|
Dataset with len and getitem returning dicts containing at least 'factual' and 'alternative' (modality-specific, e.g. tokenizer outputs). |
required |
gradient_creator
|
Any
|
Callable(inputs) -> gradients tensor (e.g. model.forward_pass_create_gradients). |
required |
source
|
str
|
'factual' | 'alternative' | 'diff' | None. When None (e.g. supervised_decoder), source gradients are not computed. |
'factual'
|
target
|
str
|
Same options or None. When None (e.g. supervised_encoder), target gradients are not computed. |
'diff'
|
cache_dir
|
Optional[str]
|
Optional directory for caching gradients. |
None
|
use_cached_gradients
|
bool
|
If True and cache_dir set, load/save gradients. |
True
|
cache_key_fields
|
Optional[List[str]]
|
When caching is used, list of batch keys to include in cache hash. Required when cache_dir is set and use_cached_gradients is True. All keys must exist in batch. |
None
|
dtype
|
dtype
|
Tensor dtype. |
float32
|
device
|
Optional[device]
|
Device for tensors. |
None
|
return_metadata
|
bool
|
If True, pass through batch 'metadata'. |
False
|
get_padding_value
|
Optional[Callable[[str], int]]
|
Callable(subkey: str) -> int used when batching variable-length tensors (pad_sequence). Default 0. Text uses tokenizer.pad_token_id for 'input_ids'. |
None
|
Source code in gradiend/trainer/core/dataset.py
_get_padding_value
instance-attribute
__getitem__
Source code in gradiend/trainer/core/dataset.py
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
__iter__
__len__
_exclusive_gradient_access
Serialize tokenization and base forward/backward for one gradient row.
Source code in gradiend/trainer/core/dataset.py
_merge_batch
Collect items at indices and merge into one batch; pad variable-length tensors when needed.