kempnerforge.training.entry¶
Library entry point for a KempnerForge training job.
scripts/train.py is a thin CLI wrapper over run_training(): it
parses argv, loads the config, and calls in. Each phase below is a
separate function so an experiment can reuse the scaffold — or swap one
phase — without editing the shared script.
Functions
|
Create the checkpoint manager, scoping DCP to the PP stage when needed. |
|
Build the model with the full parallelism stack applied. |
|
Auto-resume from |
|
Run a full training job: build every phase, run the loop, tear down. |
- kempnerforge.training.entry.build_model(config, runtime, loss_fn)[source]¶
Build the model with the full parallelism stack applied.
With
distributed.pp > 1this builds this rank’s pipeline stage plus its schedule (which is whyloss_fnis needed here); otherwise it delegates tobuild_parallel_modeland returns no pipeline. A stage is wrapped with FSDP2’sreshard_after_forward=False: the schedule sends every microbatch of a step through it, and its parameters stay gathered after each forward, so the next forward or backward through it does not all-gather them again.- Parameters:
config (JobConfig)
runtime (RuntimeContext)
loss_fn (Callable[[torch.Tensor, torch.Tensor], torch.Tensor])
- Return type:
tuple[torch.nn.Module, PipelineBundle | None]
- kempnerforge.training.entry.build_checkpoint_manager(config, runtime, model, optimizer, pipeline)[source]¶
Create the checkpoint manager, scoping DCP to the PP stage when needed.
With PP, each stage has different parameters — DCP needs a group scoped to ranks within the same PP stage (all non-PP mesh dimensions), and each stage saves DCP shards to its own subdirectory to avoid file collisions. The manager records the pipeline degree with every save and compares it on load. It records each rank’s data-parallel coordinates, the partition
build_data_pipelinereads, with the per-rank loader/RNG state.- Parameters:
config (JobConfig)
runtime (RuntimeContext)
model (torch.nn.Module)
optimizer (torch.optim.Optimizer)
pipeline (PipelineBundle | None)
- Return type:
- kempnerforge.training.entry.restore_checkpoint(config, model, scheduler, ckpt_mgr)[source]¶
Auto-resume from
latest(orcheckpoint.load_path).Returns
(step, tokens_seen)—(0, 0)when there is nothing to resume from. Also restores the metrics run ids, runs the MoT text warm start when no checkpoint supplied the model weights and the run starts at step 0, and applies the effective freeze at the step it returns, a fresh run’s step 0 and theFreezeStagethere included. A module that a freeze stage unfreezes gets its saved optimizer state back, or empty state when the saved optimizer had none for it.A checkpoint in
checkpoint.dirtakes precedence overcheckpoint.load_path, so a requeued warm start resumes rather than starting over; rank 0 then warns thatload_pathis not used, naming both paths, unlessload_pathnames the resumed checkpoint and excludes nothing.Only that auto-resume restores the metrics run ids. A
load_pathwarm start is a new run: each enabled tracking backend opens a run of its own unlessmetrics.mlflow_run_idormetrics.wandb_run_idnames one, and with MLflow on, rank 0 tags the new run with the checkpoint and the MLflow run that saved it (_tag_warm_start_run).A checkpoint saved with another
pluginslist is refused before any state is read (seeCheckpointManager.load’splugins_expected). A VLM run, unless it warm starts from the weights alone, compares the freeze the checkpoint records with its own at the step the checkpoint records, for the checkpointloadsettles on (see itsvlm_freeze_expected).- Parameters:
config (JobConfig)
model (torch.nn.Module)
scheduler (object)
ckpt_mgr (CheckpointManager)
- Return type:
- kempnerforge.training.entry.run_training(config, *, step_fn=None, hooks=None, data_fn=None)[source]¶
Run a full training job: build every phase, run the loop, tear down.
step_fnandhookslet an experiment own the step body or register hooks, anddata_fnits data pipeline, without copying the build phases. All default to whatscripts/train.pyuses;data_fnis called asbuild_data_pipelineis, after the model is built, so it receives the model’stokens_per_imageon a packed or multi-image VLM run, and must follow theDataFncontract. A customstep_fnmust run its forward and loss undersession.autocast(), which casts fp32 master weights to the compute dtype when no FSDP2 wrap does (one GPU, a pp-only mesh).The shutdown handler stays registered, and its forced-exit timer armed, until the process group is destroyed, so a second signal cannot cut the emergency checkpoint’s flush or the teardown short. On the exception path nothing retires it, so the timer still bounds the exit. The timer’s timeouts come from
resolve_shutdown_timeouts, which fits aSIGTERM’s to the launch script’s signal lead unlesstrain.shutdown_timeout_secis set; they are resolved before the process group starts, so a malformed lead fails first.- Parameters:
config (JobConfig)
step_fn (Callable[[TrainingSession, int], StepResult] | None)
hooks (HookRunner | None)
data_fn (DataFn | None)
- Return type:
None