Repository navigation
feat(models): port Kimi-VL / Kimi-VL 2.5 MoonViT vision encoder - #597
Merged
Merged
Conversation
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.
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
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.
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
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 aspatial_merge_sizepatch merger.src/vision/kimi_vl.rs:KimiVLMultiModalProjectorconnector (bothkimi_vlandkimi_k25layouts) andKimiVLModel(MoonViT + DeepSeek-V3 MoE + processor), fusing image features intomedia_placeholder_token_idpositions viamerge_llava.src/vision/processors/kimi_vl.rs: native-resolution image processor (token-budget rescale, crop to amerge*patchmultiple, CLIP normalize, patchify + per-image grid).src/loading/vlm_kimi_vl.rs:load_kimi_vl_vlmwith the Kimi weight remap (visionencoder.strip + conv transpose +wqkv->attn.wqkv; languagelanguage_model.model.*->model.*), reusing the DeepSeek-V3 sanitize (expert stacking + MLA decomposition), returningLoadedModel::KimiVL.LoadedModel::KimiVL+ dispatch arm,VlmRuntimeRef::KimiVLcapability, thevlm_runtimeimage dispatch withkimi_vl_promptplaceholder expansion, and agenerateCLI preparation summary.src/models/deepseek_v3.rs: additiveget_embed_tokensandforward_impl_with_embeddingsfor the VLM embeddings path.ModelType::KimiVL/KimiK25,detection.rsarms,metadata()/ALL_MODEL_TYPES/model_metadata/ tensor-parallel arch fallback, anddocs/supported-models.md.Test plan
cargo fmt --checkcargo clippy --lib --tests -- -D warningscargo 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 --testsFollow-up (out of scope here)
Closes #525