Skip to content

perf(models): project only the sampled row through the LM head at prefill for more families #1970

Closed

Description

@inureyes

Problem / Background

Follow-up to #1968 (PR #1969, which covered Llama 3.x, Mistral, Qwen2/2.5, and the vision wrapper). Eight more families still use the LanguageModel::forward_last_logits default (src/loaded_model.rs:448, plus forward_last_logits_with_sequence_id at :458 and forward_last_logits_with_embeddings_and_sequence_id at :485). That default computes [1, L, vocab] logits through the LM head during prefill and then keeps one row. The eight families are Qwen3, Qwen3-MoE, Phi-3, Mixtral, Gemma 2, Gemma 3, Jamba, and Nemotron-H.

Proposed Solution

Split each model's forward into a final-normed hidden-state path and the head. For Gemma 2 the head includes the final logit softcap, which is per position and so is applied after slicing. Then override all three forward_last_logits* entry points to slice the hidden state at last_pos before the head. Gemma 3 (wrapper), Jamba, and Nemotron-H keep per-sequence state, so for them the slice goes inside the same sequence-state closure that forward and forward_with_sequence_id already use.

Measured on M1 Ultra (pp512 prefill, main vs change, ABBA): Gemma 2 2B went from 2105 to 2662 tok/s (+26.5%), Gemma 3 4B from 1026 to 1222 (+19.2%), Qwen3 1.7B from 2170 to 2574 (+18.6%), and Qwen3-30B-A3B from 887 to 946 (+6.7%). Phi-3 mini gained about 3.8%, Nemotron 3 Nano 30B about 0.2 to 3%, Jamba 3B 0.8%, and Mixtral 8x7B 0.7%. Greedy output was identical on two prompts per model. The gain tracks vocabulary size relative to model size.

Out of scope: DeepSeek V3. No checkpoint is available locally to validate it.

Acceptance Criteria

  • A shared test helper checks all three entry points against the sliced full forward, at the last position and at an interior position. Every family with a tiny-model builder uses it, which means at least Qwen3 and Gemma 2.
  • Greedy output is identical to main on real checkpoints for all eight families.
  • A benchmark record is added under docs/benchmark_results/.
  • Workspace tests, clippy, and fmt pass.

Verification

cargo test --release
cargo clippy --all-targets -- -D warnings
cargo fmt --check

Activity

  1. added
    status:in-progress
    type:performance
    priority:medium
    area:models
    platform:macos
    on Sep 24, 2026
  2. added
    status:done
    and removed
    status:in-progress
    on Sep 24, 2026
  3. added a commit that references this issue on Sep 29, 2026
    2dfc33e
  4. added this to the 0.8 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:modelsplatform:macospriority:mediumstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions