Skip to content

Merge final_linear shards when loading a Megatron checkpoint on fewer TP ranks - #8946

Open
udsy19 wants to merge 1 commit into
deepspeedai:masterfrom
udsy19:fix/merge-final-linear
Open

udsy19 wants to merge 1 commit into
deepspeedai:masterfrom
udsy19:fix/merge-final-linear

Conversation

@udsy19

@udsy19 udsy19 commented Oct 29, 2026

Copy link
Copy Markdown

Problem

MegatronSDLoader.split_state_dict splits the vocab-parallel final_linear.weight (GPT-NeoX output layer) along dim 0 when a checkpoint is loaded on more TP ranks than it was saved with. merge_state_dict has no matching branch, so when a checkpoint is loaded on fewer TP ranks the key falls through to else: new_client_sd[key] = value_list[0] and only the first shard is kept.

Example: final_linear.weight of shape [32, 8] split to TP=2 gives two [16, 8] shards (correct); merging them back to TP=1 returns [16, 8] instead of [32, 8], so the model's load_state_dict fails with a size mismatch. The asymmetry dates back to #1899, which added final_linear to the split side only.

Fix

Add final_linear.weight to the dim-0 concatenation branch of merge_state_dict, mirroring the split side.

Tests

New tests/unit/checkpoint/test_state_dict_factory.py: builds a Megatron-style checkpoint, splits it to TP=2 with SDLoaderFactory, merges the shards back to TP=1, and asserts every key equals the original tensor.

DS_BUILD_OPS=0 pytest tests/unit/checkpoint/test_state_dict_factory.py
  • main: fails with AssertionError: final_linear.weight
  • this PR: passes

Ran on macOS (Apple Silicon) CPU only, no accelerator, torch 2.15.0, Python 3.13. pre-commit passes on the changed files.

Scope / validation

  • Pure state-dict manipulation; no numerics, performance or memory impact.
  • Covers whole-model mp_rank_* checkpoints. The PipelineModule per-layer path is not changed (there, sanity_check(ckpt_list[0]) already rejects a layer file that only holds final_linear, a separate issue).
  • Not validated on GPU, or with ZeRO / offload / quantized (quantize=True) loading. The quantize path is unaffected, since the quantizer only applies to mlp.dense_h_to_4h.weight in this branch.

Duplicate check

Searched open PRs and issues for final_linear, state_dict_factory, MegatronSDLoader, merge_state_dict: no existing work. No open PR touches state_dict_factory.py.

AI assistance was used to prepare this change; I reviewed the diff and ran the tests above.

… TP ranks

MegatronSDLoader.split_state_dict splits final_linear.weight (the
vocab-parallel output layer in GPT-NeoX checkpoints) along dim 0, but
merge_state_dict had no matching branch. That sent final_linear.weight to
the fallback, which keeps only the first shard. Loading a TP=2 checkpoint
on TP=1 therefore produced a [vocab/2, hidden] tensor where [vocab, hidden]
was expected.

Concatenate final_linear.weight along dim 0 when merging, the same way
split_state_dict divides it.

Signed-off-by: Udaya Tejas <udayatejas2004@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-29T17:18:25.050294Z
We couldn’t complete this review. Comment @codex review to try again.
59c4748 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant