Repository navigation
Description
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
DynamicSharedArray::<u8, 128>::get_raw()— every kernel imports
cuda_devicefor this becausekittenshas no accessor for the base of the
launch's dynamic shared memory.cuda_device::barrier::Barrierimported purely as a cast target, because
plans are*mut u8andSemaphore::attachtakes*mut Barrier
(gemm.rs:2700,gemm_ws.rs:1113,softmax.rs:277,layernorm.rs:217,
flash_forward.rs:243).- 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). ClcQueue::ALIGNMENTis the caller's proof obligation — both GEMMs carry
an identicalconstblock 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 beconst fnover the same
parameters the handles are —gemm.rs'sshared_plan(BLOCK_N, BLOCK_K, STAGES)
is what that looks like today. - A cursor cannot be a
&mutbuilder on the device if it is to stay free:
every handle isCopyand register-resident, and the walk must fold away.
Building it in aconst fnon the host side and indexing it on the device is
one way; aCopycursor 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).
Activity
The shared plan is one walk now, and the two ends of it cannot share …
The shared plan is one walk now, and the two ends of it cannot share …
The shared plan is one walk now, and the two ends of it cannot share …
The shared plan is one walk now, and the two ends of it cannot share …