Repository navigation
tokenizer memo: keep segments of 64 bytes or more, not 1,024 - #2509
Conversation
|
Thanks @mrv777. The segment table makes the case for 64 well, and the exactness argument holds when we read it: One thing: at 64-byte segments the cap stops being a bound. We'll settle the CHANGELOG conflict at landing. |
|
Thanks. Done in |
|
Correction to my last comment: the cap test in |
|
@mrv777 thanks, and for the correction. With the cap fixed in text and id bytes, twenty 64-byte segments under a ten-segment budget keep exactly ten if the overhead isn't counted, so the test now fails without it, as you say. The overhead in |
9e8e58f to
3c6a475
Compare
|
@mrv777, the branch's merges of main stopped our landing rebase, so I've rebuilt it linearly on today's main ( |
A chat request renders and encodes its conversation four or five times, and the memo skipped every segment under 1,024 bytes, so an agent conversation's short messages, tool calls and tool results were tokenized on every render. On 83 real agent conversations (Qwen3.8-27B tokenizer, 3 to 60K tokens) those segments are 89% of all segments and 11.5% of the bytes; at 64 bytes they are 0.3%, and going lower gains nothing. Kept ids are looked up by the segment's exact text and first flag, so ids are unchanged at any minimum. The tests keep a 64-byte segment, refuse one under the minimum, and read a 70-byte body back in every position the long-body test covers; with the old minimum (or 128, or 65) they fail.
…t fails without the overhead
3c6a475 to
5cdecc2
Compare
|
Thank you @mrv777. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
Figures corrected to the PRs' measured ones (#2699 head runs, #2509, #2633, #2436, #2644, #2593, #2430, #2568), scope fixed (#2253, #2287, #2511, #2569, #2337, #2627, #2351, #2626), unsourced figures dropped (#2181, #2606, #2257's pricing clauses), the shipped #2176, #2565, #2548 and gfx12 silu entries and the first #2625 copy removed, and the #2568 and #2352 entries unwrapped to one line each.
|
Thanks @mrv777! This is in TensorFold 1.2.6, out now: https://github.com/ashhart/TensorFold/releases/tag/v1.2.6
|
What this changes
A chat request renders and encodes its whole conversation four or five times. The tokenizer's memo kept only segments (text between added tokens) of 1,024 bytes or more, so every shorter segment was tokenized again on each render: an agent conversation's short messages, tool calls and small tool results. The memo now keeps segments of 64 bytes or more. Kept ids are looked up by the segment's exact text and first flag, so the ids an encode returns are the same at any minimum; only how often short segments are re-tokenized changes.
857b11bb8, merged in); commits1a64b1016(the change) and the changelog line's PR number.atc overlap --base upstream/main --json(2026-11-08): the only file shared with open pull requests isCHANGELOG.md.atc wholists no open PR or worktree onmemo.zig,tokenizer.zigortokenizer_test.zig.Why 64
I took 83 real agent conversations from a local prompt cache and decoded them with the Qwen3.8-27B tokenizer: 3 to 60,693 tokens, median 17K, 6,723 segments. Segments under 1,024 bytes are 89% of the segments and 11.5% of the bytes. Encode cost follows bytes, so 11.5% is roughly what a warm render still tokenizes today.
Most of the remaining short segments are a few bytes, such as role names and newlines, where a lookup would cost what the encode does. Going below 64 measured no further gain (the table under Receipt).
Checks on this head
9e8e58f51, its cap test tightened, overef3946665, the per-entry overhead asked for in review, over90d2c0d98(a merge of main, latest857b11bb8, changelog conflicts only). On this head:tokenizer_test.zig52/52,memo.zig7/7,zig build native,zig fmt --check,git diff --checkandtools/pr_check.py --pr 2509(0 errors, 0 warnings, lean 398 vs 398, 0 new) pass.python3 tools/zig/lean_check.py, on the base and on this head: 398 and 398, 0 new (tools/pr_check.py --base upstream/main; 116 and 116 on the earlier base). The 398 are inherited.zig test -lc zig/src/core/tokenizer/tokenizer_test.zig: 52/52 (includesmemo.zig,memo_reuse_test.zig,masked_test.zigand the EOS tests).zig test -lc zig/src/core/tokenizer/memo.zig: 7/7.zig build test test-golden -Dcpu=apple_m1on the same code before the changelog-only rebase (18e9def96on9450a3624): exit 0, 4m51s, Metal device checks included. Golden shows 0 unexpected differences; the known differences are as recorded.zig build -Dcpu=apple_m1: exit 0.zig fmt --check build.zig zigandgit diff --check: clean.heldwithin the cap.overheadback at 0 (the old accounting): the new cap test and the map-growth recency test fail.min_bytesback at 1024, at 128 and at 65: the new tests fail.firstfor segments under 1,024 bytes: the widened test and two existing memo tests fail.Receipt
9450a3624vs18e9def96; the code is unchanged by the rebase).server/hf_text.zigcreates the 64 MiB memo).z-lab/Qwen3.8-27B-DFlash2at50307d4c4cde6860d4eee73e2547cd786fe8e8a4.Tokenizer.encodeandencodeMasked(the chat route takes the masked path; the Qwen tokenizer has no normalized added tokens, so request gaps use the memo too). Ran: macOS. Not run: Linux and Windows, which share the same host code.A one-off prompt pays nothing measurable for the extra saves.
--prompt-cache-gib 8. 20,650 tokens are cached, so the time is mostly host work.serve MODEL --drafter z-lab/Qwen3.8-27B-DFlash2 --parallel 2 --context 65536 --prompt-cache-gib 8.ef3946665): a kept segment now counts a fixed overhead of@sizeOf(Entry) + @sizeOf(u64)against the cap, added inputand subtracted inremovethrough onebyteshelper, so the 64 MiB cap bounds the entries and their keys too. Before it, filled to its cap with distinct entries, the live heap was 1.08xheldat 1,024-byte entries, 1.63x at 128 and 2.27x at 64 (about 145 MiB at the server's 64 MiB cap). The memo tests' caps are now written in that unit (bytes(1, 1)a one-byte entry,60 + overhead). On the 83 real conversations the memo held 1.5 MB, far under the cap either way.Checklist
The title and description match the head commit, including the receipt and the stack.
New tests fail before the change, pass after it, and skip cleanly without their dependency. (They fail at the old 1,024 minimum and need no device.)
Comments and docstrings are one line. No measurements or history in the source.
No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
Drafted output equals
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (The prompt ids are unchanged, so nothing after tokenization can differ; the full suite and golden corpus pass.)No precision traded for speed. If the bits change, the description says which sums change and why. (No arithmetic changes.)
Decode and prompt processing are no slower than main at one stream and the widest admitted; with
--drafter, no slower than without it, greedy and sampled. (Decode is untouched; the host part of a cached prompt is faster and a one-off prompt is not slower.)