Skip to content

perf(cohere2): use maskless causal SDPA for prefill inside the window - #1957

Merged
inureyes merged 1 commit into
mainfrom
update/issue-1956-cohere2-causal-prefill
Sep 23, 2026
Merged

inureyes merged 1 commit into
mainfrom
update/issue-1956-cohere2-causal-prefill

Conversation

@inureyes

Copy link
Copy Markdown
Member

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.sh tg128 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:

Prompt per-quartet prefill change time saved
512 +0.75%, +0.30%, +0.90% about 5 ms
2048 +4.6%, +3.3%, +3.7% about 90 ms
4096 +3.3%, +3.7%, +3.3% about 175 ms

Decode is unchanged. Record: docs/benchmark_results/cohere2-causal-prefill-m1ultra-2026-09-23.md.

Validation

  • Greedy output identical to the perf(cohere2): project only the sampled row through the LM head at prefill #1955 build on three short prompts and the 512-token prompt (100 tokens each).
  • New cohere2::tests::prefill_matches_token_by_token_decode_inside_and_beyond_the_window compares 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.
  • Prompts beyond 4096 tokens and CUDA were not measured.

Closes #1956

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
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:low Low priority area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific labels Sep 23, 2026
@inureyes
inureyes merged commit b12744a into main Sep 23, 2026
20 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 23, 2026
@inureyes
inureyes deleted the update/issue-1956-cohere2-causal-prefill branch October 6, 2026 12:12
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific priority:low Low priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cohere2): use maskless causal SDPA for prefill inside the sliding window

1 participant