Skip to content

flash_forward asked for 144 KiB and nobody opted it in: the 0 blocks/SM was the attribute, not the tiles (#70) - #73

Merged
feitreim merged 2 commits into
mainfrom
flash-shared-memory-opt-in
Jul 28, 2026
Merged

feitreim merged 2 commits into
mainfrom
flash-shared-memory-opt-in

Conversation

@feitreim

@feitreim feitreim commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #70.

The evidence

The issue named two candidates and said not to assume. Both were measured on a
B200 (modal run modal_app.py::examples), on the same function, at the same
147536 B, with one cuFuncSetAttribute call between the two rows:

max dynamic shared blocks/SM
before the opt-in 49152 B 0
after the opt-in 147536 B 1

Device opt-in ceiling on the B200: 232448 B. Static shared: 0. So the plan
had 84912 B of headroom and was never close to too large.

This is hypothesis (1), and (2) is refuted.
cuOccupancyMaxActiveBlocksPerMultiprocessor returns 0 for a function nobody
opted in for in exactly the same way it returns 0 for tiles that do not fit,
and that ambiguity is the whole of the bug.

Where the opt-in actually lives, and what gemm relies on

The issue asked explicitly whether gemm is silently relying on something. It
is not, and it is not cluster_launch either:

  • CudaFunction::set_max_dynamic_shared_memory_bytes is pub(crate) in
    cuda-core (module.rs:514).
  • Its one and only call site is PreparedLaunch::__prepare (launch.rs:946).
  • __prepare is reachable only from a #[launch_contract] kernel's generated
    prepare_*; generate_cuda_module_prepare_launch_methods returns None for
    a kernel without a contract, so no non-contract kernel ever issues the
    opt-in on any code path
    .

gemm declares #[launch_contract(.., dynamic_shared = 73_792, ..)] and goes
through prepare_gemm_cg2. That is the whole reason it launches at 72 KiB.
flash_forward is a plain #[kernel] with no launcher, so nothing had ever
issued its opt-in — which is also why this was invisible until #3 put it in the
default build and #47's table could reach it.

The second measurement, which is the more useful one

Since the plan turned out not to be the constraint, I swept it. Each probe gets
a freshly loaded module, because the opt-in is a persistent mutation of the
function: admit it to 147536 once and every later query at a smaller size still
answers 1, so a naive sweep measures nothing.

flash_forward, blocks/SM at 128 threads:

dynamic shared 147536 114752 98368 73792 65536 49152 32800 16384 0
blocks/SM 1 1 1 1 1 1 1 1 1

At zero shared memory it is still 1. And across block widths, at 0 shared:

entry 32 64 128 256 max_threads_per_block
flash_forward 1 1 1 1 512
softmax_rows 28 14 7 3 896
layernorm_rows 8 4 2 1 256

The two controls scale inversely with block width, which is what a
register/warp ceiling looks like — layernorm_rows at 255 registers a thread
gives 256 threads/SM, which is #67's 2 blocks exactly. flash_forward is
flat: flat in shared memory, flat in warp count. ptxas prices it at 124
registers a thread with 0 spills and 0 static shared, which would allow 4
blocks.

A ceiling that ignores both shared memory and warps is a per-CTA resource,
and the one flash_forward holds that neither control does is TMEM — it and
gemm are the only tcgen05 entries in the crate, and gemm is a cluster
kernel the query cannot describe. I have not confirmed that mechanism and
have not written it down as fact; it wants its own issue.

The practical conclusion for #70's second scope bullet: the budget is below,
and shrinking it buys nothing. Two stages instead of three would free
32768 B and leave occupancy at 1.

The shared budget by component

component shape bytes share
Q tile [128, 128] bf16 32768 22%
K ring 3 x [64, 128] bf16 49152 33%
V ring 3 x [64, 128] bf16 49152 33%
P staging [128, 64] bf16 16384 11%
barriers + TMEM word 9 x 8 B + 8 B 80 —
total 147536

What changed

src/launch.rs (new, host-only) — kittens::launch::admit_shared_plan.
One place owns the opt-in, per the issue's "a launcher helper in the library
beats every example repeating it". It queries
CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK_OPTIN first and returns
AdmitError::TooLarge { bytes, limit } when the plan genuinely does not fit, so
the two causes this issue is about are two different values rather than two
zeroes. Idempotent and monotonic; a no-op under 48 KiB.

Why not just put a #[launch_contract] on flash_forward. I tried this
first and backed it out. A domain = 2 contract requires the output
DisjointSlice to implement __LaunchContractDisjointSlice<f32, 2>, i.e. to
carry a 2-D index-space marker — Index2D<S>, RowMajorTiles<..> or
Runtime2DIndex. flash_forward partitions its output in warp bands through a
raw GlobalRows cursor, which none of those describe, and domain = 1 is
wrong because the kernel's grid is genuinely 2-D (blockIdx.y is the head). So
a contract would mean stamping a false claim about the output partition onto
the signature in order to get 144 KiB of shared memory. The library helper
carries the same device check without the false claim. A kernel that can
state a contract still should, and gets this for free — gemm is unchanged.

occupancy() in examples/src/main.rs admits each kernel's plan before
querying it, one line. The column reported the omission rather than the
envelope, and that is precisely how an hour goes into the wrong hypothesis.

Docs: the budget table and both measurements in flash_forward's module
doc; a "a zero in that column is two different things" section in
examples/README.md beside the existing register/occupancy discussion; the
gemm comment corrected to say what it actually relies on; launch added to
the module table in README.md.

Before / after

                      before                          after
kernel           blocks/SM  warps/SM      kernel           blocks/SM  warps/SM
gemm               cluster         -      gemm               cluster         -
softmax                  6        24      softmax                  6        24
layernorm                2         8      layernorm                2         8
flash_forward            0         0      flash_forward            1         4

Verification

  • modal run modal_app.py::build — green. clippy --features host --all-targets on all three crates, host tests, cargo doc -D warnings, and
    real cargo oxide build --arch sm_100a codegen of both kernel crates.
  • modal run modal_app.py::examples — green on a B200. Table above; gemm
    512x256x256 exact, softmax worst relative error 1.97e-3, both unchanged.
  • modal run modal_app.py::regcount — the 124 registers / 0 spills figure for
    flash_forward above.
  • cargo fmt, cargo clippy --all-targets -- -D warnings, cargo test (74
    passed) locally.

Not verified: admit_shared_plan's TooLarge branch has no test — it needs
a plan past 232448 B on a real device, and no kernel here has one. The TMEM
attribution for the 1-block ceiling is an inference from the control table, not
a measurement, and is written up as an open question rather than a finding.

On routing around cuda-core's pub(crate) setter

To be explicit, since this is the one place the PR reaches past a dependency's
chosen API: there is no public cuda-core path to the opt-in that I missed.
I searched the whole checkout at b099f64 for cuFuncSetAttribute,
MAX_DYNAMIC_SHARED, and set_max_dynamic_shared_memory_bytes. There are
exactly three hits: the pub(crate) setter (module.rs:514), its single call
site in PreparedLaunch::__prepare (launch.rs:946), and the read-only
max_dynamic_shared_memory_bytes getter, which is public. The only public
route to writing the attribute is a #[launch_contract] kernel's generated
prepare_*, and the section above says why flash_forward cannot honestly
state that contract.

So admit_shared_plan calls sys::cuFuncSetAttribute through cuda-core's own
re-exported raw bindings, with the same device-limit check __prepare performs
in front of it. That makes this a candidate for upstreaming on the same
argument #42 makes about cp.reduce: the capability is already in the crate
and only the entry point is missing, and a kernel whose output partition no
launch contract describes still needs more than 48 KiB of shared memory.

Review follow-up

admit_shared_plan carries TooLarge { bytes, limit } so that the two causes
of a zero are two distinct values, and the first version of this PR then threw
that away: .ok()? at the only call site collapsed it into a None the loop
had already spent on "cluster". A plan the device genuinely will not admit
would have rendered identically to the gemm row — #70's own ambiguity rebuilt
one layer up. Fixed in 6da1bb2: an Occupancy enum keeps the four outcomes
apart (Blocks, a Cluster absence that is deliberate, TooLarge, and a
driver Failed that is not an answer about the kernel), and the two that have
something to say print it under the table.

Verified on a B200 by temporarily oversizing the plan to 262144 B, past the
device's 232448 B ceiling:

flash_forward        128      262144 B   too large          —
  flash_forward: shared plan of 262144 B is past this device's 232448 B opt-in limit per block

reverted after the check; modal run modal_app.py::build and
modal run modal_app.py::examples both re-run green afterwards, with the
happy-path table unchanged.

feitreim added 2 commits July 28, 2026 11:18
…e plan (#70)

Same function, same 147536 B, one admit_shared_plan call between them:
0 blocks/SM -> 1, with the device's opt-in ceiling at 232448 B. The plan was
never within 84 KiB of being too large.

The opt-in lives in cuda-core behind a pub(crate) setter, reachable only from
PreparedLaunch::__prepare and therefore only from a #[launch_contract]
kernel's prepare_*. That is how gemm launches at 72 KiB; cluster_launch never
had anything to do with it. A contract is a claim about the whole launch,
including the index space each output slice is partitioned by, and
flash_forward partitions its output in warp bands through a raw cursor -- so
kittens::launch::admit_shared_plan is the same opt-in with the same device
check in front of it, on the path that does not go through a contract.

The occupancy table calls it before the query and not only before a launch: a
plan nobody opted into and a plan too big to fit both answer 0.
admit_shared_plan carries TooLarge { bytes, limit } precisely so the two
causes of a zero are two values, and the only caller collapsed it with .ok()?
into a None that the loop already spent on "cluster". A plan the device will
not admit would have rendered identically to the GEMM row, which is #70's own
ambiguity rebuilt one layer up.

There is now an Occupancy enum with the four outcomes kept apart: Blocks, a
Cluster absence that is on purpose, TooLarge, and a Driver failure that is not
an answer about the kernel at all. The two that have something to say print a
note under the table rather than widening a cell for the case that should
never fire.

Verified the TooLarge rendering on a B200 by temporarily oversizing the plan
to 262144 B, past the device's 232448 B ceiling:

  flash_forward        128      262144 B   too large          -
    flash_forward: shared plan of 262144 B is past this device's 232448 B
    opt-in limit per block

reverted after the check.
@feitreim
feitreim merged commit 189538a into main Jul 28, 2026
3 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.

flash_forward asks for 147536 B of shared memory and the driver admits 0 blocks per SM

1 participant