Skip to content

[checkpoint] Save and load torch_checkpointing checkpoints under a fake process group - #6446

Open
fegin wants to merge 1 commit into
fegin/stack/44from
fegin/stack/48
Open

fegin wants to merge 1 commit into
fegin/stack/44from
fegin/stack/48

Conversation

@fegin

@fegin fegin commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


[checkpoint] Save and load torch_checkpointing checkpoints under a fake process group

Summary (human-written)

It is useful to support fake pg to measure the storage performance and memory usage with single GPU. This PR enables this feature for torch_checkpointing.

Why

A fake process group (--comm.backend fake or real_pp_fake_spmd) lets one GPU run as one rank of a large job to test memory and speed. TorchCheckpointingManager cannot save under it. torch_checkpointing reads its rank info from the default process group, so it expects the fake world size: the save barrier waits for peers that do not exist, and metadata all_gather moves no data.

Example. NGPU=4 with the fake backend, logical rank 2, interval=1:

Before: manager.save(curr_step=1)
  -> ValueError: FakeStore ... is unusable as a distributed barrier
After:  <folder>/checkpoint/rank_2/step-1/ is a one-rank checkpoint of rank 2's shards
        manager.load() restores the same values

What changed

  • Under a fake default process group, every backend CheckpointManager config (main, HF load, last-step save) gets a one-rank RankInfo. Each process saves its own shards as an independent one-rank checkpoint. This uses CheckpointManager.Config.rank_info, which the pinned torch_checkpointing commit already has.
  • In fake mode the checkpoint folder gets a rank_<global rank> subfolder. Under real_pp_fake_spmd several real processes share the folder, and each has a distinct logical rank (pp_rank * NGPU / PP), so their paths do not collide.
  • Under a fake process group, a native (non-HF) initial_load_path raises ValueError. With a one-rank RankInfo, the direct-read path picks the file from RankInfo.global_rank, so a checkpoint from a real multi-rank job would silently load rank 0's shards. HF loads still work: the resharder takes the target shard from dist.get_rank(). Resuming from the process's own rank_<r> folder also still works.
  • Retention runs on every process under a fake process group. BaseCheckpointManager._should_purge only purges on dist.get_rank() == 0, which is the logical rank, so every other process would keep all of its checkpoints. TorchCheckpointingManager overrides _should_purge, so the shared base is unchanged.
  • README.md: describe the rank_<r>/step-N layout, the retention, and the native initial_load_path ban.
  • The HF consolidation callback calls dist.barrier(). Under a fake group this returns at once, so no change is needed there.

Alternatives considered

  • Special-case the fake backend inside torch_checkpointing: reviewers asked to keep the override in the caller.
  • A new config field: the fake backend is already known from the process group, so a knob adds nothing.

Numerics

No change. The code only runs under a fake process group.

Testing

  • New test_fake_process_group_round_trip_through_a_real_backend: under a fake group of world size 4 at rank 2, saves steps 1-3 with the real async backend and keep_latest_k=2, checks that rank_2/ holds only step-2 and step-3, and loads the values back into a fresh model. Without this commit, the save fails at the barrier. Without the _should_purge override, step-1 is kept. It also checks that a native initial_load_path raises ValueError.
  • pytest tests/unit_tests/cpu/components/checkpointer with the pinned torch_checkpointing: 133 passed.

Known issues / follow-ups

  • A native checkpoint from a real multi-rank job cannot be loaded under a fake process group. Supporting it needs torch_checkpointing to pick the direct-read file from the process group rank.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Nov 30, 2026
fegin added a commit that referenced this pull request Nov 30, 2026
…ke process group

## Summary (human-written)
It is useful to support fake pg to measure the storage performance and memory usage with single GPU. This PR enables this feature for torch_checkpointing.

## Why
A fake process group (`--comm.backend fake` or `real_pp_fake_spmd`) lets one GPU run as one rank of a large job to test memory and speed. `TorchCheckpointingManager` cannot save under it. torch_checkpointing reads its rank info from the default process group, so it expects the fake world size: the save barrier waits for peers that do not exist, and metadata all_gather moves no data.

