Skip to content

gemma4 cuda: opt-in greedy argmax inside the target graph, no unread download - #4616

Merged
ashhart merged 2 commits into
mainfrom
lead/gemma4-argmax-graph
Nov 23, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/gemma4-argmax-graph

Conversation

@ashhart

@ashhart ashhart commented Nov 22, 2026

Copy link
Copy Markdown
Owner

What this changes

TF_GEMMA4_ARGMAX_GRAPH=1 (opt-in, default off) trims Gemma 4's CUDA verify round after the target graph:

  • Argmax in the graph. On a verify round whose windows are all greedy and unmasked, the tf_round_bf16_argmax launch over the round's rows is captured at the end of the forward's graph (a second graph set per row count), instead of being launched eagerly after it. The kernel gives each row its own block over the same logits and vocabulary, so each row's pick is the one today's per-window launches make. Sampled, grammar-masked and plain-decode rounds keep today's path.
  • No unread download. A round whose entries are all greedy picks reads only picks; the tok download that greedy entries never read is skipped. Any drawn entry downloads tok as before.
  • Pinned uploads. Each forward's ids, positions and slots are built in a pinned staging buffer and uploaded from it. An event after the copies is waited on before the next forward writes it. Wider forwards fall back to today's allocation.
  • The argmax graphs are dropped wherever the forward graphs are (taps, pages, deinit).

Same kernel, same inputs, same order: tokens cannot change. No pinned file changes.

Checks

cuda_argmax_graph_test.zig (which rounds fold, which downloads are skipped, staging reuse), the Gemma 4 CUDA host step, compile-cuda-tests for Linux; fmt, ast-check, the 600-line cap. No GPU was available.

Still to run

On a GB10, Gemma 4 31B at one stream, plain and with its DFlash drafter, switch off/on/on/off with the same prompts at temperature 0: tokens identical, and decode speed.

…download

TF_GEMMA4_ARGMAX_GRAPH=1 (default off): a verify round whose windows are all
greedy and unmasked captures tf_round_bf16_argmax over the round's rows in the
target forward's graph (a second graph set by row count; eager forwards launch
it right after), instead of one launch a window after the graph. Each row has
its own block, so every pick is the one the per-window launch made. Entries
that are all picks no longer download tok, which no greedy entry reads, and
each forward's ids, positions and slots are staged in pinned memory, reused
only after an event shows the last copy done.
@ashhart
ashhart force-pushed the lead/gemma4-argmax-graph branch from 52fcaca to 479ba3d Compare November 23, 2026 06:29
@ashhart
ashhart merged commit fba0532 into main Nov 23, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/gemma4-argmax-graph branch November 23, 2026 06:29
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.

1 participant