mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3922] [CLOSED] [Engine] GBNF constrained decoding: stable tool calls + JSON schema #4837
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4837
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3922
Author: @sd133cn
Created: 10/24/2026
Status: ❌ Closed
Base:
main← Head:feature/gbnf-constrained-decoding📝 Commits (10+)
3429acbSource probe: --source auto measures Hugging Face and ModelScope per repository (a 2 MB Range read of each, the MB/s printed) and downloads from the faster host; the file is checked against ModelScope's published SHA-256 (#908's never-switch rule relaxed for the measured winner; explicit --source skips the probe).50813a0Merge origin/main (v0.1.42-0.2.2): keep the source probe (model_source(url) + probe_source against upstream's fetch_mtp, which honors an explicit STRATA_SOURCE); docs' ModelScope notes sit in upstream's strata launch structure; Swift IQ3_S converges on upstream's wording (swift Q2_0 stays qwen-only, #171).db1549etests: regenerate the setup golden file after the merge (upstream's launch plans record 'exe'; the source-probe announcement words ride along)a9cc2c2chore: 添加 .zcodeignore 配置及引擎更新状态文件f796d48docs: GBNF constrained-decoding spec (reviewed) and 13-task implementation plan677efdcGrammar: port the llama.cpp GBNF core into strata::grammar (parser, stacks, accept; MIT attribution kept)34d8145Grammar: GGUF vocab (full token arrays, control/eog flags) and the bitmask builder with a stack-key LRU cache0a9ffa0Grammar: per-row bitmask in the sampler kernels at the logit_bias site (null default = byte-identical path); parity pins bans, order, and immutability51c8643fix: MSVC full-build blockers on Windows: gdn_chunk_parity/gdn_chunk_bench/hc_persist_parity env setters (decode_cluster_parity pattern), native_hadamard_test std::popcount, ggml_tq_overlay_test.cmake CMP0057 NEWdc16a10Grammar: GRAMMAR registration line + grammar= request key + hard grammar+spec rejection; per-slot state stacks with per-step advance at the token readback and a mask ring H2D into the sampler; mask cache keyed against the owning grammar's rules (clone stacks); closed-loop GPU decode test📊 Changes
74 files changed (+35708 additions, -93 deletions)
View changed files
➕
.zcodeignore(+84 -0)📝
CMakeLists.txt(+43 -1)📝
bench/gdn_chunk_bench.cpp(+10 -2)➕
bench/results/2026-10-24-1332-grammar-4090/grammar-acceptance.json(+123 -0)➕
bench/results/2026-10-24-1332-grammar-4090/json-items.jsonl(+50 -0)➕
bench/results/2026-10-24-1332-grammar-4090/serve.cfg.json(+1 -0)➕
bench/results/2026-10-24-1332-grammar-4090/tool-items.jsonl(+30 -0)📝
cmake/ggml_tq_overlay_test.cmake(+4 -0)📝
data/gguf_fingerprints.json(+20 -0)📝
docs/AI_SETUP.md(+12 -3)📝
docs/DETAILS.md(+61 -9)📝
docs/INSTALL.md(+10 -7)📝
docs/TERMINAL_LAUNCH.md(+3 -0)➕
docs/superpowers/plans/2026-10-24-gbnf-constrained-decoding.md(+471 -0)➕
docs/superpowers/specs/2026-10-23-gbnf-constrained-decoding-design.md(+121 -0)➕
engine-feed-new/.updates/state.json(+11 -0)➕
engine-feed/.updates/state.json(+29 -0)📝
include/strata/artifact/gguf_reader.hpp(+9 -6)➕
include/strata/grammar/compat.hpp(+10 -0)➕
include/strata/grammar/grammar.hpp(+208 -0)...and 54 more files
📄 Description
The problem
Strata's JSON and tool-call outputs are prompt-and-validate today: the server appends an output-format
directive, the model improvises the shape, and the server validates (and on failure, the whole request
fails with 502). Long tool calls and deeply nested schemas are exactly where prompting runs out:
ZCode's and Claude Code's tool grammars (ZCode's own tool calls run through GBNF), and the agent harnesses
most of our users plug in, need decoding that cannot leave the shape.
The result
Constrained decoding is now real: every token is sampled from the set the grammar allows.
response_format(json_object,json_schema) decodes schema-valid output directly. On agrammar-capable engine the server registers the converted grammar, drops the prompt directive, streams
unbuffered, and keeps the validation as a canary (502 only on an engine bug).
/v1/responses, Anthropic) are grammar-constrained from the templateserialization: required/named/auto (with prose), parallel calls, the
</think>prologue. No morecalls that fail to parse.
grammarparameter (GBNF text) rides the same path for agents that write their own grammars./v1/statusreportsmethod: "grammar",constrained_decoding: true.Measured on the RTX 4090 rig (Swift IQ3_S, engine 0.1.47 build, 2026-10-24, results in
bench/results/2026-10-24-1332-grammar-4090/): the JSON suite answers 50/50 inside its schemas(5 schemas × 10 prompts, effort low, temperature 0.6), the tool suite parses 30/30 calls with 0
failures and 0 rescued from the reasoning stream, protocol smoke 6/6. Cost: a 400-token answer at
effort none ran at a median of 84.8 tok/s plain vs 82.6 tok/s constrained (−2.6%), with a one-time
mask-kernel warm-up on a session's first constrained request (26.9 tok/s once); with 6 concurrent slots
the aggregate fell 172.1 → 140.1 tok/s (−18.6%) — the per-slot mask upload rides every generated token.
The approach
src/grammar/(MIT kept, per-fileattribution). The GGUF vocab is loaded from the model's
tokenizer.ggml.tokens+ token_type.per-row bitmask; the CUDA/HIP sampler applies it at the logit-bias read site (bit=1 bans → -inf),
before bias and penalties.
grammar_mask = nullptrkeeps the default path byte-identical.GRAMMAR <sha16> <byte_len> <text>(register once per grammar; parsed, cached),and
grammar=<sha16>rides the GEN line per request. Slots advance their state stacks at the tokenreadback; the per-slot mask ring is one H2D per token — free next to decode.
solo) resends the prompt with what it generated and names the tail with
gseed=<n>: the enginereplays those tokens through the grammar so the state and mask continue where the last pass left
off. A tail that does not fit the grammar is refused (400), never silently unconstrained.
vendor/json_schema_to_grammar/) and built asthe
grammar_schemaCLI; the server shells out to it (zero new dependencies).Ċ→\n,Ġ→, 0xA1-0xFF byte tokens) is applied at vocab load —Qwen's GGUF tokens are symbols, and GBNF literals are text. (Found on the rig: without it every
whitespace token is banned.)
--spec,spec_min_p) is refused at the request boundary;suffix/n-gram drafting is silently off for grammar requests (drafted windows sample masks built for
the wrong prefix).
Validation
(grammar mask, 3 kernel paths), GPU accept→mask→sample closed loop (
grammar_decode_test, 100rounds, all legal), grammar_schema 77 golden cases from the vendored llama.cpp converter suite, serve
grammar suite (24), builder suite (12), full ctest on this rig (156/157 passed, 7 skipped; the one
failure is
ple_parity, which needs Q2_0 model fixtures this rig does not have), full serve suite(953 tests; the one error is
test_winjob's tasklist read, which fails the same way with thisbranch's changes stashed), tools suite (1142; the failures are the setup/MCP download and process
tests that fail on this box regardless of branch), acceptance on the 4090 rig (above).
spec refusal — all green.
mirrored in the SYCL tree; recorded in the Task-7 commit.
npm test: 4 failures are Windows symlink-permission (EPERM) on fixtures this rig's gitcheckout does not create; they fail the same way without this branch.
tests/terminal: main tree on the same rig fails 59/28 (failures/errors, 86 skipped); this branchfails 54/30 — the same environmental class (download pins, MCP process tests, Windows fixtures),
no grammar-related failure.
Limits
those engines keep the prompt path (the server detects
grammar=1in INFO).in the acceptance numbers; the model's thinking budget is untouched.
grammar+ tools/MCP on one request: tool grammars take the tools route; baregrammar+ toolsis a 400.
%llguidancebranch is bypassed.process-time read returned 0); the 6-slot aggregate throughput stands in as the measured proxy.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.