Skip to content

feat(cuda): response_format JSON schemas on the 27B, exact under drafting and --parallel - #80

Closed
philip-pentatonic wants to merge 9 commits into
ashhart:mainfrom
machinegenieorg:q36/response-format
Closed

philip-pentatonic wants to merge 9 commits into
ashhart:mainfrom
machinegenieorg:q36/response-format

Conversation

@philip-pentatonic

Copy link
Copy Markdown
Contributor

Adds response_format (json_schema, json_object) and vLLM's guided_json / structured_outputs.json to the CUDA server. Qwen3.8-27B on one GPU enforces them with xgrammar, a new optional extra: pip install 'tensorfold[grammar]'.

How it stays exact. The mask goes where the 27B chooses tokens: the prefill's first token, each draft_decode round, and MultiDecoder's first token and rounds for --parallel N. Before the forward, the window is walked depth first under the grammar, and drafts no accepted path could hold are dropped: a token the grammar rejects, a stop token, and the rows under either. After the forward, each kept row's disallowed tokens go to -inf before sample_rows / sample_streams. A row's logits don't depend on the other rows and its mask depends only on its path, so greedy and keyed sampling pick the same token in a drafted window as in a serial step, and in a shared round as alone. The grammar follows accepted tokens only; with thinking on it starts after </think>. A request without response_format makes the same engine calls as before. Under --parallel N a grammar failure ends only its own stream (500, or an error event when streaming).

Evidence. Vontra/Qwen3.8-27B-MLX-4bit + z-lab/Qwen3.8-27B-DFlash2, --context 8192, over HTTP, at --parallel auto and 4; NVIDIA PyTorch 26.07 on a DGX Spark (GB10) and an RTX PRO 6000.

GB10 RTX PRO 6000
Unconstrained token_sha against 0.3.6.2 (greedy and seeded, drafted and "draft": false, one at a time and all at once) 72/72 identical 72/72
Constrained, 4 public schemas: drafted = "draft": false, incl. thinking on 18/18 18/18
Constrained and plain requests at once: concurrent = solo 24/24 24/24
Replies that validate against their schema 52/52 52/52
Drafted JSON, one stream, against the same prompts unconstrained 54–60 against 75–79 tok/s 226 against 290 tok/s

Time per round is the same with and without a grammar. The gap is acceptance (4.6–5.1 against 6.1–6.6 tokens a round), because DFlash2 drafts without seeing the grammar. Tests: tests/test_cuda_response_format.py (CPU, 39) and tests/cuda/test_qwen27_grammar.py (checkpoint); with the existing 27B CUDA tests, 32 pass on both GPUs.

Question. CUDA engines ignore response_format today. With this PR, an engine that can't enforce it (the other families, --tp 2) or a server without xgrammar answers 400 instead. vLLM does the same, and a client asking for JSON gets told rather than getting prose, but clients that send json_object as a hint will break. Would you rather keep ignoring it there, perhaps with a log line?

Two small changes to shared code: Stream carries the constraint and Scheduler.submit takes it.

…ke a grammar constraint

tensorfold.cuda.grammar reads OpenAI response_format (json_schema, json_object, text)
and vLLM guided_json / structured_outputs.json without xgrammar, and compiles schemas
with xgrammar (the new optional extra tensorfold[grammar]). A Constraint is one reply's
grammar state:

- window(tokens, parents) walks a verify window depth first (accept going down, roll
  back going up). It drops the drafts no accepted path can hold (one the grammar
  rejects, a stop token, and the rows under either) and fills each kept row's
  allowed tokens from its path.
- mask(logits, window) sets each constrained row's other tokens to -inf before
  sampling.
- advance(tokens) follows the chosen tokens only; window leaves the matcher there.
- With thinking on, the grammar starts after </think>.

The server hands a fresh Constraint to an engine with structured_output set. It answers
400 when the engine cannot enforce a schema, xgrammar is missing, the schema does not
compile, guided_regex/choice/grammar are sent, or a schema comes with a required tool
call. A grammar failure mid-reply ends that request with 500 (an error event when
streaming). A request without response_format calls the engine exactly as before.
…nd concurrent alike

The mask goes where the 27B chooses tokens: the prefill's first token and
draft_decode's verify rows, and on the --parallel N path MultiDecoder's first token and
rounds. Before the forward, a constrained window drops the drafts no path can keep; after
it, each row is masked by its path; the grammar then follows the accepted tokens. A
row's logits do not depend on the other rows and its mask depends only on its path, so a
constrained reply equals its "draft": false reply, and under --parallel N its solo run.

Under --parallel N a grammar failure ends its own stream after the round; the other
streams go on. Stream carries the constraint and Scheduler.submit takes it. Two ranks
leave structured_output off: rank 1 would need every row's mask too.
…its checkpoint

