fix: MLA and sliding window KV cache accuracy#10
Open
stmatengss wants to merge 1 commit into
Open
Conversation
Fix 15x overestimate for MLA models (DeepSeek V2/V3/V3.1, Kimi K2) by replacing the fake n_kv_heads=16/head_dim=512 hack with proper MLA compressed KV formula: (kv_lora_rank + qk_rope_head_dim) for K + kv_lora_rank for V = 1088 values per layer instead of 16384. Also add sliding_window and sliding_window_pattern params to Gemma 3 models (27B, 12B, 4B) to expose hybrid attention metadata. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
n_kv_heads=16, head_dim=512hack with proper MLA compressed KV parameters (mla: true, kv_lora_rank: 512, qk_rope_head_dim: 64). ThekvBytesPerToken()function now uses the correct formula:(kv_lora_rank + qk_rope_head_dim + kv_lora_rank) * dtype_bytesper layer = 1088 values instead of 16384 (a ~15x correction).sliding_window: 1024, sliding_window_pattern: 6params to expose hybrid attention properties in the model profile.Technical details
MLA (Multi-head Latent Attention) compresses KV cache using low-rank projection:
(kv_lora_rank + qk_rope_head_dim) * dtype_bytes=(512 + 64) * 2= 1152 byteskv_lora_rank * dtype_bytes=512 * 2= 1024 bytes2 * 16 * 512 * 2= 32768 bytesTest plan
node tests/validate_js_parity.mjs)