Skip to content

feat(models): port Kimi-VL / Kimi-VL 2.5 MoonViT vision encoder - #597

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-525-port-kimi-vl
Jul 1, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-525-port-kimi-vl

Conversation

@inureyes

@inureyes inureyes commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Ports the Kimi-VL / Kimi-VL 2.5 (kimi_vl / kimi_k25) vision-language architecture end to end: a net-new MoonViT native-resolution vision encoder, connector, and processor, composed with the reused DeepSeek-V3 MoE text backbone, and wired so a checkpoint detects, loads, and serves the image path. Validated with reference-derived unit and parity tests (no checkpoint download).

What changed

  • src/vision/encoders/kimi_vl.rs (+ kimi_vl_rope.rs, kimi_vl_pos_emb.rs, kimi_vl_tests.rs): MoonViT encoder with a Conv2d patch embed, a learned 2D position embedding with bicubic resampling, a shared 2D RoPE (real-valued interleaved rotation), block-diagonal cross-image attention, and a spatial_merge_size patch merger.
  • src/vision/kimi_vl.rs: KimiVLMultiModalProjector connector (both kimi_vl and kimi_k25 layouts) and KimiVLModel (MoonViT + DeepSeek-V3 MoE + processor), fusing image features into media_placeholder_token_id positions via merge_llava.
  • src/vision/processors/kimi_vl.rs: native-resolution image processor (token-budget rescale, crop to a merge*patch multiple, CLIP normalize, patchify + per-image grid).
  • src/loading/vlm_kimi_vl.rs: load_kimi_vl_vlm with the Kimi weight remap (vision encoder. strip + conv transpose + wqkv -> attn.wqkv; language language_model.model.* -> model.*), reusing the DeepSeek-V3 sanitize (expert stacking + MLA decomposition), returning LoadedModel::KimiVL.
  • Runtime: LoadedModel::KimiVL + dispatch arm, VlmRuntimeRef::KimiVL capability, the vlm_runtime image dispatch with kimi_vl_prompt placeholder expansion, and a generate CLI preparation summary.
  • src/models/deepseek_v3.rs: additive get_embed_tokens and forward_impl_with_embeddings for the VLM embeddings path.
  • Registration: ModelType::KimiVL / KimiK25, detection.rs arms, metadata() / ALL_MODEL_TYPES / model_metadata / tensor-parallel arch fallback, and docs/supported-models.md.

Test plan

  • cargo fmt --check
  • cargo clippy --lib --tests -- -D warnings
  • cargo test --lib kimi_vl (encoder rope/merger/bicubic, connector, processor, prompt expansion, loader remap: 15 passed)
  • cargo test --test kimi_vl_parity (detection, metadata, encoder+connector shape parity: 3 passed)
  • cargo check --lib --tests

Follow-up (out of scope here)

  • Kimi-VL 2.5 3D MoonViT video (image + video patch embedding).

Closes #525

Port the Kimi-VL and Kimi-VL 2.5 (kimi_k25) vision-language architecture. The net-new piece is the MoonViT native-resolution vision encoder (src/vision/encoders/kimi_vl.rs): a Conv2d patch embedding, a learned 2D position embedding with on-the-fly bicubic resampling for off-grid resolutions, a shared 2D rotary embedding (real-valued interleaved rotation equivalent to the reference complex freqs_cis), block-diagonal cross-image attention, and a spatial_merge_size patch merge. The 2D rope and bicubic resampling live in kimi_vl_rope.rs and kimi_vl_pos_emb.rs, and the encoder is validated against hand-derived reference values: the rope cos/sin table, the interleaved rotation, the patch-merger permutation, bicubic partition-of-unity, and a synthetic end-to-end forward.

The vision-to-language connector (KimiVLMultiModalProjector) applies a LayerNorm plus a Linear/GELU/Linear MLP over the merged patches and supports both the kimi_vl (multi_modal_projector.*) and kimi_k25 (mm_projector.proj.*) weight layouts, which are numerically identical per token. KimiVLModel composes the MoonViT tower, the connector, and a reused DeepSeek-V3 MoE text backbone, scattering image features into media_placeholder_token_id positions via merge_llava. DeepSeekV3Model gains get_embed_tokens and forward_impl_with_embeddings so the backbone can consume the merged embeddings without re-embedding input_ids.

