kempnerforge.checkpoint.layout

Where a checkpoint keeps its DCP shards, and whether a step directory holds a whole save.

A save without pipeline parallelism writes one DCP index, .metadata, at the top of its step_N directory: the flat layout. A save under pipeline parallelism writes one per stage, in pp0/ to pp{n-1}/, and records n as pp in the checkpoint’s metadata.json. A checkpoint saved before that record existed shows its degree only by the stage directories it holds.

Beside the shards, a save’s rank 0 writes train_state.pt and then metadata.json. is_complete_checkpoint says whether a step directory holds every file of a save, by the rule auto-resume applies, so code that reads a run’s checkpoints while the run saves them agrees with it.

Functions

is_complete_checkpoint(ckpt_dir[, ...])

Whether ckpt_dir holds a complete save, by the rule auto-resume applies.

saved_layout(ckpt_dir[, meta])

How ckpt_dir lays out its DCP shards, None when it holds no DCP index.

stage_dir(ckpt_dir, stage)

The directory that holds pipeline stage stage's DCP shards in ckpt_dir.

stage_dirs(ckpt_dir, layout)

The stage directories of layout in ckpt_dir, in stage order.

Classes

SavedLayout

How a checkpoint lays out its DCP shards.

class kempnerforge.checkpoint.layout.SavedLayout[source]

Bases: object

How a checkpoint lays out its DCP shards.

stages is 0 for the flat layout, and otherwise the number of pipeline stages, each with its own DCP index in pp{k}/. recorded is whether the checkpoint’s metadata.json records stages; when it does not, stages is the number of stage directories the checkpoint holds.

stages: int
recorded: bool = False
property flat: bool

Whether the checkpoint was saved without pipeline parallelism.

describe()[source]

The layout in words, as the errors that refuse it name it.

Return type:

str

__init__(stages, recorded=False)
Parameters:
Return type:

None

kempnerforge.checkpoint.layout.stage_dir(ckpt_dir, stage)[source]

The directory that holds pipeline stage stage’s DCP shards in ckpt_dir.

Parameters:
Return type:

Path

kempnerforge.checkpoint.layout.saved_layout(ckpt_dir, meta=None)[source]

How ckpt_dir lays out its DCP shards, None when it holds no DCP index.

The degree metadata.json records comes first. Without one, a .metadata at the top makes the layout flat, and pp{k}/ directories make it a stage layout of as many stages, whether or not each holds its index yet. A record of something other than a positive integer is logged and counts as none.

Parameters:
  • ckpt_dir (Path) – The checkpoint directory.

  • meta (Mapping[str, Any] | None) – The JSON object the checkpoint’s metadata.json holds, when the caller has read it. Without it the file is read here, and one that cannot be read is logged and counts as recording no degree.

Return type:

SavedLayout | None

kempnerforge.checkpoint.layout.stage_dirs(ckpt_dir, layout)[source]

The stage directories of layout in ckpt_dir, in stage order.

These are pp0/ to pp{n-1}/ when metadata.json records n, whether or not each exists, and otherwise the pp{k}/ directories ckpt_dir holds.

Parameters:
Return type:

list[Path]

kempnerforge.checkpoint.layout.is_complete_checkpoint(ckpt_dir, num_stages=None, *, require_train_state=True)[source]

Whether ckpt_dir holds a complete save, by the rule auto-resume applies.

resolve_resume_path picks a checkpoint this accepts whenever there is one, and so does the fallback of an exact resume in CheckpointManager.load. Code that reads a run’s checkpoints while the run saves them, such as a process that evaluates each saved step, calls it to count the same steps as saved.

A save is complete when every DCP index of its layout is there (see saved_layout): .metadata at the top of a checkpoint saved without pipeline parallelism, and in each of pp0/ to pp{n-1}/ of one that records its pipeline degree n. A stage checkpoint saved before the degree was recorded needs an index in every pp{k}/ it holds and, with num_stages, in pp0/ to pp{num_stages - 1}/, since a stage that stopped before its save began leaves no directory.

With require_train_state, a checkpoint whose shards hold optimizer state, or that has a train_state.pt, also needs train_state.pt and metadata.json. A save’s rank 0 writes the two, metadata.json after train_state.pt: after every stage’s shards when the save is synchronous, and while they flush when it is asynchronous, so either may land first. A checkpoint with neither optimizer state nor train_state.pt, such as an hf-to-dcp import, needs neither file. To tell which it is, the DCP index is read only when train_state.pt is absent; reading it unpickles it, as a load does. An index that cannot be read is logged and counted as holding no optimizer state, which leaves the load to report it.

With require_train_state and num_stages, a process that polls a live run gets a step only once its save has written every file, except a save made before the optimizer’s first step: its shards hold no optimizer state, so, like an import, it counts once its DCP indexes are there, before rank 0 writes train_state.pt and metadata.json. It then drops out while train_state.pt is there without metadata.json, and counts again once metadata.json lands, so a poller counts a step the first time this accepts it. Without num_stages, a pipeline-parallel save can count early too: it records its degree only in metadata.json, which can land after the first stage’s index, so it counts once the stage directories that exist hold their indexes, before a later stage has created its own.

Parameters:
  • ckpt_dir (str | PathLike[str]) – A step_N directory, or a link to one such as latest.

  • num_stages (int | None) – The pipeline degree of the run that reads the checkpoint, or None. It counts only for a stage checkpoint that records no degree, as a live save does until its metadata.json lands: one that records it is complete at its own degree, and a load refuses a degree the run cannot load.

  • require_train_state (bool) – True for a resume, which restores the step, LR schedule and data position from train_state.pt; False for a reader of the weights alone, such as scripts/eval.py, which needs only the DCP shards.

Returns:

Whether the save is complete; False for a path that is no directory, or one that holds no DCP index.

Return type:

bool