**Example.** `NGPU=4` with the fake backend, logical rank 2, `interval=1`:

```
Before: manager.save(curr_step=1)
  -> ValueError: FakeStore ... is unusable as a distributed barrier
After:  <folder>/checkpoint/rank_2/step-1/ is a one-rank checkpoint of rank 2's shards
        manager.load() restores the same values
```

## What changed
- Under a fake default process group, every backend `CheckpointManager` config (main, HF load, last-step save) gets a one-rank `RankInfo`. Each process saves its own shards as an independent one-rank checkpoint. This uses `CheckpointManager.Config.rank_info`, which the pinned torch_checkpointing commit already has.
- In fake mode the checkpoint folder gets a `rank_<global rank>` subfolder. Under `real_pp_fake_spmd` several real processes share the folder, and each has a distinct logical rank (`pp_rank * NGPU / PP`), so their paths do not collide.
- Under a fake process group, a native (non-HF) `initial_load_path` raises `ValueError`. With a one-rank `RankInfo`, the direct-read path picks the file from `RankInfo.global_rank`, so a checkpoint from a real multi-rank job would silently load rank 0's shards. HF loads still work: the resharder takes the target shard from `dist.get_rank()`. Resuming from the process's own `rank_<r>` folder also still works.
- The HF consolidation callback calls `dist.barrier()`. Under a fake group this returns at once, so no change is needed there.

## Alternatives considered
- Special-case the fake backend inside torch_checkpointing: reviewers asked to keep the override in the caller.
- A new config field: the fake backend is already known from the process group, so a knob adds nothing.

## Numerics
No change. The code only runs under a fake process group.

## Testing
- New `test_fake_process_group_round_trip_through_a_real_backend`: under a fake group of world size 4 at rank 2, saves with the real async backend, checks the `rank_2/step-1` folder, and loads the values back into a fresh model. Without this commit, the save fails at the barrier. It also checks that a native `initial_load_path` raises `ValueError`.
- `pytest tests/unit_tests/cpu/components/checkpointer` with the pinned torch_checkpointing: 133 passed.

## Known issues / follow-ups
- A native checkpoint from a real multi-rank job cannot be loaded under a fake process group. Supporting it needs torch_checkpointing to pick the direct-read file from the process group rank.

stack-info: PR: #6446, branch: fegin/stack/48
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 30, 2026
@github-actions
github-actions Bot requested a review from ivy-zhou November 30, 2026 21:56
@fegin
fegin requested a review from aelavender November 30, 2026 21:58
@fegin

fegin commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@claude reviews this PR

@claude

claude Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 3m 20s —— View job


Approve -- please address the items before merging (fake-PG processes with logical rank > 0 never purge old checkpoints): review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve -- please address the items below before merging -- the approach is small and in the right layer, but retention silently stops for every fake-PG process whose logical rank is not 0.

What this PR does. When the default process group is fake, every torch_checkpointing backend config gets a one-rank RankInfo. Each process then saves its own shards as an independent one-rank checkpoint under <folder>/rank_<logical rank>/, so it never waits on barriers or all_gathers whose peers don't exist. A native (non-HF) initial_load_path is rejected, because the one-rank RankInfo would read rank 0's shards.

Example. NGPU=4, --comm.backend real_pp_fake_spmd, PP=2, so the physical ranks 0 and 1 are logical ranks 0 and 2. interval=1, default keep_latest_k=10:

Before: save(step=1) on either process -> ValueError: FakeStore ... unusable as a distributed barrier
After:  checkpoint/rank_0/step-1, checkpoint/rank_2/step-1; load() restores each process's own shards
        After step 20: rank_0 keeps 10 steps, but rank_2 keeps all 20 (retention is gated on get_rank()==0)

Must fix

  1. torchtitan/components/checkpointer/torch_checkpointing.py:308 -- retention only runs on logical rank 0, so every other fake-PG process keeps all of its checkpoints. The inline comment has a drop-in _should_purge override.