A native-resolution KimiVLProcessor (src/vision/processors/kimi_vl.rs) rescales to the token budget, centre-crops to a merge*patch multiple, CLIP-normalizes, and patchifies into [num_patches, C, p, p] plus the per-image patch grid.

Registration: ModelType::KimiVL and ModelType::KimiK25 with detection arms for the kimi_vl and kimi_k25 model_type strings, the arch/metadata tables (ALL_MODEL_TYPES, metadata(), model_metadata, and the tensor-parallel architecture fallback), and a docs/supported-models.md entry.

Scope: image path only. The Kimi-VL 2.5 3D MoonViT video path, and the safetensors directory loader plus image-runtime dispatch (LoadedModel variant, capabilities, and vlm_runtime), are tracked as follow-ups; a kimi_vl checkpoint is detected and reports the pending directory loader.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:models Model architectures, weights, loading, metadata status:done Completed labels Jul 1, 2026
inureyes added 2 commits July 2, 2026 02:42
Complete the Kimi-VL / Kimi-VL 2.5 runtime path so a real kimi_vl or kimi_k25 checkpoint loads and runs end to end instead of reporting a missing directory loader.

Loader (src/loading/vlm_kimi_vl.rs): load_kimi_vl_vlm reads the sanitized config, parses the nested deepseek_v3 text_config and the moonvit vision_config, inherits a top-level quantization block into both, and runs the Kimi weight remap (strip the vision encoder segment, transpose the patch-embed conv kernel to MLX channel-last, rename blocks.{i}.{wqkv,wo} onto the attn submodule, rewrite language_model.model.* and language_model.lm_head.* onto model.* and lm_head.*, and drop position_ids/rotary_emb). MoE expert stacking and the MLA kv_b_proj decomposition are delegated to the shared DeepSeekV3Model::sanitize_weights_with_args. It builds the MoonViT tower, the connector (auto-selecting the multi_modal_projector.* or mm_projector.proj.* layout), the native-resolution processor, and the DeepSeek-V3 backbone, returning LoadedModel::KimiVL.

Runtime: LoadedModel gains a KimiVL variant and its LanguageModel dispatch arm; capabilities expose VlmRuntimeRef::KimiVL so is_vlm() and the multimodal switchboard recognise it; and the vlm_runtime image path preprocesses each image into patches plus a per-image grid, expands each media placeholder into (h/merge)*(w/merge) tokens via the new kimi_vl_prompt helper, runs MoonViT plus the connector, and scatters the features with merge_llava. KimiVLModel now carries the processor and spatial_merge_size the runtime needs, and the generate CLI prints a Kimi-VL preparation summary.

The loader weight remap and the prompt expansion are unit-tested, and the encoder, connector, and processor tests from the initial port continue to pass. Real-checkpoint execution stays a deferred, orchestrator-run step.
…t-kimi-vl

# Conflicts:
#	docs/supported-models.md
#	src/commands/generate_vlm.rs
#	src/lib.rs
#	src/loaded_model.rs
#	src/loaded_model_capabilities.rs
#	src/loading/mod.rs
#	src/model_metadata.rs
#	src/multimodal/vlm_runtime.rs
@inureyes
inureyes merged commit 17a28c3 into main Jul 1, 2026
4 checks passed
@inureyes
inureyes deleted the feature/issue-525-port-kimi-vl branch July 2, 2026 13:49
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…up#597)

* feat(models): port Kimi-VL / Kimi-VL 2.5 MoonViT vision encoder

