Skip to content

fix: qwen3_5_moe CUDA graphs capture into a new pool after the buffers grow - #78

Closed
philip-pentatonic wants to merge 1 commit into
ashhart:mainfrom
machinegenieorg:q36/fix-graph-pool
Closed

philip-pentatonic wants to merge 1 commit into
ashhart:mainfrom
machinegenieorg:q36/fix-graph-pool

Conversation

@philip-pentatonic

Copy link
Copy Markdown
Contributor

With PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True, Qwen3.6-35B-A3B on CUDA fails the first request whose prompt plus max_tokens passes 8192 rows, and every request after it:

File ".../qwen3_5_moe/cuda/decode.py", line 119, in mtp_decode
File ".../qwen3_5_moe/cuda/graphs.py", line 105, in draft
File ".../qwen3_5_moe/cuda/graphs.py", line 62, in _capture
RuntimeError: it->second->use_count > 0 INTERNAL ASSERT FAILED at ".../c10/cuda/CUDACachingAllocator.cpp":3332

Cause: Graphs.load drops every graph when it grows the buffers, but kept the pool handle. Torch removes an unused private pool only once its cudaMalloc_count is back to zero. With expandable segments that count goes up once per allocation that extends a segment and down once per free range it unmaps; freed blocks merge first, so it stays above zero. The pool stays registered with use_count 0, and the next capture into that handle trips the assert (torch 2.13.0a0+9186a08, CUDACachingAllocator.cpp lines 3906, 4238, 4093, 3332). With the default allocator, empty_cache removes the pool and growth works.

Fix: a growth takes a new torch.cuda.graph_pool_handle(). empty_cache still unmaps the old pool's memory; one bookkeeping entry is left per growth, at most one per doubling up to the context window.

Repro with public text: serve Vontra/Qwen3.6-35B-A3B-MLX-4bit-MTP (81169a9) with expandable segments and --context 32768. Send 40 JSON chat requests from 8 clients, each the opening of A Tale of Two Cities repeated to 2.7k–5.9k tokens with max_tokens 1410, and three of them repeated to 9k tokens.

NVIDIA PyTorch 26.07 DGX Spark (GB10) RTX PRO 6000
Before 22 of 40; request 22 (the first past 8192 rows) and every later one fail the same
After 40 of 40 40 of 40

After the fix:

  • every reply's token SHA-256 is the same on both GPUs (40/40);
  • the 22 replies before the growth equal the unpatched run's;
  • drafted replies equal "draft": false (12/12 distinct prompts on each GPU).

Tests: the new test_graphs_recapture_after_the_buffers_grow runs a tiny model with a 1M vocabulary, so two chain widths extend the pool's segment twice; greedy and sampled, expandable segments off and on. It fails 2 of 4 before and passes after, on both GPUs. tests/cuda/test_qwen36_moe.py and tests/test_qwen36_family.py: 16 passed on both.

…s grow

With expandable segments (PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True), the first request whose
prompt plus max_tokens passed 8192 rows, and every request after it, failed at capture:
"it->second->use_count > 0 INTERNAL ASSERT FAILED at CUDACachingAllocator.cpp:3332".

Growing the buffers drops every graph but kept the pool handle. Torch counts a private pool's expandable
mappings per allocation and its unmaps per free range, so once freed blocks coalesce the count stays above
zero, the pool is never unregistered, and a capture into it finds a pool no graph uses. A growth now takes
a new handle.

RTX PRO 6000, torch 2.13 (NGC 26.07), Vontra/Qwen3.6-35B-A3B-MLX-4bit-MTP: 40 label-style requests from
8 clients, three past 8192 rows, fail from the first growth on before and pass after, with unchanged token
SHA-256s equal to "draft": false. The new test_graphs_recapture_after_the_buffers_grow fails 2 of 4 cases
before; tests/cuda/test_qwen36_moe.py and tests/test_qwen36_family.py: 16 passed.
@ashhart

ashhart commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Thank you, @philip-pentatonic. This is in 0.3.6.3 under your name (9b8bc3ad). We ran
Qwen3.6 on a Spark with it: drafted replies equal "draft": false ones, and the four speed cells match 0.3.6.2's
(177-178, 137-140, 166-167 and 162-163 tok/s). We didn't reproduce the growth crash ourselves, so the release notes
cite your 40-of-40 runs on the Spark and the RTX PRO 6000. Thanks for pinning the allocator's bookkeeping down to
the lines. #83 and #84 are next in line.

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