Skip to content

The TMEM column count and the MMA shape are the accumulator's, and the types say so now (#128) - #203

Merged
feitreim merged 2 commits into
mainfrom
fix/128-typed-tmem
Aug 6, 2026
Merged

feitreim merged 2 commits into
mainfrom
fix/128-typed-tmem

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Closes #128.

Both halves of §7.5: the two places the TMEM surface was untyped are the two
places being untyped had already cost something.

1. The column count is a const parameter

alloc_block / alloc_cluster / dealloc_block / dealloc_cluster take
const COLUMNS: usize and assert tcgen05.alloc's rule — a power of two in
[32, 512] — in a const block, which fires at codegen. dealloc_* takes
the same parameter, so the pair that has to agree is type-checked rather than
remembered.

flash_forward sheds the rule it was carrying by hand and keeps only the half
that is its own (TMEM_COLUMNS >= KEYS + HEAD). The doc example on
TmemTile::split_columns was itself allocating the illegal 192 and now says why
it cannot.

No runtime escape hatch. The one caller in the tree with genuinely no
constant is device-tests' tmem_ladder_runtime, whose entire purpose is a
column count ptxas cannot record — "only the first can see a constant, and
this rung has none to see". It writes alloc_block's body out by hand, exactly
as its sibling tmem_ladder_none already writes it with the allocator removed.
An escape hatch on the library entry point would be a hole in the assert for the
sake of one probe whose point is to be illegible to it.

2. The walks take the accumulator, and derive the shape

All nine entry points take TmemTile<M, N> instead of u32, and none takes an
MmaShape.

The derivation, and what it rejects. Two lookups, both const fn, both in
src/mma.rs:

M N
mma::shape(m, n) — cta_group::1 32, 64, 128 64, 128, 256
mma::pair_shape(m, n) — cta_group::2, one rank's tile 128 64, 128, 256

Anything else panic!s in a const block, i.e. fails at codegen. Nothing
rounds: a [128, 192] accumulator rounded up to M128_N256 would write 64
columns of whatever segment sits after it, which is #175's failure arriving from
a different direction.

cta_group::2 is a second lookup and not shape(2 * m, n) because M256 is
the pair's only class: a 32-row rank tile doubles to M64, which is a legal
shape for one CTA and no shape at all for a pair. That case is a unit test.

The four typed walks gain no generic parameter. The accumulator is
TmemTile<AR, BR> for mma_abt, TmemTile<AR, N> for mma_ab and
TmemTile<M, N> for mma_atb/mma_atbt — const parameters those signatures
already had. So the operands' shape and the accumulator's are now one thing the
type checker holds together, on top of losing the shape argument.

mma_walk_cg2 / mm_walk_cg2 erase their operands into OperandWalk, so their
M and N are new. A const parameter cannot be elided from a turbofish that
names any other, so callers write mma_walk_cg2::<Bf16, CHUNKS, _, _> — the two
_ are read off the tile, and writing them out would be the caller restating
what the argument already says.

Three pair_shape lookups deleted, not one: examples/src/gemm.rs,
examples/src/gemm_sol.rs (as mma_shape) and experiments/src/gemm.rs each
had a const fn from a tile width to a shape with a panic! for its default
arm. That was the only thing in the repo checking an accumulator against the ISA.

Every shape in the tree is unchanged by the derivation — checked one call site at
a time before the first Modal run, and confirmed by the device cases.

Two call sites gained a const parameter, and why

generic_const_exprs is not on, so a const argument cannot be arithmetic on a
const parameter. Two bodies allocate a multiple of a generic width:

  • gemm_sol::small_body gains ACCUM_COLUMNS (was 2 * N as u32)
  • device-tests' fragment_probe gains HELD (was if RESTAGE { 2 * COLUMNS })

Each is held to its own N by a const { assert!(..) }, which is the pattern
experiments/src/gemm.rs already uses and documents for HALF_N beside
BLOCK_N. The 22 small_body::<..> instantiations across sol_ablate and
sol_watch are the mechanical part of the diff.

Explicitly not done

No accumulator element type. fp32 accumulate stays hardcoded in descriptor;
fp16 accumulate is a .kind::f16-only mode with no caller. The point is that
TmemTile is now in a position to carry one.

Gates

