kempnerforge.checkpoint.async_save

Async checkpointing for non-blocking saves.

Uses dcp.async_save() to snapshot state to CPU and write to disk in the background, returning control to the training loop once the snapshot is taken.

Modes:
  • disabled: Synchronous save (the default; simple, for debugging).

  • async: Standard async via dcp.async_save(); the snapshot goes to pageable CPU memory.

  • async_with_pinned_mem: async, with the snapshot copied into pinned CPU memory by a DefaultStager that serves every save on torch 2.13 and later (torch 2.12 gets a new one per save). The first save pins a copy of the rank’s state; later saves refill the copy of every tensor whose storage is unchanged (parameters and optimizer state updated in place), so they stall less. The stager keeps a copy while its source storage lives, until close() or until the checkpointer is collected; a save still being written keeps its snapshot until the write ends. Without a CUDA device the mode saves as async.

Classes

AsyncCheckpointer

Non-blocking checkpoint saver.

class kempnerforge.checkpoint.async_save.AsyncCheckpointer[source]

Bases: object

Non-blocking checkpoint saver.

Wraps dcp.async_save() and manages the background save future. Each new save waits for the previous async save to complete first. Not thread-safe: call it from one thread, as the training loop does.

Parameters:

mode – Checkpoint mode (disabled/async/async_with_pinned_mem). The pinned mode stages through pinned memory only where a CUDA device is available, and saves as async elsewhere, with one warning.

__init__(mode=AsyncCheckpointMode.disabled)[source]
Parameters:

mode (AsyncCheckpointMode)

Return type:

None

save(state_dict, checkpoint_id, process_group=None, planner=None)[source]

Save distributed state, potentially asynchronously.

Parameters:
  • state_dict (dict) – DCP-compatible state dict (model + optimizer).

  • checkpoint_id (str) – Checkpoint directory path.

  • process_group – Process group for DCP. Required for PP where each stage has a different state dict — pass a group scoped to ranks within the same PP stage. None uses the default global group.

  • planner (SavePlanner | None) – DCP save planner. None calls DCP as before, with its default planner.

Return type:

None

close()[source]

Wait for any pending save, then release the pinned copies the stager holds.

The stager is released and the pending save dropped even when that save failed, whose error still propagates. A later save in the pinned mode builds a new stager.

Return type:

None

wait()[source]

Block until any pending async save completes.

Return type:

None

property is_pending: bool

Whether an async save was started and wait() has not drained it yet.

property is_flushing: bool

Whether the pending save’s background write is still running; never blocks.

False with nothing pending, and once the write has ended, whether it succeeded or failed: wait() then returns, or raises the write’s error, at once.