Skip to content

perf(models): run attention-sink prefill in MLX causal and window mode - #3119

Merged
inureyes merged 2 commits into
mainfrom
update/issue-3098-sink-causal-mode
Nov 16, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-3098-sink-causal-mode

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

The attention-sink decoders still built a dense f32 prefill band because the sink SDPA bridge took only an array mask. MLX already accepts mask_mode = "causal" and window_size together with sinks, so this adds that entry and routes the sink models' plain-causal and sliding prefill through it wherever MLX fuses the call.

  • New fast_scaled_dot_product_attention_with_sinks_causal bridge entry (cxx Result, so an MLX validation error is an Err rather than an abort). The fused-kernel probe gains has_sinks, memoized under its own key.
  • New mlxcel_core::prefill_sdpa: causal_prefill_attention(q, k, v, scale, window, sinks) takes MLX's causal mode, plus window_size once keys outgrow the window, when prefill_uses_causal_mode allows it and MLX fuses the call (asked with sinks). Otherwise it builds the same band the layer built before. Padded, batched, caller-masked and speculative forwards keep the array.
  • PrefillAttention::with_sinks() lets prefill_causal_mask and prefill_sliding_window_mask decide with sink geometry. GPT-OSS (single model and pipeline stage, full and sliding layers), Falcon-OCR text prefill, MiMo v2 Flash and all-local DeepSeek-V4 stacks use them. Laguna's layers call causal_prefill_attention instead of building a band per layer.
  • Not converted: the OpenAI privacy filter (symmetric bidirectional band, already per-window), and DeepSeek-V4 compressed/sparse layers, which extend the local mask with pooled rows.
  • The create_causal_mask roster sentence is now 13 files.

Measurements (M5 Max, main 00cd2d69 vs branch)

Model Tokens Path Peak Prefill
gpt-oss-20b-mxfp4-q4 19773 single pass 22.54 to 17.86 GB 6.63 to 3.62 s
gpt-oss-20b-mxfp4-q4 19773 2048 chunks 13.42 to 13.16 GB 3.96 to 3.84 s
laguna-xs.2-4bit 28747 single pass 35.84 to 29.25 GB 39.03 to 7.32 s
laguna-xs.2-4bit 28747 2048 chunks 22.32 to 21.99 GB 9.71 to 7.49 s
falcon-ocr 9067 single pass (x2) 5.11 to 4.70 GB 0.79/0.94 to 0.59/0.28 s

Greedy output is byte-identical in every cell, so no logit trace was run. laguna-xs.2-4bit has no sinks enabled, so it covers the per-layer route without sinks; falcon-ocr carries sinks on every layer. One run per cell on a shared host; the laguna main single-pass prefill likely includes memory pressure. Details: docs/benchmark_results/prefill-sink-causal-mode-m5max-2026-11-15.md.

Test plan

  • prefill_sdpa_tests: causal sink entry and routed helper vs the explicit-mask sink call and an f64 host reference (f32 and f16, causal, prior keys, in-window, over-window, head dims 64/128, GQA); without-sinks route vs layers::attention; bad window and mis-shaped sinks return Err
  • window_sdpa_tests, utils::prefill_*, create_causal_mask_roster_count_matches_repo
  • Model tests: gpt_oss, laguna, falcon_ocr, mimo_v2_flash, deepseek_v4, openai_privacy_filter, qwen3_next
  • clippy (mlxcel-core, mlxcel), fmt, env-var docs, benchmark raw-log check, kernel dtype keys

Closes #3098

@inureyes inureyes added type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed labels Nov 16, 2026
The sink SDPA bridge took only an explicit array mask, so GPT-OSS, Laguna, Falcon-OCR, MiMo v2 Flash and DeepSeek-V4 built a dense f32 prefill band that #3056 and #3097 had removed everywhere else. MLX already accepts mask_mode "causal" and window_size together with sinks.

Add fast_scaled_dot_product_attention_with_sinks_causal (a cxx Result, so MLX validation errors do not abort) and let the fused-kernel probe ask with sinks. prefill_sdpa::causal_prefill_attention takes the maskless call where prefill_uses_causal_mode allows it and MLX fuses it, else builds the same band. PrefillAttention gains a sinks flag so prefill_causal_mask and prefill_sliding_window_mask decide with sink geometry. GPT-OSS (single model and pipeline stage), Falcon-OCR, MiMo v2 Flash and all-local DeepSeek-V4 stacks use the shared helpers; Laguna's layers call causal_prefill_attention. The OpenAI privacy filter's symmetric band is unchanged.

M5 Max, main vs branch: gpt-oss-20b-mxfp4-q4 at 19,773 tokens peaks 17.86 GB instead of 22.54 GB single pass (prefill 6.63 to 3.62 s) and 13.16 instead of 13.42 GB in 2048-token chunks; laguna-xs.2-4bit at 28,747 tokens 29.25 instead of 35.84 GB; falcon-ocr 4.70 instead of 5.11 GB. Greedy output is byte-identical in every cell. New unit tests compare the causal sink entry and the routed helper with the explicit-mask call and an f64 host reference.

Closes #3098
@inureyes
inureyes force-pushed the update/issue-3098-sink-causal-mode branch from a623fcf to c9c4927 Compare November 16, 2026 06:32
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 06:32
@inureyes inureyes closed this Nov 16, 2026
@inureyes
inureyes deleted the update/issue-3098-sink-causal-mode branch November 16, 2026 06:32
@inureyes
inureyes restored the update/issue-3098-sink-causal-mode branch November 16, 2026 06:32
@inureyes inureyes reopened this Nov 16, 2026
@inureyes
inureyes merged commit 08da8ed into main Nov 16, 2026
27 of 32 checks passed
@inureyes
inureyes deleted the update/issue-3098-sink-causal-mode branch November 16, 2026 06:33
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(models): run attention-sink prefill in MLX causal and window mode

1 participant