Skip to content

Element carries MMA kind and conversion, ready for sub-bf16 types #2

Closed

Description

@feitreim

Groundwork so sub-bf16 element types drop in cleanly later, without committing to them now.

What Element is today

pub trait Element { const BYTES: usize; }
pub struct Bf16;

Byte width only. Everything else about bf16 is hardcoded elsewhere: mma.rs names Bf16 concretely and calls tcgen05_mma_f16, and ldst.rs hardcodes the cvt_f32x2_bf16x2 pack.

What cuda-oxide actually gives us at the pinned rev (b099f64) — better than assumed:

Need Status
Generic MMA over element kind tcgen05_mma_shared / tcgen05_mma_tensor take a KIND const: 0=f16, 1=tf32, 2=f8f6f4, 3=i8
Weight-stationary MMA per dtype tcgen05_mma_ws_{bf16,f16,tf32,e4m3,e5m2,e2m1,e2m3,e3m2}
f32 -> fp8 conversion cvt_rn_satfinite_{e4m3,e5m2}x2_f32 (+ relu variants)
MXFP block-scale smem->tmem tcgen05_cp_*_b4x16_p64 (fp4), tcgen05_cp_*_b6x16_p32 (fp6)
f32 -> fp4/fp6 conversion absent — needs hand bit-packing
e8m0 (MXFP scale) conversion absent

So fp8 is not blocked upstream. The blocker is entirely on our side: we call the f16-specialized wrapper instead of the KIND-parameterized one.

Scope of this issue — trait design only, still one impl:

  • Element gains: the tcgen05 KIND constant, elements per packed word, and the f32 -> element conversion as an associated fn.
  • ldst.rs's pack goes through that associated fn instead of calling cvt_f32x2_bf16x2 directly.
  • mma.rs stops naming Bf16 in its bounds and takes E: Element (the intrinsic swap is MMA: AtB/AtBt walks, mm entry points, generic KIND routing #12).
  • Bf16 remains the only impl. No fp8 impl here.

Adding Fp8E4M3 afterward should be a new impl and nothing else. FP4 stays out until the conversion intrinsics exist (#16).

Activity

  1. added
    enhancement
    area:types
    area:mma
    on Jul 26, 2026
  2. feitreim commented on Jul 26, 2026

    @feitreim
    OwnerAuthor

    Landed. Element now carries type Unpacked, BYTES, a derived PER_WORD, and fn pack; MmaElement: Element carries MMA_KIND: u32. Bf16 remains the only impl.

    Two decisions worth recording:

    • pack arity via associated type, with the restriction on the function rather than the trait. store_fragment<E: Element<Unpacked = [f32; 2]>> — the 2-per-word limit belongs to stmatrix.m8n8.x2 (two b16 matrices), not to elements. A 4-per-word element gets a type error at the call site instead of silently packing half its bytes.
    • MMA_KIND: u32, not u8 — cuda-oxide's tcgen05_mma_shared takes const KIND: u32, so MMA: AtB/AtBt walks, mm entry points, generic KIND routing #12 is a substitution rather than a cast.

    mma.rs is generic over E: MmaElement with const { assert_two_byte_element::<E>() } guarding the bf16-shaped chunk geometry. Note that post-monomorphization const asserts fire at codegen, not cargo check — weaker than the type bound store_fragment gets.

  3. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    Concrete follow-on from #8: an impl Element for F32 is ~15 lines and unblocks the global layout for fp32 buffers.

    #8 landed GlobalLayout<E: Element, const RANK: usize>, which is generic over the element but reachable only for bf16 — because Element is implemented only for Bf16. So layernorm's fp32 parameter vectors are still undescribable, and that is now one impl away rather than a hardcode.

    Nothing hard is hiding in it: Unpacked = [f32; 1], BYTES = 4, and pack/unpack are identity bit-casts. Worth noting that unlike Bf16::pack — which is cvt_f32x2_bf16x2, a device intrinsic whose host body is unreachable!() and therefore untestable off a GPU — an fp32 element would be fully host-testable. The TensorMapElement impl on top is one line (CU_TENSOR_MAP_DATA_TYPE_FLOAT32).

    But it will not unblock layernorm on its own, and the reason is worth recording here so nobody expects it to. TileBox — the trait that supplies a tensor map's box shape — is implemented only for SharedTile, and SharedTile const-asserts a whole swizzle atom per row. The narrowest fp32 tile it admits is 32 columns and swizzled; gamma/beta want an unswizzled [1, N] box. Swizzle has no "none" mode, and inventing one is a shared-memory-layout decision rather than an element-type one.

    That second half is #13 (shared vectors), already filed and already on layernorm's blocked-on list. Whatever #13 lands as a shared-vector type is the natural second TileBox impl, and it will want an unswizzled mode beside it.

    So the split is: the element work belongs here and is mechanical; the thing actually gating layernorm is #13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:mmaarea:typesenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions