Skip to content

Benchmark the examples: checked, then timed, at six sizes - #45

Merged
feitreim merged 3 commits into
mainfrom
issue-40-bench
Jul 28, 2026
Merged

feitreim merged 3 commits into
mainfrom
issue-40-bench

Conversation

@feitreim

@feitreim feitreim commented Jul 27, 2026 •

Copy link
Copy Markdown
Owner

modal run modal_app.py::bench (B200): every example that runs, at six sizes
each, every one checked against its CPU reference and only then timed. Kept
separate from modal_app.py::examples so the correctness path stays a few
seconds.

What it found before it found any numbers

gemm on main deadlocks the second time it is launched in a process. The
first launch is fine, which is why nothing has seen this: examples launches
each kernel exactly once, and a benchmark is the first thing here that ever
launched one twice.

gemm_cg2 issued all three cta_group::2 TMEM allocator instructions from
rank 0 alone — the peer read the leader's address back over distributed shared
memory — and never issued tcgen05.relinquish_alloc_permit at all. All three
intrinsics 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.

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_block have the same omission on
the cta_group::1 path — no relinquish — and it will bite the first kernel that
outlives one wave of CTAs. Not touched here (src/ is out of scope for this
issue); reported instead.

The table

One run, this branch, modal run modal_app.py::bench:

NVIDIA B200, sm_100, 148 SMs

gemm — compute-bound, so the metric is flops per second, over 30 timed launches after 5 warm-up
shape             blocks     min ms  median ms     max ms     TFLOP/s   % of peak
256x128x256            2     0.0204     0.0211     0.0225         0.8        0.0%
512x256x256            8     0.0208     0.0222     0.0232         3.2        0.1%
1024x1024x1024        64     0.0256     0.0263     0.0282        83.8        3.7%
2048x2048x2048       256     0.0457     0.0467     0.0500       375.7       16.7%
4096x4096x4096      1024     0.2100     0.2116     0.2139       654.6       29.1%
8192x8192x8192      4096     1.0505     1.0619     1.0675      1046.6       46.5%
% of peak is against 2250 TFLOP/s, dense bf16 for one B200: NVIDIA's HGX
B200 page lists 36 PFLOPS FP16/BF16 across 8 GPUs, footnoted "Sparse. Dense
is ½ sparse spec shown" — https://www.nvidia.com/en-us/data-center/hgx/

softmax — memory-bound, so the metric is bytes moved per second, over 30 timed launches after 5 warm-up
shape             blocks     min ms  median ms     max ms        GB/s
128x128x2              2     0.0508     0.0515     0.0527         2.6
512x128x2              8     0.0507     0.0510     0.0530        10.3
4096x128x2            64     0.0514     0.0521     0.0535        81.7
32768x128x2          512     0.1171     0.1200     0.1242       286.5
262144x128x2        4096     0.7749     0.7820     0.7883       346.4
524288x128x2        8192     1.4827     1.4934     1.5040       362.1
no % of peak: no sourced per-GPU HBM bandwidth figure to divide by

not run:
  layernorm       aspirational (#3, #13) — would report GB/s, memory-bound like softmax
  flash_forward   aspirational (#7, #11, #23, #31) — would report TFLOP/s

A softmax shape is rows x columns x planes; a GEMM shape is M x N x K.

What the shape says

  • The first two GEMM rows take the same time. 2 blocks and 8 blocks are both
    ~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 knee is past one wave. 256 blocks (1.7 waves of CTAs on 148 SMs) is
    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 all
    the 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, a
    blocking 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.
  • Nothing throttled. min/median/max are within 1.5% at the large end and
    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.
  • Between runs is wider than within one. An earlier run of the same commit
    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. 256x128x256 is the smallest legal problem there is — one cluster,
two CTAs. 512x256x256 is the size examples checks, kept so the two runs
share 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 of C and a pipeline stage is a whole
BLOCK_K = 64, and the kernel bounds-checks none of its walk, so m % 256,
n % 128 and k % 64 must all be zero; softmax wants rows % 128. Both run
functions reject a size that does not divide instead of launching it into
somebody else's memory. There is no tail handling in either kernel: gemm
cannot do 4000×4000 at all, only 4096×4096.

Timing methodology

CUDA events recorded on the stream either side of the launch (cuEventRecord /
cuEventElapsedTime through cuda_core::CudaEvent — the build script's
cuEventElapsedTime_v2 probe is what makes that work on CUDA 13). Wall clock
around 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

Bound is stated per example, not assumed by the harness. GEMM does 2·M·N·K
of real arithmetic against O((M+N)·K) bytes and TFLOP/s describes it. Softmax
does one exp2 and one divide per element it reads; a FLOP/s figure for it
would 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 — is
one Case: its sizes, its work function, its grid, and its verify-then-time
entry 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::run and softmax::run launch, compare every element of the output
against the CPU reference, and only then hand the launch to a then callback —
bench::time for a timed run, a no-op for check. The timing hook is
unreachable from a launch whose output was wrong, so the harness has no path
that prints throughput for a kernel that did not compute. check keeps its
signature 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 dot
products 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, and
depth * 5 % 5 is zero — B is constant along K. The check is still exact
and still catches a wrong row, column, stride or operand half, but a wrong K
index into B would pass it. Changing the pattern changes what examples
asserts, 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, real
    cargo oxide build --arch sm_100a for 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 (main grew four cases
    since the 14 this issue was written against).
  • cargo fmt --check.

Closes #40

feitreim added 3 commits July 27, 2026 19:50
`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.
@feitreim
feitreim merged commit f404406 into main Jul 28, 2026
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.

Benchmark the examples: timed runs at several sizes, reporting achieved throughput

1 participant