Skip to content

perf(vlm): reduce MoonViT RoPE and metadata overhead - #31638

Closed
mickqian wants to merge 2 commits into
mainfrom
codex/kimi-moonvit-fused-rope
Closed

mickqian wants to merge 2 commits into
mainfrom
codex/kimi-moonvit-fused-rope

Conversation

@mickqian

@mickqian mickqian commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • use the existing CUDA JIT RoPE kernel for Kimi-K2.5/K2.7 MoonViT
  • prepare one real-valued cos/sin cache and one positions tensor per vision
    forward, then reuse them across all 27 encoder layers
  • compute FlashAttention's host max_seqlen before moving sequence metadata
    to CUDA
  • retain the portable complex implementation on non-CUDA and RL target paths
  • add both dispatch and real-kernel numerical coverage

The current implementation converts Q/K to FP32 complex tensors, performs a
complex multiply, and copies the result back to the activation dtype in every
MoonViT layer. The fused in-place kernel removes those temporaries and launches.

Performance

Measured on NVIDIA H200 with BF16 MoonViT tensors (4 heads, head dim 72):

Vision tokens Portable Fused Speedup
256 56.20 us 8.65 us 6.50x
1,024 56.39 us 8.71 us 6.48x
4,096 75.39 us 8.84 us 8.52x
8,192 138.65 us 17.95 us 7.73x

The maximum BF16 absolute difference against the portable path was 0.0078125.

Same-seed Kimi-K2.7-Code TP8 torch-profiler comparison on 8 requests with 4
random-sized images each:

  • removed 216 cast/copy kernels (2.955 ms GPU time)
  • removed 216 complex-multiply kernels (2.419 ms)
  • removed 216 output-copy kernels (1.490 ms)
  • added 108 fused RoPE kernels (1.062 ms)
  • net relevant GPU time: -5.802 ms
  • launch count for this operation: 648 -> 108
  • whole-trace GPU kernel count: 12,904 -> 12,372

Moving the host max-sequence calculation before the metadata H2D copy also
removed exactly one synchronization per MoonViT forward in a follow-up paired
trace:

Event Before After
cudaStreamSynchronize 48 44
Memcpy DtoH (Device -> Pinned) 78 74
cumulative aten::item CPU time 55.9 ms 34.7 ms

bench_serving is neutral at this workload size, as expected for a roughly
6 ms optimization inside a much larger prefill. Across three seeds, burst
throughput changed -0.65%, mean TTFT +0.21%, and median TTFT -0.02%; rate=4
results were also within run-to-run noise. This PR claims the isolated and
profiled MoonViT reduction, not a material end-to-end serving speedup alone.

Validation

  • pre-commit run --files python/sglang/srt/models/kimi_k25.py test/registered/unit/layers/attention/test_vision_max_seqlen.py
  • H200: python3 -m pytest -q test/registered/unit/layers/attention/test_vision_max_seqlen.py (9 passed)
  • CUDA numerical test compares the real fused kernel with the portable path

CI States

Latest PR Test (Base): ❌ Run #29627829432
Latest PR Test (Extra): ❌ Run #29627829372

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-actions github-actions Bot added the Multi-modal multi-modal language model label Jul 18, 2026
@mickqian mickqian changed the title perf(vlm): fuse MoonViT rotary embedding on CUDA perf(vlm): reduce MoonViT RoPE and metadata overhead Jul 18, 2026
@mickqian mickqian closed this Jul 28, 2026
encoder.final_layernorm = nn.Identity()

output = encoder(
torch.ones(7, 4, device="meta"),

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.

what is the device of "meta" ?

@Jiminator
Jiminator deleted the codex/kimi-moonvit-fused-rope branch September 14, 2026 04:41
@alexnails
alexnails restored the codex/kimi-moonvit-fused-rope branch September 14, 2026 05:41
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 run-ci-extra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants