Repository navigation
Stop the per-request host heap growth (#73): share xgrammar's TokenizerInfo, dedupe the kernel audit - #82
Conversation
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>
|
Validated on GB10: reiner job 274, arm V. Setup: merge-train tree Server RssAnon slope, req 12 → 90:
The full 995-request ST leg with thinking on is job 280. It is the real long-run check on this slope. |
|
Merged as part of the GB10 merge train (main Gates on the combined tree:
Tree check: |
…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>
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:5b07b9531ea4e20ca--speculativeAt 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):spark_runtime::kernel_audit::record←GpuBackend::kernel←hc_pre_split← decode stepcompile_tool_grammar→GrammarCompiler::compile_normalized(half of it one::clone)snapshot_aux_into←decode_marconi_checkpoint), bounded by the slot poolxgrammar: share TokenizerInfo storage across clones.CompiledGrammarheld a deep copy ofTokenizerInfo. That isdecoded_vocab+sorted_decoded_vocab+ trie ranges, about 20 MB on the 248k Qwen vocab.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.TokenizerInfois now a handle overArc<TokenizerInfoData>, which is the C++ pimpl/shared_ptrsemantics. The API and equality semantics are unchanged.spark-runtime: record each kernel lookup once in the audit.recordpushed a row with two freshStrings for everyGpuBackend::kernel()call. A dispatcher that looks kernels up on each call (the qwen4_exp mHC split path in qwen4_exp: land the 2026-08-31 long-context TTFT campaign (QSA tensor cores, mHC grid fix, MoE CTA striding) #68, 4-5 per layer per decode step) grew the global Vec every step.recordhashes(module, func, loaded)without allocating and keeps only the first occurrence.audit_rows()output is unchanged; it already collapsed per(module, func).Validation
cargo test -p xgrammar: 824 lib + 2 integration tests pass, including new tests showing that clones and compiled grammars share storage.kernel_audittests on GB10;