Skip to content

MMA walks build their own instruction descriptor - #34

Merged
feitreim merged 4 commits into
mainfrom
issue-30-derive-descriptor
Jul 27, 2026
Merged

feitreim merged 4 commits into
mainfrom
issue-30-derive-descriptor

Conversation

@feitreim

Copy link
Copy Markdown
Owner

mma_abt, mma_ab and mma_walk_cg2 each read their operands under a
different transpose configuration, and each took the instruction descriptor as
a raw u32 the caller assembled. Mismatching the two does not fault — the MMA
reinterprets the operands and fills the accumulator with wrong numbers — and
the only thing standing between a kernel and that was a paragraph in mma.rs's
header. The walks now build the descriptor themselves.

The API

mma_abt(tmem, q, k, MmaShape::M128_N64, false);
mma_ab(tmem, p, v, MmaShape::M128_N128, false);
mma_walk_cg2::<Bf16, CHUNKS>(tmem, a.k_walk(), b.k_walk(), MmaShape::M256_N128, k > 0);

MmaShape is cuda_device's Tcgen05MmaShape, re-exported from kittens::mma
so a kernel names one thing rather than four.

Derived from the function you called. The transpose bits. mma_abt passes
(false, false) and mma_ab (false, true) to a private descriptor<E>
helper; neither is expressible by a caller any more.

Derived from the operands. The element type, through a new
MmaElement::ELEMENT_TYPE beside MMA_KIND. The two are genuinely different
fields: f16 and bf16 share KIND 0 and are told apart only by the descriptor's
atype/btype, so a bf16 tile under an F16 descriptor was a silent
miscompute the type system can now rule out. Tcgen05ElementType has exactly
three values upstream (F16/BF16/TF32), all reachable from an MmaElement, so
the const is total rather than a bf16-shaped stub.

Left to the caller. The shape, because it names the accumulator band the
caller allocated and the CTA-group mode — nothing in the operands knows it.

mma_walk_cg2

Split into the two fields rather than treated as one question:

  • Transpose bits: derived. OperandWalk gained a transpose flag set by
    k_walk (false) / mn_walk (true) and read back through
    OperandWalk::transposed. This is the case the issue is really about: gemm
    picks K-major vs MN-major at runtime, so the descriptor bits are runtime
    data too, and a caller-side descriptor could not be checked against the walk
    even in principle. The flag is the same fact as the walk's chunk step and
    leading offset, so it belongs in the walk; it costs two ORs that hoist out
    of the chain.

  • Element: still the caller's, as mma_walk_cg2::<Bf16, CHUNKS>. An
    OperandWalk has erased its element, and I did not force it back:

    Since CHUNKS is turbofished anyway, the caller has to write E either way,
    and a type parameter is what MMA: AtB/AtBt walks, mm entry points, generic KIND routing #12 will need at this call site regardless. It
    is a narrower assertion than before — Bf16, in the same vocabulary as the
    tiles, not descriptor bits — but it is still an assertion, and I would rather
    say so than pretend the symmetry is there.

Accumulator type

Left hardcoded fp32, documented at the helper. The walks name TMEM by a bare
u32, so there is no accumulator type to derive one from; fp16 accumulation is
a .kind::f16-only mode no kernel here uses. Deriving it means giving the
walks a TmemTile instead of an address, which is a real refactor of every
signature and belongs with whatever issue does that — not this one.

Verified

  • modal run modal_app.py (B200), once, at the end: 7 of 7 cases pass.
    The fragment map cases issue real mma_abts, so a wrongly built descriptor
    shows up as wrong values — this is the check that the change is correct, not
    just well-typed.
  • modal run modal_app.py::build: library clippy (--features host --all-targets), host tests, harness clippy, and real cargo oxide build --arch sm_100a for both device-tests and kittens-examples.
  • Host tests: the descriptor builder is plain const fn bit arithmetic with no
    device intrinsic behind it, so it does test on the host. Two new cases in
    mma.rs — the three shipped configurations encoded against the PTX field
    layout written out independently of the builder, and the walks' transpose
    flags. 35 tests pass.
  • cargo clippy --all-targets, cargo fmt --check clean.

Examples

  • gemm (hard requirement: it compiled before and must after) still builds
    for sm_100a — verified through cargo oxide build, not cargo check. Its
    six-line descriptor block is one let shape = MmaShape::M256_N128;, and the
    comment that used to explain why the transpose bits were absent is now a
    statement about where they come from.
  • flash_forward (aspirational, --features flash) updated the same way:
    two shapes instead of two hand-built descriptors, including the transpose_b
    it had to remember for mma_ab. Still aspirational, still blocked on the
    same issues — this was never what blocked it.
  • examples/README.md: §9's descriptor bullet is gone, and the fix is
    recorded under "what the examples confirm already works", with the cg2
    element noted as the one field a caller still states. The status table is
    unchanged: nothing moved between compiles and aspirational.

Closes #30

feitreim added 4 commits July 27, 2026 18:01
Two facts the instruction descriptor needs that its operands already know.
`MmaElement::ELEMENT_TYPE` sits beside `MMA_KIND` because f16 and bf16 share
kind 0 and are told apart only by the descriptor's atype/btype. `OperandWalk`
carries the transpose bit its layout implies — the same fact as its chunk step
and leading offset — so a kernel that picks K-major vs MN-major at runtime
moves the walk and the instruction together.
Each walk requires a different transpose configuration and nothing checked the
raw `u32` descriptor against it; a mismatch does not fault, it fills the
accumulator with wrong numbers. The walks now take an `MmaShape` — the one
field they cannot know, since it is the caller's accumulator band — and build
the descriptor from their own transpose configuration plus `E::ELEMENT_TYPE`.
`mma_walk_cg2` reads its bits from `OperandWalk::transposed` so a runtime
layout choice stays paired, and takes `E` from the caller because a walk has
erased it (#12 owns whether the walk carries it back, since routing `KIND`
wants it as a type there anyway).

The accumulator stays fp32: a walk names TMEM by a bare `u32`, so there is no
accumulator type to read one off.

First host tests of the descriptor: it is pure bit arithmetic, so the encoding
is checked against the PTX field layout spelled out independently of the
builder.
device-tests, gemm and flash_forward each hand-built a descriptor and relied on
prose to keep it matched to the walk. All three now pass an `MmaShape`.
# Conflicts:
#	device-tests/src/main.rs
@feitreim
feitreim merged commit 9930c7e into main Jul 27, 2026
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.

The MMA instruction descriptor is unchecked against the walk it is paired with

1 participant