Repository navigation
tool_parse: a Gemma call's strings go back in one pass - #881
Conversation
gemmaCall put each <|"|> string back with its own replaceOwned over the whole call, so a call took time in proportion to its strings times its length. putBack replaces every mark in one pass and gives the same text as replacing the marks in turn, lowest index first.
|
Thank you, @jishnuvenugopal, for covering overlapping NUL markers against the old replacement order. At 651bbe7, the JSON-quoted replacements and index/offset priority preserve the production parsing choices. Please narrow the complexity claim to removing quadratic restoration, with the JSON-quoted replacement precondition stated alongside it. The functional change looks ready for parser integration checks. Posted by Ash's AI Assistant Synthena |
|
Thank you for checking the shared-NUL order. Both points are right: |
… against the old search #881's putBack is exact only for replacements without a NUL, which json.quote guarantees for its one caller; its doc line now says so, and the sort line says the cost: one pass over the text, then M log M for M marks. The two timing tests from #882 and #883 now name what they measure (each opener, and each key and value end, searched once) instead of claiming that all tool parsing is linear. A new test parses 20,000 random GLM calls (keys without values, values without closers, repeated keys, stray tags, whitespace) and checks each against the same pairs found by #883's parent loop, written out plainly: the arguments must be identical.
…VIEW names the refused GLM pair requests rightly One line each for #881, #882, #883, #889, #890, #709, #848, #892, #867, #868, #871 and #873, crediting their authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0 (backend.zig and engine.zig both test temperature > 0).
|
Thank you, @jishnuvenugopal, for removing the repeated whole-call copies when restoring strings. #881 is included in 1.0.12 as merge 5b8cf1a, at 651bbe7, and your last commit 8e7038e, which states putBack's precondition in its doc comment, is merged after it in 1ace081, so this pull request shows as merged. Its 20,000-case comparison against the old replacement loop runs in the server tests. In 084406f, putBack's comment now states the no-NUL precondition that json.quote guarantees for its caller, and the sort's cost is described as M log M for M marks. The macOS suite passes 2,968 of 2,978 tests with 10 skipped. The Linux suite passes 1,368 of 1,375 with 7 skipped. Thank you for tracing the parsing cost to this operation. Posted by Ash's AI Assistant Synthena |
…copy of the call per string
… against the old search ashhart#881's putBack is exact only for replacements without a NUL, which json.quote guarantees for its one caller; its doc line now says so, and the sort line says the cost: one pass over the text, then M log M for M marks. The two timing tests from ashhart#882 and ashhart#883 now name what they measure (each opener, and each key and value end, searched once) instead of claiming that all tool parsing is linear. A new test parses 20,000 random GLM calls (keys without values, values without closers, repeated keys, stray tags, whitespace) and checks each against the same pairs found by ashhart#883's parent loop, written out plainly: the arguments must be identical.
…VIEW names the refused GLM pair requests rightly One line each for ashhart#881, ashhart#882, ashhart#883, ashhart#889, ashhart#890, ashhart#709, ashhart#848, ashhart#892, ashhart#867, ashhart#868, ashhart#871 and ashhart#873, crediting their authors. ZIG-PREVIEW said a two-Mac GLM pair refuses "temperature-1" requests; it refuses any temperature above 0 (backend.zig and engine.zig both test temperature > 0).
…ndition @jishnuvenugopal's newest ashhart#881 head (56885ed) words putBack's doc comment to state that no quoted string holds a NUL. cae5e8aad, landed with ashhart#881, already states it in that comment, so the tree stays as it was.
v1.0.12 (7943cad) brings b12's fifteen pull requests, Nemotron's oQ widths on Metal, ashhart#881's last head, the oQ docs and the bench tools' tile fix. It merged without a conflict.
What this changes
gemmaCall(zig/src/server/tool_parse.zig) reads Gemma 4'scall:NAME{...}by swapping each<|"|>string for anumbered NUL mark, quoting the bare keys, and putting the strings back. It put them back with one
std.mem.replaceOwnedover the whole call per string, which is quadratic in the number of strings.putBacknowsorts the marks and writes the call out in one pass: O(M log M) in M marks at worst, when the call's own NULs put
marks out of order (they are in order otherwise). The result equals replacing the marks in turn because every
replacement is JSON-quoted, so none contains a NUL (
json.quoteescapes bytes below 0x20). That includes two markssharing a NUL, which only the call's own NUL bytes can cause: the lower index still wins, then the leftmost.
Verified outcome
took about 4 seconds of CPU after the model had finished writing it.
results, and a test that checks the new way against the old one on 20,000 random inputs passes.
Receipt
Environment:
mainat c7137bb, Zig 0.17.0, Apple M4, macOS 27.0.1. Host code: no model or GPU.Exactness: n/a. No engine change; the reply's tokens and text are the same.
Decode speed before and after: n/a.
bench_openai.py's prompts offer no tools, and only replies to requests thatoffer tools are parsed.
Prompt speed before and after: n/a, for the same reason.
Speed of
parseon one call ofq:<|"|>a<|"|>,repeated, in ReleaseSafe as the server ships, fastest of three:Same output: a local fuzz harness (not part of this change) ran 50,000 edited replies through
parse,closeCall,hideToolCallsandfinishedProseon c7137bb and on this branch. A digest of everything they returned, call idsleft out, is equal (4c8dc88dffd56f19).
Tests run:
a Gemma call's strings go back in one pass(tool_parse_test.zig): one call with 1,200 strings and one with9,600. 8x the strings may take at most 24x the time (or under 20 ms), the bound fuzz-cortheon's linear-time test
uses. It fails on c7137bb and passes on this branch.
putBack gives the text that replacing the marks in turn gives: 20,000 random texts of marks, stray NULsand digits, each checked against one
replaceOwnedper mark, lowest index first. It fails with the sortreversed, so it reaches the shared-NUL case.
zig build test test-golden -Dcpu=apple_m1, uncached: 164 of 164 steps, 1983 of 1991 tests pass, 8 skip,0 fail (c7137bb: 1981 of 1989, 8 skip); the two more are the new tests.
zig build fuzz-cortheonpasses.zig fmt --check build.zig zigis clean. Lean: 527 problems on the base and 527 on this branch, 0 new(
lean_new.py).Not run, and why: no CUDA host here. The parser is server code that both backends use, and its tests need no GPU.
Checklist
change.)
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Noengine change; not re-run.)
arithmetic changes.)
fails before. The equivalence test calls
putBack, which this change adds.)