Skip to content

Stop the per-request host heap growth (#73): share xgrammar's TokenizerInfo, dedupe the kernel audit - #82

Merged
AzeezIsh merged 2 commits into
mainfrom
fix/host-heap-growth-73
Sep 26, 2026
Merged

AzeezIsh merged 2 commits into
mainfrom
fix/host-heap-growth-73

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Fixes the per-request host-heap growth tracked in #73. Two independent leaks, both found with a jemalloc heap profile on GB10.

Symptom

Job 271 served the shipped Flash-Next recipe profile: thinking on (reasoning_effort: low), MTP K=2, prefix caching on, util 0.90. Each arm ran 28 min of BFCL traffic, with the client in its own process. The server's anonymous RSS grew:

build server RssAnon client RSS
main66 5b07b953 +20.6 MB/req flat
#68 1ea4e20ca +24.6 MB/req flat
main66, no --speculative +28.0 MB/req flat

At that rate no main-lineage build can finish the 995-question ST leg at the recipe's util. The 2.5 GB watchdog stops it around request 300, and job 245 stopped at about 388.

Root cause

Job 273 LD_PRELOADed jemalloc 5.3.0 (--enable-prof, 128 KiB sampling) into the recipe serve and diffed interval heap dumps across ~8 minutes of BFCL traffic (1.22 GB of growth):

share of growth call path
51 % spark_runtime::kernel_audit::record ← GpuBackend::kernel ← hc_pre_split ← decode step
32 % compile_tool_grammar → GrammarCompiler::compile_normalized (half of it one ::clone)
11 % QSA snapshot aux (snapshot_aux_into ← decode_marconi_checkpoint), bounded by the slot pool
6 % xgrammar adaptive token masks (counted and LRU-bounded)
  1. xgrammar: share TokenizerInfo storage across clones.
    • The problem: every CompiledGrammar held a deep copy of TokenizerInfo. That is decoded_vocab + sorted_decoded_vocab + trie ranges, about 20 MB on the 248k Qwen vocab.
    • The Tier-1 grammar cache is LRU-bounded by memory_size_bytes, which counts only the FSM and masks, so the copy was invisible to the budget. Agentic/BFCL traffic sends a distinct tool schema per request.
    • The fix: TokenizerInfo is now a handle over Arc<TokenizerInfoData>, which is the C++ pimpl/shared_ptr semantics. The API and equality semantics are unchanged.
  2. spark-runtime: record each kernel lookup once in the audit.

Validation

AzeezIsh and others added 2 commits September 25, 2026 23:09
Every CompiledGrammar held a deep copy of TokenizerInfo (decoded_vocab,
sorted_decoded_vocab and trie ranges: ~20 MB on Qwen's 248k vocab), made in
compile.rs for each compiled grammar. The Tier-1 grammar cache is
LRU-bounded by CompiledGrammar::memory_size_bytes, which counts only the FSM
and masks, so the copy was invisible to the budget. Agentic/BFCL traffic
sends a distinct tool schema per request, so each request left an uncounted
vocabulary copy in the cache.

GB10 jemalloc heap profile (reiner job 273, Flash-Next recipe profile, BFCL
traffic): about a third of the server's heap growth sat under
compile_tool_grammar -> GrammarCompiler::compile_normalized, and half of that
was the TokenizerInfo clone.

TokenizerInfo is now a handle over Arc<TokenizerInfoData>, as the C++ pimpl
over shared_ptr is. Clones and every CompiledGrammar share one vocabulary.
Public API and equality semantics are unchanged. New tests assert that clones
and compiled grammars share storage.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
kernel_audit::record pushed a (module, func, loaded, site) row with two
fresh Strings for every GpuBackend::kernel() call. The design assumes every
lookup is eager, but a dispatcher that looks kernels up on each call (the
qwen4_exp mHC split path does 4-5 per layer per decode step) grew the
global audit Vec on every decode step. In the GB10 jemalloc heap profile
(reiner job 273, Flash-Next recipe profile, BFCL traffic) this was about half
of the server's heap growth.

record now hashes (module, func, loaded) without allocating and pushes only
the first occurrence of each triple. audit_rows() output is unchanged: it
already collapsed per (module, func) with loaded = any resolved and site =
the first lookup's.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Validated on GB10: reiner job 274, arm V.

Setup: merge-train tree 53f4a3d3a (main66 + #78 + #67 + #68 + this PR + #83), all-targets release build, binary sha256 29906e48…566c. The harness is job 271's, unchanged: the Flash-Next recipe serve line (thinking on with reasoning_effort: low, MTP K=2, prefix caching on, util 0.90), driven by BFCL traffic for 28 minutes, with the client in its own process.

Server RssAnon slope, req 12 → 90:

build server RssAnon job
main66 +20.6 MB/req 271 M
#68 (1ea4e20ca) +24.6 MB/req 271 F
merge-train tree with this PR +1.86 MB/req (1144 → 1338 MB) 274 V
  • MemAvailable moved 7933 → 7736 MB, which is −1.83 MB/req. Client RSS and server file/shmem stayed flat.
  • Per-window growth fell from about 10 MB/req early in the run to about 2 MB/req by request 40, so the remaining growth looks like caches filling toward their bounds, not a leak.
  • GB10 unit tests pass: xgrammar 824 + 2, and spark-runtime kernel_audit 7, including the three new dedupe tests.

The full 995-request ST leg with thinking on is job 280. It is the real long-run check on this slope.

@AzeezIsh

Copy link
Copy Markdown
Contributor Author

Merged as part of the GB10 merge train (main 1ab38536b). The train was #78, #67, #68, #82, #83, then #89 and #90.

Gates on the combined tree:

  • ST-995 (job 280): PASS, 84.72 / 84.68 against bars of 82.72 / 81.65.
  • Agentic perf leg (job 298): VALID. 1007 of 1007 turns, 0 failed, 0 missing, 89 min.
  • Support matrix (job 346): green. Nemotron-3.5-Lightning NVFP4, Qwen3.6-35B-A3B NVFP4 and Qwen3.6-35B-A3B FP8 each boot with 0 unresolved lookups on main66 + fixes and on the train tree. Greedy probes are identical between the two trees (mean |Δlogprob| 0.0000), and 10k TTFT is equal or slightly faster.

Tree check: main has the same tree as the job-346 build (f8a04db1f): git diff is empty. main also differs from the tree the gates ran on (53f4a3d3a) only by the marker [expected_absent] declarations (f261446bd), which are TOML and tests only.

andrew-vogulkin pushed a commit to andrew-vogulkin/atlas that referenced this pull request Sep 26, 2026
…as-Inf#73)

Follow-up to the Atlas-Inf#82 dedupe, prompted by a review. seen was a
HashSet<u64> of DefaultHasher keys, so a collision between two distinct
(module, func, loaded) triples would silently drop the second from the
audit, and the boot kernel gate reads that audit. seen now maps each hash
to the row indices carrying it, and a hit requires an exact triple match.
A collision appends, and hits stay allocation-free. The hash is now
computed before taking the mutex that audit_rows also holds.
insert_row takes the hash as a parameter, so a test can stage a
collision.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@AzeezIsh
AzeezIsh deleted the fix/host-heap-growth-73 branch September 27, 2026 07:15
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.

1 participant