Fix Kimi-k2 checkpoint conversion#3528
Merged
copybara-service[bot] merged 1 commit intomainfrom Apr 6, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
🤖 Hi @gagika, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
0239f6b to
2ff39d3
Compare
There was a problem hiding this comment.
This PR addresses a KeyError during Kimi-k2 checkpoint conversion by gracefully handling keys that are present in the checkpoint but not in the MaxText mapping. This change improves robustness for model variants with unexpected weight names or metadata.
🔍 General Feedback
- The use of
.get(key)and checking forNoneis a standard defensive pattern for weight mapping scripts. - This fix aligns the
unscannedscript with the main DeepSeek conversion script which already includes this pattern.
src/maxtext/checkpoint_conversion/standalone_scripts/convert_deepseek_family_unscanned_ckpt.py
Show resolved
Hide resolved
Contributor
|
LGTM! Tested on Kimi-K2-1t-Instruct-0905 model on |
shuningjin
approved these changes
Apr 6, 2026
RissyRan
reviewed
Apr 6, 2026
| mapped_key = ds_ckpt.hf_to_maxtext_mapping( | ||
| layer, num_experts, first_num_dense_layers, base_num_decoder_layers | ||
| ).get(key) | ||
| if mapped_key: |
Collaborator
There was a problem hiding this comment.
Could we add else branch for debugging in the future?
if mapped_key:
chkpt_vars[mapped_key] = f.get_tensor(key)
else:
# This catches keys that are allowed but missing from the mapping dictionary
print(f"[DEBUG] Key allowed but no mapping found: {key}")
RissyRan
approved these changes
Apr 6, 2026
Collaborator
RissyRan
left a comment
There was a problem hiding this comment.
One nit comment, LGTM!
2ff39d3 to
fb99a79
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.
Description
This PR updates the DeepSeek checkpoint conversion script to safely ignore HuggingFace weights that lack a corresponding MaxText mapping, preventing crashes during conversion.
Context & Problem: The conversion script was failing with
KeyError: 'model.layers.0.self_attn.rotary_emb.inv_freq'. Because MaxText computes certain weights (like RoPE frequencies) on the fly, they are intentionally excluded from the HF-to-MaxText mapping dictionary. The previous strict dictionary indexing crashed when it encountered these expected missing keys.Implementation & Solution:
.get(key)so unmapped weights returnNoneinstead of throwing an exception.if mapped_key:block to safely bypass weights that don't need conversion.This solution makes the conversion script more robust by gracefully handling dynamically computed weights and any unexpected extra variables in the checkpoint.
Tests
convert_deepseek_family_unscanned_ckpt.pyon the target Kimi-k2 HuggingFace checkpoint.rotary_emb.inv_freqwithout throwing aKeyError.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.