Should consider

  • torchtitan/components/checkpointer/README.md -- add one line on the fake-PG layout (<folder>/rank_<r>/step-N, plus the ban on native initial_load_path), so users know where checkpoints land and why resuming from a real job's folder finds nothing.

Questions

  • Did a native initial_load_path work under a fake group before this PR? Without a rank_info override, the backend's RankInfo comes from the fake group (rank 2 of 4), so the direct-read path would pick rank 2's file. If the load path has no collectives that need real peers, this PR turns a working load into a ValueError. Keeping the default RankInfo for the initial_load_path load config, and passing the one-rank one only to save and resume configs, could keep it working. That load is also a natural part of "measure storage performance", the stated goal. If it never worked on main, the ban is fine.
Details
  • Gate. The problem is real and stated with a concrete failure. torch_checkpointing reads rank info from the default PG, and the fix uses the existing CheckpointManager.Config.rank_info field rather than a new knob or a backend special case. Probing dist.get_backend() == "fake" matches existing usage (distributed/pipeline_parallel.py:111, models/deepseek_v4/model.py:130). The DCP manager (dcp.py) has no fake-PG handling to reuse.
  • Folder uniqueness. Under real_pp_fake_spmd, init_fake_mode(logical_world_size, rank=physical_rank * spmd_world_size) (distributed/utils.py:394-396) makes the default group fake, with a distinct logical rank per process. So rank_<r> does not collide, and _is_fake_process_group() is true there as well.
  • Purge. _purge_stale_checkpoints (base.py:724) gates on _should_purge(), which requires dist.get_rank() == 0. Under a fake PG, dist.get_rank() returns the logical rank, so logical rank 0 purges only rank_0/.
  • HF last-step export. Each process consolidates only its own shards into rank_<r>/exports/step-N. Under fake comms the values are meaningless anyway, so I'm not flagging it.
  • Overlap search. I searched "fake process group checkpoint" and "torch_checkpointing". Related open PRs are #6179 (base of this stack), #6197, #6374 and #5167. None of them touches the fake-PG path.

Not verified: the description's claims about torch_checkpointing internals (that the direct-read path picks its file from RankInfo.global_rank, and that the HF resharder uses dist.get_rank()). The torch_checkpointing source wasn't available in this runner.

A maintainer still needs to review.