Port the Kimi-VL and Kimi-VL 2.5 (kimi_k25) vision-language architecture. The net-new piece is the MoonViT native-resolution vision encoder (src/vision/encoders/kimi_vl.rs): a Conv2d patch embedding, a learned 2D position embedding with on-the-fly bicubic resampling for off-grid resolutions, a shared 2D rotary embedding (real-valued interleaved rotation equivalent to the reference complex freqs_cis), block-diagonal cross-image attention, and a spatial_merge_size patch merge. The 2D rope and bicubic resampling live in kimi_vl_rope.rs and kimi_vl_pos_emb.rs, and the encoder is validated against hand-derived reference values: the rope cos/sin table, the interleaved rotation, the patch-merger permutation, bicubic partition-of-unity, and a synthetic end-to-end forward.

The vision-to-language connector (KimiVLMultiModalProjector) applies a LayerNorm plus a Linear/GELU/Linear MLP over the merged patches and supports both the kimi_vl (multi_modal_projector.*) and kimi_k25 (mm_projector.proj.*) weight layouts, which are numerically identical per token. KimiVLModel composes the MoonViT tower, the connector, and a reused DeepSeek-V3 MoE text backbone, scattering image features into media_placeholder_token_id positions via merge_llava. DeepSeekV3Model gains get_embed_tokens and forward_impl_with_embeddings so the backbone can consume the merged embeddings without re-embedding input_ids.

A native-resolution KimiVLProcessor (src/vision/processors/kimi_vl.rs) rescales to the token budget, centre-crops to a merge*patch multiple, CLIP-normalizes, and patchifies into [num_patches, C, p, p] plus the per-image patch grid.

Registration: ModelType::KimiVL and ModelType::KimiK25 with detection arms for the kimi_vl and kimi_k25 model_type strings, the arch/metadata tables (ALL_MODEL_TYPES, metadata(), model_metadata, and the tensor-parallel architecture fallback), and a docs/supported-models.md entry.

Scope: image path only. The Kimi-VL 2.5 3D MoonViT video path, and the safetensors directory loader plus image-runtime dispatch (LoadedModel variant, capabilities, and vlm_runtime), are tracked as follow-ups; a kimi_vl checkpoint is detected and reports the pending directory loader.

* feat(models): wire Kimi-VL runtime loader, processor, and dispatch

Complete the Kimi-VL / Kimi-VL 2.5 runtime path so a real kimi_vl or kimi_k25 checkpoint loads and runs end to end instead of reporting a missing directory loader.

Loader (src/loading/vlm_kimi_vl.rs): load_kimi_vl_vlm reads the sanitized config, parses the nested deepseek_v3 text_config and the moonvit vision_config, inherits a top-level quantization block into both, and runs the Kimi weight remap (strip the vision encoder segment, transpose the patch-embed conv kernel to MLX channel-last, rename blocks.{i}.{wqkv,wo} onto the attn submodule, rewrite language_model.model.* and language_model.lm_head.* onto model.* and lm_head.*, and drop position_ids/rotary_emb). MoE expert stacking and the MLA kv_b_proj decomposition are delegated to the shared DeepSeekV3Model::sanitize_weights_with_args. It builds the MoonViT tower, the connector (auto-selecting the multi_modal_projector.* or mm_projector.proj.* layout), the native-resolution processor, and the DeepSeek-V3 backbone, returning LoadedModel::KimiVL.

Runtime: LoadedModel gains a KimiVL variant and its LanguageModel dispatch arm; capabilities expose VlmRuntimeRef::KimiVL so is_vlm() and the multimodal switchboard recognise it; and the vlm_runtime image path preprocesses each image into patches plus a per-image grid, expands each media placeholder into (h/merge)*(w/merge) tokens via the new kimi_vl_prompt helper, runs MoonViT plus the connector, and scatters the features with merge_llava. KimiVLModel now carries the processor and spatial_merge_size the runtime needs, and the generate CLI prints a Kimi-VL preparation summary.

The loader weight remap and the prompt expansion are unit-tested, and the encoder, connector, and processor tests from the initial port continue to pass. Real-checkpoint execution stays a deferred, orchestrator-run step.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): port Kimi-VL / Kimi-VL 2.5 (MoonViT vision encoder)

1 participant