[PR #1328] [CLOSED] serve + engine: token masks for JSON formats that keep MTP drafts under the mask #3387

Closed
opened 2026-11-07 14:41:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • 6cb9538 serve + engine: constrained decoding for JSON response formats: a token mask per window (mask=1, MQ/MF/MK) on the chat route
  • d851324 Merge branch 'main' into f-json-mask
  • 52922ef serve + 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=1
  • a79dd4d Merge remote-tracking branch 'origin/main' into json_mtp
  • 8fc68d7 Merge branch 'main' into f-json-mask
  • eb4e23c Merge 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) accepts tools + JSON on /v1/responses and validates the answer after the turn: prose that fails the schema is a retry or a structured_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-row MKN). 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 (no validate_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.
  • Engine: GEN ... mask=1 asks MQ with the draft window before every verify window; the server answers MKN (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 as token_mask=2 in INFO. STRATA_MASK_STATS=1 prints one line per request (MQ exchanges, avg rows asked/back, avg adopted, MQ round-trip ms). Masked requests never take the pipelined path.
  • Verifier: 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/).
  • The chat route's 400 for tools+response_format is the merged c87a9cb0 behavior, deliberately unchanged: the tools+JSON path is /v1/responses (725022a), which this PR's mask also serves via format_of.
  • /v1/status.structured_output.constrained_decoding says whether masks are live.
  • requirements.txt/setup.py: llguidance (pinned, no transitive deps).
  • Tests: 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 a json_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 deterministic nums: [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 Detokenizer U+FFFD line is untouched; the two tool-call constrain tests that failed on the base pass now via a REST: /[\s\S]*/ grammar tail in constrain.py.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1328 **Author:** [@ncphri](https://github.com/ncphri) **Created:** 10/7/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `f-json-mask` --- ### 📝 Commits (6) - [`6cb9538`](https://github.com/Niko1221/Strata/commit/6cb953893ed114cc910b3d9c11bce255259334cc) serve + engine: constrained decoding for JSON response formats: a token mask per window (mask=1, MQ/MF/MK) on the chat route - [`d851324`](https://github.com/Niko1221/Strata/commit/d851324399ca7a3b1607d8694aac5912faa81852) Merge branch 'main' into f-json-mask - [`52922ef`](https://github.com/Niko1221/Strata/commit/52922ef5c4e0fdf66d238bc4f3403e955010c04b) serve + 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=1 - [`a79dd4d`](https://github.com/Niko1221/Strata/commit/a79dd4d1610a172511f31b9a5a8266a05be89b79) Merge remote-tracking branch 'origin/main' into json_mtp - [`8fc68d7`](https://github.com/Niko1221/Strata/commit/8fc68d7ee2978b538a6d8fd78d70e96fafa4e84f) Merge branch 'main' into f-json-mask - [`eb4e23c`](https://github.com/Niko1221/Strata/commit/eb4e23c69a6f10ae7a888100a611e8fddd5ed7cb) Merge remote-tracking branch 'origin/main' into f-json-mask ### 📊 Changes **14 files changed** (+1338 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 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) accepts `tools` + JSON on `/v1/responses` and validates the answer after the turn: prose that fails the schema is a retry or a `structured_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-row `MKN`). 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 (no `validate_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. - Engine: `GEN ... mask=1` asks `MQ` with the draft window before every verify window; the server answers `MKN` (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 as `token_mask=2` in INFO. `STRATA_MASK_STATS=1` prints one line per request (MQ exchanges, avg rows asked/back, avg adopted, MQ round-trip ms). Masked requests never take the pipelined path. - Verifier: `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/`). - The chat route's 400 for tools+response_format is the merged c87a9cb0 behavior, deliberately unchanged: the tools+JSON path is /v1/responses (725022a), which this PR's mask also serves via `format_of`. - `/v1/status.structured_output.constrained_decoding` says whether masks are live. - `requirements.txt`/`setup.py`: `llguidance` (pinned, no transitive deps). - Tests: `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 a `json_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 deterministic `nums: [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 `Detokenizer` U+FFFD line is untouched; the two tool-call constrain tests that failed on the base pass now via a `REST: /[\s\S]*/` grammar tail in constrain.py. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:41:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3387
No description provided.