skip to content

A 512-GPU PyTorch run saved with torch.distributed.checkpoint must resume on 256 GPUs after half the cluster goes down — how does the load work, and what else must change?

level: seniorimportance: should knowfreq 9%

answer

  1. the checkpoint is not tied to 512
  2. .metadata maps every saved chunk
  3. build and shard the model first
  4. each rank reads only its overlap
  5. batch, data position and RNG do not reshard

basics

~20 s

Distributed Checkpoint reshards at load time: build and shard the model for 256 ranks, call dcp.load, and each rank reads only the saved chunks overlapping its new shards. Global batch, data position and per-rank RNG state are yours to adjust.

solid answer

~50 s

In PyTorch 2.14, `dcp.save` writes each rank's local shards to `.distcp` files plus a `.metadata` file that records every tensor's global shape and which file holds which chunk. Loading is in place: on the 256 ranks you first build the model and optimizer and shard them for the new layout, take `get_state_dict(model, optimizer)` (or wrap both in a `Stateful` object), call `dcp.load(state_dict, checkpoint_id=...)`, then `set_state_dict`. The load planner intersects each rank's new shards with the saved chunks, so every rank reads just the bytes it now owns — that is load-time resharding. What does not reshard by itself: the global batch halves unless you double gradient accumulation, the data position saved per rank must be recomputed from a global sample count, per-rank RNG states no longer map one-to-one, and each rank now holds twice the shard, which may not fit.

code

python · 22 lines
python
import torch.distributed.checkpoint as dcp
from torch.distributed.checkpoint.state_dict import get_state_dict, set_state_dict
from torch.distributed.checkpoint.stateful import Stateful

class AppState(Stateful):
    def __init__(self, model, optimizer, step=0):
        self.model, self.optimizer, self.step = model, optimizer, step

    def state_dict(self):
        model_sd, optim_sd = get_state_dict(self.model, self.optimizer)
        return {"model": model_sd, "optim": optim_sd, "step": self.step}

    def load_state_dict(self, state_dict):
        set_state_dict(self.model, self.optimizer,
                       model_state_dict=state_dict["model"],
                       optim_state_dict=state_dict["optim"])
        self.step = state_dict["step"]

# model and optimizer are already built and sharded for the 256-rank layout
app = AppState(model, optimizer)
dcp.load({"app": app}, checkpoint_id="/ckpt/pretrain/step_041000")
global_samples = app.step * 2048  # derive each rank's data position from this

go deeper

for a junior

Know that Distributed Checkpoint saves each rank's shard plus a metadata file, and that the checkpoint can be loaded on a different number of GPUs.

for a middle

Explain the load sequence: build and shard the model, get_state_dict, dcp.load in place, set_state_dict, and why every rank must pass the same keys.

for a senior

List what DCP does not reshard — global batch, data position, RNG state, per-rank memory — and how you recompute each from a global step count.

for a principal

Decide whether a run should resume shrunk or wait for capacity, weighing slower steps and changed batch arithmetic against idle time.

