Skip to content

Support quantized kv cache for Minimax M3 - #26180

Merged
am17an merged 1 commit into
ggml-org:masterfrom
timkhronos:MSA-QuantKV
Jul 31, 2026
Merged

am17an merged 1 commit into
ggml-org:masterfrom
timkhronos:MSA-QuantKV

Conversation

@timkhronos

Copy link
Copy Markdown
Contributor

Overview

The M3 graph calls ggml_flash_attn_ext directly instead of going through build_attn. As a result, it did not apply the rotations required for quantized KV cache types, therefore quantized -ctk and -ctv were asserted out.

This change should enable quantized KV caches to be used with M3's MSA. Unquantized cache types are unchanged since for those self_k_rot and self_v_rot are null. Should also close #26155

Additional information

Requirements

@timkhronos
timkhronos marked this pull request as ready for review July 27, 2026 14:37
@timkhronos
timkhronos requested a review from CISC as a code owner July 27, 2026 14:37
@github-actions github-actions Bot added the model Model specific label Jul 27, 2026
@pwilkin

pwilkin commented Jul 27, 2026

Copy link
Copy Markdown
Member

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Automated code review

Static review complete. Here's the review.


Review of PR #26180: Support quantized kv cache for Minimax M3

The change is small, single-purpose, and addresses a documented issue (#26155). It mirrors the rotation pattern already used by llm_graph_context::build_attn (src/llama-graph.cpp:2655-2692), so the new manual rotations in M3's MSA branch faithfully reproduce what the non-sparse layers and other archs already get for free via build_attn. I checked the quick-reject gate and security items (no attacker-controlled counts/sizes here; the rotation matrices are internally generated Hadamards), and found nothing actionable.

Correctness (verified)

  • self_k_rot is applied to both Qcur and Kcur before cpy_k, and self_v_rot to Vcur before cpy_v, exactly as build_attn does. Since the Hadamard matrix is symmetric/orthogonal (H^2 == I), Q^T K is invariant under the shared H, so attention scores in the FA path are unchanged; the rotated K/V are what gets quantized into the cache (the whole point of these rotations).
  • The post-attention self_v_rot on cur un-rotates the FA output back into the original V space, matching build_attn's placement before wo. Order vs. cb(cur,"kqv_out") and build_lora_mm(wo,...) matches the non-sparse path.
  • The rotation is deliberately not applied to the index branch (iq/ik). That is correct: the indexer K cache (k_idx) is always F32 (src/llama-kv-cache.cpp:247) and is never quantized, and iq @ ik^T block scoring must stay self-consistent in the un-rotated space. Applying k_rot there would silently corrupt block selection.
  • Backward compatibility holds: for M3, attn_rot_k/attn_rot_v are only set when the cache is quantized (src/llama-kv-cache.cpp:346-366; M3 is not in the DeepSeek/GLM special-case list). With a default f16 cache, self_k_rot/self_v_rot are null and all four new if blocks are no-ops, so unquantized inference is byte-for-byte unchanged, as the PR description claims.
  • The rope-shift path (build_rope_shift) already handles the unrotate-RoPE-rotate dance for quantized K, so shifted rotated K in the MSA layers is consistent with the rotation now being written into the cache.

No correctness, security, or lifetime issues found.

Will slow the review

(point 1) No test exercises the path this PR enables. tests/test-llama-archs.cpp runs every arch (including LLM_ARCH_MINIMAX_M3) with the default f16 KV cache only; it never sets a quantized type_k/type_v, so the new rotation logic is not covered by CI. The skill guidance is explicit that the quantized-KV path (-ctk q8_0 -ctv q8_0) must be tested, not just default f16. At minimum, please add to the PR description concrete verification that M3 run with -ctk q8_0 -ctv q8_0 (and ideally q4_0/bf16) produces sane output vs. f16 (e.g., logits NMSE against the f16 run, plus a smoke test that it no longer hits the removed assert). Better still, extend test-llama-archs.cpp to let the cache type be parametrized so this is covered going forward; given how many archs now route through these rotations, that general fixture would be high-value.

Nits

(point 2) Optional: a one-line comment noting that the index branch is intentionally left un-rotated (because k_idx is F32 and block scoring must stay in the original space) would save the next reader a careful trace. Not required; build_attn itself is also uncommented here, so omitting it is consistent.

(point 3) The removed blank line at the second self_v_rot block is fine, but keep the two new if blocks visually grouped with the cache-store code above them rather than drifting; current placement reads cleanly, just flagging it for awareness.

Summary

No blocking issues. The logic is correct and the approach reuses the established build_attn rotation convention rather than inventing new machinery. The main ask before merge is evidence (tests or PR-description data) that quantized KV with M3 actually works end-to-end given that the entire PR exists to enable exactly that path.

This review was generated automatically by pi coding agent using zai-org/GLM-5.2. It may contain mistakes. Maintainers make the final call.

@stew675

stew675 commented Jul 31, 2026 •

Copy link
Copy Markdown

@CISC @am17an Sorry for pinging you guys, but this looks to actually fix one of the last issues that makes MiniMax-M3 viable for local deployments with quantized caches. It's only about 14 LOC and it'd be great to see this one go in and somewhat bookend the recent M3 support work.

@am17an am17an 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.

@ngxson probably need to add this in skill for adding new models.

Check whether quantized kv-cache works since it undergoes a hadamard rotation. If you added a new type of attention mechanism, make sure it to check inp_attn->self_{k,v}_rot

@am17an
am17an merged commit a09d8ab into ggml-org:master Jul 31, 2026
20 of 26 checks passed
@smalinin

smalinin commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Performance degradation, it works 2 times slower after the update
Before update I had about 24tok/sec for IQ3_S ( https://huggingface.co/AesSedai/MiniMax-M3-GGUF)
Now I have only 12-14tok/sec
It may be related also with #26297 , because my prev binary was created before these two new fixes.

@timkhronos

timkhronos commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

@smalinin I think this may be because you were necessarily running with an unquantized KV cache before. If you are running quantized caches now, could you please try with -ctk f16 and -ctv f16? If that matches your previous speed, then I'd expect the difference to be kv cache quantization overhead.

@timkhronos
timkhronos deleted the MSA-QuantKV branch July 31, 2026 16:24
@smalinin

smalinin commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

@timkhronos

@smalinin I think this may be because you were necessarily running with an unquantized KV cache before. If you are running quantized caches now, could you please try with -ctk f16 and -ctv f16? If that matches your previous speed, then I'd expect the difference to be kv cache quantization overhead.

I tested with both q8_0 and f16, the degradation is existed in both cases.
But the issue is related with fix #26297
This fix for quantizied kv cache is Ok.

huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 2, 2026
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: crash with MiniMax-M3 MSA in and KV cache-type q8_0 q4_x q5_x

5 participants