Skip to content

grammar: compile off the lock, wait at most 2,000 ms, run at most 4 at once - #3231

Merged
ashhart merged 2 commits into
mainfrom
lead/grammar-compile-bound
Nov 18, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/grammar-compile-bound

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Main compiles every grammar under the server-wide grammar lock with no time bound. xgrammar's compile time grows faster than the square of an object's optional properties, so one such schema holds every other grammar request behind it, cached ones included. An object of 8,000 optional properties, 207 KB, takes minutes.

What changes

  • Each compile runs on its own thread, outside any lock other requests take, against the same long-lived xgrammar compiler and caches as before. The thread gets the 8 MiB stack of the server's request threads, where compiles ran until now.
  • A request waits at most 2,000 ms for its grammar. Past that it gets HTTP 400 naming the limit, for example response_format: the grammar cannot be enforced: grammar compilation exceeds the 2000 ms time limit. This covers JSON objects and schemas, regexes, choices, EBNF and the structural tags of tool calls.
  • Each model runs at most 4 compiles at once. A request that finds all 4 busy waits for a slot within the same 2,000 ms, then gets a 400 naming both limits.
  • Identical requests share one compile. The last 8 grammars that finished in time are served without a slot or a thread, and older ones come from xgrammar's own 256 MiB cache as before.
  • The Zig caller drops its compile mutex. The C ABI only gains tfg_compiling, a slot count for tests, and an older libtfgrammar still compiles without the limit.

What happens to an abandoned compile: xgrammar has no point at which a compile can be stopped. An abandoned compile runs to the end on its thread and keeps its slot until it returns, so no more than 4 run at once. While it runs, the same input joins it instead of compiling again. Its grammar goes into xgrammar's cache, so the same request sent after it finishes is served. The new cache never stores a failure.

Why grammars within the limit are unchanged: the compiler object, calls and options are main's; only the thread they run on changed. Mask hashes after every step of an accepted reply, over 14 grammars in all six formats, match main's library exactly; first masks over a 248K-token vocabulary match too, and invalid inputs give the same errors. test-grammar-tools and test-grammar-budget pass unchanged.

Tests

  • test-grammar-compile-host, also run by zig build test, drives the compile runner with fake compiles: the deadline, late results kept out of the cache, a small compile passing a running large one, two callers sharing one compile, slot waits, ownership past close, failures, and a compile that needs 4 MiB of stack.
  • A lanes test shows one caller's slow compile no longer holds another caller (fails on main). A server test checks the time-limit refusal is a 400 naming the limit for every structured-output field and for tool-call grammars.
  • Through a real libtfgrammar, test-grammar-tools -Dxgrammar=DIR refuses the 8,000-property schema at the 2,000 ms limit while a small schema compiles beside it; a retry joins the running compile, and once that compile returns the grammar is served from the cache (fails on main).

The Windows compile-thread path builds and links here; the zig-windows CI job is its first run.

Fixes #3209

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/grammar-compile-bound branch 2 times, most recently from 3be8bcb to 5bc8588 Compare November 18, 2026 14:35
…t once

A grammar request compiled under the server-wide grammar lock with no time
bound. xgrammar's compile time grows faster than the square of an object's
optional properties, so one such schema held every other grammar request,
cached ones included, for as long as it took. A 207 KB schema takes minutes.

tf_grammar.cc now hands each compile to tfc::Cache in tf_compile.h. A hit
among the last 8 grammars that finished in time is served under its lock.
A miss runs on its own thread, with the 8 MiB stack the request threads
have, against the same long-lived xgrammar compiler, options and caches as
before. Identical pending requests share one compile. At most 4 compiles
run at once, and a request that finds all 4 busy waits for one. A request
waits at most 2,000 ms in all, then gets the 400 naming the limit.

xgrammar has no point at which a compile can be stopped. An abandoned
compile runs to the end in the background and keeps its slot until it
returns. The grammar it finishes goes into xgrammar's cache but never into
the in-time cache, so the same request sent after that is served. The Zig
caller drops its compile mutex. The C ABI is unchanged apart from
tfg_compiling, a slot count for tests, and an older library still compiles
without the time limit.

A grammar that compiles within the limit is the one main compiles, from the
same compiler object, calls and options. Masks over 14 grammars in all six
formats hash equal to main's library's at every step, and two invalid
inputs give the same errors.

Tests: tf_compile_test.cc drives the runner with fake compiles through the
deadline, late results kept out of the cache, a small compile passing a
running large one, shared jobs, slot waits, ownership past close, failures
and stack depth. It runs in zig build test and test-grammar-compile-host. A
lanes test shows one caller's slow compile no longer holds another's. A
server test checks the 400 for every format. Through a real libtfgrammar,
the 8,000-property schema is refused at 2 s while a small schema compiles,
a retry joins the running compile, and once that returns the grammar comes
from the cache.

Fixes #3209
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grammar compile time: bound it, and keep one slow schema from blocking every grammar request

1 participant