Repository navigation
perf(cohere2): project only the sampled row through the LM head at prefill - #1955
Merged
Merged
Conversation
…efill Cohere2 used the default forward_last_logits, which computes [1, L, 256000] logits through the tied LM head, scales the whole tensor by logit_scale, and keeps one row. At 512 prompt tokens that is about 1 TFLOP and two 262 MB buffers nobody reads. mlx-lm evaluates only the cache for the prompt and computes logits for the last token alone. Split the forward into hidden states (embeddings, layers, final norm) and the head (LM head plus logit scale), and override forward_last_logits and its sequence-id and embeddings variants to slice the hidden state to last_pos before the head. CLI generate, the bench harness, and server prefill all go through these entry points. command-r7b 4-bit on M1 Ultra, scripts/bench_decode.sh tg128, six ABBA runs per arm: prefill pp512 727.2 to 831.7 tok/s (+14.4%), pp2048 699.9 to 796.6 (+13.8%), decode unchanged. Server /completion with a 512-token prompt: prompt_ms 713 to 624. Same-day mlx-lm prefill median 781.0. Greedy output identical to main on four prompts. A new unit test checks all three entry points against the sliced full forward; shifting the slice by one row fails it. Closes #1954
This was referenced Sep 23, 2026
inureyes
added a commit
that referenced
this pull request
Sep 23, 2026
…#1957) 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 24, 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 now projects only the sampled row through the LM head. The default
forward_last_logitscomputed[1, L, 256000]logits through the tied head, scaled the whole tensor bylogit_scale, and kept one row: about 1 TFLOP and two 262 MB buffers at 512 prompt tokens. mlx-lm computes logits for the last prompt token alone.forward_last_logits,forward_last_logits_with_sequence_idandforward_last_logits_with_embeddings_and_sequence_idslice the hidden state tolast_posbefore the head, so CLI generate, the bench harness and server prefill all take the short path.forwardis unchanged.Measurement
command-r7b 4-bit on M1 Ultra,
scripts/bench_decode.shtg128, six ABBA runs per arm, indexers paused:Runs within an arm agree to 0.1%. Decode is unchanged. Server
/completionwith a 512-token prompt andcache_prompt: false:prompt_ms713 to 624. Same-day mlx-lm 0.31.3 prefill median 781.0, so mlxcel goes from 6.9% behind to 6.5% ahead. Record:docs/benchmark_results/cohere2-last-row-prefill-m1ultra-2026-09-23.md.Validation
cohere2::tests::last_logits_match_the_sliced_full_forward: all three entry points against the sliced full forward at the final and an interior position on a tiny synthetic model, and the caches still advance. Shifting the slice by one row makes it fail (max diff 0.35).cohere,last_logits,prefillfilters, 241), workspace clippy with-D warnings, and fmt pass.Other large-vocabulary families still take the default path; only gemma4, inkling and iquestloopcoder override it. A generic fix is a follow-up.
Closes #1954