Skip to content

server: build the tokenizer memo on a dense dedicated allocator - #4455

Merged
ashhart merged 2 commits into
ashhart:mainfrom
bikepresentball:fix/memo-dense-allocator
Nov 22, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
bikepresentball:fix/memo-dense-allocator

Conversation

@bikepresentball

Copy link
Copy Markdown
Contributor

Fixes #4389 (direction (a) — the memo gets an allocator of its own).

The change

One line: zig/src/server/hf_text.zig builds the segment memo on
std.heap.smp_allocator instead of the server's process gpa.

The ReleaseSafe serve's process gpa is SafeAllocator over page_allocator
(zig/build/cuda.zig; lib/std/start.zig), whose minimum bucket is
1 << max(page_size_log2, 13) = 64 KiB on aarch64. Memo.put copies each new
segment's text and ids with gpa.dupe (zig/src/core/tokenizer/memo.zig), so a
kept segment lands in a bucket among that request's short-lived allocations, and
the bucket stays mapped until the segment evicts — the ~64 KiB-per-request
VmData the issue measures.

std.heap.smp_allocator (Zig 0.17.0, lib/std/heap/SmpAllocator.zig) is a
global thread-safe singleton with dense size classes; freed segment space is
reused by later segments, so the process's VmData now tracks the memo's own
byte count instead of one 64 KiB bucket per request.

Tokenizer.overrideSplit reads const gpa = m.gpa before recreating the memo,
so the dedicated allocator propagates there; Memo.create's own unit tests keep
std.testing.allocator. The memo's cap accounting (bytes() + eviction) is
unchanged — the 64 MiB bound still governs what it keeps.

Measured on GB10 (aarch64)

Same instrument as the issue's probes: tensorfold-native serve of
qwen3-embedding-0.6b (same binary and server path as a chat seat), one request at
a time over fresh connections to /v1/tokenize, VmData from /proc/<pid>/status
around each arm. Base = main a4ea27a11, head = this change; both built
zig build native -Dnvcc -Dsm=121 on the same box, one fresh serve per build.

arm base (main) head (this change)
30 repeats of the same message +128 kB +8,384 kB (one-time allocator warm-up; 0.00 units/req after)
1,000 new ~100 B messages +72,356 kB = 72.4 kB/req (1.10 units/req) +676 kB = 0.7 kB/req (0.01 units/req)

The base arm reproduces the issue's 0.5–1.1 units/request; the head arm's
1,000-new-segment growth is +676 kB total — the per-request bucket cost is gone.

