Skip to content

Pick the decode kernel from each row's unpadded length - #9

Draft
ronaldmannak wants to merge 5 commits into
claude/metal-sdpa-left-padding-skipfrom
claude/metal-sdpa-solo-parity
Draft

ronaldmannak wants to merge 5 commits into
claude/metal-sdpa-left-padding-skipfrom
claude/metal-sdpa-solo-parity

Conversation

@ronaldmannak

@ronaldmannak ronaldmannak commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #8.

In a left-padded decode batch, #8 picks the attention kernel from the padded length. A row shorter than the two-pass cutoff (1,024 keys on Max and Ultra chips, 4,096 with grouped queries elsewhere) therefore runs single-pass alone but two-pass once it is padded into a longer batch, and its result changes. This PR picks the kernel per row from its unpadded length, so such a row matches its solo result exactly.

It is scoped to one shape and kept out of #8 on purpose: it adds a launch at the cutoff, and full-model solo parity still depends on quantized matmul (ml-explore/mlx#4819). Treat it as evidence for the batch-invariance discussion rather than a change to upstream as is.

What changed

  • When a left-padded batch reaches the two-pass cutoff, the single-pass kernel runs first and skips rows at or above the cutoff; then the two-pass kernel skips rows below it, in both stages, so it neither reads their unwritten partials nor overwrites their output.
  • If the single-pass kernel cannot launch at 1,024 threads, every row uses two-pass, as it would alone.
  • The cutoff rule moves into two_pass_min_keys(devc, grouped_queries), shared with the stock dispatch instead of copied.
  • Scope: bfloat16, one query, head dimension 128, four query heads per key/value head. There the two-pass block count does not depend on the key count, so the same kernel gives the same block layout as the solo row. With two or more queries, Max chips choose the block count from the key count, so verification rows are not covered.
  • New test test_sdpa_vector_left_padding_uses_each_rows_unpadded_length (prototype by Codex): rows of 1,023, 1,024, 4,095 and 4,096 unpadded keys in one 4,245-key batch, covering both cutoffs, plus a 1,023-key batch for the single-pass kernel alone.

Evidence

Apple M5 Max (applegpu_g17s).

The new test on the parent commit d94e146f (#8's head):

$ MLX_ENABLE_TF32=0 python -m pytest -q python/tests/test_fast_sdpa.py -k unpadded_length
SUBFAILED(length=4245) test_fast_sdpa.py::TestFastSDPA::test_sdpa_vector_left_padding_uses_each_rows_unpadded_length
1 failed, 1 passed, 44 deselected, 1 subtests passed

At this head (dc49d649):

$ MLX_ENABLE_TF32=0 python -m pytest -q python/tests/test_fast_sdpa.py
43 passed, 2 skipped, 1645 subtests passed

Cost per attention call against #8's head. Qwen3-8B shape (32/8 heads, head dimension 128, bfloat16), single query, 100 chained calls per evaluation, medians of three runs in ABBA order. Microseconds.

Batch (unpadded row lengths) Keys #8 This PR Change
1,023 / 1,024 / 1,025 / 1,056 1,056 37.74 41.50 +10.0%
1,023 / 1,024 / 4,095 / 4,245 4,245 73.89 78.59 +6.4%
16,384 / 8,192 / 4,096 / 2,048 16,384 274.76 274.73 −0.0%
16,384 / 8,192 / 4,096 / 512 16,384 262.33 265.07 +1.0%

The cost is the extra single-pass launch, largest where the attention call itself is short.

Not verified

  • Other chips. Codex checked the prototype of this change on an M1 Max (exact solo results for 18 rows across the cutoff), but not at this head.
  • Full-model effect. In Codex's Qwen replays this change left the outputs identical to Skip left-padded keys in Metal vector attention #8's, because the remaining divergences from solo come from quantized matmul.

Not done on purpose

  • Other shapes and data types, and multi-query rows.
  • Choosing the two-pass block count per row.

🤖 Generated with Claude Code

In a left-padded batch that reaches the two-pass cutoff, a row shorter than the cutoff now uses the single-pass kernel, as it would alone. The single-pass launch skips the long rows, and both two-pass stages skip the short ones. The cutoff rule moves into two_pass_min_keys, shared with the stock dispatch.

Scope: bfloat16, one query, head dimension 128, four query heads per key/value head. There the two-pass block count does not depend on the key count, so the padded row reproduces the solo row exactly. Prototype by Codex; regression test fails on the parent commit for the 4,245-key batch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 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-14T03:25:42.477061Z 07c90a5 New commits
ℹ️ 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.

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