Repository navigation
perf(cohere2): use maskless causal SDPA for prefill inside the window - #1957
Merged
Merged
Conversation
Cohere2 prefill always passed an explicit mask array to SDPA. When every live key is inside the window (global layers always, sliding layers while the cache holds at most sliding_window keys), that mask is plain bottom-right causal, so reading it only kept SDPA off MLX's causal mode. mlx-lm passes "causal" in the same case. Route such calls to causal_attention with window 0; a sliding layer whose cache has outgrown the window keeps the windowed mask path. command-r7b 4-bit on M1 Ultra, scripts/bench_decode.sh tg128, three ABBA pairs per length against the #1955 tree: prefill pp512 +0.3% to +0.9%, pp2048 +3.3% to +4.6%, pp4096 +3.3% to +3.7%; decode unchanged; greedy output identical on four prompts. A new unit test compares prefill against token-by-token decode inside the window, beyond it, and across it in a chunked prefill; forcing the causal path beyond the window fails it. Closes #1956
This was referenced Sep 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cohere2 prefill takes MLX's maskless causal SDPA whenever every live key is inside the window: global layers always, sliding layers while the cache holds at most
sliding_window(4096) keys. In that case the explicit prefill mask is plain bottom-right causal, so reading it only kept SDPA off the causal mode. mlx-lm passes"causal"in the same case. A sliding layer whose cache has outgrown the window keeps the windowed mask path unchanged.Measurement
command-r7b 4-bit on M1 Ultra,
scripts/bench_decode.shtg128 against the #1955 tree, three ABBA quartets per prompt length. Load drifted from 7.0 to 2.5 during the run, so the comparison is the ratio inside each quartet:Decode is unchanged. Record:
docs/benchmark_results/cohere2-causal-prefill-m1ultra-2026-09-23.md.Validation
cohere2::tests::prefill_matches_token_by_token_decode_inside_and_beyond_the_windowcompares prefill last-position logits with a token-by-token decode for a prefill inside the window, one beyond it, and a chunked prefill whose second chunk crosses it. Dropping the window condition (causal path beyond the window) fails it with max diff 0.017.cohere2::tests, workspace clippy with-D warnings, and fmt pass.Closes #1956