typedecide.training.trainer¶
finetune, TrainResult and the manifest writer. Needs the train extra at call time, not at import time.
typedecide.training.trainer
¶
LoRA fine-tuning on the answer token, and the manifest that makes the run traceable.
torch, transformers and peft are optional extras and are imported inside the
functions that need them, so import typedecide stays fast and a base install can still
load, validate and split data. Anything missing raises TrainingError naming the extra.
Nothing here re-implements the prompt or the masking: both come from prompt.py and
dataset.py. What this module owns is the surrounding run -- seeding, the adapter, the
schedule, and manifest.json.
TrainResult
dataclass
¶
library_versions
¶
Versions of everything that can change a weight, for the manifest.
Reported as "not installed" rather than omitted, because a manifest that is silent about a library cannot be told apart from one written before it mattered.
Source code in src/typedecide/training/trainer.py
seed_everything
¶
Seed python, numpy, torch and transformers from the one number in the config.
Called before the model is built, because LoRA's B matrices and the dataloader shuffle are both drawn from these generators.
Source code in src/typedecide/training/trainer.py
build_manifest
¶
build_manifest(
config: TrainConfig,
*,
train: Sequence[Decision],
eval_set: Sequence[Decision] | None,
answer_slots: dict[str, dict[str, int]],
started_at: float,
finished_at: float,
extra: dict[str, Any] | None = None
) -> dict[str, Any]
Everything needed to reproduce, or to disbelieve, this run.
A result you cannot trace to its input is not a result, so this records the base model, the fully resolved config, the dataset fingerprints and row counts, the seed, the library versions, wall-clock start and end, and the resolved answer-slot token ids per criterion.
Source code in src/typedecide/training/trainer.py
redacted_argv
¶
argv with anything that looks like a credential blanked out.
The manifest is written beside the adapter and travels with it -- to a model hub, a
ticket, a colleague. finetune is a library call, so sys.argv belongs to whatever
program imported us and may carry --hf-token hf_.... The command line is worth
recording; the secret on it is not.
Source code in src/typedecide/training/trainer.py
write_manifest
¶
Write manifest.json beside the adapter and return its path.
Source code in src/typedecide/training/trainer.py
finetune
¶
finetune(
train: Sequence[Decision],
config: TrainConfig,
*,
eval_set: Sequence[Decision] | None = None,
progress: Callable[[dict[str, Any]], None] | None = None
) -> TrainResult
LoRA-tune config.base_model so that one token carries all the loss.
Every example is encode(prompt) + [answer_token] with every position but the last
masked to -100; see dataset.build_example. Option order is re-randomised at the
start of each epoch when config.randomise_option_order is set, which is the single
most valuable preprocessing step here: without it the model learns "answer A".
progress, if given, receives dicts -- {"event": "preflight" | "log" |
"epoch" | "done", ...} -- so a CLI or notebook can show something without this
module owning a progress bar.
Source code in src/typedecide/training/trainer.py
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |