Repository navigation
[FlyDSL] Allocate split-K scratch buffers from a private MemPool - #5430
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
6a815af to
1cd2e68
Compare
b10afa6 to
13a16a8
Compare
|
Advisory review (static + hand-run; not a merge gate). Validation/Perf ran no GPU stage — reasons are on their lines below. Findings tagged Routes the three lazily-cached split-K scratch/semaphore buffer sets (preshuffle split-K, gfx950 a16w16 split-K, ASM a16w16 splitK) through a new per-device private torch.cuda.MemPool, so a buffer first allocated on a cache miss inside a CUDA graph capture can no longer inherit the address of an intermediate the same capture freed. Review (advisory): 🔴 HIGH RISK 🔴 [verified] The fix silently no-ops on torch 2.9.x: during a capture the use_mem_pool routing loses to the capturing graph's private pool — torch 2.9.1's get_pool scans captures_underway forward so the graph-pool entry registered at capture-begin wins, while 2.10.0 scans in reverse so the persistent_alloc entry wins (read from the v2.9.1 and v2.10.0 tags of c10/cuda/CUDACachingAllocator.cpp). Verified on gfx942 + torch 2.9.1: this PR's own regression test fails (buffers aliased graph intermediates: [0, 1, 2, 3]), and a direct A/B shows a persistent_alloc allocation inside a capture still lands on the freed intermediate's address. torch 2.9.1 sits inside aiter's stated floor (setup.py: "upgrade torch to 2.9.1 or later") while CI pins torch 2.10.0, so a 2.9.x user keeps the original capture NaN bug with no error and a red test. Author must state and enforce the torch floor for this fix — skip/xfail test_graph_alloc.py below 2.10 or document the requirement — or switch to a mechanism that also works on 2.9.x. |
|
Thanks — three of these changed the code (b39a8c5). torch 2.9.x no-op. Confirmed from source: Missing Sibling sites. Took the two that share the failure mode most directly: On the memset window: not reachable in the vLLM flow this targets. The capture stream is torch's process-wide |
|
Re-review of the updated head (
Advisory re-review (static + hand-run; not a merge gate). Findings tagged Routes five lazily-cached split-K/semaphore/zero-bias scratch allocations (preshuffle split-K, gfx950 and gfx1250 a16w16 split-K, ASM a16w16, a8w8 zero-bias) through a new per-device private torch.cuda.MemPool so a buffer first allocated on a cache miss inside a CUDA graph capture can no longer inherit a freed intermediate's address, with a torch-version gate that warns and skips the regression test below torch 2.10. Review (advisory):
Biggest remaining blast radius is the two sibling sites still bare, not the torch-2.9.x path. The 2.9.x defect is now behind a once-per-process warning + skipped test + a documented floor (mitigated, though the underlying aliasing is unchanged there — re-confirmed |
|
Reproduced the defect and then tested this branch against it on 8x MI355X (gfx950), torch 2.9.1+rocm7.1.1 — i.e. below the 2.10 floor documented in the description. Posting the numbers since the PR states the limitation but doesn't show what it looks like in practice. The harness is your minimal demonstration wired into an lru-cached scratch, with three arms sharing one graph pool: a decoy graph whose intermediate is freed during capture, then
So on this torch the branch is inert: the warn-once fires, and the buffer is still served from the graph private pool. That matches what the description says rather than contradicting it — the point is just that below 2.10 the failure mode goes from silent to logged, not from broken to fixed, and the Two small things that follow from that, take or leave:
Happy to rerun any of this if it would help, or to test on a 2.10 build if you can point me at one. |
|
Thanks both — all of this changed the code (5a351da). @valarLip: the string-comparison trap is real and worse than a @zufayu:
Took your Separately: the mechanism in the description was wrong and has been rewritten. The reduction does not read unwritten workspace slots — it never runs. Only the graph that missed the cache records |
5a351da to
6df66dc
Compare
|
@JohnQinAMD — this reworks the ASM a16w16 counter you fixed in #4916, so flagging it for your review. The question I want your read on is whether the counter's workspace is GEMM-private. If it is, nothing outside the GEMM writes there, and the only way it goes into a capture dirty is by inheriting an address some other graph in the pool owns. This PR removes that inheritance (a per-device Your repro passes on this branch: but only after one change I would rather you agree with than not: So: is there a writer outside the GEMM I should be accounting for? Concretely — the counter you caught holding 2 under |
792f1a6 to
b50400a
Compare
|
LGTM |
The split-K semaphore and workspace are cached per (device, stream) and allocated lazily on the first miss. When that first miss happens inside a CUDA graph capture, the allocation is served from the graph's private mempool, where it can inherit the address of an intermediate tensor the same capture already freed. The kernel producing that intermediate is a node in the graph, so every replay overwrites the semaphore and the reduction reads workspace slots the launch never wrote. Take the buffers from a per-device torch.cuda.MemPool instead, which is not part of that reuse chain. Cache keys, kernels, the split-K protocol and the steady-state path are unchanged; the pool is entered only on a cache miss. Co-Authored-By: Claude Opus 5 <[email protected]>
use_mem_pool() was called without device=, so the routing registered on torch.cuda.current_device(). When the buffers live on another GPU the allocation bypassed the private pool entirely and the fix silently did nothing inside a capture. torch < 2.10 scans captures_underway in registration order, so the graph pool registered at capture begin always wins and use_mem_pool cannot route out of a capture at all; 2.10 scans it in LIFO order. Expose the version gate, warn once when a capture is hit on an older torch, and skip the capture test there instead of failing it. The same lazy-allocation hazard applies to the gfx1250 twin of the a16w16 split-K counters and to the a8w8 zero-bias buffer, so both take the helper. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The version gate compared torch.__version__ against "2.10" and only worked because TorchVersion overloads the comparison: as plain strings every 2.x release sorts above "2.10", so the gate inverts on exactly the versions it exists to catch. Parse both sides instead. functools.cache does not stop concurrent first callers from each building a MemPool -- sixteen threads got sixteen pools -- so the comment claiming the cache owns the pool was wrong. Blocks stay valid after their pool object is dropped, but a second pool still hands out a second set of segments, so creation is serialized and the comment now says what was measured. topk's workspace is keyed (device, stream, size), the same shape as the scratch caches already covered, so it takes the helper too. mla_reduce's key carries no stream; its "capture-safe" docstring now says why that is a different reachability argument rather than asserting safety. The new test asks every one of the six cached accessors whether its buffer's segment_pool_id is the private pool's. It needs no capture and no particular arch, so it runs on the torch floor where the capture test is skipped, and it fails if any site loses its wrapper. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
ROCm#4916 fixed the same capture hazard on this path by handing every recorded launch its own ring slot and recording a zero_ per launch, so a replay starts from zero whatever the counter held. That tolerates a dirty counter rather than preventing one, and the recorded node costs 2.2-2.4 us per launch -- measured, and independent of the counter's size -- plus 16 MiB per device allocated whether or not the process ever captures a graph. The kernel already hands the counter back at zero, so nothing has to re-zero it between launches; what it cannot survive is another graph in the same pool owning its address. Route the allocation through persistent_alloc like the other scratch caches and the address is never one a graph writes. check_graph dirtied the counter from outside to stand in for that state. The workspace belongs to the GEMM, so nothing outside it writes there; the decoy graph this uses instead produces the dirt the way a capture actually does, and still fails on an unfixed build. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Below torch 2.10 use_mem_pool is ignored inside a CUDA graph capture, so the buffer still comes from the graph's private pool and can inherit the address of an intermediate that every replay overwrites. A warning there is easy to miss until the result is already wrong, so fail at the allocation instead. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
b50400a to
cf9bb17
Compare
Problem
FlyDSL's split-K preshuffle GEMM reduces through a workspace guarded by an
atomic semaphore. Both are cached and reused across launches, keyed on
(device, stream)and allocated lazily on the first miss for that key:A CUDA graph capture always runs on a freshly created stream, so that key is
always new and the first capturing GEMM always misses — allocating inside the
capture region, where memory is served from the graph's private mempool and can
land on the address of an intermediate the same capture already freed. Keying on
the stream is also why the cache cannot be pre-warmed: the capture stream does
not exist until the capture begins.
The failure surface is the semaphore, not the workspace. Three things have
to line up:
intermediate's address;
allocation alone is harmless, because during capture the producing kernel is
only recorded, never run;
recorded
torch.zeros' memset, so only that one re-zeroes the semaphore onevery replay. Every later graph shares the buffer with no memset of its own.
splitk_reduce_epiloguecounts relatively: each CTA doesatomic_add(sem, +1),the one that sees
arrival == split_k - 1reduces, and it restores withatomic_add(sem, -split_k)rather than storing 0. A dirty slot therefore nevermatches, the reduction never runs, and that tile's output keeps whatever it
held. The workspace being clobbered is harmless by comparison — every launch
rewrites the planes it is about to reduce.
Reproduction
Pure torch, no aiter, no model — the buffer is an lru cache keyed like aiter's:
9.0 = 3.0 + 6.0, the decoy's own intermediate. The same three-graph shapedriving the real kernel (
M=1 N=2048 K=2048 split_k=4) gives 64/2048 wrongoutputs with
max_err=778.5— one dirty semaphore slot xtile_n=64columns —and 0/2048 with this branch.
Why it needs a specific snapshot to show up
Reaching the defect needs a capture on a stream the cache has never seen:
torch.cuda.graph(graph, self.pool)with nostream=, which puts the capture on torch's process-wide singletondefault_capture_stream— a different stream from the onegraph_capture()runs warmup on. Fixed upstream by vllm-project/vllm#53818; older snapshots still have it. PIECEWISE capture was never affected: it captures on the current stream.vLLM captures every batch size into one shared
graph_pooland replays them inturn during decode, so conditions 2 and 3 above are met by construction once the
first one is.
Fix
Take the buffers from a per-device
torch.cuda.MemPool, which is not part ofthe graph private pool's reuse chain, so the address cannot be inherited from a
freed intermediate:
Cache keys, kernels, the split-K protocol, tuned configs and the steady-state
path are unchanged. The pool is entered only on a cache miss — once per
(device, stream)— so the hot path is bit-identical.The same lazy-allocation pattern guards five sibling scratch caches, which take
the same helper: the a16w16 split-K semaphore/signal buffers (gfx950 and its
gfx1250 twin), the ASM a16w16 counter, the a8w8 zero-bias buffer, and topk's MoE
workspace.
mla_reduce's_get_splitk_scratchis deliberately left alone — itskey carries no stream, so a first miss inside a capture needs a shape
combination no eager launch has used; its docstring now says so instead of
claiming to be capture-safe.
The a8w8 zero-bias buffer is the one with no protocol behind it: the kernel only
reads it, so nothing hands it back at zero and a clobbered one silently adds the
intermediate's values as bias on every replay. Unfixed, an lru-hit graph reads
6.0there; fixed,0.0.Replaces the ASM a16w16 mechanism from #4916
That PR fixed this hazard on the ASM path by giving every recorded launch its
own ring slot and recording a
zero_per launch, so a replay starts from zerowhatever the counter held. Measured here, that node costs 2.2–2.4 us per
launch — independent of the counter's size, so it is the node itself, not the
bytes — plus a fixed 16 MiB per device allocated whether or not the process ever
captures a graph. On a 10.9 us decode GEMM that is +20%.
Both kernels hand the counter back at zero on their own (verified: 0/1024 and
0/256 nonzero after six launches), so nothing has to re-zero it between
launches. What neither survives is another graph in the same pool owning the
counter's address — which is what this PR removes instead of tolerating.
check_graphdirtied the counter withfill_(2)from outside to stand in forthat state. The workspace belongs to the GEMM and nothing outside it writes
there, so the decoy graph it now uses produces the dirt the way a capture
actually does. #4916's own repro command passes:
@JohnQinAMD — flagging this for your review since it reworks your fix.
torch floor.
use_mem_poolcan only win inside a capture on torch 2.10 ornewer: older allocators scan
captures_underwayin registration order, so thegraph pool registered at capture begin always wins, while 2.10 scans it in LIFO
order (
c10/cuda/CUDACachingAllocator.cpp::get_pool). On an older torch thehelper warns once per device when it is entered during a capture, and the
capture test is skipped rather than failed. Thanks to @valarLip for measuring
what that looks like on 2.9.1.
Verification (gfx950, torch 2.10.0+rocm7.2.4)
max_err=778.5. This branch: 0/2048.g2=9.0. This branch:g2=3.0.op_tests/test_graph_alloc.pysegment_pool_idis the private pool'stest_gemm_a16w16.py --graph)device=: 0 bytes routed. Withdevice=: routed.op_tests/test_graph_alloc.pyis a plain pytest regression on allocator /CUDA-graph pointer semantics -- there is no kernel, no shape sweep and nothing
to time, so it does not follow the op-test benchmark template.
🤖 Generated with Claude Code