Gates

  • zig fmt --check clean on the touched file.
  • zig test zig/src/core/tokenizer/memo.zig: 7/7 (the memo's cap/recency/eviction suite).
  • Full host suite on the build Mac has pre-existing environmental failures on
    both this tree and a pristine origin/main worktree (Metal-vision and
    timing-sensitive steps on a host without the device); none touch the tokenizer
    or server paths, and the CUDA build compiles and serves on the GPU box.
  • The reviewer-passed trade-off, stated plainly: the memo's own allocations lose
    SafeAllocator's per-allocation checks; the memo bounds itself through its
    byte accounting, and every other server allocation keeps the process
    allocator's checks.

@AIFlowML

Copy link
Copy Markdown
Contributor

I trial-merged 69eb998316 into main 2ce240cf0 on M5 Max (macOS 27.2, Zig 0.17.0). The merge and git diff --cached --check were clean. zig build test-tokenizer-host test-token-stream-host -Dcpu=apple_m1 -j4 --summary all passed 8/8 steps and 95/95 tests.

I added two scratch assertions to the existing Clef Split fixture. Loading with and without a Split override uses smp_allocator for the memo in both cases, keeps its 64 MiB cap, and preserves the expected encoded IDs. With the asserts, the focused run passed 25/25 on the trial. With main's allocator line restored, the allocator assertion failed as expected (24 passed, one failed).

The cap still counts live memo entries. Small freed allocations go back to the global allocator's freelists, so that cap should be read separately from process high-water memory. I verified the server wiring on macOS. The GB10 VmData comparison in the description is the author’s device result.

Igor Lessio - AIFLOW LABS

@AIFlowML

Copy link
Copy Markdown
Contributor

I independently reproduced the memory slope over the real /v1/tokenize route on Linux ARM64. I compared main 25586070b against its clean trial merge of 69eb9983, using fake_serve with the real HfText tokenizer, no model weights, and Zig 0.17.0 ReleaseSafe. The tokenizer files came from TensorFold/Qwen3.8-27B-MLX-4bit at 22d8d538154e0e5b6f8dcb1fcc74a60a0798104c.

VmData growth Main PR
First request and 49 repeats 8,584 KiB 9,864 KiB
Next 1,000 requests with distinct 100-byte tails after 20 fixed messages 64,100 KiB 548 KiB

Two fresh processes per build, run main → PR → PR → main, gave the same numbers. All 1,000 returned token arrays matched across all four runs (SHA-256 c54c4c1bcf34c0a2211c46fe61ca9999ad76f485e5799f8b1c0b0d063890bc21).

To replay on Linux ARM64, build each tree with zig build server-cpu -Doptimize=ReleaseSafe, set TF_FAKE_TOKENIZER to the tokenizer directory, and run zig-out/server/fake_serve serve zig/tests/server/fixtures --host 127.0.0.1 --port 0 --keep-warm 0. Send one request at a time to /v1/tokenize over fresh connections and read VmData in /proc/PID/status after each arm. I cross-compiled on the Mac by selecting the Linux target for the CPU fixture build in both scratch trees. This exercises the server route and allocator on ARM64. The author's GB10 run covers the native CUDA server.

Igor Lessio - AIFLOW LABS

@Bizuayeu

Copy link
Copy Markdown
Contributor

This exercises the server route and allocator on ARM64. The author's GB10 run covers the native CUDA server.

#4455 (comment)

As offered in #4389, I ran the Issue's probe on this PR's base and head, serving a GLM-5.3-Flash checkpoint at TP=2 on two GB10s. On rank 0, for messages of 100 and 1,000 bytes, the 64 KiB step per request is gone on both the tokenize and chat routes. For 17.6 KB of new text in each request, the step falls from two units a request to one, and growth still stops where the memo's own count reaches 64 MiB.

VmData a request on rank 0, one request at a time over fresh connections unless noted:

what each request adds to the memo route requests base head
one 100-byte user message after 20 fixed messages tokenize 1,000 +64.1 kB +1.1 kB
one 100-byte user message after 20 fixed messages chat 100 +67.9 kB +3.2 kB
a conversation's next turn, 100-byte messages tokenize 2,000 +67.5 kB +1.1 kB
a conversation's next turn, 100-byte messages chat 200 +75.9 kB +1.6 kB
one bare 100-byte message tokenize 1,000 +64.1 kB +0.8 kB
one bare 100-byte message, kept-alive connection tokenize 1,000 +32.4 kB +0.6 kB
one bare 1,000-byte message tokenize 1,000 +63.9 kB +1.8 kB

In 64 KiB units from smaps, base added 0.44 to 1.19 a request and head 0.002 to 0.032, in line with the description's 0.01. Head's figures are averages, not slopes: in the first row, 11 of its 15 units came in the first 200 requests. On base, the first and fifth rows each grew by 64,100 kB over 1,000 requests, the same figure as the main run in the comment above.

With one new 17,599-byte message in each request (tokenize, 1,800 requests), the memo's count reaches 64 MiB at request 1,630 or 1,631:

growth stopped at request rank 0 VmData up to there 64 KiB units a request before it
base 1,629 +208,676 kB 2.0
head 1,628 +104,676 kB 1.0

Base repeats the Issue's tokenize row exactly. On head, rank 0's VmData had grown by about 102 MiB when the memo's count reached its 64 MiB cap (base: about 204 MiB). I have not looked into why head still adds one unit a request in this shape.

Setup:

@bikepresentball

Copy link
Copy Markdown
Contributor Author

Thanks both — the description's GB10 result now has independent corroboration on macOS ARM, Linux ARM, and TP=2 CUDA over RoCE, all with the same signature: the fixed 64 KiB-per-request step is gone, growth stops at the memo's own 64 MiB count, and token outputs are unchanged (SHA-equal across builds in AIFlowML's ARM64 runs).

@Bizuayeu, on the one unit a request head still adds in the 17.6 KB shape — that reads as the memo filling, not leaking. Your own rows pin it: the memo's count reaches the 64 MiB cap at request ~1,630, which puts ~40 KiB of cap-accounted storage per entry (64 MiB / 1,630), and the allocator serves that entry from one 64 KiB unit — so rank-0 VmData grows one unit per request while the memo fills, then eviction recycles and growth stops exactly where your table stops. On base the same entry took two units, which is the gpa side of this diff. Happy to produce a per-allocation breakdown if that reading is worth confirming.

@AIFlowML, agreed on the cap semantics: the cap counts live memo entries, and small freed allocations returning to the global freelists means the cap and process high-water move independently — the description's GB10 VmData rows stay framed as that device's result, not a process-wide claim.

Head is unchanged since the reports: one commit, one line in zig/src/server/hf_text.zig, MERGEABLE against current main. The pytest and zig-windows reds on this tip are the repo-wide ones (the missing yaml collection error and the runner DeviceLost class), not this diff.

@Bizuayeu

Copy link
Copy Markdown
Contributor

the allocator serves that entry from one 64 KiB unit — so rank-0 VmData grows one unit per request while the memo fills, then eviction recycles and growth stops exactly where your table stops. [...] Happy to produce a per-allocation breakdown if that reading is worth confirming.

#4455 (comment)

Thank you. That reading fits my rows. 64 MiB over about 1,630 entries is about 40 KiB an entry, and head grew by 104,676 kB over the 1,628 requests before the stop, which is 64.3 kB a request. I don't need the breakdown for this PR. The receipts above are all I had to add.

@bikepresentball

Copy link
Copy Markdown
Contributor Author

Following up on the per-allocation breakdown — with a correction to my mechanism read in the comment above.

Setup: fake_serve on Linux ARM64 (the same method as AIFlowML's second report), real HfText tokenizer files, Zig 0.17.0 ReleaseSafe, one fresh serve per arm, one request at a time over fresh connections. For the breakdown I built a scratch instrument that prints the memo's own entries/held counters every 256 saves, and ran the 17.6 KB distinct-message shape to 5,000 requests on both trees.

arm main head
100 B message × 1,000 72.4 kB/req 9.1 kB/req (0.24 kB/req marginal after warmup)
17.6 KB bare message × 2,500 67.4 kB/req 48.3 kB/req
17.6 KB message after 20 fixed × 2,500 106.8 kB/req 48.3 kB/req
same 17.6 KB message repeated × 2,500 flat after warmup (8.3 MB) flat after warmup (8.4 MB)

Three findings:

  1. The memo's cap behaves exactly as designed. On head, held climbs to 67,101,7xx of the 67,108,864 cap, entries plateau at 2,528, and eviction cycles continuously — the cap-stop is visible in the memo's own counters, matching @Bizuayeu's real-server table.

  2. The residual growth is not the memo. With the memo frozen at its cap, process VmData still grows ~28–32 kB/request, linear through all 5,000 requests (218 MB total). The component is keyed on distinct text (the repeated-message arm is flat on both trees) and sized like one entry (~17.6 KB text + ~8.9 KB ids), but it lives outside memo.zig — evicted entries' memory is not reused on this path. It is present on both trees (67.4 vs 48.3 kB/req bare), so this PR does not introduce it; the PR roughly halves the shape's total growth.

  3. Correction: my comment above attributed the one-unit-per-request residual to memo entry granularity. The instrument says the memo fill stops at the cap and the residual is this separate outside-the-memo component — @Bizuayeu, your "one unit a request" is that, not the memo.

Caveat: this is fake_serve. Your real-server run's VmData stopped at the cap, so the real server's outside component is smaller or absent — if it reproduces on a real serve it may deserve its own issue; happy to keep digging either way. The 100 B row is a third-platform confirmation of the fix (Linux ARM64 numbers align with AIFlowML's).

@Bizuayeu

Copy link
Copy Markdown
Contributor

@Bizuayeu, your "one unit a request" is that, not the memo.

#4455 (comment)

Thank you for the breakdown. My reply above checked only the arithmetic. On the real serve, head's one unit a request ended at request 1,628, where the memo's count reaches 64 MiB. The remaining 172 requests added +256 kB in all (four steps of +64 kB, and +0 over the last 100), where 28 to 32 kB a request would have come to about 5 MB. Base stopped the same way (+384 kB over the same 172 requests). The tail is short, so I cannot rule out a smaller component there.

If it helps, I can run the 17.6 KB shape to 5,000 requests on the real serve, on both trees.

@Bizuayeu

Bizuayeu commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

if it reproduces on a real serve it may deserve its own issue

#4455 (comment)

I ran your bare 17.6 KB row on the real serve: one bare 17,599-byte user message a request, each with a new nonce line, 5,000 requests to /v1/tokenize over fresh connections, one fresh serve a tree. The checkpoint and setup are the same as above, and both trees were rebuilt from an empty global cache for this run. Growth stopped where the memo's count reaches 64 MiB, and rank 0's VmData did not grow after request 2,000 on either tree:

growth stopped at request rank 0 VmData up to there from there to request 2,000 requests 2,001 to 5,000
base 1,629 +208,676 kB +256 kB +0 kB
head 1,628 +104,676 kB +256 kB +0 kB

At 28 to 32 kB a request, the 3,372 requests after head's stop would have added about 92 to 105 MiB. The figures up to the stop are the same as in the 1,800-request runs, to the kB. This covers the native server only. The two runs also differ in the tokenizer files and the text, so mine do not show where the fake_serve growth comes from.

bikepresentball and others added 2 commits November 22, 2026 11:00
The ReleaseSafe serve's process gpa is SafeAllocator over page_allocator, whose
minimum bucket is 64 KiB on aarch64. Memo segments land in those buckets among a
request's short-lived allocations, so each request that keeps a new segment maps
about 64 KiB that stays until the segment evicts (ashhart#4389). Build the memo on
std.heap.smp_allocator instead: dense size classes, freed segment space reused by
later segments, so VmData tracks the memo's own 64 MiB count. overrideSplit
propagates the memo's allocator; the other create call sites (its own tests) are
unchanged.
@ashhart
ashhart force-pushed the fix/memo-dense-allocator branch from 69eb998 to ed6cfd3 Compare November 22, 2026 11:00
@ashhart
ashhart merged commit b112ad6 into ashhart:main Nov 22, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thank you @bikepresentball. 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.

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

Labels

None yet

Projects

None yet

4 participants