Skip to content

TMEM: relinquish the allocation permit, and lift alloc_cluster into the library - #54

Merged
feitreim merged 3 commits into
mainfrom
issue-46-relinquish
Jul 28, 2026
Merged

feitreim merged 3 commits into
mainfrom
issue-46-relinquish

Conversation

@feitreim

Copy link
Copy Markdown
Owner

The fix, and where it goes

alloc_block never issued tcgen05.relinquish_alloc_permit. It does now, from
warp 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_block returns. This library allocates once per CTA,
so the permit is spent the moment tcgen05.alloc returns, and holding it for
the 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 allocating
after relinquishing is illegal.

The cta_group::1 participation 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::1 path.

  1. Fixed: 19/19, repeated launch passes.
  2. Fix stashed (tcgen05_relinquish_alloc_permit() deleted, library
    genuinely rebuilt — the unused-import warning confirms it): 19/19. No hang.
  3. Fix stashed, grid raised to 4736 blocks (32 waves, ~14k CTAs, ~96 leaked
    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_block
and 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×.

round 0 relinquished: 20.286662ms      round 0 leaked: 20.13727ms
round 1 relinquished: 20.141694ms      round 1 leaked: 20.149031ms

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::2 dealloc issued from rank 0 alone returns only half the pair's
columns, 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's
entire 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_abort is what makes a case that can hang safe to ship: the wait is
a polled CUDA event with a 30 s deadline rather than cuStreamSynchronize, and
a launch that blows it prints a FAIL line and aborts the process. 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 burns its full
30-minute timeout.

alloc_cluster lifted (#24 item 5)

gemm.rs's private alloc_cluster is now tmem::alloc_cluster, moved
verbatim — it is the body that ran on silicon, so it is not the place to
improve anything — with dealloc_cluster beside it so the pair's "one full
warp in each peer CTA" dealloc is stated once instead of open-coded per kernel.
gemm.rs calls both and keeps its own fence/sync ordering, as it does around
dealloc_block. Its GAP comment and module-doc entry are gone, and
examples/README.md §5 is marked closed (its old text described the DSMEM
workaround 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 — gemm 512x256x256 exact, softmax
    passes. gemm still runs and still compares with ==.
  • modal run modal_app.py::build — clippy on all three crates, host tests, real
    cargo oxide build --arch sm_100a for the harness and the examples.
  • modal run modal_app.py::regcount — sttm_roundtrip 36 → 40 registers, no
    spills; fragment_map_32x128 and reduction_probe unchanged.
  • cargo clippy --all-targets, cargo fmt --check clean in all three crates.

No GPU run hung; the watchdog never fired.

Closes #46

feitreim added 3 commits July 27, 2026 21:44
`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.
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.

TMEM allocation never relinquishes its permit — deadlocks the second wave of CTAs

1 participant