Skip to content

The SFU exp2 wins on softmax and loses on flash_forward, and the two-CTA shared step is a kilobyte under half the SM (#81) - #208

Merged
feitreim merged 1 commit into
mainfrom
flash-harness-exp2
Aug 6, 2026
Merged

feitreim merged 1 commit into
mainfrom
flash-harness-exp2

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Closes #81, refs #85.

#81 asked for the exp2 question to be settled on flash_forward rather than
asserted, and #85's prerequisite — a launcher and a CPU reference — landed in
#182. What was still missing was a clock on that kernel, and a counted
value for the shared-memory step #85 does by arithmetic. Both exist now, and
both answers are not the expected ones.

The SFU exp2 does not win on flash_forward

softmax is 2.7× faster for exp2_hw (#76), and flash exponentiates every
element of every key block, so it looked like the place the polynomial would
cost the most. Timed through the new bench --case flash, each arm checked
against the f64 reference before it was timed, and the polynomial run twice
so the difference carries its arms' own repeatability (#122):

sequence × heads CTAs exp2 exp2 again exp2_hw
1024 × 2 16 0.1789 ms 0.1741 ms 0.1820 ms
2048 × 4 64 0.3336 ms 0.3254 ms 0.3343 ms
2048 × 16 256 0.6513 ms 0.6492 ms 0.6756 ms

The last row is the one with a denominator: 256 CTAs is the first size past a
full wave, it repeats to 0.3% across two runs of the same arm, and the SFU
is 3.9% slower there. The other two rows are 16 and 64 CTAs on 148 SMs and
repeat to 2.7%, so they say only that nothing large happened.

So the kernel keeps the polynomial and the swap is recorded rather than
shipped. Exactness was checked on the SFU arm before the clock was believed:
worst relative error 1.73e-3 against the polynomial's 1.66e-3, tolerance
2⁻⁸ = 3.91e-3 — neither spelling is what the tolerance is made of.

Registers say nothing either way. Two regcount runs, one per arm:

examples regs experiments regs stack .local st/ld spills
exp2 (ships) 168 255 1568 B 702 / 395 0 / 0
exp2_hw 168 255 1568 B 702 / 395 0 / 0

Byte for byte identical — which is #81's own point from the other side: the
column the original claim was made on cannot see this difference in either
direction. (The 168/255 gap is crate composition, not the exp2 spelling: same
source file, two crates, both arms. groupnorm_tile's 168 → 236 across the same
split is the precedent, and it is recorded in docs/kernels/flash_forward.md.)

src/reg.rs's header says all of this now: which spelling to call is a
measurement each kernel takes, exp2 stays the polynomial because
ex2.approx.f32 is device-only and panics on a host build — so it is the only
one a CPU reference, host test or doctest can evaluate — and the ex2.approx
serialization claim the header used to carry is gone, with a note that settling
it needs a warp-count probe of its own rather than either of these kernels.

The shared-plan step, counted: [115712, 116736)

#85 puts it at 116736 B, half the SM's 233472. Four rungs on device-tests'
tmem residency census, 1 KiB apart, no allocator so only shared memory binds:

declared plan CTAs an SM
114688 B (half − 2 KiB) 2
115712 B (half − 1 KiB) 2
116736 B (half, #85's arithmetic) 1
117760 B (half + 1 KiB) 1

At exactly half the SM a second CTA is not admitted — the arithmetic is a
kilobyte too generous. 2 × (115712 + 1024) = 233472 exactly, which points at
the per-CTA slice the driver keeps (RESERVED_SHARED_MEMORY_PER_BLOCK); that
reading is not proven by these rungs and locating the step inside that kilobyte
would need another ladder, which would change nothing for this kernel. So
flash_forward's plan has to give up 31824 B, not 30800 — one stage off
both K/V rings is 32768 B and still clears it, with 944 B to spare.

These rungs are the one place in that file where a count is reported rather
than asserted (Measured::budget is None for them): what they measure is the
number #85 designs against, so a step that is not where the arithmetic puts it
is the finding, not a failure. The census's verdict string prints the interval.

#85's housekeeping

flash_forward.rs and docs/kernels/flash_forward.md said the allocation could
not be shrunk because a CTA is charged the SM's whole tensor memory. The advice
was right and the reason was wrong: #84 counts 512 / columns CTAs holding at
once, so 256 columns admit two, and the 1 CTA/SM is the shared plan,
which is the resource #70 ruled out on four occupancy queries that were all
pinned at 1 by the allocator. Corrected in both, with the doc's "shrinking the
rings buys nothing" replaced by what it now is: the only lever this kernel has.

examples/src/main.rs's occupancy table needed no number changed — it asks the
driver — but its cluster cell implied the question was unanswerable. It is
answerable with cuOccupancyMaxActiveClusters; the comment now says so and says
what is missing here (a grid), rather than implying no such query exists.

This PR does not shrink the ring. That is #85's follow-up, and it is what
this enables: a reference, a clock at three shapes, and a counted threshold to
aim at.

Also

flash_forward gets a bench case in experiments/, which empties that file's
SKIPPED list. The denominator it owed is the flops it issues — both MMAs run
over every key block of every query block, so 4 · sequence² · HEAD a head; the
kernel does not skip fully-masked key blocks, so a causal-optimal kernel would
do a little under half of that, and the table quotes what is on the clock. Sizes
are bounded by the host: the f64 reference is heads · sequence² · HEAD
multiply-adds.

Gates — all run by hand (GitHub Actions is out today)

gate result
scripts/fmt --check clean
cargo test (library, 4 manifests' worth of doctests) 202 pass
scripts/modal-run build pass, twice — before and after the rebase onto 817d852
scripts/modal-run regcount pass, both arms; every gated kernel inside its step
scripts/modal-run device_tests 54 of 54
scripts/modal-run examples pass, twice; flash_forward 2x256x128 rows attended, worst relative error 1.66e-3
scripts/modal-run bench --case flash three runs, the table above

Rebased onto 817d852 part-way through (#206 and #207 landed while this was
measuring); the only conflict was a section heading in
docs/kernels/softmax.md, and build and examples were re-run on the
rebased tree — the numbers above are from the pre-rebase runs of regcount,
device_tests and bench, whose subject matter neither of those PRs touches.

Pre-existing and not from this branch: cargo clippy -D warnings on
device-tests and examples is red on the pinned toolchain for four lints in
files this branch does not touch (device-tests/src/main.rs:1304/1324
duplicated #[inline(always)], tmem_rescale.rs:297, tmem_warpgroup.rs:495
empty format strings). Tier 1 only lints the library, and modal_app.py::build
runs clippy without -D warnings, so nothing gates on them today.

@feitreim
feitreim force-pushed the flash-harness-exp2 branch from 9f0184b to eb2bfb0 Compare August 6, 2026 20:54
…CTA shared step is a kilobyte under half the SM (#81)

`flash_forward` is on a clock now — a `flash` case in `experiments/`, checked
against the f64 reference at every size it times — and the two things that
needed measuring came out against the guess.

`exp2_hw` in the inner loop is 3.9% slower at 2048 queries x 16 heads, where
that shape repeats to 0.3% across two runs of the same arm, so the kernel keeps
the FMA polynomial and the swap is recorded rather than shipped. The register
column is byte-identical between the arms, which is #81's own point.

Four census rungs 1 KiB apart put the two-CTAs-an-SM shared step in
[115712, 116736): at exactly half the SM a second CTA is not admitted, so #85's
arithmetic is a kilobyte too generous and the ring has to give up 31824 B.

And the housekeeping #85 asks for: a CTA is not charged the SM's whole tensor
memory, 256 columns admit two CTAs, and the 1 CTA/SM is the shared plan.
@feitreim
feitreim force-pushed the flash-harness-exp2 branch from 594e301 to 8cd6e5a Compare August 6, 2026 21:06
@feitreim
feitreim merged commit bc02ae7 into main Aug 6, 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.

reg.rs says the measurement does not favour the SFU exp2, on a register count; timed, it is 2.7x

1 participant