Repository navigation
fix: per-rank checkpoint names for TP/DP (#165) - #188
Merged
Merged
Conversation
save_ckpt named files by pipeline stage only, so with tp > 1 the TP
ranks overwrote each other's shards, and all DP replicas wrote and
pruned the same files. save_lora_ckpt already named TP shards but let
every DP replica write. Following save_hf_ckpt, only DP rank 0 writes
and save_ckpt/load_ckpt add a _tp{rank} suffix when tp > 1 (PP-only
names unchanged). The checkpoint directory no longer becomes
checkpoint_None_<stage> without a postfix (the "or" fallback never
triggered).
Adds tests/test_ckpt_names.py.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
save_ckptandsave_lora_ckptwrote checkpoints from every rank to names that depend on the pipeline stage only (#165). With tp > 1, the TP ranks of a stage wrote different shards to one file (one shard lost, reload loads wrong weights). With dp > 1, every replica wrote and pruned the same files (FileNotFoundError race onos.remove). Withoutckpt_dir_postfixthe directory wascheckpoint_None_<stage>, because theorfallback on a non-empty f-string never triggers.save_hf_ckptalready did this right: only DP rank 0 writes, files are named per TP rank.Changes
All in
opt_prime/opt_prime/opti_pri.py, followingsave_hf_ckpt(rank = (pp*dp + dp_i)*tp + tp_i, sodp_i = (rank // tp) % dp,tp_i = rank % tp):__init__(:465):checkpoint_{postfix}_{stage}only when a postfix is given, otherwisecheckpoint_{stage}.save_ckpt: returns on DP ranks other than 0; the file gets a_tp{tp_i}suffix when tp > 1, and pruning of old files globs only this TP rank's files.load_ckpt: reads the same name (all DP replicas load DP rank 0's file for their TP rank).save_lora_ckpt: returns on DP ranks other than 0 (it already named TP shards;load_lora_ckptis unchanged).opt_prime/tests/test_ckpt_names.py: four ranks of tp=2, dp=2 built without a process group (Optimus_p.__new__, just the attributes the save/load paths read), each with different weights. It checks that exactly one file per TP shard exists, holding DP rank 0's weights, and that DP rank 1 reloads them, for bothsave_ckptandsave_lora_ckpt. It also checks that PP-only names are unchanged, and the directory name with and without a postfix (runs__init__with a fakeCommup to the point where it picks the GPU).Verification
Environment: Python 3.10, torch 2.10.0+cu128, transformers 5.1.0, CPU only. Base: optimus-rm @253be20.
3 failed, 2 passed.save_ckptproduced onlyckpt_step_1_epoch_0.pt, not one file per TP shard.save_lora_ckpt, the reloaded TP-rank-1 weights were DP rank 1's (the last writer), not DP rank 0's.checkpoint_None_0.5 passed.Full suite (
cd opt_prime && TORCH_DEVICE_BACKEND_AUTOLOAD=0 CUDA_VISIBLE_DEVICES=1 PYTHONPATH=$PWD python3 -m pytest -q tests):400 passed, 23 skipped, 135 subtests passed405 passed, 23 skipped, 135 subtests passedCompatibility and limits
checkpoint_<stage>instead ofcheckpoint_None_<stage>. To resume from an old run, rename that directory or pass the old name's postfix.save_hf_ckpt/merge_hf_ckpt.pyalready assume.Related Issue
Closes #165
🤖 Generated with Claude Code