Skip to content

perf(cohere2): project only the sampled row through the LM head at prefill - #1955

Merged
inureyes merged 1 commit into
mainfrom
update/issue-1954-cohere2-last-row-prefill
Sep 23, 2026
Merged

inureyes merged 1 commit into
mainfrom
update/issue-1954-cohere2-last-row-prefill

Conversation

@inureyes

Copy link
Copy Markdown
Member

Cohere2 prefill now projects only the sampled row through the LM head. The default forward_last_logits computed [1, L, 256000] logits through the tied head, scaled the whole tensor by logit_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.

  • The forward is split into hidden states (embeddings, layers, final norm) and the head (LM head plus logit scale).
  • forward_last_logits, forward_last_logits_with_sequence_id and forward_last_logits_with_embeddings_and_sequence_id slice the hidden state to last_pos before the head, so CLI generate, the bench harness and server prefill all take the short path.
  • Full-sequence forward is unchanged.

Measurement

command-r7b 4-bit on M1 Ultra, scripts/bench_decode.sh tg128, six ABBA runs per arm, indexers paused:

Prompt main prefill this branch change
512 727.2 tok/s (704 ms) 831.7 (616 ms) +14.4%
2048 699.9 (2926 ms) 796.6 (2571 ms) +13.8%

Runs within an arm agree to 0.1%. Decode is unchanged. Server /completion with a 512-token prompt and cache_prompt: false: prompt_ms 713 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

  • Greedy output identical to main for three short prompts and the 512-token prompt (100 tokens each).
  • New 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).
  • Related lib tests (cohere, last_logits, prefill filters, 241), workspace clippy with -D warnings, and fmt pass.
  • CUDA and prompts beyond one prefill chunk were not measured.

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

…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
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific labels Sep 23, 2026
@inureyes
inureyes merged commit 25fdb13 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 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
@inureyes
inureyes deleted the update/issue-1954-cohere2-last-row-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:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cohere2): project only the sampled row through the LM head at prefill

1 participant