Skip to content

Sort checkpoint shard files by numeric rank - #8452

Closed
ebarkhordar wants to merge 1 commit into
deepspeedai:masterfrom
ebarkhordar:fix/1381-ckpt-shard-natural-sort
Closed

ebarkhordar wants to merge 1 commit into
deepspeedai:masterfrom
ebarkhordar:fix/1381-ckpt-shard-natural-sort

Conversation

@ebarkhordar

Copy link
Copy Markdown
Contributor

get_rank_repr pads the rank field in a shard file name to two digits, and the readers that glob those names sort them as plain strings before indexing the result by model-parallel rank. Past a parallel degree of 100 the pad overflows and the order stops matching the index: model_100 sorts between model_10 and model_11, so rank 11 loads rank 100's shard. Nothing raises and the shapes still match; the weights are just wrong. That is the failure this issue predicted.

A globbed shard list has to be ordered by numeric rank, because MegatronSDLoader indexes it positionally and get_merge_state_dicts/get_split_state_dict slice contiguous rank ranges out of it. Two call sites feed that loader, PipelineModule.ckpt_layer_path_list and DeepSpeedEngine._get_all_ckpt_names, both added in the same commit and both affected. They now sort with natural_keys, which get_checkpoint_files in zero_to_fp32.py and _get_checkpoint_files in ds_to_universal.py already use for globbed checkpoint files; it moves to checkpoint/utils.py so ds_to_universal.py can drop its copy, while zero_to_fp32.py keeps its own because it ships standalone.

Widening the pad would rename shards on disk and break existing checkpoints. Sorting on the read side leaves the format alone: the two orders agree whenever every rank field is two digits or fewer, so nothing written so far is reordered. I left out the pad-overflow assert suggested in the issue, since the read path no longer depends on the pad width.

This only bites at a tensor or model parallel degree of 100 or more, which is rare and is probably why it has sat since 2021. The failure is silent, though, and the fix is a sort key.

Verified in a CPU container:

  • Three tests in tests/unit/checkpoint/test_ckpt_file_ordering.py. Reverting the module.py sort key on its own fails the pipeline test (assert 100 == 11), and reverting the engine.py one on its own fails the engine test. The third pins the sub-100 order and is green either way, so I reordered the list in place to confirm it can fail.
  • unit/checkpoint/ and unit/runtime/pipe/: the same 10 failures and 154 errors as unmodified master, plus the three new passes. Those are the GPU and multi-process tests this box cannot run.
  • pre-commit on the changed files: clean.

I have not run a real 100-way parallel job. The repro drives the actual naming, listing and loader index over 128 shards in a single process.

Fixes #1381

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bc7b134f58

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -0,0 +1,93 @@
# Copyright (c) Microsoft Corporation.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required Signed-off-by trailer

This is a non-merge commit, but its commit message has no Signed-off-by trailer, so it violates the repository's mandatory commit requirement and may fail the corresponding CI/DCO check. Recreate the commit using --signoff with the configured Git identity.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

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.

The trailer is there. The commit message ends Signed-off-by: Ehsan Barkhordar <realbarkhordar@gmail.com>, and the DCO check on this same commit is green.

@ebarkhordar
ebarkhordar force-pushed the fix/1381-ckpt-shard-natural-sort branch from bc7b134 to 1fc3023 Compare September 12, 2026 12:20
@ebarkhordar

Copy link
Copy Markdown
Contributor Author

Five of the six workflow runs on this head are still waiting on the fork approval gate (Formatting, cpu-torch-latest, nv-pre-compile-ops, python, DCO / required), so modal-torch-latest is the only one that has executed. Could someone approve the run?

The sort fix is verified locally against a 128-rank layout, where master hands rank 11 the shard named model_100. The repo's own matrix hasn't seen it yet.

Shard file names pad the rank field to two digits, and both readers that
glob those names sort them as plain strings before indexing the result by
model-parallel rank. Past a parallel degree of 100 the pad overflows,
model_100 sorts between model_10 and model_11, and the index no longer
identifies the shard it names.

Sort with natural_keys instead, which get_checkpoint_files in
zero_to_fp32.py and _get_checkpoint_files in ds_to_universal.py already
use for globbed checkpoint files. The two orders agree whenever every
rank field is two digits or fewer, so existing checkpoints keep loading
and the on-disk name format is untouched.

Signed-off-by: Ehsan Barkhordar <realbarkhordar@gmail.com>
@ebarkhordar
ebarkhordar force-pushed the fix/1381-ckpt-shard-natural-sort branch from 1fc3023 to 82c991f Compare September 24, 2026 08:20
@ebarkhordar

Copy link
Copy Markdown
Contributor Author

Closing this one. A month without a review, and the five required checks here have never run on it: Formatting, python, cpu-torch-latest, nv-pre-compile-ops and DCO / required have all sat at action_required since this opened, so the only leg that reports anything is modal-torch-latest. There is no green suite for anyone to judge it against.

The part worth keeping is the shape of the bug rather than the patch. topology.py pads the rank to exactly two digits, module.py:604 sorts those filenames as plain strings, and state_dict_factory.py then indexes that list positionally, so once the tensor-parallel degree reaches 100 the pad overflows and model_100 sorts between model_10 and model_11. A rank then loads another rank's shard, quietly, with nothing to tell you it happened. tests/unit/checkpoint/test_ckpt_file_ordering.py on this branch pins that case.

Say the word and I will reopen.

@ebarkhordar ebarkhordar closed this Oct 8, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] String sort based on rank_repr may lead to problems?

1 participant