Repository navigation
Native structured output: libtfgrammar (xgrammar, loaded at runtime), masks in the lane core, enforced by Qwen3.5-2B on Metal - #596
Conversation
c390c62 to
0e03020
Compare
…itmask a verify row libtfgrammar (zig/src/grammar/tf_grammar.cc): a C ABI over xgrammar's C++ library, as tensorfold/engine/grammar.py drove the Python package: the tokenizer.json vocabulary, the logits' width and the eos ids; json and json_schema with 32 blanks, regex, choice, EBNF; matchers that accept, roll back, report termination and fill next-token bitmasks; xgrammar's errors without their timestamp and source location. `zig build grammar -Dxgrammar=DIR` builds it from an xgrammar v0.2.8 checkout into zig-out/native/lib; nothing from xgrammar is in the tree, and the server builds and serves text without it. The server opens one compiler per model on the first structured request, compiles each request's grammar before admission (a 400 with xgrammar's reason when it cannot, or naming the library when it cannot load) and hands the engine the compiler, whose compile is then a cache hit. The lane core owns each stream's grammar (lanes/grammar.zig, Python's Constraint): it follows every committed token, and before each verify keeps the rows an accepted path can use (a draft the grammar rejects goes with its rows, nothing is verified after the stop token; held chains come back by the head's ranks) and hands the backend one allowed-token bitmask per kept row (Window.masks), the first token's through Backend.first_masked. A grammar stream takes verify windows, never pipelined steps; under a grammar the thinking budget's close ends at the think end, and the grammar's stop token ends a reply that ignores eos. A token the grammar rejects ends that reply alone. Engine tests on the fake: drafted equals one-token decode, shared rounds of grammar and plain streams equal each alone, greedy and sampled, through a forced close.
The 2B's draws take only a row's allowed tokens: the draw Python's -inf logits give, since a -inf candidate never wins the keyed draw nor moves top-p or min-p (a host test checks it against the full-vocabulary draw). tools/zig/grammar_masks.py: libtfgrammar's masks, accepts, rollbacks and termination against xgrammar 0.2.8's own matcher on a fixed grammar set, through the C ABI the server loads. tools/zig/structured_served.py: every field and kind, greedy and sampled, thinking off and on, valid and drafted equal to "draft": false; all at once equal to alone with plain requests mixed; the 400 refusals.
0e03020 to
8ced201
Compare
|
Thank you, @BobClawblaw. This is in 1.0.6: merge What we added on top, and why:
Checks on this tree: your mask oracle, extended with the two tool grammars, finds 0 mismatches in 1,134 steps against One thing we saw on the 2B in the release gate: on an integer field it wants to write as a decimal (an invoice's |
lane_host.zig was at 596 lines with ashhart#578 and ashhart#596 still to add; the lesson queue and its idle-only step are their own job. A move, with words() made public and the two lock wrappers inlined.
…loaded at runtime), masks in the lane core, enforced by Qwen3.5-2B on Metal Thanks @BobClawblaw. Conflicts with ashhart#623 kept both (lane_host.zig's fillOne/unfill beside the constraint; addStream's two changes ported into lanes/open.zig: drawFirst for the first token, no pipelined first step under a grammar; both engine tests kept).
The API page says which native engines enforce structured output and required tool calls, where libtfgrammar comes from and what is refused. The 2B's masked-draw comment and the served harness's docstring are one line each.
…nds across runner slots, DFlash2 for one stream, and tf-qwen27-bench On the 1.0.6 candidate: the stream spec and the fake backend carry ashhart#596's grammar beside ashhart#578's decode spans and memory lanes, and the lane host gives a stream both. ashhart#596 gave the 2B's draw a grammar mask; the 27B's lane backend passes none, as it serves no structured output. Thanks @BobClawblaw.
lane_host.zig was at 596 lines with ashhart#578 and ashhart#596 still to add; the lesson queue and its idle-only step are their own job. A move, with words() made public and the two lock wrappers inlined.
…loaded at runtime), masks in the lane core, enforced by Qwen3.5-2B on Metal Thanks @BobClawblaw. Conflicts with ashhart#623 kept both (lane_host.zig's fillOne/unfill beside the constraint; addStream's two changes ported into lanes/open.zig: drawFirst for the first token, no pipelined first step under a grammar; both engine tests kept).
The API page says which native engines enforce structured output and required tool calls, where libtfgrammar comes from and what is refused. The 2B's masked-draw comment and the served harness's docstring are one line each.
The author rebased ashhart#596 onto main after our merge; this keeps the PR's current head in the history with no change to the tree.
…nds across runner slots, DFlash2 for one stream, and tf-qwen27-bench On the 1.0.6 candidate: the stream spec and the fake backend carry ashhart#596's grammar beside ashhart#578's decode spans and memory lanes, and the lane host gives a stream both. ashhart#596 gave the 2B's draw a grammar mask; the 27B's lane backend passes none, as it serves no structured output. Thanks @BobClawblaw.
Closes #564. This is structured output landed with the first family that enforces it, per your answers there: an optional xgrammar library loaded at runtime, masking in the lane core, and the gate you set.
What changes
libtfgrammar(zig/src/grammar/tf_grammar.cc, about 200 lines): a C ABI over xgrammar's C++ library, driven the waytensorfold/engine/grammar.pydrove the Python package.Build.
zig build grammar -Dxgrammar=DIRbuilds it from an xgrammar v0.2.8 checkout (pinned inzig/build/grammar.zig) intozig-out/native/lib; it took about 10 s on an M5 Ultra. Nothing from xgrammar is vendored. The server loads the library on the first structured request, looking beside itsbinfirst. It builds and serves text without it, and a structured request then gets a 400 that names the library.Server.
vocab_sizeand the eos ids.Lane core (
lanes/grammar.zig, Python'sConstraint). The core owns each stream's grammar:alternatives); a held tree verifies no drafts.Window.masks). The first token's mask goes throughBackend.first_masked. Forced rows stay in the window.error, with a message); the round goes on.First family: Qwen3.5-2B on Metal sets
Info.structures. Its draws consider only a row's allowed tokens. That is the draw Python's -inf logits give: a -inf candidate never wins the keyed draw and doesn't move top-p or min-p. A host test checks this against the full-vocabulary draw at several top-k, top-p and min-p settings.Gate
M5 Ultra, macOS 27.0.1,
mlx-community/Qwen3.5-2B-MLX-4bit@93760be4, xgrammar 0.2.8.tools/zig/grammar_masks.py). It drives libtfgrammar through the C ABI the server loads, side by side with the Python package'sGrammarMatcher, on a fixed set of 11 grammars: json object, three schemas (flat, nested with arrays/enums/limits, anyOf), three regexes (date, email, unicode), two choices (one with quotes, backslash and tab) and two EBNF grammars. The walks accept random allowed tokens, try disallowed ones and roll back. 855 steps, 0 mismatches in bitmask, accept and termination. Three malformed grammars are refused by both.tools/zig/structured_served.py,--parallel 4). Every field and kind (response_formatjson_object and json_schema,guided_json,guided_regex,guided_choice,guided_grammar,structured_outputs, plus a plain request), each greedy and seeded-sampled, thinking off and on. The thinking runs carry a 256-token thinking budget, so a forced close comes before the grammar."draft": false.response_formattype, a grammar withtool_choice: "required", and a server that can't loadlibtfgrammar.zig build testpasses. One step (run test w) fails intermittently on main as well, 1 in 5 runs on an unmodified checkout.Not covered here
zig build grammaron Linux/CUDA isn't built yet. The step and the loader (libtfgrammar.so) are written for it, and the CUDA families' mask step would follow as its own PR.