Skip to content

perf(models): project only the sampled row at prefill for 8 families - #1971

Merged
inureyes merged 2 commits into
mainfrom
update/issue-1970-last-row-prefill-families
Sep 24, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-1970-last-row-prefill-families

Conversation

@inureyes

Copy link
Copy Markdown
Member

Extends the prefill last-row LM head projection from Cohere2 (#1955) and Llama3 (#1969) to Qwen3, Qwen3-MoE, Phi-3, Mixtral, Gemma 2, Gemma 3, Jamba and Nemotron-H. These models used the forward_last_logits trait default, which projects every prompt position through the head and keeps one row.

Each model now splits its forward into a hidden-state path ending at the final norm and a head (Gemma 2's final-logit softcap stays on the head side, since it is per position). The three forward_last_logits* entry points slice the hidden state at last_pos before the head. Gemma 3, Jamba and Nemotron-H reuse the same sequence-state closures and mask handling as their existing forward / forward_with_sequence_id. Models without an embeddings input keep routing the embeddings variant to the plain path, as the default did.

Measurements

M1 Ultra, mlxcel-bench-decode with bench_decode.sh arguments, pp512 prefill, main 13b6d33a against this branch, ABBA:

Model vocab Change
Gemma 2 2B 256k +26.5%
Gemma 3 4B 262k +19.2%
Qwen3 1.7B 152k +18.6%
Qwen3-30B-A3B 152k +6.7%
Phi-3 mini 32k about +3.8%
Nemotron 3 Nano 30B 131k +0.2% to +3%
Jamba reasoning 3B 65k +0.8%
Mixtral 8x7B 32k +0.7%

Full record: docs/benchmark_results/last-row-prefill-families-m1ultra-2026-09-24.md.

Validation

  • Greedy output byte-identical to main on all eight checkpoints (short prompt and 512-token prompt, 100 tokens).
  • New shared helper test_support::last_logits::assert_last_logits_match_full_forward, used by new Qwen3 and Gemma 2 tests; shifting the slice by one row fails both.
  • fmt, workspace clippy and make verify-test pass locally.

Not covered: CUDA/ROCm, and DeepSeek V3 (still on the trait default, no local checkpoint).

Closes #1970

Qwen3, Qwen3-MoE, Phi-3, Mixtral, Gemma 2, Gemma 3, Jamba and Nemotron-H used the trait default for forward_last_logits, which runs the full [1, L, vocab] LM head at prefill and keeps one row. Each model now splits its forward into a hidden-state path ending at the final norm and a head (Gemma 2 keeps its logit softcap on the head side), and the three forward_last_logits entry points slice the hidden state at the sampled position first. Gemma 3, Jamba and Nemotron-H keep the same per-sequence state and mask rules as their existing forward paths.

pp512 prefill on M1 Ultra against main: Gemma 2 2B +26.5%, Gemma 3 4B +19.2%, Qwen3 1.7B +18.6%, Qwen3-30B-A3B +6.7%, Phi-3 mini about +3.8%, Nemotron, Jamba and Mixtral within 0.2% to 3%. Greedy output is identical to main on all eight. A shared test helper checks all three entry points against the sliced full forward; Qwen3 and Gemma 2 tests use it and fail when the slice is shifted by one row.

Closes #1970
@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 24, 2026
@inureyes
inureyes merged commit 2dfc33e into main Sep 24, 2026
20 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 24, 2026
@inureyes
inureyes deleted the update/issue-1970-last-row-prefill-families branch October 6, 2026 12:12
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(models): project only the sampled row through the LM head at prefill for more families

1 participant