Repository navigation
Structured output on the Flash Next CUDA engine: response_format and guided_* enforced (patch 0032) - #69
Structured output on the Flash Next CUDA engine: response_format and guided_* enforced (patch 0032)#69devopsfortunato wants to merge 1 commit into
Conversation
|
I tested this on one DGX Spark with the Qwen3.8 Flash Next FP4 weights, current main 656879f against this branch at 06ce0fe, thinking off. |
|
Thanks for this, and for the benchmark numbers; a third party reproduced the basic (Review notes prepared with Claude Code on behalf of the maintainer.) |
Constrained decoding for response_format (json_object, json_schema) and the guided_* fields, over xgrammar v0.2.8 (libtfgrammar, from TensorFold PR #596) with a CUDA mask kernel, masked first token and MTP drafts checked against the grammar. Rebased on main and renumbered from 0016 to 0032, after 0031. The server compiles each grammar once, off the engine thread, within 10 s and at most 4 at a time; grammars over 64 KiB or nested past 64 levels are refused. prepare.sh fetches only xgrammar's dlpack submodule and runs the new libtfgrammar tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
06ce0fe to
dc5242c
Compare
|
Thanks for the review. I pushed a rebased version: one commit on Compile lock and double compile. Both are gone. The server compiles a request's grammar once, on its own thread, before the request reaches the engine. Time, size and depth limits.
Submodules. Tests.
Patch number. It is now Conflicts with the other PRs.
xgrammar as a dependency. That decision is yours. The facts:
A bug I found while re-testing. With Validation on a DGX Spark: this branch's image against main
Before the fix, this repeated test hit that 500 in round 1. After it, 60 of 60 pass. |
|
Thank you for this work. Structured output and required tool calls now work on (Review notes prepared with Claude Code on behalf of the maintainer.) |
Today the Zig engine answers
response_formatand theguided_*fields with HTTP 400 ("structured output ... is not supported by this engine yet"). Clients that rely on it (tool-eval-bench, mem0, see #66) then fall back to prompt-only JSON, and the model often answers in prose after a tool call. This PR enforces structured output by constrained decoding.What changes
patches/0032-engine-structured-output.patch(after 0031; on current main the 20 patches apply on a cleandb281878and give exactly the tested engine tree):libtfgrammar(a C ABI over xgrammar v0.2.8, loaded on the first structured request), the lane core's grammar constraint (per-row masks, drafts the grammar rejects are trimmed), and the server's parsing ofresponse_format(json_object,json_schema),structured_outputs,guided_json/guided_regex/guided_choice/guided_grammar, Responsestext.formatand Anthropicoutput_config.format.tf_fn_grammar_masksets the bf16 logits of disallowed tokens to -inf. The existing greedy, top-k or nucleus draw then runs unchanged. The mask is uploaded on the engine's stream, ahead of the kernel.held) and walked through the grammar, and each verify row is masked. A drafted structured reply therefore equals its"draft": falsereply.structuresis on at one rank only.zig build grammar-test: libtfgrammar's C ABI through the lane core (masks, a drafted window and its rollback, the grammar after</think>, xgrammar's error text for a bad schema, the compile limits including the timeout and the slot wait).zig build flashnext-grammar-mask: the CUDA mask kernel against a host reference on 5 shapes, Flash Next's 248,320-token rows and a padded 16-row drafted window among them, and the shapes it refuses.scripts/prepare.sh:3rdparty/dlpacksubmodule (googletest and cpptrace are not built);zig build grammarto buildlibtfgrammar.so, thenzig build grammar-teston it;/usr/local/libwithldconfig, and the build fails unlessctypes.CDLL("libtfgrammar.so")loads;/opt/tensorfold/share/doc/xgrammar.libtfgrammar.sois 48 MB); xgrammar's source stays in the build stage. The image hash changes, sostart.shrebuilds once.The engine commits behind the patch, for review: https://github.com/devopsfortunato/TensorFold/tree/flashnext-structured-output-v2
7ebf4431is the pin plus the recipe's patches through 0031.de2ded8aports #596's lane core and server;25d48efdnames the reply's grammar apart from the constraint method.55457fdcis the Flash Next CUDA side.53d7980cmoves the compile to the request thread with its limits and adds the tests;d7f30ac3orders the mask uploads on the engine's stream and makes a request wait for a compile slot.Evidence (one DGX Spark, NVIDIA FP4 checkpoint, THINKING=0, port 8000)
tool-eval-bench 2.8.1.dev13 (97 scenarios,
--hardmode --seed 42 --temperature 0 --timeout 300 --no-think). "Before" is the stock imagezig-db28187in two runs; "after" is this branch.response_formatfallback in the report* TC-68 sends no
response_formaton purpose (the bench's tc68.py: "intentionally NO response_format_override"), so there is nothing for the server to enforce.Every other scenario scores the same in every run. The rebased branch scores the same 97 results as the first version of this PR.
Plain requests are unchanged, against main
e1f564e's image:tools/exact.py: per-prompt text and token hashes are equal to main's; drafted =="draft": false(495 drafts accepted); concurrent == serial. PASS.tools/toolcheck.py: 3/3.tools/bench.pydecode (main → this branch): code greedy 69.9 → 70.2 tok/s; chat sampled 42.9 → 43.4 tok/s.Structured requests (a small client script, temperature 0):
json_schemawith a prose-inviting prompt,json_object(the first token is{),guided_choiceandguided_regex;</think>."draft": falseforjson_schemaandjson_object. 60 structured replies (10 rounds, drafted and"draft": false) all succeed, and each equals the first reply of its kind.json_object40.4,json_schema40.8 (main plain 41.4).tf-flashnext-grammar-maskpasses on the GB10.Limits
response_formatin one request: the grammar holds from the reply's first token, so the model cannot open a tool call in that turn. vLLM behaves the same way, and tool-eval-bench sendsresponse_formatonly after the first tool round.max_tokens: a reply cut bymax_tokenscan end mid-JSON.libtfgrammar.soonly structured requests are refused ("the server cannot load libtfgrammar.so"), plain serving does not load it.Refs #66.