Repository navigation
flash_forward asked for 144 KiB and nobody opted it in: the 0 blocks/SM was the attribute, not the tiles (#70) - #73
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 same147536 B, with one
cuFuncSetAttributecall between the two rows: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.
cuOccupancyMaxActiveBlocksPerMultiprocessorreturns 0 for a function nobodyopted 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
gemmrelies onThe issue asked explicitly whether
gemmis silently relying on something. Itis not, and it is not
cluster_launcheither:CudaFunction::set_max_dynamic_shared_memory_bytesispub(crate)incuda-core (
module.rs:514).PreparedLaunch::__prepare(launch.rs:946).__prepareis reachable only from a#[launch_contract]kernel's generatedprepare_*;generate_cuda_module_prepare_launch_methodsreturnsNonefora kernel without a contract, so no non-contract kernel ever issues the
opt-in on any code path.
gemmdeclares#[launch_contract(.., dynamic_shared = 73_792, ..)]and goesthrough
prepare_gemm_cg2. That is the whole reason it launches at 72 KiB.flash_forwardis a plain#[kernel]with no launcher, so nothing had everissued 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:At zero shared memory it is still 1. And across block widths, at 0 shared:
max_threads_per_blockflash_forwardsoftmax_rowslayernorm_rowsThe two controls scale inversely with block width, which is what a
register/warp ceiling looks like —
layernorm_rowsat 255 registers a threadgives 256 threads/SM, which is #67's 2 blocks exactly.
flash_forwardisflat: flat in shared memory, flat in warp count.
ptxasprices it at 124registers 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_forwardholds that neither control does is TMEM — it andgemmare the onlytcgen05entries in the crate, andgemmis a clusterkernel 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
Qtile[128, 128]bf16Kring[64, 128]bf16Vring[64, 128]bf16Pstaging[128, 64]bf16What 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_OPTINfirst and returnsAdmitError::TooLarge { bytes, limit }when the plan genuinely does not fit, sothe 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]onflash_forward. I tried thisfirst and backed it out. A
domain = 2contract requires the outputDisjointSliceto implement__LaunchContractDisjointSlice<f32, 2>, i.e. tocarry a 2-D index-space marker —
Index2D<S>,RowMajorTiles<..>orRuntime2DIndex.flash_forwardpartitions its output in warp bands through araw
GlobalRowscursor, which none of those describe, anddomain = 1iswrong because the kernel's grid is genuinely 2-D (
blockIdx.yis the head). Soa 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 —
gemmis unchanged.occupancy()inexamples/src/main.rsadmits each kernel's plan beforequerying 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 moduledoc; a "a zero in that column is two different things" section in
examples/README.mdbeside the existing register/occupancy discussion; thegemmcomment corrected to say what it actually relies on;launchadded tothe module table in
README.md.Before / after
Verification
modal run modal_app.py::build— green. clippy--features host --all-targetson all three crates, host tests,cargo doc -D warnings, andreal
cargo oxide build --arch sm_100acodegen of both kernel crates.modal run modal_app.py::examples— green on a B200. Table above;gemm512x256x256 exact,
softmaxworst relative error 1.97e-3, both unchanged.modal run modal_app.py::regcount— the 124 registers / 0 spills figure forflash_forwardabove.cargo fmt,cargo clippy --all-targets -- -D warnings,cargo test(74passed) locally.
Not verified:
admit_shared_plan'sTooLargebranch has no test — it needsa 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)setterTo 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
b099f64forcuFuncSetAttribute,MAX_DYNAMIC_SHARED, andset_max_dynamic_shared_memory_bytes. There areexactly three hits: the
pub(crate)setter (module.rs:514), its single callsite in
PreparedLaunch::__prepare(launch.rs:946), and the read-onlymax_dynamic_shared_memory_bytesgetter, which is public. The only publicroute to writing the attribute is a
#[launch_contract]kernel's generatedprepare_*, and the section above says whyflash_forwardcannot honestlystate that contract.
So
admit_shared_plancallssys::cuFuncSetAttributethrough cuda-core's ownre-exported raw bindings, with the same device-limit check
__prepareperformsin 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 crateand 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_plancarriesTooLarge { bytes, limit }so that the two causesof 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 aNonethe loophad already spent on
"cluster". A plan the device genuinely will not admitwould have rendered identically to the
gemmrow — #70's own ambiguity rebuiltone layer up. Fixed in 6da1bb2: an
Occupancyenum keeps the four outcomesapart (
Blocks, aClusterabsence that is deliberate,TooLarge, and adriver
Failedthat is not an answer about the kernel), and the two that havesomething 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:
reverted after the check;
modal run modal_app.py::buildandmodal run modal_app.py::examplesboth re-run green afterwards, with thehappy-path table unchanged.