Skip to content

MMA: AtB/AtBt walks, mm entry points, generic KIND routing - #69

Merged
feitreim merged 1 commit into
mainfrom
mma-operand-orders
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
mma-operand-orders

Conversation

@feitreim

Copy link
Copy Markdown
Owner

What this is

The three things #12 asked for, and — because the MMA layer is the part of this
library nothing here was waiting on — a harness that can tell whether the new
walks are right.

The walks. mma_atb ([K, M] × [K, N], both transpose bits) and
mma_atbt ([K, M] × [N, K], transpose A only) close
{AB, ABt, AtB, AtBt}. Both take their transpose bits back off
OperandWalk::transposed, the way mma_walk_cg2 already did, so the operand
order and the descriptor cannot disagree because only one of them is stated.
The MN-major operand is SharedTile::mn_walk — a K=16 chunk is 16 rows, and MN
past the first 64 columns is reached through the descriptor's leading offset,
so one instruction covers a 128-wide MN rather than one per stacked subtile.

mm entry points. mm_abt, mm_ab, mm_atb, mm_atbt, mm_walk_cg2 —
the same instruction, with the accumulator-init invariant moved out of a bool
and into the name. The accumulate argument stays on the mma_* walks for the
one caller whose choice is genuinely runtime (gemm's k > 0, threaded through
one chain that a typed two-arm branch would duplicate). flash_forward passed
false twice and now says mm_abt/mm_ab.

Generic KIND routing. The walks issue through MmaElement::mma /
mma_cg2, which each impl writes as
tcgen05_mma_shared::<{ Self::MMA_KIND }, CTA_GROUP, 0>. fp8 is now an
Element impl and not a second MMA layer.

Why the harness is most of the diff

A walk no kernel calls emits no PTX and measures nothing, so the four orders
needed a device case each before they were worth having. Six new cases:

case claim
walk blind spots host-only. Removes each of the 64 K planes, permutes the K chunks four ways, confuses the two stacked MN subtiles five ways — 73 wrong walks, and fails if the reference cannot see one of them.
mma ABᵀ / mma AB / mma AᵀB / mma AᵀBᵀ one pair of logical matrices staged four ways (no TMA box transposes), one reference, == on all 16384 values.
mma transpose control mma_atb's operands under mma_abt. Required to disagree — otherwise the transposed cases prove only that some walk computes Aᵀ·B.

Why the transposed cases cannot pass against the untransposed walk: the staged
A is [64, 128] against [128, 64] and neither generator is symmetric in
its arguments, so no operand here is its own transpose; and the control
measures it directly rather than arguing it — 4096 of 4096 values differ.

Why it cannot repeat #48: walk blind spots is the standing proof that the
reference varies along every coordinate the walk computes, K planes included.
#48's b_value was constant along K and certified a kernel that read one plane
of B forever.

What I verified, and on what

  • modal run modal_app.py::device_tests — 28 of 28 cases pass, the 22 that
    existed and the 6 new ones.
  • modal run modal_app.py::examples — gemm 512x256x256 exact, so the
    KIND-generic cta_group::2 intrinsic computes what the f16 wrapper did.
  • modal run modal_app.py::build — clean; device-tests clippy has no
    warnings of mine.
  • modal run modal_app.py::gaps --features flash — exit 0, so flash_forward
    still compiles on mm_abt/mm_ab.
  • modal run modal_app.py::regcount, this tree against main — every
    pre-existing kernel's register count, spill and stack frame is unchanged.
    The four new walk kernels cost 32 registers each, the same as
    fragment_map_32x128's identical drain.
  • Locally: cargo fmt --check in all three crates, clippy --all-targets --locked -D warnings, cargo test --locked (67 host tests, 4 new),
    cargo doc -D warnings, cargo metadata --locked in all three.

Two things the issue got wrong

The generic intrinsic does not lower identically for KIND=0. The issue
called it "an assumption to check rather than assert", and checking it says no.
At the pinned revision tcgen05_mma_f16 emits the .disable_output_lane form
with an all-zero mask (four mov.u32 of zero); tcgen05_mma_shared::<0, 1, 0>
emits .collector::a::discard and no mask. Same semantics — an all-zero
disable mask disables nothing, and collector::a::discard is the default — and
ptxas prices them the same, but they are not the same instruction and the
switch is a real change to a validated path. That is why examples and the
whole device suite are in the verification list rather than just build.

KIND cannot come off the const at the call site.
tcgen05_mma_shared::<{ E::MMA_KIND }, ..> for a type parameter E needs
generic_const_exprs. An impl writing Self::MMA_KIND for itself is legal, so
the routing is a trait method with the const kept beside it as the thing the
method is a substitution of.

And the line estimate was low, in the place that matters. GAPS.md said
"~80 lines for the missing walks"; the walks are 70 with their docs. The
harness that makes them worth having is ~420.

Closes #12

`mma_atb` and `mma_atbt` complete `{AB, ABt, AtB, AtBt}`; every walk gets an
`mm_*` twin that starts the accumulator fresh rather than taking a `bool`; and
the walks now issue through `MmaElement::mma`/`mma_cg2`, which routes tcgen05's
`KIND` off the element instead of calling the f16-specialized wrapper.

Nothing in this repo calls the new walks, and an unused walk that is wrong is
worse than an absent one — so most of this is the check. `device-tests` runs
all four orders over one pair of logical matrices staged four ways (no TMA box
transposes) and requires the same product from each. `walk blind spots` runs on
the host and fails if the reference cannot see a dropped K plane, a permuted K
chunk or a confused MN subtile; `mma transpose control` is the same operands
under `mma_abt` and is required to *disagree*, so the transposed cases prove
their transpose bits and not merely that some walk multiplies.

Two findings. `tcgen05_mma_f16` and `tcgen05_mma_shared::<0, 1, 0>` do not
lower to the same PTX at the pinned revision — the f16 wrapper emits the
`.disable_output_lane` form with an all-zero mask, the generic one emits
`.collector::a::discard` and no mask — though `ptxas` prices them identically
(every pre-existing kernel's register count is unchanged). And `KIND` cannot be
read off an associated const at the call site: `tcgen05_mma_shared::<{ E::KIND
}, ..>` needs `generic_const_exprs`, so the routing is a trait method each impl
writes for itself.

28 of 28 device cases pass; `gemm` is still exact.
@feitreim
feitreim merged commit 7113a4d into main Jul 28, 2026
2 checks passed
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.

MMA: AtB/AtBt walks, mm entry points, generic KIND routing

1 participant