Skip to content

Use fused attention for head dim 256 from 512 query rows with enough heads #4695

Closed

Description

@jvmenen

On M5 chips, mx.fast.scaled_dot_product_attention with head dim 256 (Qwen3.5 and similar models) uses the fused kernel only from 1024 query rows. With 512 to 1023 rows it takes the unfused path (matmul, softmax, matmul). The fused kernel is faster there as well, as long as there are enough heads to fill the GPU.

This matters for prefill in chunks smaller than 1024 tokens, and for the last chunk of a prompt.

Measurements (M5 Pro)

Time saved by the fused kernel compared with the unfused path, for 512 to 1023 query rows (fp16 and bf16, causal, key lengths up to 64K):

batch, query / KV heads typical (median) range
1, 16/4 30% faster 15% to 46%
1, 24/4 29% faster 17% to 51%
1, 8/2 25% faster 6% to 43%
2, 4/1 (8 heads in total) 25% faster 5% to 42%

The fused kernel needs enough heads to keep the GPU busy. With fewer than 8 heads in total (batch times query heads) it is sometimes slower, up to 54% more time, so the rule below requires at least 8. Below 512 rows the results were mixed, so we left that range out.

In a full prefill the gain is smaller, because attention is only part of the work (in Qwen3.5-9B, 8 of the 32 layers are full attention) and only chunks of 512 to 1023 tokens are affected. Measured with mlx-lm on Qwen3.5-9B:

  • Default prefill step (2048): only the last, shorter chunk of a prompt can be affected. No measurable difference.
  • Prefill step 512: every chunk is affected. No measurable difference up to 17K tokens of context, 10% faster at 33K, where attention takes a larger share of the time.

Proposal

One extra routing rule in ScaledDotProductAttention::use_fallback: use the fused kernel on NAX devices for fp16/bf16, head dim 256, causal, no array mask, at least 512 query rows and at least 8 heads in total. No kernel changes.

The results differ from the unfused path by at most 2.4e-4 (bf16) and 3.8e-5 (fp16), and are not further from a float32 reference than the unfused result.

Related

#4476 proposed the same range on an M5 Max and was closed; there were questions about end-to-end numbers and how the thresholds were chosen. This proposal adds the head-count condition, a sweep over all key lengths and the mlx-lm numbers above.

Branch with the rule and tests: jvmenen/mlx@main...sdpa-d256-short-query-v3

Would a PR along these lines be welcome?

Activity

  1. changed the title [-]Fused attention for head dim 256 from 512 query rows[/-] [+]Use fused attention for head dim 256 from 512 query rows with enough heads[/+] on Oct 23, 2026
  2. aaishwarymishra commented on Nov 12, 2026

    @aaishwarymishra
    Contributor

    @zcbenz @jvmenen will it be fine if I pick this up if you are not already working on this :)

  3. jvmenen commented on Nov 15, 2026

    @jvmenen
    ContributorAuthor

    Thanks for offering, @aaishwarymishra! I had the change ready, so I opened it as #4893. Reviews are very welcome, and so are measurements on other machines: so far I have only measured on an M5 Pro, so numbers for other configurations would be helpful.

  4. aaishwarymishra commented on Nov 16, 2026

    @aaishwarymishra
    Contributor

    Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions