-
Notifications
You must be signed in to change notification settings - Fork 485
feat(model): add Kimi K3 language backbone support #5130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
741e1b8
feat(model): add Kimi K3 language backbone support
yaoyu-33 6e72a44
refactor(model): simplify Kimi K3 bridge configuration
yaoyu-33 f29bdac
Delete tests/unit_tests/examples/test_create_hf_toy_model.py
yaoyu-33 caa6c44
fix(model): restore Kimi K3 training compatibility
yaoyu-33 eb91f8b
fix(model): enable dynamic pipeline shapes for Kimi K3
yaoyu-33 d4cbc9a
docs(model): add Kimi K3 verification card
yaoyu-33 cec012e
fix(model): restore Kimi K3 A_log export padding
yaoyu-33 ae77a67
fix(model): preserve Kimi K3 multimodal export state
yaoyu-33 a5c5cc4
docs(model): record Kimi K3 import and inference verification
yaoyu-33 26aa16d
docs(model): clarify Kimi K3 export verification
yaoyu-33 d708406
[model] fix: Account for Kimi K3 KDA layers in FLOPs calculation
yaoyu-33 31f8fa1
[docs] feat: Add Kimi K3 model page and document layer indexing
yaoyu-33 901ea09
[test] fix: make AutoBridge registration manifest a subset check
yaoyu-33 b3d2872
[docs] fix: use repo-relative links for Kimi K3 source files
yaoyu-33 05fd6df
[ci, docs] fix: exclude not-yet-merged Kimi K3 paths from link-check
yaoyu-33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Kimi K3 | ||
|
|
||
| [Kimi K3](https://huggingface.co/moonshotai/Kimi-K3) is a large sparse MoE model from Moonshot AI. Megatron Bridge supports the **language backbone** of the published multimodal checkpoint through the `KimiK3Bridge`. | ||
|
|
||
| ```{note} | ||
| Support for this model is in progress. Conversion (HF → Megatron) and Megatron greedy inference are verified; strict full-checkpoint export, exact round-trip parity, and every training workflow are not. See [Known Limitations](#known-limitations) and the machine-readable [verification card](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/examples/model_verification_cards/kimi-k3/card.yaml) before relying on this path. | ||
| ``` | ||
|
|
||
| ## Supported Variants | ||
|
|
||
| Megatron Bridge supports checkpoints with the `KimiK3ForConditionalGeneration` architecture and the `kimi_k3` model type: | ||
|
|
||
| | Variant | HF Path | | ||
| |---------|---------| | ||
| | Kimi-K3 | [moonshotai/Kimi-K3](https://huggingface.co/moonshotai/Kimi-K3) | | ||
|
|
||
| Requires `transformers >= 4.56.2` and `--trust-remote-code`. | ||
|
|
||
| ## Architecture Notes | ||
|
|
||
| K3 uses a heterogeneous attention schedule rather than a single attention type: | ||
|
|
||
| - **KDA (Kimi Delta Attention)** on the layers listed in the HF config's `linear_attn_config.kda_layers` — a gated delta-rule linear-attention block with short depthwise convolutions over Q/K/V, a low-rank forget gate, and a per-head beta projection. In the published 93-layer checkpoint, 69 layers are KDA. | ||
| - **No-RoPE MLA** on the remaining layers (`full_attn_layers`, 24 layers in the published checkpoint). | ||
|
|
||
| Both layer lists hold **1-indexed** global layer numbers and together partition `1..num_hidden_layers`. | ||
|
|
||
| Other notable properties: | ||
|
|
||
| - Latent MoE (`moe_latent_size`) with shared experts, grouped GEMM, and all-to-all token dispatch. | ||
| - AttnRes residual banks with a configurable block size (`attn_res_block_size`), which the pipeline payload carries between stages. | ||
| - Published routed-expert weights are MXFP4 (`uint8`-packed E2M1 values with UE8M0 scales) and are dequantized to BF16 on import. | ||
| - The published KDA `A_log` tensors carry 96 active entries plus 32 zero-only padding entries. Import validates the padding is all-zero and removes it; export restores it. | ||
| - Export of the language backbone passes the checkpoint's `vision_tower.*` and `mm_projector.*` tensors through unchanged. | ||
|
|
||
| ## Conversion | ||
|
|
||
| ```bash | ||
| # HF → Megatron | ||
| ./scripts/conversion/convert.sh import \ | ||
| --hf-model moonshotai/Kimi-K3 \ | ||
| --megatron-path /workspace/kimi-k3 \ | ||
| --torch-dtype bfloat16 \ | ||
| --tp 2 --pp 3 --ep 8 --etp 2 \ | ||
| --distributed-timeout-minutes 180 \ | ||
| --trust-remote-code | ||
| ``` | ||
|
|
||
| The full checkpoint needs a multi-node allocation — import was validated on 48 GB200 GPUs at TP2/PP3/EP8/ETP2. | ||
|
|
||
| For a fast local iteration loop, build a truncated proxy checkpoint with | ||
| [`examples/conversion/create_hf_toy_model.py`](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/examples/conversion/create_hf_toy_model.py), which truncates the heterogeneous layer schedule and downloads only the safetensor shards the selected layers need. | ||
|
|
||
| ## Inference | ||
|
|
||
| Megatron greedy generation was validated on 24 GB300 GPUs at TP1/PP3/EP8/ETP1. See the verification card for the exact command and the recorded deterministic completion. | ||
|
|
||
| ## Training | ||
|
|
||
| No training recipe ships for K3 yet. Pretraining, SFT, and PEFT configs, checkpoint-resume validation, and performance tuning are pending. | ||
|
|
||
| ## Known Limitations | ||
|
|
||
| - Strict full-checkpoint Megatron → HF export, HF reload, and exact round-trip parity are unverified. | ||
| - Full HF/Megatron forward-logit correlation is unverified. A four-layer proxy reached cosine similarity `0.9998` and Pearson correlation `0.9998`. | ||
| - Virtual pipeline parallelism (VPP) is not supported. | ||
| - KDA layers do not support context parallelism (`CP > 1`). | ||
| - Only the language backbone is covered. Native K3 vision/video modeling and multimodal inference are not implemented; export only preserves the published vision and projector tensors unchanged. | ||
| - The model has not been performance-tuned. Reported timings are sanity checks, not optimized throughput results. | ||
|
|
||
| ## Related Implementation | ||
|
|
||
| - Bridge: [`src/megatron/bridge/models/kimi/kimi_k3_bridge.py`](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/src/megatron/bridge/models/kimi/kimi_k3_bridge.py) | ||
| - Provider: [`src/megatron/bridge/models/kimi/kimi_k3_provider.py`](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/src/megatron/bridge/models/kimi/kimi_k3_provider.py) | ||
| - Layer spec and KDA/MLA modules: [`src/megatron/bridge/models/kimi/kimi_k3_layers.py`](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/src/megatron/bridge/models/kimi/kimi_k3_layers.py) | ||
| - Verification card: [`examples/model_verification_cards/kimi-k3/card.yaml`](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/examples/model_verification_cards/kimi-k3/card.yaml) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
linear_attn_configlayer lists are filtered withlayer_number <= num_hidden_layers, while the sibling lists just above use exclusive bounds (layer_types[:num_hidden_layers],mlp_only_layerswithlayer < num_hidden_layers). This is only correct ifkda_layers/full_attn_layersare 1-indexed in the HF config (which matches thelayer_number in config.kimi_kda_layers1-indexed check inkimi_k3_layers.py). If they were 0-indexed,<=would incorrectly retain a layer at indexnum_hidden_layers. Worth a confirming comment given the mixed indexing conventions in this one function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed 1-indexed, so the inclusive bound is correct — and added a comment saying so in
31f8fa102.Checked against the pinned revision
9f62e4e9:num_hidden_layersis 93,kda_layershas 69 entries spanning 1..91,full_attn_layershas 24 entries spanning 4..93. 69 + 24 = 93 and the two partition 1..93 exactly, so there is no layer 0 and the maximum isnum_hidden_layersitself. The consumer side agrees —KimiK3Attention.__init__testslayer_number in config.kimi_kda_layersagainst MCore's 1-indexedlayer_number.