Repository navigation
template: a chat template is held to a nesting depth, and its render to steps, stack and memory - #3072
Conversation
|
Review: the limits are right and far above real use. All 10,867 cases of the template parity corpus render byte-identically on main and here, and so do 16 real templates on agent requests of up to 1,000 turns with 128 tools. The most any real template used:
One fix before landing: rendering is 3–9% slower on large requests, and templates re-render every turn, cache hits included. The cause is
We'll push these.
|
|
Review fixes pushed (1ed0f31):
Results:
|
…to steps, stack and memory A checkpoint's chat template had no limit at all. 100,000 nested parentheses ran the parser off its stack, a sum of 100,000 terms built a tree that tall for the scope pass and the renderer, two nested loops over range(100000) never finished, `'' * 10**18` counted to its end, and a macro that calls itself inside a filter chain overflowed the stack before the macro limit. The parser now refuses more than 256 levels of its own recursion and any expression taller than that. A render stops by name past 50 million steps, 4 MiB of stack or 1 GiB of scratch, and an empty sequence repeated is empty at once. The stack is measured, not counted in levels, because frames differ too much in size between expression kinds.
… byte cap counts only what was given Every expression took a step and a stack check, which slowed renders of large requests by a few percent. The check now runs once a statement list, which every macro call and loop body passes through; an expression between two checks is at most the parser's 256 levels tall. A loop's filter runs no statement list, so each item it tests is now a step of its own, or a filtered loop inside another loop ran uncounted. The render arena asks to grow a block in place before it takes a new one, and the cap kept the bytes of every refused request; it gives them back. test-template-limits-host runs the limits tests on Linux and macOS.
1ed0f31 to
79062a9
Compare
Figures and scope corrected to the PRs (#2948, #2863, #2934, #2933, #2921, #2906, #2878, #2892, #2851, #2728, #3072, #2915, #2864, #2854 and others), promises of later work removed (#2903, #2875, #1630, #1720), #3074 and #3047/#3046 said as their PRs scope them, the duplicate #2861, #2585, #2321 and #2719 entries and two copies of released 1.2.5 lines removed, #1953's line named for its PR, and a line for #3255. Released sections unchanged.
What this changes
Before: a checkpoint's chat template had no limit. 100,000 nested parentheses ran the parser off its stack, a sum of 100,000 terms built a tree that tall for the scope pass and the renderer, two nested loops over
range(100000)never finished,'' * 10**18counted to its end, and a macro that calls itself inside a filter chain overflowed the stack before the macro limit. After: the parser refuses more than 256 levels of its own recursion and any expression taller than that, a render stops by name past 50 million steps, 4 MiB of stack or 1 GiB of scratch, and an empty sequence repeated is empty at once.SECURITY.mdasks for private reports, so there is no public issue.atc overlap --json: not run.Checks on this head
d8816683591d87798ada0da36506d9d1cf22cf6fpython3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.zig fmt --checkandgit diff --check: passzig test --dep sys -Mroot=zig/src/core/template/template.zig -Msys=zig/src/platform/sys.zig -lc: all 38 tests passed, and again with-OReleaseFastzig build test-server-cpu: 5/5 steps succeeded; 1325/1332 tests passed (7 skipped)zig build test-cuda-host: 391/391 steps succeeded; 4254/4289 tests passed (35 skipped)zig build test-golden test-server-security test-pull-security: 76/76 steps succeededzig build check-native-cuda-host compile-flashnext-mtp-parity-host compile-glm-native-open: 11/11 steps succeededzig build native: 66/66 steps succeededsecurity: a template nested past the parser's depth is refused before anything recurses through itsecurity: a render stops at its steps, its recursion and its memory, and says whichzig build testand every step that runs on a GPU were not run: this Mac's GPU was serving a model, and no CUDA, ROCm or Intel hardware was available.Receipt
Checklist
For a change that reaches inference:
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Not measured: no served run.Notes for the reviewer
Suggested changelog line
Chat templates: a template nested too deeply is refused at load, and a render stops by name at 50 million steps, 4 MiB of stack or 1 GiB of memory.