## What a DCP checkpoint contains **Distributed Checkpoint** (`torch.distributed.checkpoint`, imported as `dcp`) saves a model whose tensors are spread across ranks — sharded `DTensor`s from FSDP2 or tensor parallelism, or replicated tensors under DDP. Unlike `torch.save`, it does not gather anything to one rank: - every rank writes its **own local shards** to one or more `.distcp` files in the checkpoint directory; - the coordinator (rank 0) writes a **`.metadata`** file last, after all ranks have finished, describing each tensor's global shape and the chunks (offset and size) stored in each file; - the PyTorch 2.14 docs give **no backward-compatibility guarantee** for saved state dicts across PyTorch versions, so a resume should run the release that saved it or convert first. Because the metadata describes tensors by global coordinates, a checkpoint is not tied to the 512 ranks that wrote it. The DCP docs describe this as **load-time resharding**: save in one cluster topology, load into another. ## How the 512-rank save was written The save side is symmetric. Every N steps each of the 512 ranks calls `dcp.save({"app": app_state}, checkpoint_id=step_dir)` with the same keys; the ranks agree on a plan, write their own shards in parallel, and rank 0 commits the `.metadata` file once all writes are reported. No rank ever holds the full model, which is what makes a 512-rank save of a model too large for one GPU possible at all. ## How the 256-rank load works 1. **Build the model and optimizer on the new layout first.** `dcp.load` works **in place**: all tensors must already be allocated on their destination device and sharded the way the 256-rank job will run. 2. **Collect the target state dict.** `get_state_dict(model, optimizer)` from `torch.distributed.checkpoint.state_dict` returns model and optimizer state with fully qualified parameter names, independent of the wrapper. Wrapping both in an object that implements the `Stateful` protocol lets DCP call `state_dict()` and `load_state_dict()` for you. 3. **Load.** `dcp.load({"app": app_state}, checkpoint_id=path)` reads `.metadata`, and the load planner works out, for every rank, which saved chunks overlap the shards that rank now owns. Each rank reads only those byte ranges. 4. **Apply.** `set_state_dict(model, optimizer, model_state_dict=..., optim_state_dict=...)` copies the loaded values into the live objects. Every rank must pass the same keys; the docs warn that mismatched keys can hang or error. ## What does not reshard by itself DCP restores tensors. The run's *meaning* is still 512-rank-shaped: - **Global batch.** With the same per-GPU micro-batch, 256 ranks process half the samples per step. Doubling gradient accumulation keeps the global batch, and with it the optimizer state's assumptions, unchanged. - **Data position.** A rank-aware sampler's offset or a per-rank data-loader cursor was computed for 512 ranks. Save a **global** sample or step count instead, and derive each rank's position from it after the restart. - **Random state.** Per-rank RNG states (dropout, augmentation) were saved for 512 ranks; there is no right one-to-one mapping onto 256, so re-seed from the global step and the new rank. - **Memory per rank.** Each rank now holds twice as much of every sharded tensor. A layout that fitted at 512 may not at 256 without a different sharding or more offload. - **Throughput and schedule.** Steps take roughly twice as long; wall-clock-based logic such as time-based checkpoint triggers or warm-up measured in hours needs to follow steps, not time. ## Checking the directory before you trust it | file | written by | meaning when missing | |---|---|---| | `.metadata` | rank 0, last | the save did not finish; use the previous checkpoint | | `__<n>_<m>.distcp` | each writer | data for some ranks is absent; the load fails | Pick the newest step directory that **has** a `.metadata` file. A directory with all the data files but no metadata is an interrupted save, not a checkpoint. ## A worked example The run trains on 64 nodes x 8 GPUs with a micro-batch of 4 and accumulation 1: a global batch of 512 x 4 = 2,048 sequences per step. A rack fails and only 32 nodes remain. On 256 ranks with the same micro-batch, one accumulation step gives 1,024 sequences, so accumulation goes to 2 to keep 2,048. The checkpoint stored step 41,000; samples consumed so far are 41,000 x 2,048 = 83,968,000, and each of the 256 ranks recomputes its position in the shuffled order from that number. If the full cluster comes back, the same checkpoint loads onto 512 ranks again with accumulation back at 1.

  • Why must the model be built and sharded before calling dcp.load rather than after?
    DCP loads in place: it reads saved chunks directly into the tensors you pass, and it uses their current shard layout to decide which byte ranges each rank needs. Without allocated, already-sharded tensors there is no target layout to reshard into.
  • How do you hand a DCP checkpoint to a tool that expects one torch.save file?
    PyTorch 2.14 ships `dcp_to_torch_save` in `torch.distributed.checkpoint.format_utils`, which reads a DCP directory offline and writes a single `torch.save` file (and `torch_save_to_dcp` for the reverse). The docs recommend running it on a single rank, because it materialises the whole model in host memory.

  • A sharded checkpoint can only be loaded by the same number of ranks that wrote it.
  • dcp.load allocates the model's tensors from the checkpoint, so build order does not matter.
  • Resuming on half the GPUs keeps the global batch size the same automatically.
  • A step directory with every .distcp file but no .metadata is a usable checkpoint.
  • A DCP checkpoint is guaranteed to load in any later PyTorch release.
  • Rank 0 must gather the full model before a resumable checkpoint can be written.

Assumes PyTorch 2.14's torch.distributed.checkpoint (save, load, get_state_dict and set_state_dict, the Stateful protocol); DCP gives no cross-version compatibility guarantee for saved state dicts.