Skip to content

perf(gemma4): project only the sampled row at prefill on every entry point #1972

Closed

Description

@inureyes

Problem

Gemma 4 prefill still builds the full [1, L, 262144] logits tensor, plus a final_logit_softcapping copy, and then keeps a single row. This happens in most real runs:

Why the 4096 gate can go

#1955, #1969 and #1971 moved 11 other families to single-row head projection at every length, and greedy output stayed byte-identical. mlx-lm also computes the last prompt token as a one-token step. Gemma 3 4B, which has the same 262k vocabulary, gained +19.2% on pp512 prefill.

Plan

Remove the gate. Override all three entry points on Gemma4Wrapper, handling sequence state and per-layer inputs exactly as the existing forward paths do. Forward the three entry points from Gemma4VLModel and Gemma4UnifiedModel, covering the embeddings path with per-layer inputs and the unified bidirectional vision block ids.

Acceptance Criteria

  • On the text wrapper and both VLM wrappers, all three entry points slice the hidden state before the LM head at every prompt length.
  • A unit test checks that each entry point matches the sliced full forward at the last position and at an interior position, and fails when the slice is shifted by one row.
  • Greedy output on Gemma 4 E4B and 12B matches main for a short prompt, a 512-token prompt, and an image prompt.
  • pp512 and pp2048 prefill are measured against main using the bench_decode.sh arguments, and the results are recorded in docs/benchmark_results/.
  • The change is wired into the real CLI and server prefill paths.

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
    c61898e
  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