[Bug Fix] Fix exported checkpoint with random embed_tokens and wrong dtype#39
Merged
[Bug Fix] Fix exported checkpoint with random embed_tokens and wrong dtype#39
Conversation
f978c3d to
d36d631
Compare
yubofredwang
previously approved these changes
Mar 11, 2026
f41e0d6 to
2d54fac
Compare
Collaborator
Author
VerificationTested on an existing FSDP checkpoint (Kimi-K2.5 Eagle3, 16-rank training). 1. FSDP checkpoint already contains correct embed_tokens: 2. Export without 3. Export with Both paths produce identical, correct results. |
a18454c to
ed56787
Compare
…wrong dtype - Include embed_tokens from FSDP checkpoint instead of skipping it (the FSDP checkpoint already contains the correct target embeddings) - Optionally override embed_tokens from target model via --target-model-path (for compatibility or verification) - Add --embedding-key option for models with non-standard embedding key - Infer dtype from checkpoint weights and cast HF model before loading to avoid bf16→fp16 precision loss - Set torch_dtype in config to match actual weight dtype Fixes #38
ed56787 to
91c82a0
Compare
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.
Summary
Root cause:
_extract_model_weights()skipped all keys containing "embed", so the HF model's randomly initializedembed_tokenswas saved bysave_pretrained. The FSDP checkpoint itself already contains the correct target embeddings (verified bytorch.equalwith the target model).Changes in
convert_to_hf.py:embed_tokensfrom FSDP checkpoint (no longer skip "embed" keys)embed_tokensfrom target model via--target-model-path(for verification or old checkpoints)--embedding-keyoption for models with non-standard embedding key (e.g.language_model.model.embed_tokensfor VLMs)load_state_dictto avoid bf16→fp16 precision losstorch_dtypein config to match actual weight dtypeNote: vocab pruning path is unaffected — it only trims
lm_head, notembed_tokens(embed needs full vocab for target token id lookup).Verified on kimi_eval_test checkpoint
Test plan
torch.equalreturnsTruebfloat16, config dtype correctFixes #38