Repository navigation
feat(cuda): response_format JSON schemas on the 27B, exact under drafting and --parallel - #80
philip-pentatonic wants to merge 9 commits into
Conversation
…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.
|
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 ( New checkpoint tests: a constrained prompt over two prefill steps admitted while others decode, beside a stream past its end tokens; and under RTX PRO 6000, Qwen3.8-27B MLX 4-bit with DFlash2: |
|
Landed in 0.5.0 as 98fb8f9, 7ae9da2, f41bcd3 and 7f9c8d3. Thank you! |
Adds
response_format(json_schema,json_object) and vLLM'sguided_json/structured_outputs.jsonto 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_decoderound, andMultiDecoder'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 beforesample_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 withoutresponse_formatmakes the same engine calls as before. Under--parallel Na 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 autoand4; NVIDIA PyTorch 26.07 on a DGX Spark (GB10) and an RTX PRO 6000.token_shaagainst 0.3.6.2 (greedy and seeded, drafted and"draft": false, one at a time and all at once)"draft": false, incl. thinking onTime 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) andtests/cuda/test_qwen27_grammar.py(checkpoint); with the existing 27B CUDA tests, 32 pass on both GPUs.Question. CUDA engines ignore
response_formattoday. 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 sendjson_objectas a hint will break. Would you rather keep ignoring it there, perhaps with a log line?Two small changes to shared code:
Streamcarries the constraint andScheduler.submittakes it.