Conversation
Fixes hpcaitech#6160. GeminiDDP builds `name2param` from every module's own parameters, so a tied parameter (e.g. `lm_head.weight` / `transformer.wte.weight` with `tie_word_embeddings=True`) is saved under all of its names, which matches `torch.nn.Module.state_dict`. The strict check in `_load_from_state_dict`, however, builds `local_state` from `named_parameters()`, which deduplicates and only keeps the first name. Loading a checkpoint that Gemini itself just saved therefore fails with Unexpected key(s) in state_dict: "lm_head.weight" for both sharded and unsharded checkpoints. Treat every name in `name2param` as expected. The existing Gemini checkpoint tests only use models without tied weights (LLaMA, BERT for sequence classification), so add a save/load round trip with a tied-embedding GPT-2.
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.
📌 Checklist before creating the PR
[doc/gemini/tensor/...]: A concise descriptionpip install pre-commit && pre-commit install(ran the pinned hooks manually instead: black 24.10.0--line-length=120, isort 5.13.2--profile black, autoflake 2.3.1, all clean)🚨 Issue number
fixed #6160
📝 What does this PR do?
A checkpoint saved by
GeminiPlugincannot be loaded back byGeminiPluginwhen the model has tied parameters (e.g.tie_word_embeddings=True: GPT-2, Qwen2.5 base models, ...):Cause. The save and load paths of
GeminiDDPenumerate parameter names differently:_save_to_state_dict,state_dict_shard) iteratesself.name2param, which is built from every module's own parameters, so a tied parameter is written under all of its names. This matchestorch.nn.Module.state_dict._load_from_state_dictbuildslocal_statefromself.named_parameters(), which deduplicates and keeps only the first name, so the other name is reported as unexpected.Fix. Treat every name in
name2paramas expected during the strict check. The save format is unchanged.Test. The existing Gemini checkpoint tests only use models without tied weights (LLaMA, BERT for sequence classification). Added
exam_tied_weights_state_dict: a save/load round trip with a tied-embedding GPT-2, sharded and unsharded, asserting that both names are present in the state dict and that the loaded weights are equal.Results (4x H200, torch 2.5.1+cu124)
shard=FalseUnexpected key(s) in state_dict: "lm_head.weight"shard=Trueexam_tied_weights_state_dict(new)exam_state_dict,exam_state_dict_with_originwithuse_async=FalseNotes on what was not run: the
use_async=Trueparametrizations needtensornvme, which does not build on my machine. Thetp_size=2cases ofexam_state_dictadditionally need #6260 fixed on a machine without apex, so they were verified together with that fix (submitted separately).💥 Checklist before requesting a review
⭐️ Do you enjoy contributing to Colossal-AI?