[kimi k2_7] add kimi k2_7 - #3532
Conversation
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """MoonViT3d Vision Encoder for Kimi K2.5. |
There was a problem hiding this comment.
this one has vision encoder - is it literally kimi 2.6?
There was a problem hiding this comment.
yes, this is k2.5 which share the same architecture of k2.6, you can use this to load kimi k2.6 weights.
601d1b6 to
0ac24c5
Compare
tianyu-l
left a comment
There was a problem hiding this comment.
quality seems very high, left some minor comments
| "<|media_pad|>": 2016, | ||
| "<|media_begin|>": 2017, | ||
| "<|media_end|>": 2018, | ||
| "[PAD]": 2019 |
There was a problem hiding this comment.
add a comment on for which model they are introduced
Would be good to backfill the info for previous tokens (e.g. Qwen 3.5, RL, etc.)
There was a problem hiding this comment.
agreed, but met a mechanical issue that this json file is loaded by a strict parser and does not allow inline comments starting with // and i tested it locally.
| "gpt_oss_pp+fsdp+ep+sacop", | ||
| ngpu=8, | ||
| ), | ||
| # Integration Test Cases for Kimi K2.5 |
There was a problem hiding this comment.
If K2.7 is sharing the same code, we should use the latest naming.
There was a problem hiding this comment.
kimi 2.5, 2.6, 2.7 all share the same code, updated to kimi 2.7 to be latest. module wise, we still stick to kimi 2_5, matching the convention for kimi models on huggingface.
| spatial_merge_size: int | ||
| """Spatially merge visual tokens after encoder. e.g. 2 means 2x2=4 patches merged.""" | ||
|
|
||
| patch_order: str = "block" |
There was a problem hiding this comment.
type should be a Literal?
There was a problem hiding this comment.
indeed, otherwise will accept invalid strings not in "block" or "raster".
| ``spatial_merge_size**2`` group contiguous, or ``"raster"`` (row-major). | ||
| Must be ``"block"`` when ``build_mrope_positions`` is set.""" | ||
|
|
||
| resize_fn: Callable[..., tuple[int, int, int, int]] = resize_to_pixel_budget |
There was a problem hiding this comment.
this may not be tyro-friendly, do we need to do some extra annotation to suppress warning / failure?
There was a problem hiding this comment.
nice catch. used Annotated to suppress warning.
| "moonlight-16B-A3B": _moonlight_16b_a3b, | ||
| "Kimi-VL-A3B": _kimi_vl_a3b, | ||
| "1T-A32B": _1t_a32b, |
There was a problem hiding this comment.
names are confusing, is there a pattern?
There was a problem hiding this comment.
i was following the naming in https://huggingface.co/moonshotai/collections. The reasoning is that i found we can reuse the code for kimi 2.5 to config the other two kimi models, moonlight-16b-a3b, and kimi-vl-a3b. i keep them because it provides users with more model choices if the 1t kimi 2.5-2.7 is too big for them.
There was a problem hiding this comment.
where is 32 coming from in "A32B" ? I see "A3B" from "Kimi-VL-A3B" and "Moonlight-A3B"
I was thinking about 3 models from https://huggingface.co/collections/moonshotai/kimi-k25
- moonshotai/Kimi-K2.5
- moonshotai/Kimi-K2.6
- moonshotai/Kimi-K2.7-Code
There was a problem hiding this comment.
it's from the model summary, section 2 table: https://huggingface.co/moonshotai/Kimi-K2.7-Code
There was a problem hiding this comment.
maybe changing to Kimi-K2_5 for the last one? more direct, and K2.6, K2.7-code share the same code.
There was a problem hiding this comment.
it's from the model summary, section 2 table: https://huggingface.co/moonshotai/Kimi-K2.7-Code
do you mean 32B activated parameters? is there a convention to use activated parameters for model name?
There was a problem hiding this comment.
yeah, like for qwen moe series: https://huggingface.co/collections/Qwen/qwen35, and Kimi-VL-A3B, meaning activated parameter number is 3b. now i am inclined to using Kimi-K2_5 to reduce confusion, since this is the name for the model.
| NOTE: the passed-in model should preferably be on meta device; otherwise it | ||
| must fit in GPU or CPU memory. | ||
| """ | ||
| if parallelism.spmd_backend == "full_dtensor": |
There was a problem hiding this comment.
Is spmd_types backend supported? If possible, that should be the only backend we support onwards. Also fine to wait for #3895
There was a problem hiding this comment.
no. i would suggest doing this in a batch with qwen 3_5 and other models.
There was a problem hiding this comment.
I published a PR for review to unblock myself from benchmarking kimi k2.7 #4063
| if parallel_dims.cp_enabled: | ||
| raise NotImplementedError( | ||
| "Context Parallel is not yet supported for Kimi K2.5: vision scatter " | ||
| "needs the full sequence before CP would shard it." | ||
| ) |
There was a problem hiding this comment.
do we have any plans to support cp for vlms soon?
| nparams_shared_experts = 0 | ||
| nparams_experts = 0 | ||
| nparams_dense = 0 | ||
| # TODO: add a per-batch vision encoder FLOP term for accurate VLM MFU. |
There was a problem hiding this comment.
oh good point, didn't realize we were computing it wrong
4d71750 to
e79df70
Compare
| attn_backend="flex", | ||
| converters=[ | ||
| Float8LinearConverter.Config( | ||
| filter_fqns=["output", "router.gate"], |
There was a problem hiding this comment.
do you mean "lm_head" as the fqn ?
There was a problem hiding this comment.
nice catch. i copied from dsv3, it looks like dsv3, llama3 both had the same bug.
There was a problem hiding this comment.
made a pr to fix this for llama3 and dsv3 first: #4008. this is from a shared decoder refactor.
There was a problem hiding this comment.
i removed this quantization for kimi_k2_5 after checking the technical report, where FP8-E4M3 is only used to compress selected saved activations, not for GEMMs.
| if self.vision_encoder is None: | ||
| return super().to_hf(state_dict) | ||
|
|
||
| to_hf_map = {v: k for k, v in self.vision_from_hf_map.items()} |
There was a problem hiding this comment.
from codex
Kimi-VL checkpoint loading still emits mm_projector.* before DCP load;
multi_modal_projector.* normalization happens afterward.
There was a problem hiding this comment.
but honestly I don't know why we import models from HF "Kimi-VL-A3B" collection
I was assuming K2.5 - 2.7 referring to https://huggingface.co/collections/moonshotai/kimi-k25 by default. but correct me if i am wrong
There was a problem hiding this comment.
these are smaller models that can reuse the kimi k2.5 code, we used a smaller model to validate numerics, and i think these smaller models are also useful for users.
There was a problem hiding this comment.
from codex
Kimi-VL checkpoint loading still emits mm_projector.* before DCP load; multi_modal_projector.* normalization happens afterward.
this is indeed a problem, updated.
There was a problem hiding this comment.
kimi 2.5, 2.6, 2.7 all share the same code, updated to kimi 2.7 to be latest. module wise, we still stick to kimi 2_5, matching the convention for kimi models on huggingface.
Module name sounds fine, but can we modify the folder name to 2.7? I feel it is more eye-catching.
| @@ -0,0 +1,61 @@ | |||
| # Kimi K2.5 | |||
There was a problem hiding this comment.
| # Kimi K2.5 | |
| # Kimi K2.7 |
b217cb5 to
aa4ac72
Compare

Summary:
Adds the Kimi K2.7 model family — a DeepSeek-V3 (MLA + sigmoid-routed MoE) decoder paired with a MoonViT3d vision encoder. It maximizes reuse of existing torchtitan code: KimiK25Model extends DeepSeekV3Model, so the decoder inherits DeepSeek-V3's model, sharding, and state-dict adapter unchanged; the vision encoder, the vision to text scatter, and the vision-side adapter keys are the new parts.
This is a long pr, hope the following the partition can help reviewers.
Part 1 — Shared primitives (foundational, small)
Part 2 — Shared data pipeline
Part 3 — Kimi model
Part 4 — Qwen (shared-refactor blast radius; confirm no regression)
Part 5 — Tests
Part 6 — Docs
Verification