Skip to content

Commit 9b8bc3a

Browse files
philip-pentatonicashhart
authored andcommitted
fix: qwen3_5_moe CUDA graphs capture into a new pool after the buffers 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.
1 parent 603d3bc commit 9b8bc3a

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎src/tensorfold/families/qwen3_5_moe/cuda/graphs.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Graphs:
2020
def __init__(self, w, head: Head, capacity: int) -> None:
2121
self.w, self.head, self.capacity = w, head, capacity
2222
self.st, self.mc, self.rows = None, None, 0
23-
self.pool = torch.cuda.graph_pool_handle()
23+
self.pool = None # a new pool each time the buffers grow (``load``)
2424
self.target: dict[tuple[int, int], tuple] = {}
2525
self.mtp: dict[tuple[int, int], tuple] = {}
2626

@@ -36,6 +36,9 @@ def load(self, st: State, mc: Cache, need: int) -> tuple[State, Cache]:
3636
self.mtp.clear()
3737
self.st = self.mc = None
3838
gc.collect()
39+
# the dropped graphs' pool can stay registered with no graph left (expandable segments leave it so),
40+
# and torch refuses a capture into such a pool: capture into a new one
41+
self.pool = torch.cuda.graph_pool_handle()
3942
self.st = State(self.w)
4043
reserve(self.st, self.rows)
4144
self.mc = Cache(self.w, self.rows)

‎tests/cuda/test_qwen36_moe.py‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
V, D, E, WIDTH, TOP = 256, 256, 16, 64, 4
2222

2323

24-
def _model(seed: int = 11):
24+
def _model(seed: int = 11, vocab: int = V):
2525
gen = torch.Generator(device="cuda").manual_seed(seed)
2626
dev = "cuda"
2727

@@ -56,12 +56,12 @@ def attn():
5656

5757
layers = [Layer(True, norm, norm, gdn, None, None, None, None, routed()),
5858
Layer(False, norm, norm, None, attn(), None, None, None, routed())]
59-
config = Config(hidden=D, intermediate=0, layers=2, heads=2, kv_heads=1, head_dim=128, vocab=V, k_heads=1,
59+
config = Config(hidden=D, intermediate=0, layers=2, heads=2, kv_heads=1, head_dim=128, vocab=vocab, k_heads=1,
6060
v_heads=1, dk=128, dv=128, conv_kernel=4, interval=2, eps=1e-6, rope_dims=32,
6161
rope_theta=10000000.0, eos=(0,), experts=E, top_k=TOP, moe_width=WIDTH)
62-
s, b = affine(V, D // 64)
63-
embed = QLinear(words(V, D // 8), s, b)
64-
w = Weights(config, embed, layers, norm, qlinear(V, D), torch.ones(16, device=dev))
62+
s, b = affine(vocab, D // 64)
63+
embed = QLinear(words(vocab, D // 8), s, b)
64+
w = Weights(config, embed, layers, norm, qlinear(vocab, D), torch.ones(16, device=dev))
6565
m = MTP(norm_e=norm, norm_h=norm, fc_e=qlinear(D, D), fc_h=qlinear(D, D), input_norm=norm, post_norm=norm,
6666
attn=attn(), moe=routed(), norm=norm)
6767
return w, Head(w, m)
@@ -154,6 +154,33 @@ def test_graph_replays_equal_eager_rounds(sampling):
154154
assert runner.target and runner.mtp # captured once, replayed by the later requests
155155

156156

157+
@pytest.mark.parametrize("expandable", [False, True])
158+
@pytest.mark.parametrize("sampling", [None, Sampling(1234, 1.0, 20, 0.95)])
159+
def test_graphs_recapture_after_the_buffers_grow(expandable, sampling):
160+
"""A request past the first buffers' rows grows them and recaptures (expandable segments leave the dropped
161+
graphs' pool registered); drafted tokens stay serial before, across and after the growth."""
162+
163+
import os
164+
165+
from tensorfold.families.qwen3_5_moe.cuda.graphs import BUCKET, Graphs
166+
167+
w, head = _model(vocab=1 << 20) # two chain widths' logits map the pool's expandable segment twice
168+
runner = Graphs(w, head, 4 * BUCKET)
169+
long = [3 + (i * 7) % 200 for i in range(BUCKET)] # with its reply, past the first BUCKET rows
170+
torch.cuda.memory._set_allocator_settings(f"expandable_segments:{expandable}")
171+
try:
172+
for prompt, depth, rows in ((PROMPTS[1], 3, BUCKET), (PROMPTS[0], 2, BUCKET), (long, 3, 2 * BUCKET),
173+
(PROMPTS[2], 3, 2 * BUCKET)):
174+
want = _serial(w, prompt, sampling, 24)
175+
st, mc, first, carry = decode.prefill(w, head, prompt, sampling)
176+
res = decode.mtp_decode(w, head, st, mc, carry, first, 24, sampling, depth=depth, confidence=0.0,
177+
runner=runner)
178+
assert res.tokens == want and runner.rows == rows
179+
finally:
180+
default = "expandable_segments:True" in os.environ.get("PYTORCH_CUDA_ALLOC_CONF", "")
181+
torch.cuda.memory._set_allocator_settings(f"expandable_segments:{default}")
182+
183+
157184
def test_a_long_prompt_absorbs_through_the_prefill_kernel_and_decodes_serially():
158185
"""Prompt chunks past the tree kernel's 128 rows absorb into the head with the prefill kernel; tokens stay serial."""
159186

0 commit comments

Comments
 (0)