cargo test (host, 199 incl. doctests) pass
scripts/fmt --check clean
tier 2 scripts/modal-run build pass — clippy ×4, host tests, cargo doc --features host, cargo oxide build --arch sm_100a of all three kernel crates, the cublas FFI link, the vendored upstream build
tier 2 scripts/modal-run regcount pass — occupancy step gate: "every gated kernel is inside its step"; depot census unchanged at 1 of 12 shipped, flash_forward at 690 st.local, the figure modal_app.py already records
tier 3 scripts/modal-run device_tests 52 of 52 pass, including the five this issue names — mma A·Bᵀ, mma A·B, mma Aᵀ·B, mma Aᵀ·Bᵀ, mma transpose control — plus fragment map ×3, tmem occupancy ladder (6 rungs, allocations balanced), tmem residency census, repeated launch
tier 3 scripts/modal-run examples pass — both binaries, all rungs, both check sizes

The tier-2 build is the load-bearing one here: a post-monomorphization
const { assert!(..) } is invisible to cargo check, and this change adds six
of them.

…e types say so now (#128)

`tmem::alloc_block` / `alloc_cluster` took a runtime `columns: u32` straight to
an instruction operand, so `tcgen05.alloc`'s power-of-two-in-`[32, 512]` rule had
nothing in a position to see it and `flash_forward` asked for 192 from the day it
was written. They take `const COLUMNS: usize` and assert the rule in a `const`
block, which fires at codegen — tier 2's job. `dealloc_*` takes the same
parameter, so the pair that has to agree is type-checked.

All nine MMA entry points took a bare `u32` accumulator plus an `MmaShape`, where
`TmemTile<R, C>` already knew that shape. They take the tile. `mma::shape` reads
the instruction off `[M, N]` and `mma::pair_shape` off one rank's, and both
**reject**: the set is the ISA's, `M ∈ {32, 64, 128}` by `N ∈ {64, 128, 256}` at
`cta_group::1` and 128 rows a rank by the same `N` at `cta_group::2`, so a
`[128, 192]` is a codegen error rather than an `M128_N256` writing 64 columns of
whatever is after it. `cta_group::2` is a second lookup and not `shape(2 * m, n)`
because a 32-row rank tile doubles to `M64`, legal for one CTA and no shape at
all for a pair. Three kernel-side `const fn`s from a tile width to a shape, each
with a `panic!` for its default arm, are gone with it.

The four typed walks gain no parameter — the accumulator is `[AR, BR]`, `[AR, N]`
or `[M, N]` in const parameters they already had, which is one more thing the
operands and the accumulator now have to agree about. `mma_walk_cg2` erases its
operands' types, so its `M` and `N` are new and its callers write
`::<Bf16, CHUNKS, _, _>`.

No runtime escape hatch. The one caller with genuinely no constant is
`device-tests`' `tmem_ladder_runtime`, whose whole point is a column count
`ptxas` cannot record; it writes `alloc_block`'s body out by hand, as
`tmem_ladder_none` already writes it with the allocator removed.

`gemm_sol`'s `small_body` gains `ACCUM_COLUMNS` and `fragment_probe` gains
`HELD`, both for the reason `HALF_N` sits beside `BLOCK_N`: a const argument
cannot be arithmetic on a const parameter, and each is held to its `N` by an
assert.

Not done here, deliberately: the accumulator has no element type, so fp32
accumulate stays hardcoded in `descriptor`. The point is that the type is now in
a position to carry one.
@feitreim
feitreim merged commit 0c50ad6 into main Aug 6, 2026
feitreim added a commit that referenced this pull request Aug 6, 2026
…are (#204)

#198 and #203 each merged green and composed into a build error: the STTM
probe was written against the untyped MMA surface (#198), which #203 removed.
The probe now speaks the typed one — alloc_block::<COLUMNS>, the two [128, 64]
halves via split_columns/columns_right, and the shape argument gone because
the tile is the shape. Same instructions, same launch, no semantic change.
feitreim added a commit that referenced this pull request Aug 6, 2026
…ype the drains must cover (#219)

Two finds from the compressed-API audit, neither of which changes what any
kernel emits.

`mma::descriptor`'s note read as though a typed accumulator were waiting on
#128. It is not: #203 closed #128 by typing the walks' accumulator *address*
and deliberately stopping there, so an accumulator element type is owned by
nothing and asked for by nobody. The note now says that.

`access_width` returned a `usize` and both drains ended in `_ =>`, so a fifth
rung would have arrived at the 4-byte arm instead of failing to compile. It
returns `AccessWidth` now, a `#[repr(usize)]` enum whose discriminants are the
byte counts, and `store_shared_rows`/`accumulate_shared_rows` name every rung.
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 columns are an unchecked u32 and the MMA takes a raw address, not the TmemTile that exists

1 participant