Repository navigation
Benchmark the examples: checked, then timed, at six sizes - #45
Merged
Merged
Conversation
`gemm_cg2` issued all three `cta_group::2` allocator instructions from rank 0 alone, with the peer reading the leader's address over distributed shared memory, and never relinquished the allocation permit at all. The intrinsics all say the same thing about who issues them -- "one full warp in each peer CTA" -- and the cost of ignoring it is invisible to a one-shot run: a CTA that exits without relinquishing its permit, or without taking its half of the pair's dealloc, leaves the SM's allocator short, and the next CTA scheduled there waits in `tcgen05.alloc` for columns that are never coming back. So the first launch was always fine and the second one in the same process hung forever. #40 is what found it, by being the first thing to launch this kernel twice; `modal_app.py::examples` cannot see it, because one launch is a strictly weaker claim than two. Both CTAs now allocate, relinquish and deallocate, and each reads the address out of its own staging word -- the collective writes one into each, so the DSMEM read is gone with the bug it was covering for.
`modal run modal_app.py::bench` (B200), separate from `examples` so the correctness path stays a few seconds. `gemm::check` grows a size parameter and a tail: `run` launches `[m, n, k]`, compares every element of `C` against the CPU reference, and only then hands the launch to a `then` callback. That order is the whole design -- the timing hook is unreachable from a launch whose output was wrong, so there is no path that reports throughput for a kernel that did not compute. `check` passes a no-op and is otherwise what it was. `bench.rs` owns the clock and the table. Timing is CUDA events either side of the launch rather than wall clock around the driver call, which at the small end of the sweep is the difference between measuring the kernel and measuring the host. Five launches are thrown away before thirty timed ones; the headline is the minimum, with the median and maximum printed beside it because a gap between them is a finding rather than noise to hide. An example states what it is bound by. GEMM is compute-bound and gets TFLOP/s; a memory-bound kernel gets GB/s, because `2*M*N*K` for a normalization kernel is a large-looking number that describes nothing. A `% of peak` column appears only where the peak behind it is sourced -- 2250 TFLOP/s dense bf16 for one B200, from NVIDIA's own HGX page and its "Dense is 1/2 sparse spec shown" footnote -- and the run prints that provenance under the table. Everything not in the table is in the skip list with the issue it waits on, so a missing row never reads as "slow". The CPU reference is now 35 dot products rather than `m*n*k` of them: `a_value` repeats every 7 rows and `b_value` every 5 columns, so at 8192^3 the naive form is minutes of host time for the same 35 numbers. Every element is still compared against its own expected value. Sizes that do not divide the 256x128x64 tiling are rejected rather than launched, since the kernel bounds-checks none of its walk.
#41 promoted softmax to running while this branch was in flight, so the skip list was wrong: it is not aspirational any more, and the first thing the harness claimed to be good at was making the next kernel cheap to add. It is one `Case` -- sizes, work, grid, entry point -- plus the same verify-then-time refactor `gemm` got, and it is the first row that exercises the other half of the metric decision: bytes moved per second, because a kernel doing one `exp2` and one divide per element it reads has no meaningful FLOP/s. No `% of peak` for it. NVIDIA's HGX B200 page states the flops per GPU and does not state the HBM bandwidth per GPU, so there is no sourced denominator and the run says so in place of a ratio. The CPU reference is a 128-entry table rather than a per-row `exp2` sweep: every row is the same 128 values in a different order -- 11 is odd, so the stride is a bijection mod 128 -- so the peak and the denominator are the same for every row at any size, and each output is still compared against its own expected value. That is what makes a half-million-row check affordable.
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.
modal run modal_app.py::bench(B200): every example that runs, at six sizeseach, every one checked against its CPU reference and only then timed. Kept
separate from
modal_app.py::examplesso the correctness path stays a fewseconds.
What it found before it found any numbers
gemmonmaindeadlocks the second time it is launched in a process. Thefirst launch is fine, which is why nothing has seen this:
exampleslauncheseach kernel exactly once, and a benchmark is the first thing here that ever
launched one twice.
gemm_cg2issued all threecta_group::2TMEM allocator instructions fromrank 0 alone — the peer read the leader's address back over distributed shared
memory — and never issued
tcgen05.relinquish_alloc_permitat all. All threeintrinsics say the same thing about who issues them, "one full warp in each
peer CTA", and the cost of ignoring it is invisible to a one-shot run: a CTA
that exits without relinquishing its permit, or without taking its half of the
pair's dealloc, leaves the SM's allocator short, and the next CTA scheduled
there waits in
tcgen05.allocfor columns that are never coming back.The first commit makes both CTAs allocate, relinquish and deallocate, and each
read the address out of its own staging word — the collective writes one into
each, so the DSMEM read goes away with the bug it was covering for. That is a
kernel change in an example rather than the host-side work this issue asked
for, and it is here because without it there is no benchmark at all: I confirmed
the hang twice on a B200 (once with the relinquish alone, which is necessary and
not sufficient) before changing the participation.
The library's own
tmem::alloc_block/dealloc_blockhave the same omission onthe
cta_group::1path — no relinquish — and it will bite the first kernel thatoutlives one wave of CTAs. Not touched here (
src/is out of scope for thisissue); reported instead.
The table
One run, this branch,
modal run modal_app.py::bench:A softmax shape is
rows x columns x planes; a GEMM shape isM x N x K.What the shape says
~20 µs — a 4× larger problem for free. That is the launch floor, not work, and
it is the whole reason the small end is in the sweep: below ~20 µs this kernel
does not have a throughput. 64 blocks is still on that floor, doing 32× the
arithmetic of the 8-block row for 25% more time.
the first GEMM row that means anything, and 376 → 655 → 1047 TFLOP/s across
the last three is the kernel converging on its own ceiling rather than the
device's.
softmax's floor is 2.5× the GEMM's — flat at ~51 µs from 2 blocks allthe way to 64 — and it tops out at 362 GB/s, which on a device whose
memory system is measured in TB/s is not bandwidth-bound at all. The kernel
runs one
[128, 128]tile per CTA with a single thread issuing the load, ablocking wait, the arithmetic, then a store with
tma_store_wait::<0>()before the CTA exits: there is nothing in flight at either end, so what the
number describes is latency, not bandwidth. Worth an issue; not this one.
within 4% at the floor. The median never sits closer to the max than to the
min, so no reported minimum is a lucky outlier.
measured 1113.8 TFLOP/s at 8192³ where this one says 1046.6 — 6% apart, which
is four times the within-run spread. Quote a number from this table with the
run it came from, not as a constant.
Sizes, and why those
GEMM.
256x128x256is the smallest legal problem there is — one cluster,two CTAs.
512x256x256is the sizeexampleschecks, kept so the two runsshare a point. From there each row roughly quadruples the cluster count,
crossing one full wave of CTAs at 2048 and running well past it at 4096 and
8192. Softmax. Same idea in rows: 2 CTAs to 8192 CTAs, the last moving half
a gigabyte. The point of either sweep is the shape of the curve, not the last
row.
The tiling constrains what is legal. A GEMM cluster owns a whole
2·BLOCK_M x BLOCK_N= 256×128 tile ofCand a pipeline stage is a wholeBLOCK_K= 64, and the kernel bounds-checks none of its walk, som % 256,n % 128andk % 64must all be zero; softmax wantsrows % 128. Bothrunfunctions reject a size that does not divide instead of launching it into
somebody else's memory. There is no tail handling in either kernel:
gemmcannot do 4000×4000 at all, only 4096×4096.
Timing methodology
CUDA events recorded on the stream either side of the launch (
cuEventRecord/cuEventElapsedTimethroughcuda_core::CudaEvent— the build script'scuEventElapsedTime_v2probe is what makes that work on CUDA 13). Wall clockaround the driver call would fold the host's launch path into a number that at
the small end is 20 µs.
Five launches are discarded before thirty timed ones — the first pays module
load and the driver's first-launch setup for a shape. The headline is the
minimum, the least noise-contaminated estimate of the achievable time, and
the median and maximum are printed beside it because a divergence between them
is itself a result.
Operands are staged once per size, so a repeat costs a launch and nothing else.
Progress goes to stderr, so a reader can tell a slow size from a stuck one
without it landing in the table — which is exactly how the deadlock above got
localised.
Which metric, and why
Boundis stated per example, not assumed by the harness. GEMM does2·M·N·Kof real arithmetic against
O((M+N)·K)bytes and TFLOP/s describes it. Softmaxdoes one
exp2and one divide per element it reads; a FLOP/s figure for itwould be a small number dressed as a large one, so it reports bytes read plus
bytes written. Adding the next kernel —
layernorm, when #3 and #13 land — isone
Case: its sizes, its work function, its grid, and its verify-then-timeentry point.
On % of peak
2250 TFLOP/s, dense bf16, one B200. NVIDIA's HGX B200 page lists 36 PFLOPS
FP16/BF16 across 8 GPUs, footnoted "Sparse. Dense is ½ sparse spec shown" —
36 / 8 / 2 = 2.25 PFLOP/s dense per GPU.
https://www.nvidia.com/en-us/data-center/hgx/
The run prints that provenance under the table. The same page states the flops
per GPU and does not state HBM bandwidth per GPU, so the softmax table has
no ratio and says so in place of one. An unsourced denominator is worse than
none.
Correctness gates the number
gemm::runandsoftmax::runlaunch, compare every element of the outputagainst the CPU reference, and only then hand the launch to a
thencallback —bench::timefor a timed run, a no-op forcheck. The timing hook isunreachable from a launch whose output was wrong, so the harness has no path
that prints throughput for a kernel that did not compute.
checkkeeps itssignature and its meaning;
main's correctness path is untouched.Both CPU references are now O(1) per element instead of O(K) or O(columns):
gemm's operands repeat every 7 rows and 5 columns, so its reference is 35 dotproducts at any size, and every softmax row is the same 128 values permuted, so
its reference is one 128-entry table. Every element is still compared against
its own expected value — this is what makes checking an 8192³ GEMM and a
half-million-row softmax affordable rather than minutes of host time each.
Also noticed, not fixed
gemm::b_value(column, depth)is((column * 3 + depth * 5) % 5) - 2, anddepth * 5 % 5is zero —Bis constant along K. The check is still exactand still catches a wrong row, column, stride or operand half, but a wrong
Kindex into
Bwould pass it. Changing the pattern changes whatexamplesasserts, so it is left to whoever owns the kernel's checks.
Verified
modal run modal_app.py::build— clippy on all three crates, host tests, realcargo oxide build --arch sm_100afor both kernel crates. Clean.modal run modal_app.py::bench— the table above.modal run modal_app.py::examples—pass gemm 512x256x256 exact,pass softmax 2x256x128 rows normalized, worst relative error 1.97e-3.modal run modal_app.py::device_tests— 18 of 18 (maingrew four casessince the 14 this issue was written against).
cargo fmt --check.Closes #40