Repository navigation
TMEM: relinquish the allocation permit, and lift alloc_cluster into the library - #54
Merged
Merged
Conversation
`alloc_cluster` was a private helper in `examples/src/gemm.rs` behind a GAP comment, which is the worst place for it: the `cta_group::2` allocator's participation rules are a hardware fact about a cluster accumulator, and getting them wrong is what hung that kernel's second launch (#40). Keeping the fixed version in an example while the library shipped none at all meant the next `cg2` kernel would rediscover it. Moved verbatim -- this is the body that ran on a B200, so it is not the place to improve anything -- as `tmem::alloc_cluster`, with `dealloc_cluster` beside it so the pair's dealloc participation ("one full warp in each peer CTA") is stated once rather than open-coded at each call site. `gemm.rs` calls both and keeps its own fence/sync ordering, exactly as it does around `dealloc_block`. #24 item 5.
`alloc_block` never issued `tcgen05.relinquish_alloc_permit`, so every CTA this library ever ran exited still holding its permit. PTX requires it back before the CTA exits, so this is a conformance hole whatever the hardware does with it, and warp 0 -- the warp that allocated, and the "one full warp" the instruction wants -- is the one that gives it back. Beside the alloc rather than beside the dealloc. The permit is the right to *call* the allocator, not the columns; the columns are `dealloc_block`'s. This library allocates once per CTA, so the permit is spent the moment the alloc returns, and holding it for the rest of the CTA's life is the thing the instruction exists to prevent. The safety note now says "at most once per CTA", because allocating again after relinquishing is illegal. What this is *not* is the deadlock #46 predicted, and the issue should not be closed believing it was. Measured on a B200 with this call removed: 4736 CTAs over three launches, each allocating all 512 columns, all completed; and a CTA sitting on an unrelinquished permit for milliseconds did not delay a co-resident CTA's own allocation (20.14 ms either way, same binary, both kernels). The hang in #40 is better explained by its other half -- a `cta_group::2` dealloc issued by one CTA of the pair leaks columns, and columns really do not come back. Costs 4 registers in `sttm_roundtrip` (36 -> 40) and none anywhere else; no spills appear.
Every case in this harness launched once, over a grid that fits in one wave. That cannot see a resource acquired at CTA entry and released at exit: when nothing is scheduled behind you, a leak is free. It is a class, not one bug -- "fine once, broken twice" applies to anything with that shape -- and the only reason #40 found it in the GEMM is that a benchmark loop happened to launch the same kernel twice. `relaunch_probe` is the standing guard: 296 blocks (twice the B200's 148 SMs), each taking the SM's entire 512 columns of tensor memory so that every CTA but the first on an SM is served out of columns its predecessor gave back, three launches, the whole dump checked each time. The round trip through TMEM is the sttm case's and proves nothing new about the store map; it is there so a CTA that finished is distinguishable from one that was never scheduled. `finish_or_abort` is what makes it safe to have a case that can hang: the wait is a polled event with a deadline instead of `cuStreamSynchronize`, and a launch that blows the deadline is printed as a failure and the process aborted. A hung context cannot be torn down cleanly and nothing after it can make progress, so the alternative is a container that reports nothing and sits until its 30-minute timeout. Honest about what it catches: not #46's leaked allocation permit, which has no observable effect on a B200 -- this case passes with and without that fix, and it was run both ways to check. It catches leaked columns, an unbalanced allocator collective, and the next resource with the same shape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The fix, and where it goes
alloc_blocknever issuedtcgen05.relinquish_alloc_permit. It does now, fromwarp 0 — the warp that allocated, and the "one full warp, uniformly" the
intrinsic's doc asks for — immediately after the alloc, not beside the
dealloc.
That placement is the point. Relinquishing the permit and deallocating are
different operations: the permit is the right to call the allocator again, the
columns are what
dealloc_blockreturns. This library allocates once per CTA,so the permit is spent the moment
tcgen05.allocreturns, and holding it forthe rest of the CTA's life is exactly what the instruction exists to prevent.
alloc_block's safety note now says "at most once per CTA", because allocatingafter relinquishing is illegal.
The
cta_group::1participation was already right (warp 0, all 32 lanes,uniform), so unlike #40 there was no second half to fix here.
The new case does not fail without the fix, and here is what I measured
You asked me to verify this and to say so rather than work around it. I did,
three ways, and the honest answer is that #46's premise does not reproduce on
the
cta_group::1path.repeated launchpasses.tcgen05_relinquish_alloc_permit()deleted, librarygenuinely rebuilt — the unused-import warning confirms it): 19/19. No hang.
permits per SM, each CTA taking all 512 columns), 3 launches: 19/19 again.
Then a controlled experiment, both variants in the same binary and the same
run so there is no cross-run variance: two kernels, one using
alloc_blockand one with a local copy minus the relinquish, each holding a 128-column
allocation for 5 ms across 296 blocks (2 per SM). If a held permit blocked a
co-resident CTA's allocation, the leaked variant would serialize and take ~2×.
No difference. I also checked the emitted PTX to rule out the fix being a
silent no-op:
tcgen05.alloc: 7 / tcgen05.relinquish: 7 / tcgen05.dealloc: 7,one per TMEM kernel. The instruction is really there and the broken build
really lacked it.
So: the change is conformance, not a bug fix. PTX requires the permit back
before the CTA exits and says nothing about what happens otherwise; the
hardware evidently reclaims it at exit. I'd keep the change — it is one free
instruction, it is what every other tcgen05 kernel does, and "undefined
behaviour that happens to work on this driver" is not a property to build a
library on — but #46 should not be closed believing a deadlock was fixed.
My reading of #40's hang is that it was the other half of that defect: a
cta_group::2dealloc issued from rank 0 alone returns only half the pair'scolumns, and leaked columns genuinely do not come back. The relinquish was
missing there too, which is why fixing it alone was not sufficient.
The device case, framed as the guard it is
relaunch_probe: 296 blocks (2× the B200's 148 SMs), each allocating the SM'sentire 512 columns so every CTA but the first on an SM is served out of columns
its predecessor gave back, launched 3 times, whole dump checked each time. The
TMEM round trip inside it is the sttm case's and proves nothing new about the
store map — it is there so a CTA that finished is distinguishable from one that
was never scheduled.
It is framed as a general "fine once, broken twice" guard rather than a
probe for this bug, and its doc says plainly that it does not catch #46's
permit leak. What it does catch: leaked columns, an unbalanced allocator
collective, and the next entry-acquired resource with the same shape.
finish_or_abortis what makes a case that can hang safe to ship: the wait isa polled CUDA event with a 30 s deadline rather than
cuStreamSynchronize, anda launch that blows it prints a
FAILline and aborts the process. A hungcontext cannot be torn down cleanly and nothing after it can make progress, so
the alternative is a container that reports nothing and burns its full
30-minute timeout.
alloc_clusterlifted (#24 item 5)gemm.rs's privatealloc_clusteris nowtmem::alloc_cluster, movedverbatim — it is the body that ran on silicon, so it is not the place to
improve anything — with
dealloc_clusterbeside it so the pair's "one fullwarp in each peer CTA" dealloc is stated once instead of open-coded per kernel.
gemm.rscalls both and keeps its own fence/sync ordering, as it does arounddealloc_block. Its GAP comment and module-doc entry are gone, andexamples/README.md§5 is marked closed (its old text described the DSMEMworkaround as a hardware fact, which was the bug).
Verified
modal run modal_app.py— 19/19, including the new case.modal run modal_app.py::examples—gemm512x256x256 exact,softmaxpasses. gemm still runs and still compares with
==.modal run modal_app.py::build— clippy on all three crates, host tests, realcargo oxide build --arch sm_100afor the harness and the examples.modal run modal_app.py::regcount—sttm_roundtrip36 → 40 registers, nospills;
fragment_map_32x128andreduction_probeunchanged.cargo clippy --all-targets,cargo fmt --checkclean in all three crates.No GPU run hung; the watchdog never fired.
Closes #46