mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1328] [CLOSED] serve + engine: token masks for JSON formats that keep MTP drafts under the mask #3387
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#3387
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/1328
Author: @ncphri
Created: 10/7/2026
Status: ❌ Closed
Base:
main← Head:f-json-mask📝 Commits (6)
6cb9538serve + engine: constrained decoding for JSON response formats: a token mask per window (mask=1, MQ/MF/MK) on the chat routed851324Merge branch 'main' into f-json-mask52922efserve + engine: MTP drafts under the token mask: multi-row MKN windows, MQ/MF/MK round-trip, consume-based lookahead on the server (no validate_tokens); mask stats behind STRATA_MASK_STATS=1a79dd4dMerge remote-tracking branch 'origin/main' into json_mtp8fc68d7Merge branch 'main' into f-json-maskeb4e23cMerge remote-tracking branch 'origin/main' into f-json-mask📊 Changes
14 files changed (+1338 additions, -19 deletions)
View changed files
📝
docs/DETAILS.md(+9 -3)➕
include/strata/core/token_mask.hpp(+162 -0)📝
include/strata/core/verify.hpp(+18 -0)📝
requirements.txt(+1 -0)➕
serve/constrain.py(+334 -0)➕
serve/fake_mask_engine.py(+89 -0)📝
serve/server.py(+70 -2)➕
serve/test_constrain.py(+430 -0)📝
setup.py(+2 -1)📝
src/core/verify.cpp(+64 -0)📝
src/program/generate.cpp(+95 -7)📝
sycl/include/strata/core/verify.hpp(+11 -0)📝
sycl/src/core/verify.cpp(+24 -0)📝
sycl/src/program/generate.cpp(+29 -6)📄 Description
Constrained decoding for the chat route's JSON response formats: a token mask per generation window, MTP drafts kept under the mask (multi-row windows).
The merged contract (
c87a9cb0,725022a) acceptstools+ JSON on/v1/responsesand validates the answer after the turn: prose that fails the schema is a retry or astructured_output_failed, and nothing stops the model from drafting off-grammar. This PR adds the enforcement half on the engine: a token mask per verify window with the MTP drafts kept under it - #334's own approach dropped drafts to single-token windows; here a T-draft window verifies as usual and cuts accepted tokens at the first masked-out one (multi-rowMKN). Where it is unique: (1) drafts are filtered, not dropped - measured 74-89 tok/s masked with the MTP head vs 52-74 without on a RTX 4090 Laptop, greedy streams sha256-identical with and without the mask; (2) optional - no llguidance or no mask-capable engine means byte-identical behavior to the merged prompt+validate path, said once in the log; (3) the validation stays ON as the mask-to-schema cross-check, not replaced.serve/constrain.py: a per-request grammar from the schema (llguidance), answered to the engine as an MKN multi-row mask block; the server computes the mask for the whole draft window with a consume-based lookahead (novalidate_tokens), so MTP drafts are filtered, not dropped. Tool-call turns leave the grammar. Optional: without llguidance or a mask-capable engine the server says so once and behavior is exactly as before.GEN ... mask=1asksMQwith the draft window before every verify window; the server answersMKN(T rows of bitmasks), the engine verifies T drafts as usual and cuts accepted tokens past the first masked-out one (token_mask.hpp: bitset parse, per-row masks,mask_cut_accepted). Advertised astoken_mask=2in INFO.STRATA_MASK_STATS=1prints one line per request (MQ exchanges, avg rows asked/back, avg adopted, MQ round-trip ms). Masked requests never take the pipelined path.set_token_mask()(chained, one row per draft slot), applied with a sampler-fallback to the best allowed token. Mirrored in CUDA (src/) and SYCL (sycl/).c87a9cb0behavior, deliberately unchanged: the tools+JSON path is /v1/responses (725022a), which this PR's mask also serves viaformat_of./v1/status.structured_output.constrained_decodingsays whether masks are live.requirements.txt/setup.py:llguidance(pinned, no transitive deps).serve/test_constrain.py(22, mock + llguidance). Measured on a RTX 4090 Laptop (swift-IQ3_XXS, same build both sides, greedy): with the mask live, the MTP head keeps paying off - natural long-form prose inside ajson_schema({title, body}): 74.0-89.5 tok/s with the MTP head vs 51.8-73.6 tok/s without the head (--spec 4 kept, prompt-lookup drafts only: the suffix drafter proposes from an earlier repeat of the context, acceptance 42-98% depending on how repetitive the answer gets), two runs each side; a deterministicnums: [0..2999]enumeration (best case: every draft is the arithmetically next token) reaches 112.5 tok/s with the head vs 60.5 without, drafts accepted 4499/4502. The mask does not fall the drafts back to T=1: 0/3692 windows on the host-side benchmark at the real vocabulary and grammar. Greedy parity holds: identical token streams (sha256) with and without the mask for the same prompt. No give-up lines in the engine log.The
DetokenizerU+FFFD line is untouched; the two tool-call constrain tests that failed on the base pass now via aREST: /[\s\S]*/grammar tail in constrain.py.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.