Repository navigation
Split the GEMM lab notebook out of the examples, into a fourth crate - #134
Merged
Merged
Conversation
`examples/src/gemm.rs` was 8107 lines and 30 entry points, a third of which compute a deliberately wrong `C`. It is now ~1100 lines and one kernel — the one the library ships — and everything else is `experiments/`, a fourth standalone crate holding every rung, probe, ablation, sweep, the cuBLASLt denominator and the measurement record. Nothing is deleted. Every entry point that existed is still launchable and still on the correctness gate it was on, from its new home.
Splitting the crates took groupnorm_tile 168 -> 236 registers and 3 CTAs an SM to 2, in the crate that is meant to be the clean one, with nothing in the tree watching. 168 is exactly _register_ceiling(3, 128), so it had been sitting on its step by luck rather than by declaration. #[launch_bounds(128, 3)] is that declaration, in the one place ptxas reads it too. The gate reads it as an alternative to the launch_contract/CTAS_PER_SM pair, so the compiler and the gate cannot be told different numbers.
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.
examples/src/gemm.rswas 8107 lines and 30 entry points, about a third ofwhich compute a deliberately wrong
C. It is 1095 lines and one kernel now.Everything else is
experiments/, a fourth standalone crate.Nothing is deleted. All 30 GEMM entry points,
gemm_ws, every sweep andevery probe are still launchable and still on the correctness gate they were on,
from
experiments/.scripts/modal-run examplesnow runs two binaries andchecks the same set it always did.
Verification
scripts/fmt --check, four manifestsscripts/modal-run buildkittens-examplesare the two pre-existingmissing_safety_doconsoftmax.rsandflash_forward.rsscripts/modal-run regcountscripts/modal-run examplesgemm,softmax,layernormpass inexamples/;gemm,gemm_wspass inexperiments/— both check sizes, all traversal widths, both schedulers where a rung has bothscripts/modal-run bench --case gemm --m 8192 --n 8192 --k 8192The regcount diff against
main, in fullmainat81c90da, this branch rebased onbd74bee. Every table compared rowby row.
device-tests, 184 kernels: identical, every counter.experiments/, 42 kernels: identical tomain'sexamples/, everycounter — registers, spill stores, spill loads, stack, smem. The 43rd is
flash_forward, which stayed inexamples/.gemm_*rows: identical, every column.examples/, 5 kernels: three identical (flash_forward,layernorm_rows,softmax_rows) and two moved. Crate composition and notsource is what moved them —
layernorm.rsis byte-identical apart from oneREADME path in a doc comment, and the same file read 168 compiled into the
42-kernel module and 236 into the 5-kernel one.
One of the two was a defect, and it is fixed. The first version of this PR
said "neither crosses its occupancy step". That was wrong for
groupnorm_tile,and the repo's own model —
_ctas_by_registers, whichregcountchecks againstcuda_occupancy.hat all 255 register counts — is what says so:So
groupnorm_tilewent 3 CTAs/SM to 2, and 168 was not a coincidence: itis exactly the 3-CTA ceiling at 128 threads. Shared memory admits six or seven
CTAs at its 33 344 B plan (the driver reports 6 for its sibling at the same
plan), so registers are this kernel's binding term and nothing else was
going to hold the line. It had been sitting on its step without declaring one,
and the split knocked it off with nothing in the tree watching.
The fix is
#[launch_bounds(128, 3)]on the kernel —__launch_bounds__,so
.maxntid 128, 1, 1and.minnctapersm 3go on the entry andptxasistold the residency the kernel is written for rather than reaching it by luck.
Measured:
groupnorm_tilemainexamples/, beforeexamples/, afterexperiments/, afterThe recovery is not free and I am not going to round it off: the
examples/copy pays 8 bytes of spill store and 8 of spill load that
maindid not.experiments/' copy is unaffected — it was already at 168, so the attributecosts it nothing and its row stays byte-identical to
main. On a memory-boundnormalization kernel, +50% CTAs against 8 bytes on a stack frame that was
already 1536 is the right side of the trade, but it is a trade.
And it cannot be timed, which is the same fact that let the loss happen.
groupnorm_tilehas no launcher anywhere in the repo — it is the "compiles"half of
layernorm's status line — so there is nothing to measure before orafter. That is precisely why it needed a gate rather than a benchmark.
Both copies are now in
GATED_KERNELS, so this is watched from here on.The GEMM's move is the benign one: residency there is tensor-memory-bound at
512 / 256= 2 CTAs/SM, and 80 and 96 registers allow 6 and 5, so neither countreaches the binding term. And there is a stronger statement than the register
column for it — its opcode census is identical to
main's, instruction forinstruction: 4
tcgen05.mma, 2tcgen05.ld, 2cp.async.bulk.tensor, 2stmatrix, 1ld.shared.v4, 1st.global.v4, 4st.global.b32, 8cvt.rn.bf16x2, 2bar.sync, 1bar.warp.sync, 1mbarrier.arrive.regcountnow prints that comparison itself and says1 kernel(s) in both crates: identical opcode counts.What this still does not prove: nothing times the
examples/copy of theGEMM either.
benchlives inexperiments/and launches the byte-identical96-register one, which is what the throughput numbers above are. The argument
that the two cannot differ is the identical census plus a residency neither
register count reaches — an argument, not a measurement, and giving the teaching
copy a clock would mean giving the teaching crate a benchmark.
Everything that is not a pure move
examples/src/gemm.rsis a rewrite, not a subset. One kernel, so theconst-generic axes it was written against have one instantiation each and are
resolved:
Tile<BLOCK_N, HALF_N, BLOCK_K, STAGES>isTile,Staged<DRAIN, WIDE, X4>folds its staging tile intoTileand disappears,produce::<LOADS>/multiply::<MMA>/drain_staged::<WIDE, X4>lose theirparameters, and
cta_originfolds intoorigin— the CTA-scope TMA ring wasits only other caller and that rung moved. Bodies are otherwise verbatim.
ClcQueueis out of the teaching crate's shared plan. Nothing theresteals work. Its 24 bytes moved
SHARED_BYTES98 392 → 98 368, and thedeclared envelope does not move at all: both round to a
STAGE_OFFSETof98 432, so
STAGED_SHARED_BYTESis 114 816 and the#[launch_contract]literal is unchanged. The
const _: () = assert!(..)reads 98 368 there and98 392 in
experiments/, which still declares both plans.shared_plan(block_n, block_k, stages)andscratch_bytes(stages)losetheir parameters in the teaching crate — the sweep that varied them moved —
and become
SHARED_BYTESandSCRATCH_BYTES.pair_shape(block_n)keepsits parameter: it is a lookup from a tile width to an ISA shape, which is the
gap
GAPS.md§7.5 names, not scaffolding.CHECK_GROUPSis[GROUP, 1, 3, 6]there, where it was[1, 3, 6].GROUPis the width a launch actually uses andbenchwas its only caller;rather than delete a measured constant or leave it unreachable, the gate now
launches it. One extra launch per size, same kernel.
bench.rsis split, andexamples/src/bench.rsis the seam.softmax.rsand
layernorm.rsare untouched and saycrate::bench::{Shape, Timings, time}; the sweeps that call theirbenchentry points are inexperiments/. So the clock —Shape,Timings,time,WARMUP,ITERATIONS— stays inexamples/src/bench.rsandexperiments/src/bench.rsincludes it with#[path]and re-exports it. Onecopy of the source, compiled by both crates.
WARMUPandITERATIONSbecamepubfor the re-export.experiments/compilesexamples/src/softmax.rsandexamples/src/layernorm.rsthrough#[path], for the same reason — thesoftmaxandlayernormrows ofbenchwould otherwise have been deleted.No kernel source is duplicated anywhere in this PR.
experiments/gains acheckargument runninggemm::checkandgemm_ws::check, becauseexamples/'mainwas what used to call them.modal_app.py::examplesinvokes both binaries.modal_app.py::regcountis crate-aware in three places._print_censusprints a section per crate and states whether the kernel both crates emit
agrees.
_check_occupancy_stepkeys on(crate, kernel)rather than on thename alone — without that the gate would silently read whichever crate
PTX_CRATESvisited last, which is the failure mode gemm_cg2 is three registers from losing a third of its occupancy, and nothing is watching #95 exists to prevent.And
GATED_KERNELSentries are(kernel, crate, source)rather than(kernel, source), because with#[path]the two diverge: both copies ofgroupnorm_tileare compiled from the oneexamples/src/layernorm.rs. Itgains three entries —
examples/' GEMM and bothgroupnorm_tiles._launch_geometryreads#[launch_bounds(threads, ctas)]as analternative to the
#[launch_contract(block = ...)]+const CTAS_PER_SMpair, which is what let
groupnorm_tilebe gated at all. This was thesmaller and the more honest of the two options offered: the alternative was a
new hand-written
const CTAS_PER_SMinlayernorm.rs, which would be aclaim about a launch nobody makes and a second copy of a number
ptxasisalso told. Reading the attribute means the compiler and the gate cannot be
given different residencies. The GEMM keeps the
CTAS_PER_SMspelling,correctly: there the number is what the grid is sized from, a host fact
launch_boundshas no way to state.experiments/build.rsandcublaslt_abi.cmoved with thecublasfeature.
examples/has no features at all now and needs no CUDA toolkitto link.
src/pipeline.rsandsrc/epilogue.rs: sixexamples/README.md→experiments/README.md, in//!////prose. This is the one place the PRtouches
src/. They are dangling paths this move creates and nothing else inthose files changed; drop the hunk if you would rather it were a follow-up.
#[launch_bounds(128, 3)]ongroupnorm_tileinexamples/src/layernorm.rs, plus thelaunch_boundsimport. This is theone change to a file the brief said stays unchanged, and item 3 of the
verification section is why. Both crates compile that file, so both get it,
and
experiments/' copy is unaffected by it.moved body. The size check is
is_multiple_ofwhere the original wasn % BLOCK_N != 0(clippy asks for it, and-D warningsis coming for thiscrate eventually);
N's doc said "twoBLOCK_Ntiles" and 256 columns isone, so the check size is two items wide in
Mand one inNand now saysso;
ITEMS_M x ITEMS_N's doc saidM x Nis four tiles, which it was at the[256, 128]pair tile and is two at this one. All three were stale in thefile this was extracted from and are left alone in
experiments/, where themove stays pure.
GAPS.md§7 and §1.3/§4/§6 citations follow the code. Paths only, noclaim and no numbering touched. Every cited line number survived the move
unchanged, so only the directory moved — except §7.4's epilogue bullet, which
now also cites
examples/src/gemm.rs:496-520as the same loop with the twoconst parameters resolved.
Build system
scripts/fmtand.github/workflows/ci.yml(tier 1) take a fourth manifest.experiments/reaches intoexamples/srcthrough#[path], so rustfmt visitsthose files twice — idempotent, and the reason the loop stays a loop.
modal_app.py:EXPERIMENTS_DIR, three new mounts,buildcompiles threekernel crates (and
experiments/twice, once withcublas),PTX_CRATEShasthree entries, and
bench/clc_bench/ws_bench/profilepoint atkittens-experiments. Every existing entry point still works.CI.mddocuments the fourth crate, the second binary on the tier-3 correctnessgate, and why the register table carries two rows for one kernel.
Cargo.tomlexcludesexperiments,.gitignoreignores its target dir,experiments/Cargo.lockis checked in andcargo metadata --lockedpasses onall four.
examples/has no README, and that is a decisionThe 4765-line one went to
experiments/with the sweeps it describes, andnothing replaced it. The teaching crate's module docs are meant to carry it:
gemm.rs's header is the kernel's whole design, andmain.rs's is thefour-kernel table plus a section saying what moved and where. A short new README
would duplicate those and go stale against them, and restoring the big one here
would undo the point of the split — so the absence is deliberate, not an
oversight.
Not in this PR
SHIPPED_EPILOGUEis stillEpilogue::StagedWideX4andgemm_ws::SHIPPED_ENTRYis still
Entry::StagedX8; they still differ, deliberately (#119/#120/#122).Plan::newis still on the register drain, so the ablation cube,bench --case epilogue,bench --case stagedand the scheduler A/B keep their control arm andclc_benchkeeps an entry point to launch. No pruning of scaffolding beyond whatthe extraction made unreachable — that is a separate decision.