tests/test_cuda_response_format.py (no GPU): request parsing and refusals, schemas
compiled or refused with xgrammar's reason, windows and masks against a fresh matcher,
thinking, the server's 400/500 paths, and drafted equals serial for random draft trees
through window, mask and advance.

tests/cuda/test_qwen27_grammar.py (TENSORFOLD_MLX_MODEL, TENSORFOLD_QWEN27_DRAFTER):
three public schemas, greedy and seeded, drafted equals serial and the replies validate;
the same with thinking on; unconstrained drafted still equals serial; constrained and
plain requests sharing rounds on the --parallel 4 decoder equal their solo runs and the
one-stream serial replies.
The API reference lists response_format, guided_json and structured_outputs.json, says
where they are enforced and what is refused; the 27B recipe points to the grammar extra.
0.3.6.3 gives the 27B's prefill a prompt-end entry one token early (keep_at, a third return item), decodes the
one-stream reply in place, ends each stream at end tokens only when its request stops there (stop_eos, from
ignore_eos), and brings images, stop strings and a server that answers failures with 500 or a stream error event.

The grammar keeps its places: the prefill's first token (prefill and first_token, which MultiDecoder's fill steps
call at a prompt's end while other replies decode), every draft_decode round, and MultiDecoder's shared rounds, whose
windows are kept and masked before sampling and whose chosen tokens the grammar follows before Stream.take, now with
the stream's own end tokens. Scheduler.submit and Qwen27Engine.generate take constraint beside stop_eos and vision;
generate hands it to the scheduler only when there is one, so plain submissions keep 0.3.6.3's arguments. The
server's PreparedRequest carries the compiled grammar on text and image prompts; a failed grammar still answers 500
with type server_error, through 0.3.6.3's handler, which now logs it.
0.3.6.3 lets a request ignore end tokens (ignore_eos), and a structured reply's grammar allows the end token only once
the value is complete: past it the grammar is spent and the reply would go on unconstrained. A schema with
ignore_eos: true now answers HTTP 400, as a schema with tool_choice "required" does, before a stream opens.
…quals its solo and serial runs

MultiDecoder prefills a long prompt STEP rows at a time between the decoding streams' rounds and chooses its first
token at the last step. The checkpoint test admits a constrained prompt of over two steps while a constrained and a
plain stream decode, the plain one past its end tokens (stop_eos=False); each reply equals its solo run through the
decoder and its "draft": false reply.
…0.3.6.3 does for a plain request

0.3.6.3's host tests stand in for prefill and draft_decode with recorders that take its arguments only. A request
without a schema now passes no constraint to them, as it passes none to the scheduler; a constrained request is
unchanged.
… its own stream

Under --parallel 4 on the checkpoint: one request's grammar fails at a round's window, another's at its first token
(the prefill's last fill step); each ends with its GrammarError, the two constrained requests beside them equal their
serial replies, and the next request is served as before.
@philip-pentatonic

Copy link
Copy Markdown
Contributor Author

Updated for TensorFold 0.3.6.3: this branch now has 0.3.6.3 (1911880) merged in, plus four commits. 0.3.6.3 adds no place where the 27B's CUDA engine chooses a token, so the mask sits where it did: the prompt's first token (decode.prefill, and first_token at a prompt's last prefill step while other replies keep decoding), every draft_decode round (now in place), and MultiDecoder's shared rounds, where the grammar follows the chosen tokens before Stream.take, now with each stream's own end tokens. keep_at, images and per-request stop_eos pass through unchanged, and constraint only travels when a request has a schema, so a plain request makes 0.3.6.3's own calls. A schema sent with ignore_eos: true now gets a 400, since the grammar's end token ends a structured reply.

New checkpoint tests: a constrained prompt over two prefill steps admitted while others decode, beside a stream past its end tokens; and under --parallel 4, a grammar failing mid-reply or at the first token ends only its own request.

RTX PRO 6000, Qwen3.8-27B MLX 4-bit with DFlash2: tests/cuda/test_qwen27_grammar.py 12/12, and the 27B multi, forward, prefill, client_gone, sampling, ignore_eos, prompt-end cache and server-error tests pass. Against 0.3.6.3 itself every unconstrained reply keeps its token SHA-256 at --parallel auto and 4 (24/24 one at a time, 12/12 concurrent). Constrained replies equal "draft": false (9/9) and their solo runs when mixed with plain requests (12/12), and 26/26 validate.

@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Landed in 0.5.0 as 98fb8f9, 7ae9da2, f41bcd3 and 7f9c8d3. Thank you! response_format JSON schemas now work on the CUDA server, exact on the 27B under drafts, serial and concurrent alike. We then moved structured output into one engine module, so every CUDA engine and the Mac lane engine take it (1da4b7e).

@ashhart ashhart closed this Sep 29, 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.

2 participants