Skip to content

gemma4 metal: prefill runs a chunk a call, so a new prompt no longer freezes other streams' decode (#3021) - #3025

Merged
ashhart merged 5 commits into
mainfrom
lead/gemma4-metal-prefill-step
Nov 23, 2026
Merged

ashhart merged 5 commits into
mainfrom
lead/gemma4-metal-prefill-step

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Fixes #3021. Gemma 4 on Metal ran a new stream's whole prompt in one go on the lane thread, so every other stream's decode waited for it: about 6 s for an 8K prompt and 26 s at the 32K default cap on the 31B on an M5 Ultra.

Change (families/gemma4/, Metal backend): the backend now declares prefill_step, reports_fill and cancel_prefill, so the lane host runs one chunk a call and decode rounds run between chunks.

  • Same arithmetic: the cuts are exactly today's: the 512-row chunk plus the server's planned starts, marks and image spans (chunkEnd is main's loop code). Gemma declares no chunk invariance, so nothing moves the cuts. A chunk runs the same prompt.run; the last chunk takes the logits copy that used to follow the loop, and no mark can sit at the prompt's end.
  • Drafters: the drafted wrapper absorbs each chunk's tapped rows after its step, and a mark at a chunk's end is kept at the next call, after that absorb, so DFlash's lane end still equals the mark.
  • Cancel between chunks keeps the last finished chunk, as Qwen3.8-27B does.
  • Behaviour to note: every Gemma Metal stream, single streams included, now goes through the fill queue, so the prefill-share settings apply to Gemma.

Tests (test-gemma4-metal-steps-host, new, in the nightly host list; recording fakes, no GPU): one call runs one chunk at today's cuts; the chunk sequence equals the unstepped run's; a decode round runs between two chunks of a long prompt; a drafter receives every chunk's taps. On main's backend each fails (the whole prompt runs in one call, no decode between chunks, GemmaTapsNotHeld). Keeping a mark before the absorb also fails the drafter test. test-gemma4-metal-vtable-host 56/56, the other Gemma host steps 742 pass, compile-gemma4-host passes; lean 0 new.

Before landing, on an M5: replies equal to main's for one stream (greedy, a ~5K prompt not a multiple of 512, with and without DFlash), for two streams where the second sends a 32K prompt mid-decode, for a resumed turn and a --vision image across a chunk boundary; cancel and resend; and stream 1's largest gap between tokens during stream 2's 32K fill (main: the whole fill; this: about one chunk and one round), with no slower time to first token.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

M5 check on this head (Gemma 4 12B, MLX 4-bit, greedy, base-new-new-base):

  • Exact: all 70 text replies equal main's, including resumed turns and a cancelled fill that is resent.

  • The freeze is gone: while a 32K prompt fills, the decoding stream's largest gap between tokens drops from 9.48 s to 185 ms.

  • Two costs to fix before landing:

    • Lone decode is about 2% slower in most cells. For a lone 32K prompt it is 78.6 vs 76.9 tok/s, with ranges that don't overlap.
    • The follower's time to first token in the paired 32K cell is 8.4% higher (9.45 s to 10.25 s).

    Lone decode shouldn't pay anything for stepping, so we're tracing what the stepped path adds per round. We'll also size the fill's chunks while other streams decode.

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/gemma4-metal-prefill-step branch 2 times, most recently from 7d5259d to d9978c1 Compare November 18, 2026 01:16
ashhart added a commit that referenced this pull request Nov 21, 2026
…al_server.zig

Follows main's 498761d (build: macOS tests build without Xcode's Metal compiler, #3923), which moved
nativeServer from metal_targets.zig to metal_server.zig; #3025's test-gemma4-metal-vtable-host step goes there,
after test-drafter-skip-metal-host as before.
@ashhart
ashhart force-pushed the lead/gemma4-metal-prefill-step branch from d9978c1 to f888179 Compare November 21, 2026 18:40
…ecode between chunks

For #3021. Gemma 4's Metal backend now declares prefill_step,
cancel_prefill and reports_fill, so the lane host fills a new prompt a
chunk a round instead of running the whole pass before the next decode
round. The chunk cuts are the old loop's, moved into prefill_steps.zig
unchanged (512 rows, planned starts, kept marks, never inside an image),
and the whole pass runs the same step in a loop. Gemma still declares no
chunk_invariant, so the host never caps a chunk.

A drafting stream's chunk rows stay in the prompt scratch as the cache's
chunk_tap until the next prompt chunk, and featuresFn serves them, so the
drafted wrapper absorbs every chunk's rows after its step. A mark at a
chunk's end is kept at the next call, after that absorb, so the kept
state carries the drafter's context as before. A cancel between chunks
keeps the last landed chunk's state.

Host tests on a recording fake: one chunk a call at the old loop's cuts,
another stream's decode between two chunks, and a drafter absorbing every
chunk before a mark is kept. test-gemma4-metal-steps-host joins the Linux
nightly host list.

Rebased onto current main: the vtable host step is registered beside
main's test-uocr step, and the file stays under 600 lines.
…al_server.zig

Follows main's 498761d (build: macOS tests build without Xcode's Metal compiler, #3923), which moved
nativeServer from metal_targets.zig to metal_server.zig; #3025's test-gemma4-metal-vtable-host step goes there,
after test-drafter-skip-metal-host as before.
@ashhart
ashhart force-pushed the lead/gemma4-metal-prefill-step branch from f888179 to 3161c85 Compare November 23, 2026 22:10
@ashhart
ashhart merged commit f1b39f0 into main Nov 23, 2026
3 of 12 checks passed
ashhart added a commit that referenced this pull request Nov 23, 2026
…al_server.zig

Follows main's 498761d (build: macOS tests build without Xcode's Metal compiler, #3923), which moved
nativeServer from metal_targets.zig to metal_server.zig; #3025's test-gemma4-metal-vtable-host step goes there,
after test-drafter-skip-metal-host as before.
@ashhart
ashhart deleted the lead/gemma4-metal-prefill-step branch November 23, 2026 22:11
ashhart added a commit to laudney/TensorFold that referenced this pull request Nov 23, 2026
ashhart added a commit that referenced this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemma 4 on Metal: a new prompt freezes every other stream's decode for its whole prefill (no prefill stepping)

1 participant