# Each process writes a one-rank checkpoint, so it needs its own
# folder. The fake rank is unique per process, also under
# real-PP/fake-SPMD, where each PP process has a distinct logical rank.
self.folder = filesystem.join(self.folder, f"rank_{dist.get_rank()}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, each process is the only writer of its own rank_<r> folder. But retention still runs only on dist.get_rank() == 0 (BaseCheckpointManager._should_purge, base.py:653-659). Under a fake group, dist.get_rank() is the logical rank. So a process at logical rank 2 (the PR's own example), any real_pp_fake_spmd process with physical rank > 0, or fake with FAKE_PP_RANK > 0 never purges. With the default keep_latest_k = 10, its folder silently keeps every checkpoint, so a long storage benchmark eventually fills the disk. The test misses this because it uses keep_latest_k=0.

Smallest fix, local to this manager so the shared base stays unchanged:

    def _should_purge(self) -> bool:
        # Under a fake process group each process is the only writer of its
        # own rank_<r> folder, so each one runs its own retention.
        if _is_fake_process_group():
            return self.keep_latest_k > 0 and self._storage.isdir(self.folder)
        return super()._should_purge()

To guard it cheaply, the existing fake-PG test could use keep_latest_k=1, save steps 1 and 2, and assert that only rank_2/step-2 remains.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed. TorchCheckpointingManager._should_purge now lets every process purge its own rank_<r> folder under a fake process group, and keeps the base behavior otherwise. The test now uses keep_latest_k=2, saves steps 1-3 as rank 2, and checks that only step-2 and step-3 remain. It fails without the override.

…ke process group

## Summary (human-written)
It is useful to support fake pg to measure the storage performance and memory usage with single GPU. This PR enables this feature for torch_checkpointing.

## Why
A fake process group (`--comm.backend fake` or `real_pp_fake_spmd`) lets one GPU run as one rank of a large job to test memory and speed. `TorchCheckpointingManager` cannot save under it. torch_checkpointing reads its rank info from the default process group, so it expects the fake world size: the save barrier waits for peers that do not exist, and metadata all_gather moves no data.

**Example.** `NGPU=4` with the fake backend, logical rank 2, `interval=1`:

```
Before: manager.save(curr_step=1)
  -> ValueError: FakeStore ... is unusable as a distributed barrier
After:  <folder>/checkpoint/rank_2/step-1/ is a one-rank checkpoint of rank 2's shards
        manager.load() restores the same values
```

## What changed
- Under a fake default process group, every backend `CheckpointManager` config (main, HF load, last-step save) gets a one-rank `RankInfo`. Each process saves its own shards as an independent one-rank checkpoint. This uses `CheckpointManager.Config.rank_info`, which the pinned torch_checkpointing commit already has.
- In fake mode the checkpoint folder gets a `rank_<global rank>` subfolder. Under `real_pp_fake_spmd` several real processes share the folder, and each has a distinct logical rank (`pp_rank * NGPU / PP`), so their paths do not collide.
- Under a fake process group, a native (non-HF) `initial_load_path` raises `ValueError`. With a one-rank `RankInfo`, the direct-read path picks the file from `RankInfo.global_rank`, so a checkpoint from a real multi-rank job would silently load rank 0's shards. HF loads still work: the resharder takes the target shard from `dist.get_rank()`. Resuming from the process's own `rank_<r>` folder also still works.
- Retention runs on every process under a fake process group. `BaseCheckpointManager._should_purge` only purges on `dist.get_rank() == 0`, which is the logical rank, so every other process would keep all of its checkpoints. `TorchCheckpointingManager` overrides `_should_purge`, so the shared base is unchanged.
- `README.md`: describe the `rank_<r>/step-N` layout, the retention, and the native `initial_load_path` ban.
- The HF consolidation callback calls `dist.barrier()`. Under a fake group this returns at once, so no change is needed there.

## Alternatives considered
- Special-case the fake backend inside torch_checkpointing: reviewers asked to keep the override in the caller.
- A new config field: the fake backend is already known from the process group, so a knob adds nothing.

## Numerics
No change. The code only runs under a fake process group.

## Testing
- New `test_fake_process_group_round_trip_through_a_real_backend`: under a fake group of world size 4 at rank 2, saves steps 1-3 with the real async backend and `keep_latest_k=2`, checks that `rank_2/` holds only `step-2` and `step-3`, and loads the values back into a fresh model. Without this commit, the save fails at the barrier. Without the `_should_purge` override, `step-1` is kept. It also checks that a native `initial_load_path` raises `ValueError`.
- `pytest tests/unit_tests/cpu/components/checkpointer` with the pinned torch_checkpointing: 133 passed.

## Known issues / follow-ups
- A native checkpoint from a real multi-rank job cannot be loaded under a fake process group. Supporting it needs torch_checkpointing to pick the direct-read file from the process group rank.

stack-info: PR: #6446, branch: fegin/stack/48
@fegin
fegin marked this pull request as draft November 30, 2026 22:27
@fegin
fegin changed the base branch from fegin/stack/44 to main November 30, 2026 22:27
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 30, 2026
@fegin
fegin changed the base branch from main to fegin/stack/44 November 30, 2026 22:27
@fegin
fegin marked this pull request as ready for review November 30, 2026 22:27
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 30, 2026
@fegin

fegin commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

README: added a short "Fake process groups with TorchCheckpointingManager" section (layout, retention, the native initial_load_path ban).

On the initial_load_path question: we do not need it. Under a fake process group no real communication happens, so loading a real job's checkpoint gives no useful values, and the ban is intended. HF loads still work.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants