Skip to content

gemma4 cuda: structured output (grammar masks in first_masked and verify) - #1386

Merged
ashhart merged 4 commits into
ashhart:mainfrom
ThomasHalwax:pr/gemma4-cuda-structured
Oct 30, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
ThomasHalwax:pr/gemma4-cuda-structured

Conversation

@ThomasHalwax

Copy link
Copy Markdown
Contributor

Fixes #1384.

  • first_masked in the Gemma 4 CUDA backend's vtable; verify takes the window's masks and checks their lengths before the forward.
  • Sampled rows under a grammar: disallowed tokens to -inf with sample.cu's existing tf_grammar_mask before the draw (no new kernel).
  • Greedy rows under a grammar: read back and picked on the host among the allowed tokens by tf_round_bf16_argmax's rule (first largest; a non-finite logit is an error), because that kernel reports a masked -inf as non-finite. Unit test for the pick (pickAllowed).

Receipts (same machine and checkpoint as #1385, with the 31B assistant):

  • Structured probe: json_schema (nested object, enum, array), json_object, tool_choice: "required" and "auto", each at temperature 0 and 0.3: 8/8 valid. Without drafter 8/8 and identical to drafted. 4 concurrent copies 8/8 each and identical to solo.
  • tool-eval-bench 2.7.0 --hardmode --trials 3: score 84 → 88 (146/174 → 154/176); TC-45 (tool_choice: required) is now scored instead of excluded.
  • Prose without a grammar is unchanged: 16/16 identical to the build without this change.
  • json_schema with thinking on: the grammar applies after the thinking part.
  • Rebased on 40c6406b: all of the above identical (structured 8/8 equal, prose 16/16 equal, 4 concurrent 16/16 equal solo).

Host: test-gemma4-cuda-host 30/30, test-cuda-host as on main, lean check adds no findings. Independent of PR A (applies alone on main); measured together with it.

Changelog suggestion: Gemma 4 on CUDA serves structured output: response_format (json_schema, json_object) and tool_choice: "required" or a named function (#1384). Greedy rows pick among the grammar's tokens on the host; drafted, undrafted and concurrent replies are identical.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @ThomasHalwax, and thanks for checking it with drafts on and off and under four streams. We read it through: verify checks every window's mask length before the forward, sampled rows go through sample.cu's existing tf_grammar_mask before the draw, and greedy rows are picked on the host by tf_round_bf16_argmax's own rule (the lower id on a tie, any non-finite logit an error), so a greedy row picks the same token in a one-row step and in a wider window. Grammar streams never reach queue, since the lane core sends them through windowed verify, so those are the only paths that need a mask. Unconstrained requests take exactly the old path, and the branch merges cleanly onto current main.

One small point: docs/api.md:347-348 lists the engines that serve structured output. Please add Gemma 4 on CUDA there, with a few words on greedy rows being picked on the host.

Next we'll run it on a GB10 with the 31B: json_schema, json_object, tool_choice: "required" and a named function at temperature 0 and 0.3, plain against drafted with DFlash (and with the assistant once #1385 lands), concurrent == solo at four streams, thinking on, and unconstrained prose against main by token hash. We'll also time a greedy structured cell, as each constrained greedy row now reads its logits back.

@ThomasHalwax

Copy link
Copy Markdown
Contributor Author

Thanks. docs/api.md names Gemma 4 on CUDA now, with greedy rows picked on the host (94c4f7b).

On the greedy structured cell, a pre-measurement on one GB10 (31B 4-bit, Google's assistant, production flags, one stream, 12-item json_schema replies of ~600 tokens, a fresh topic per request against --recall, 4 rounds per cell):

tok/s, median greedy + grammar greedy, no grammar T 0.3 + grammar
"draft": false 12.3 12.3 12.3
drafted 38.2 42.7 38.2

Undrafted, the host pick costs nothing measurable. Drafted, greedy and sampled under the grammar decode at the same rate, so the gap to the unconstrained reply comes with the grammar and its different text, not from the logits read back.

ThomasHalwax and others added 4 commits October 30, 2026 14:41
Sampled rows under a grammar get their disallowed tokens at -inf through sample.cu's tf_grammar_mask before the draw.
Greedy rows are picked on the host among their allowed tokens by the argmax kernel's rule (first largest, a non-finite
logit is an error), because tf_round_bf16_argmax reports a masked -inf as non-finite. verify checks the masks' lengths
before the forward. Host test for the pick.
@ashhart

ashhart commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Thank you @ThomasHalwax. This is in TensorFold 1.2.0, released today: https://github.com/ashhart/TensorFold/releases/tag/v1.2.0

It landed with your commits under your own name, so you're listed as a TensorFold contributor, and it's credited to you in the release notes and the changelog.

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 CUDA: structured output (response_format, tool_choice: required)

2 participants