kempnerforge.training.loop¶
The training step loop and the three step bodies it dispatches to.
run_training_loop owns everything that repeats: the step body, NaN
handling, optimizer/scheduler advance, freeze and phase transitions,
metrics, eval, checkpointing, and shutdown. The step body itself is a
plain callable (StepFn) chosen once by select_step_fn(), so a
model family with a different forward contract is a new function rather
than another branch inside the loop.
Functions
|
MoE auxiliary + router z-loss terms (only called when |
|
Metadata saved alongside every checkpoint so a resume is exact. |
|
|
|
PP step: collect microbatches, hand them to the schedule, broadcast loss. |
|
Run the step loop from |
|
Pick the step body for this job: PP wins, then VLM, then text-only. |
|
Standard step (no PP, text-only), with optional per-dataset mixture metrics. |
|
VLM step (no PP): pixel_values + input_ids forward, with a text-token count. |
Classes
The training dataloader plus its live iterator. |
|
What one training step reports back to the loop. |
|
Everything one training run needs after the build phase. |
- class kempnerforge.training.loop.BatchStream[source]¶
Bases:
objectThe training dataloader plus its live iterator.
Wraps the epoch-boundary restart and the “no dataloader configured” case so the step bodies don’t each re-implement them. The loader is read off the pipeline on every access, so swapping
pipeline.dataloadertakes effect on the next step instead of leaving a stale snapshot.- __init__(pipeline)[source]¶
- Parameters:
pipeline (DataPipeline)
- Return type:
None
- ensure_started()[source]¶
Materialize the iterator, if it isn’t already, for the loader in use.
next_batchdoes this itself; the loop calls it up front so building an iterator (which spawns dataloader workers) stays outside the regionMetricsTrackertimes.- Return type:
None
- reset()[source]¶
Drop the iterator so the next step takes a fresh one from the loader.
A
StatefulDataLoaderre-applies its recorded skip, so this picks up inside the current epoch rather than restarting it.- Return type:
None
- class kempnerforge.training.loop.StepResult[source]¶
Bases:
objectWhat one training step reports back to the loop.
The per-dataset fields stay empty for step bodies that don’t produce them;
text_tokensstays None, which a VLM run treats as an error rather than logging a zero.- __init__(loss, grad_norm, text_tokens=None, dataset_token_counts=<factory>, dataset_loss_sums=<factory>, dataset_loss_counts=<factory>)¶
- class kempnerforge.training.loop.TrainingSession[source]¶
Bases:
objectEverything one training run needs after the build phase.
- runtime: RuntimeContext¶
- model: torch.nn.Module¶
- optimizer: torch.optim.Optimizer¶
- loss_fn: Callable[[torch.Tensor, torch.Tensor], torch.Tensor]¶
- step_fn: Callable[[TrainingSession, int], StepResult]¶
- data: DataPipeline¶
- phases: PhaseState¶
- checkpointer: CheckpointManager¶
- tracker: MetricsTracker¶
- hooks: HookRunner¶
- nan_detector: NaNDetector¶
- shutdown_handler: ShutdownHandler¶
- pipeline: PipelineBundle | None = None¶
- property batches: BatchStream¶
The live batch iterator. Rebuilt if
datais replaced wholesale.
- __init__(config, runtime, model, optimizer, scheduler, loss_fn, step_fn, data, phases, checkpointer, tracker, hooks, nan_detector, shutdown_handler, pipeline=None, eval_dataloader=None, profiler=None)¶
- Parameters:
config (JobConfig)
runtime (RuntimeContext)
model (torch.nn.Module)
optimizer (torch.optim.Optimizer)
scheduler (Any)
loss_fn (Callable[[torch.Tensor, torch.Tensor], torch.Tensor])
step_fn (Callable[[TrainingSession, int], StepResult])
data (DataPipeline)
phases (PhaseState)
checkpointer (CheckpointManager)
tracker (MetricsTracker)
hooks (HookRunner)
nan_detector (NaNDetector)
shutdown_handler (ShutdownHandler)
pipeline (PipelineBundle | None)
eval_dataloader (Any | None)
profiler (Any | None)
- Return type:
None
- kempnerforge.training.loop.clip_grads(model, max_norm)[source]¶
- Parameters:
model (torch.nn.Module)
max_norm (float)
- Return type:
- kempnerforge.training.loop.add_moe_losses(loss, model, mc)[source]¶
MoE auxiliary + router z-loss terms (only called when
mc.is_moe).- Parameters:
loss (torch.Tensor)
model (torch.nn.Module)
mc (Any)
- Return type:
- kempnerforge.training.loop.pipeline_step(session, step)[source]¶
PP step: collect microbatches, hand them to the schedule, broadcast loss.
stepis unused — MoE (the only step-dependent branch) is rejected with PP inJobConfig.validate.- Parameters:
session (TrainingSession)
step (int)
- Return type:
- kempnerforge.training.loop.vlm_step(session, step)[source]¶
VLM step (no PP): pixel_values + input_ids forward, with a text-token count.
- Parameters:
session (TrainingSession)
step (int)
- Return type:
- kempnerforge.training.loop.text_step(session, step)[source]¶
Standard step (no PP, text-only), with optional per-dataset mixture metrics.
- Parameters:
session (TrainingSession)
step (int)
- Return type:
- kempnerforge.training.loop.select_step_fn(config)[source]¶
Pick the step body for this job: PP wins, then VLM, then text-only.
- Parameters:
config (JobConfig)
- Return type:
- kempnerforge.training.loop.checkpoint_extra(config, step, phases)[source]¶
Metadata saved alongside every checkpoint so a resume is exact.
vlm_freezereflects the post-transition state when a FreezeStage has fired (seefreeze_meta_at_step).- Parameters:
config (JobConfig)
step (int)
phases (PhaseState)
- Return type:
- kempnerforge.training.loop.run_training_loop(session, *, step=0, tokens_seen=0)[source]¶
Run the step loop from
steptotrain.max_steps.Returns the final
(step, tokens_seen). Drains any pending async checkpoint before returning so the caller can tear down the process group safely.session.datais read once and must stay fixed for the duration; only itsdataloadermay be swapped mid-run.