Skip to content

perf: avoid redundant Kimi vision projection copies - #31318

Closed
mickqian wants to merge 5 commits into
sgl-project:mainfrom
mickqian:codex/kimi-projector-fastpath
Closed

mickqian wants to merge 5 commits into
sgl-project:mainfrom
mickqian:codex/kimi-projector-fastpath

Conversation

@mickqian

@mickqian mickqian commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

Kimi K2.5/K2.7's non-DP image path currently does:

  1. concatenate per-image MoonViT outputs;
  2. run the multimodal projector;
  3. split the projected tensor back into per-image views;
  4. immediately concatenate those views at the call site.

The language-model embedding path only consumes the packed tensor. This PR keeps the packed result and also accepts an already-packed tensor, removing the redundant split/cat and its extra allocation/copy.

Performance

NVIDIA RTX 5090 (SM120), CUDA 13.0, BF16, 1152-wide MoonViT outputs with a lightweight flattening projector. This isolates the packing overhead and is not an end-to-end serving claim.

Image output chunks Before p50 After p50 Speedup
1 0.0242 ms 0.0140 ms 1.73x
2 0.0222 ms 0.0126 ms 1.76x
4 0.0231 ms 0.0126 ms 1.84x
8 0.0274 ms 0.0134 ms 2.05x

The benchmark uses the same projector call in both paths; only the post-projector split/cat is changed.

Correctness / validation

  • Added CPU coverage for variable-resolution per-image outputs and already-packed outputs.
  • Pre-commit hooks, AST validation, registration validation, and git diff --check pass.
  • The local macOS environment cannot collect the model test because its preinstalled huggingface_hub==1.6.0 rejects an existing SGLang strict dataclass definition; this is unrelated to the patch and is covered by the repository CI environment.

CI States

Latest PR Test (Base): 🚫 Run #29550245282
Latest PR Test (Extra): ❌ Run #29550245261

@mickqian mickqian added Multi-modal multi-modal language model run-ci labels Jul 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors mm_projection_auto in kimi_k25.py to return a single flattened feature tensor directly, avoiding redundant split and concatenate operations. It also adds unit tests to verify this behavior. The review feedback points out a correctness bug where a 3D tensor could be returned instead of a 2D tensor if mm_projector is None, and suggests a performance optimization to avoid calling torch.cat when there is only a single image in the sequence.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/sglang/srt/models/kimi_k25.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Multi-modal multi-modal language model run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant