Skip to content

Quantized-cache prefill route: keep the dequantize-and-SDPA body out of line (gemma4-qat#1725) - #13

Closed
jeethu wants to merge 1 commit into
numen/mainfrom
kv8-route-out-of-line-1725
Closed

jeethu wants to merge 1 commit into
numen/mainfrom
kv8-route-out-of-line-1725

Conversation

@jeethu

@jeethu jeethu commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Update, 2026-11-16 16:15: the gain did not hold on the real pin. This branch (50308b8, on 5d2d0c4) was pinned in a numen-tech/gemma4-qat#1725 build merged with gemma4-qat main ad8437138, and measured against main ad8437138 on the same M5 iPad (MiniCPM5 1B, 8-bit KV, 2 ABBA rounds, 16 pairs, thermal nominal):

  • decode B/A -1.8 % [-1.79, -1.76];
  • the long-prompt prefill gain held (B 261-280 ms vs A 303-317 ms).

Across the five builds measured (the table below plus this one), decode moved between -3.2 % and +1.2 %. In the short turns all five run the same code, because the route never runs there. So the spread looks like build-to-build variance, not something this change removes. The owner decides whether to keep this PR or close it. It is behavior-neutral either way.

Moves the body of the quantized-cache prefill route (dequantize K/V, then fused SDPA, from upstream's quantizedCacheAttention) into an @inline(never) private function. The route's behavior is unchanged.

Why

numen-tech/gemma4-qat#1725 moves the gemma4-qat pin to the #9 sync (1f926a4), which brought in upstream's route. On an M5 iPad, MiniCPM5 1B with 8-bit KV then decoded more slowly than on the previous pin (d949227). Each row below is one build, measured against main with tools/bench/device_ab.py: 2 ABBA rounds, 8 prompts, 16 pairs, all thermal nominal.

build (B) decode B/A [95 % CI] long-prompt prefill
sync as pinned (route inlined) -3.2 % [-3.76, -2.65] 9-14 % faster
same, plus counters -2.1 % [-2.28, -1.90] faster
route disabled (minimum length Int.max) -0.1 % [-0.18, 0.00] gain gone
this change (route body out of line) +1.2 % [-0.09, +2.55] kept (about 262-274 ms vs A's 304-332 ms on the 1.4-1.5k-token prompts)

What the other legs showed:

  • The counter build counted 0 route calls in all six short turns of every launch, and 48 per long turn. The short turns still lost 2-3 %, so the loss does not come from the route's own work.
  • The matmul kernel counts per turn (-BenchTraceKernels 1) were the same in both arms.
  • With pipelined decode off (-NumenPipelineDecode 0), the sync build was +1.1 % [-0.14, +2.43].
  • Fp16 KV (+0.3 %) and Granite 4.1 3B with 8-bit KV (+0.2 %) showed no loss.
  • On the Mac (M3 Max, numen-cli, 6 ABBA rounds), 8-bit B/A was about +0.7 % within ±3 % noise, so the Mac does not reproduce the loss.

My reading is that the inlined route body changed the compiled code of the hot quantizedCacheAttention, which every decode step over a quantized cache calls. Putting the body out of line keeps that function's decode branch as it was. The mechanism is inferred, not measured. Not measured: a control comparing two builds of identical source (build-to-build noise), and devices other than the M5 iPad.

Tests

xcodebuild test -scheme mlx-swift-lm-Package -destination platform=macOS -skipPackagePluginValidation on the M3 Max:

  • XCTest: 1018 tests executed, 8 skipped, 0 failures.
  • Swift Testing: 1463 tests in 125 suites passed (2 known issues), plus runs of 74, 195 and 7 tests, all passed.
  • This includes testDequantizedPrefillAttentionRouteGates and testAttentionKVStateQuantizedRouteSharesPrefillDispatch.

Ordering with the open gemma4-qat PRs

This PR branches from numen/main 5d2d0c4 (the merge of #11, after #10), the revision numen-tech/gemma4-qat#1731 and #1732 pin.

Refs numen-tech/gemma4-qat#1725 (epic numen-tech/gemma4-qat#1709).

…of line

Every decode step over a quantized KV cache passes through
quantizedCacheAttention and takes the quantized branch. With the route's
body inlined there, MiniCPM5 1B with 8-bit KV decoded 2-3 % slower on an
M5 iPad (pipelined decode). Counters showed the branch never ran in those
turns, and the matmul kernel counts were unchanged. With the body in an
@inline(never) function, decode matched a build without the route
(+1.2 % [-0.09, +2.55] vs the previous pin), and the long-prompt prefill
gain stayed.

Refs numen-tech/gemma4-qat#1725
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T15:31:26.380194Z 50308b8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 50308b8224

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +231 to +234
/// MiniCPM5 1B with 8-bit KV decoded 2-3 % slower on an M5 iPad (pipelined decode),
/// although the counters showed this branch never ran in those turns and the matmul kernel
/// counts were unchanged. Out of line, decode matched the build without this route, and
/// the prefill gain stayed (numen-tech/gemma4-qat#1725).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Condense the helper's benchmark commentary

The new helper includes a nine-line documentation block with device-specific benchmark results and counter observations, contrary to the root AGENTS.md requirements to “Keep code comments concise (usually 1-2 lines)” and “Avoid redundant or excessive inline commentary.” Keep a short explanation that @inline(never) protects decode performance, and leave the experimental details in the commit message, where they are already recorded.

AGENTS.md reference: AGENTS.md:L45-L46

Useful? React with 👍 / 👎.

@jeethu

jeethu commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Closing on the owner's decision of 2026-11-16. The branch kv8-route-out-of-line-1725 stays.

Final measurement on the real pin: M5 iPad, MiniCPM5 1B, Automatic 8-bit KV, 2 ABBA rounds, 16 pairs, thermal nominal. A is gemma4-qat main ad8437138. B is fork-sync-1692 merged with that main and pinned to this branch (50308b8).

  • Decode B/A: -1.8 % [-1.79 %, -1.76 %].
  • Prefill on the two long prompts: B 261-280 ms, A 303-317 ms.

So moving the route out of line did not remove the decode loss. The earlier +1.2 % reading was against an older base and did not hold. The short-turn decode code is the same in both arms, and short turns never call the route (0 calls per short turn, 48 per long turn). Same-build controls on the same iPad show launch-to-launch variance of this size (gemma4-qat#1725 carries the numbers), so this PR fixes nothing measurable. gemma4-qat#1725 goes back to mlx-swift-lm 1f926a4 (revert commit b2317d916).

@jeethu jeethu closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant