Skip to content

A kernel's shared plan is entirely open-coded, in bytes, twice per kernel #125

Closed

Description

@feitreim

Found by the 2026-07-29 library audit (GAPS.md §7). This is the largest
thing every kernel in the repo writes for itself, and almost all of it is
arithmetic over kittens types whose sizes kittens already knows.

What is duplicated

All five kernels compute a shared plan twice — once as a host-visible
SHARED_BYTES constant and once as a pointer walk inside the kernel — and
nothing but a hand-written const { assert!(..) } relates the two.

kernel plan constant in-kernel offset walk
gemm.rs shared_plan/staged_plan 533-593, SHARED_BYTES 567 attach 2689-2722
gemm_ws.rs shared_plan/staged_plan 495-519, 508 attach 1104-1135
softmax.rs SHARED_BYTES 259 275-277
layernorm.rs SHARED_BYTES 186 212-217, 326-332
flash_forward.rs SCRATCH_BYTES 213, SHARED_BYTES 214 237-249

The two GEMM walks are the same program with the type names changed:

let rings = ARing::BYTES + BRing::BYTES;
let smem = DynamicSharedArray::<u8, 128>::get_raw();
let scratch = smem.add(rings);
let tmem_slot = scratch.add(2 * STAGES * 8 + 8) as *mut u32;
... SemaphoreRing::attach(scratch as *mut Barrier)
... SemaphoreRing::attach((scratch as *mut Barrier).add(STAGES))
... Semaphore::attach((scratch as *mut Barrier).add(2 * STAGES))
... ClcQueue::attach(smem.add(rings + 2 * STAGES * 8 + 8 + 8))

Everything after rings is made of library objects — SemaphoreRing,
Semaphore, the u32 staging word tmem::alloc_cluster requires, ClcQueue
— laid out by hand, in bytes, with the barrier count spelled 2 * STAGES * 8
in one place and .add(STAGES) in Barrier units in another. flash_forward.rs
writes the same layout twice in the same file, once in each unit (213 against
244-249).

The four concrete leaks it causes

  1. DynamicSharedArray::<u8, 128>::get_raw() — every kernel imports
    cuda_device for this because kittens has no accessor for the base of the
    launch's dynamic shared memory.
  2. cuda_device::barrier::Barrier imported purely as a cast target, because
    plans are *mut u8 and Semaphore::attach takes *mut Barrier
    (gemm.rs:2700, gemm_ws.rs:1113, softmax.rs:277, layernorm.rs:217,
    flash_forward.rs:243).
  3. The TMEM staging word — alloc_cluster(slot: *mut u32, ..) makes the
    caller find, align and account for 8 bytes for a resource the library owns
    (scratch_bytes, gemm.rs:522, gemm_ws.rs:485).
  4. ClcQueue::ALIGNMENT is the caller's proof obligation — both GEMMs carry
    an identical const block showing the queue's offset is 16-byte aligned
    (gemm.rs:2679-2687, gemm_ws.rs:1098-1102).

layernorm.rs:328-330 states the general form of the problem out loud: "The
partials first, because Tile::BYTES is the only offset in this plan a vector's
128-byte alignment is promised at; the barrier behind it needs eight."
That is
an alignment argument a plan type would own.

The shape of a fix

A SharedPlan cursor: start at the launch's base, hand out typed handles in
declaration order, accumulate the total as a const, and align each handle to
what its own type requires (SharedTile 128 B, ClcQueue 16 B, Barrier 8 B).
The plan's total is then the same expression the launch declares, rather than a
second one asserted equal to it.

Two constraints a design has to survive, both already visible in the callers:

  • The total must be usable in #[launch_contract]'s literal, which is a
    const context. So the plan's arithmetic has to be const fn over the same
    parameters the handles are — gemm.rs's shared_plan(BLOCK_N, BLOCK_K, STAGES)
    is what that looks like today.
  • A cursor cannot be a &mut builder on the device if it is to stay free:
    every handle is Copy and register-resident, and the walk must fold away.
    Building it in a const fn on the host side and indexing it on the device is
    one way; a Copy cursor threaded by value is another.

Not in scope: changing any handle type. This is placement, not representation.

Estimate

~150-250 lines in src/, plus one host test per kernel shape that the plan's
total equals the literal each launch declares today (the five numbers above are
the regression suite — 98_392, 114_816, and the three smaller ones). The
kernel-side diff is a deletion in five files, but examples/ is being
restructured, so land the type first and migrate behind it.

Related: #124 (scope — Scope and a plan are the two things a kernel-shaped
abstraction needs), #125 (the epilogue, the other thing both GEMMs open-code).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:scaffolding

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions