Skip to content

tool_parse: a Gemma call's strings go back in one pass - #881

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jishnuvenugopal:fix/tool-parse-gemma-strings
Oct 16, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jishnuvenugopal:fix/tool-parse-gemma-strings

Conversation

@jishnuvenugopal

@jishnuvenugopal jishnuvenugopal commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

gemmaCall (zig/src/server/tool_parse.zig) reads Gemma 4's call:NAME{...} by swapping each <|"|> string for a
numbered NUL mark, quoting the bare keys, and putting the strings back. It put them back with one
std.mem.replaceOwned over the whole call per string, which is quadratic in the number of strings. putBack now
sorts 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.quote escapes bytes below 0x20). That includes two marks
sharing a NUL, which only the call's own NUL bytes can cause: the lower index still wins, then the leftmost.

Verified outcome

  • Why: reading a Gemma tool call slowed down with the square of its size. A call with many strings, 128 KiB long,
    took about 4 seconds of CPU after the model had finished writing it.
  • What was done: the strings go back into the call together, instead of one pass over the call per string.
  • Result: that call now reads in 3 ms, and calls read exactly as before: 50,000 varied replies give byte-identical
    results, and a test that checks the new way against the old one on 20,000 random inputs passes.

Receipt

  • Environment: main at 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 that
    offer tools are parsed.

  • Prompt speed before and after: n/a, for the same reason.

  • Speed of parse on one call of q:<|"|>a<|"|>, repeated, in ReleaseSafe as the server ships, fastest of three:

    Size c7137bb This branch
    16 KiB 58 ms 0.40 ms
    128 KiB 3.90 s 3.2 ms
  • Same output: a local fuzz harness (not part of this change) ran 50,000 edited replies through parse, closeCall,
    hideToolCalls and finishedProse on c7137bb and on this branch. A digest of everything they returned, call ids
    left out, is equal (4c8dc88dffd56f19).

  • Tests run:

    • New, a Gemma call's strings go back in one pass (tool_parse_test.zig): one call with 1,200 strings and one with
      9,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.
    • New, putBack gives the text that replacing the marks in turn gives: 20,000 random texts of marks, stray NULs
      and digits, each checked against one replaceOwned per mark, lowest index first. It fails with the sort
      reversed, 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-cortheon passes.
      zig fmt --check build.zig zig is 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

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (No engine
    change.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No
    engine change; not re-run.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No
    arithmetic changes.)
  • Prompt processing is no slower than the last release. (n/a, as above.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The timing test
    fails before. The equivalence test calls putBack, which this change adds.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

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.
@ashhart

ashhart commented Oct 15, 2026

Copy link
Copy Markdown
Owner

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. zig/src/server/tool_parse.zig:522 still comparison-sorts markers, so the worst-case work is O(M log M) when literal markers arrive out of order.

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

@jishnuvenugopal

Copy link
Copy Markdown
Contributor Author

Thank you for checking the shared-NUL order. Both points are right: putBack still sorts its marks, so the worst
case is O(M log M) when the call's own NULs put marks out of order, and the one-pass result equals replacing in turn
only because each replacement is JSON-quoted and so has no NUL. I've narrowed the description to removing the
quadratic restoration, stated the precondition there, and put it in putBack's doc comment (it is pub).

@ashhart
ashhart merged commit 1ace081 into ashhart:main Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
ashhart added a commit that referenced this pull request Oct 16, 2026
… 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.
ashhart added a commit that referenced this pull request Oct 16, 2026
…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).
@ashhart

ashhart commented Oct 16, 2026

Copy link
Copy Markdown
Owner

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

@jishnuvenugopal
jishnuvenugopal deleted the fix/tool-parse-gemma-strings branch October 17, 2026 03:06
ashhart added a commit that referenced this pull request Oct 18, 2026
v1.0.12 (833bbfc) brings b12's fifteen pull requests, Nemotron's oQ widths on
Metal, #881's last head, the oQ docs and the bench tools' tile fix. It merged
without a conflict.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… 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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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).
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…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.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.
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.

2 participants