mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4113] [Engine] GBNF constrained decoding: stable tool calls + JSON schema, now riding the MTP drafter #4906
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#4906
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/4113
Author: @sd133cn
Created: 10/25/2026
Status: 🔄 Open
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
95 files changed (+37099 additions, -83 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)➕
bench/results/p0-mtp-ab/mtp-ab.json(+57 -0)➕
bench/results/p1-mtp-grammar-4090/grammar-acceptance.json(+127 -0)➕
bench/results/p1-mtp-grammar-4090/json-items.jsonl(+50 -0)➕
bench/results/p1-mtp-grammar-4090/serve.cfg.json(+1 -0)➕
bench/results/p1-mtp-grammar-4090/tool-items.jsonl(+30 -0)➕
bench/results/p2-batch-mtp-smoke/summary.json(+12 -0)➕
bench/results/vocab-fix-grammar-4090/grammar-acceptance.json(+123 -0)➕
bench/results/vocab-fix-grammar-4090/json-items.jsonl(+50 -0)➕
bench/results/vocab-fix-grammar-4090/serve.cfg.json(+1 -0)➕
bench/results/vocab-fix-grammar-4090/tool-items.jsonl(+30 -0)➕
bench/results/vocab-fix2-grammar-4090/grammar-acceptance.json(+149 -0)➕
bench/results/vocab-fix2-grammar-4090/json-items.jsonl(+50 -0)➕
bench/results/vocab-fix2-grammar-4090/serve.cfg.json(+1 -0)...and 75 more files
📄 Description
Problem and result
Constrained decoding (Phase 1) refused every speculative request and - the incident that triggered this rework - degraded long Chinese constrained answers into filler characters. This PR makes grammar requests ride the MTP drafter /
--batch-mtpwith per-row masks, and fixes two vocabulary-layer defects the acceptance suite was blind to:--mtp/--batch-mtp: every row of a drafted verify window samples from the mask of the state its draft prefix leaves (a scratch clone walks the drafts before the window; a draft the grammar refuses truncates the chain, and the tail rows can never be emitted because the row before them was masked against the state the draft jumps from). Related: #3862, and PR #3922 (closed at the author's request; this reworked branch is that continuation).符号→符好,好好好runs under any grammar. Root cause: the grammar vocab loader decoded only part of the GPT2bytes_to_unicodemap, leaving 88,990 of 248,320 pieces invalid UTF-8, so every grammar mask banned ~36% of the real vocabulary (most CJK second bytes). With the map complete, a second defect surfaced: the byte-0x00 token's piece is a real NUL, which the accept walk treats as the code_points terminator - it "matched" every state as an empty piece and its untrained logits won greedy picks, leaking a control byte into constrained JSON. Both are fixed and pinned by unit tests.Result on the rig (RTX 4090, Swift IQ3_S, engine args
--spec 2 --mtp <rt>): JSON schema suite 50/50 with 0 canary 502s, tool calls 30/30 with 0 rescued, constrained 137.2 vs plain 130.5 tok/s median - the drafter more than covers the mask cost. Plain requests (no grammar named) are byte-identical to before: the sampler mask is a null pointer.Related discussion: #3862 (scope). PR #3922 was the Phase 1 cut, closed to rework speculative-decoding coexistence into this branch.
Approach
[0, kVerifyMaxT), the batch slots' base rows (sized with--batch), and--batch-mtp's chain rows indexed by window row (never by slot, so any--batchstays in bounds).--batch-mtpis not capped tokVerifyMaxTslots (the windows rotate), which the first cut got wrong - caught in code review before it could ship.STRATA_GRAMMAR_DEBUG=1traces the chain row by row.grammar_build_maskbans pieces with an embedded NUL exactly as llama.cpp'sapply_implbanspiece[0] == 0.Validation
Passed (RTX 4090 48GiB, Windows, Swift-Qwen3.8-Flash-Next IQ3_S, engine 0.1.50, commit
1de0d048):python -m pytest-style unit level:grammar_test,grammar_registry_test,grammar_decode_test(new chain closed-loop: legal and refused-draft windows; new byte-map and NUL-piece pins) - all pass.ple_parity- environmental, the rig has no Q2_0 fixture; fails identically on the pre-change base), 7 skipped.python -m unittest discover -s serve: 1058 tests, 0 failures (after fixing the winjob test's CJK-localetasklistdecode on this machine).tools/grammar_acceptance.py --stamp nul-fixwith--mtp: JSON 50/50, canary 0, tools 30/30, plain 130.54 vs constrained 137.17 tok/s median. Raw logs and item-level JSONL inbench/results/nul-fix-grammar-4090/.root ::= "yes" | "no"yieldsn,o,<|im_end|>and stops; before the NUL fix it spammed the NUL byte.--mtp(pre-merge base): JSON 50/50, tools 30/30, constrained cost -2.6% (bench/results/2026-10-24-1332-grammar-4090).Failed: none observed on this rig after the fixes. (Two intermediate gate runs during debugging were contaminated by my own process cleanup and an orphan engine; their numbers are superseded and noted in
bench/results/vocab-fix3-grammar-4090/notes.json.)Not run:
Limits
spec_min_p+ grammar combination stays refused (the startup--spec-min-pis allowed); the floor's drafts vs the chain ordering is unmeasured.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.