Skip to content

The item map walks in groups, and the 23% the aspect ratio cost is recovered (#89) - #102

Merged
feitreim merged 7 commits into
mainfrom
l2-aware-traversal
Jul 28, 2026
Merged

feitreim merged 7 commits into
mainfrom
l2-aware-traversal

Conversation

@feitreim

@feitreim feitreim commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #89.

pipeline::grouped replaces the GEMM's row-major item map with one that walks
GROUP tile-rows down a column before moving right. It is one line in
Job::work, the width is a launch parameter with a sweep behind it, and
GROUP = 1 is the row-major map — so the control in every table below is a
parameter value and not a second code path.

8192³ goes from 0.684 to 0.742 of cuBLASLt, and 16384³ from 0.685 to 0.793.

Two corrections to the issue text, carried into the work

#89's traffic arithmetic is wrong and is not repeated here. It argues from
"roughly 48× reuse", which is the aggregate over a whole launch. What L2 can
capture is the reuse inside one wave, which is 3×–15×. The 48× appears nowhere
in this PR.

Its sequencing says to wait for #86 to establish the bound. That was met by
something better.
#90 ran five shapes identical in flops, tiles, waves, grid,
C bytes, operand bytes and arithmetic intensity, differing only in M : N.
Throughput moved 1123.7 → 915.7 TFLOP/s, best and worst rows exact
transposes. A 23% swing nothing but access order can explain is a direct
measurement of the thing this PR exploits, and it is what I started from.

Predictions, stated before the run

  1. Best width 8 — minimizing a wave's operand footprint 256r + 128c subject
    to r · c = 222 gives r ≈ 10.5.
  2. Square gain 6–7%, per The item boundary is roughly a third of 8192 cubed, and the curve stops where the waves said (#86) #90's own decomposition.
  3. The aspect spread collapses; 2048x32768 gains most and 32768x2048,
    already walking a near-square wave, regresses.
  4. Zero register cost.
  5. From the L2 story: no gain where operands are small, largest gain at the
    largest K
    .

Four were right. The fifth was wrong, and it is the most useful row in the PR.

1. The width, at 8192³, both schedulers — losers kept

Min ms over 30 timed launches, every row checked against the CPU reference first.
reuse is arithmetic on the item map; it reproduces #90's published column for
group 1 at all five aspect ratios to the printed digit.

group wave distinct reuse static ms static TF/s clc ms clc TF/s
1 4 x 64 1.18 MB 9.2x 0.9068 1212.5 0.8942 1229.6
2 4 x 64 1.18 MB 9.2x 0.8949 1228.6 0.8858 1241.3
4 4 x 56 1.05 MB 10.4x 0.8611 1276.8 0.8630 1274.1
8 8 x 28 0.72 MB 15.1x 0.8363 1314.8 0.8301 1324.5
16 16 x 14 0.75 MB 14.5x 0.8465 1298.9 0.8453 1300.7
32 32 x 7 1.16 MB 9.4x 0.8966 1226.3 0.8932 1231.0

The curve turns over — 32 is back where 1 was — which is what says the
mechanism is the wave's shape rather than "more grouping is better". group 2
is the sharpest row: same 4 x 64 wave, same 9.2×, because at tiles_n = 64 two
tile-rows still take 128 items and 222 clusters spill into the next group either
way. It measures within 1.3% of row-major, which is this benchmark's own spread.
A width that does not move the reuse does not move the clock.

GROUP = 8 is set from this table, at this tile shape. #87 moves the tile,
which moves this
— hence a parameter and a sweep rather than a written-in
swizzle.

2. The mechanism, and the prediction I got backwards

M and N are 8192 throughout, so tiles, waves, grid, C bytes and the wave's
working set are identical and only the operand footprint moves. Static,
min ms / TFLOP/s:

shape operand group 1 group 8 gain
8192x8192x512 16 MiB 0.2710 / 253.6 0.2716 / 253.1 −0.2%
8192x8192x2048 64 MiB 0.3725 / 738.0 0.3710 / 740.8 +0.4%
8192x8192x8192 256 MiB 0.9068 / 1212.5 0.8363 / 1314.8 +8.4%
8192x8192x32768 1024 MiB 3.0459 / 1443.9 3.1135 / 1412.6 −2.2%

The gain is a band, not a ramp. The shallow end was predicted and arrives,
and it is the control the aspect table cannot supply: at K = 512 the traversal
is worth nothing, so the 8.4% is not some fixed cost the change happens to
remove — the same change against the same tiles, waves and grid does nothing when
the operand stream is trivial.

The deep end was predicted backwards. K = 32768 was supposed to gain most
and loses 2.2%.

The hypothesis, stated as one: the traversal only pays where operand delivery
is on the critical path, and neither end of this sweep is there.
At K = 512
the launch is the item boundary (0.271 ms for ten items, against #90's 18–30 µs
per tile) and the operand stream is 348 GB/s where layernorm reaches 5497. At
K = 32768 the kernel is at 1443.9 TFLOP/s — inside #90's derived steady state
of 1480–1550 — so it is pipeline-bound, the traffic the swizzle removes was not
being waited on, and what is left is its cost. That cost is real and this is
where it shows: a grouped walk makes consecutive items write C down a column
rather than across a row, which is worse locality on the one stream the traversal
does not help.

So the L2-residency framing gets K = 512 right for roughly the wrong reason —
it is not that 16 MiB fits L2, it is that 16 MiB is not enough traffic to wait
on — and gets K = 32768 wrong outright. I think #89 overclaims here, and I
would state the generalization more narrowly than it does
: a traversal change
is worth something in the regime where operands bind, and this kernel is in that
regime at its benchmark sizes and out of it at K far past them.

#101 is independent evidence for that reading, taken for an unrelated reason.
It measures a lone CTA at a flat ~585–640 TFLOP/s across 4096³, 8192³ and
16384³
— so more than half of what this kernel achieves at any size is one
CTA's stall covered by another's work, and overlap between CTAs is the dominant
mechanism everywhere. A row already at 1443.9 TFLOP/s has that resource close to
spent, and there is little uncovered stall left for cheaper operand delivery to
remove. Two measurements pointing the same way beats a better argument for one.

3. The aspect ratio — #89's falsifiable prediction, tested

shape tiles_n reuse 1 → 8 group 1 group 8 gain
32768x2048x8192 16 15.1x → 13.9x 0.8416 / 1306.5 0.8503 / 1293.0 −1.0%
16384x4096x8192 32 14.5x → 15.1x 0.8643 / 1272.1 0.8460 / 1299.7 +2.2%
8192x8192x8192 64 9.2x → 15.1x 0.9068 / 1212.5 0.8363 / 1314.8 +8.4%
4096x16384x8192 128 5.0x → 15.1x 0.9607 / 1144.5 0.8316 / 1322.1 +15.5%
2048x32768x8192 256 3.0x → 15.1x 1.0561 / 1041.1 0.8277 / 1328.4 +27.6%

Confirmed, including its sign on the row that was supposed to lose. Row-major
spans 1041.1 → 1306.5 TFLOP/s, a 25.5% spread; grouped spans 1293.0 → 1328.4, a
2.7% spread. Every gain is ordered by the reuse recovered, and
32768 x 2048 — the one shape already near-optimal, whose reuse the swizzle
lowers — is the one row that gets worse. A prediction that only says "faster"
is not falsifiable; this one named the row that would regress.

#89 predicted "up to 23% at the worst aspect ratio against 6–7% square". Measured:
27.6% and 8.4%. Conservative in both places.

(The group 1 numbers are all above #90's — 1306.5 against 1123.7 and so on —
because #91's paired fp32 store landed in between. The spread is the comparable
quantity: 22.7% there, 25.5% here.)

4. Before and after, both schedulers, with the denominator

shape group static ms static TF/s clc ms clc TF/s cuBLASLt ms ours/theirs
8192³ 1 0.9068 1212.5 0.8942 1229.6 0.6201 0.684
8192³ 8 0.8363 1314.8 0.8301 1324.5 0.6201 0.742
16384³ 1 6.9461 1266.3 6.6635 1320.0 4.7560 0.685
16384³ 8 5.9990 1466.3 5.9933 1467.7 4.7560 0.793

The two group 1 ratios reproduce #96's 0.689 and 0.694 within this file's
spread, which is what says the control is the kernel that was already there.
1466.3 TFLOP/s is the fastest this kernel has measured, past #90's 1369.9,
and 65% of dense bf16 peak.

16384³ gains more than 8192³ — 13.6% against 8.4% — and it is not noise. A
square M : N is not a square wave. 16384³ has a 64 x 128 tile grid, so 222
clusters walked row-major span two tile-rows and all 128 columns: a 2 x 128 wave
at 5.0× reuse — exactly the 4096 x 16384 row above, which gained 15.5%. The
two agree. 16384³ is still the least reproducible row in this repo, but a gain
measured against its own control in the same run, landing where a shape with the
same reuse independently landed, is outside the 5.5% cross-run spread that caveat
is about.

It composes with CLC rather than replacing it. The swizzle applies to the
item; both item sources hand out a permutation of 0..tiles, and grouped
being a bijection makes it correct under either by construction. Both schedulers
pick the same width, both gain, by the same amount (8.4% static, 7.7% CLC at
8192³).

And most of what CLC was winning, the swizzle was already winning. At 16384³
CLC was 4.2% ahead of the static stride row-major (6.9461 → 6.6635) and is
0.1% ahead of it grouped (5.9990 → 5.9933). At 8192³ the same collapse, 1.4%
→ 0.7%. #97 argued CLC's case was portability rather than throughput and had one
measurement for it; this is a second, from a different direction — the throughput
CLC was recovering was a traversal problem, and once the traversal is fixed there
is close to nothing left for the dynamic schedule to win. The portability case
is unaffected and is now the whole case.

Cost: none

ptxas -v -arch=sm_100a, same modal_app.py::regcount on this tree and on
c802627:

registers spills stack shared
gemm_cg2 before / after 168 / 168 0 / 0 528 / 528 73 816 / 73 816
gemm_cg2_clc before / after 168 / 168 0 / 0 528 / 528 73 816 / 73 816

The cliff at 170 was not approached and residency is the same 3 CTAs/SM. Two
integer divides per output tile against a 20–30 µs item boundary is not something
this harness can see. #100's occupancy-step gate passes — the shared plan is
byte-identical, so the kernel sits on the same side of the step it did before.

(Note the brief said 167; the current tooling reports 168 for c802627 as well,
so the delta is 0 either way.)

#101 is what makes this more than a null result. It priced one CTA an SM at
14–16%, with a 25–44% cliff behind it. This traversal spends no shared
memory, no registers and no TMEM, so it pays none of that: the 8.4% and 27.6%
above are not net of an occupancy step.
It is currently the only lever this
repo has measured that is free of the resource cliff, and both levers still on
the table have to buy their gains out of a budget it did not touch — lcsf (#15)
must hide most of the item boundary to clear one step, and #87's tile change can
afford the first step and must not take the second. A free lever and a lever with
a 14–16% entry fee should not be ranked as though equal nominal size meant equal
value.

Exactness, and where this map can be silently wrong

A wrong item map is a tile computed twice and a tile computed by nobody — the
same failure from either end, both silent on the device, both reaching the host
only as a wrong C. Held two ways:

  • src/pipeline.rs asserts grouped is a bijection over every tile grid
    up to 17 x 17 at every width up to 19. That is what covers the short last group
    a width not dividing tiles_m leaves, which is the one branch in the map.
  • modal run modal_app.py::examples runs the GEMM at widths [1, 3, 6]
    under both schedulers at both correctness sizes — not powers of two,
    deliberately
    : every M this project runs is one, so a swept width of 8 or 16
    always divides tiles_m and the short-group branch would never execute. At
    4096x4096's 16 tile-rows, 3 leaves a final group of one row and 6 leaves four;
    at 512x256's two tile-rows both saturate.

All twelve pass, and every timed row above is checked before it is timed by the
same entry point (bench's rule 1 has no exception here).

What I could not establish: the L2 hit rate

#89's acceptance asks for it either side and it is the right demand — a
throughput win with an unchanged hit rate would mean the cause was something
else. Saying so plainly rather than dropping it:

There is no L2 hit rate available on this harness. Nsight Compute is in the
image and cannot start — the injected driver set has no libnvidia-pcc.so, which
is not in the 580.95.05 package either — and modal_app.py::profile is checked
in documenting exactly this. Same wall #86 and #90 hit; nothing here moved it. I
did not spend a B200 container re-confirming a negative that is already written
down in the repo.

What stands in for it, and why I think it is not much weaker:

  • The reuse column is arithmetic on the item map, calibrated by reproducing
    The item boundary is roughly a third of 8192 cubed, and the curve stops where the waves said (#86) #90's published column exactly at all five aspect ratios.
  • The causal claim rests on two interventions rather than a counter: table 2,
    where the effect vanishes when the operand stream is not the bottleneck, and
    table 3, where the effect tracks predicted reuse across five shapes holding
    nine other things fixed. A counter attributes; an intervention that moves one
    variable establishes cause.

It remains true that a hit rate would have been worth having, and that this PR
cannot fully separate "fewer L2 misses" from "fewer DRAM reads".

Shape of the change

  • src/pipeline.rs — pub fn grouped(item, rows, columns, group), its
    module-doc section, three host tests. It lives in the scaffold because it
    composes with the item source instead of replacing it.
  • examples/src/gemm.rs — one line in Job::work; tiles_m and group as
    launch parameters; Plan { scheduler, group }; wave_reuse; the swizzle
    sweep. The redundant tiles argument is gone — tiles_m * tiles_n is the
    item count now that the map needs tiles_m anyway, so the launcher no longer
    passes a total it had already told the kernel twice over.
  • examples/src/bench.rs — bench swizzle dispatch, and three items made
    pub for it.

modal_app.py is untouched. The sweep is reachable as
scripts/modal-run bench --case swizzle, which rides the existing --case
pass-through and already turns on --features cublas — hence the cuBLASLt column
above from the same container. That was deliberate: two changes to that file were
in flight, and both (#99's watchdog, #100's occupancy gate) have since landed
without touching this branch.

What this invalidates, which reviewers should know before merging

This PR moves the kernel that #90's K-depth sweep was fitted on, so the fit
that ranks everything queued behind it no longer describes the kernel.
That fit
gives the item boundary as 20–22% of the launch (the intercept) and a steady
state of 1521 TFLOP/s (the slope), and both were measured row-major. Grouped
16384³ is 1466.3, close enough to the old slope that the fit is now
describing something that does not exist.

So "there is 20–22% of item boundary left to win" is what was true pre-#102 and
is not currently a supported claim.
gemm-depth needs re-running on this tree
before #15 or #87 is ranked off it. I have not done that here — it is a separate
B200 sweep and the natural first act of whoever picks up #87 — but I have said so
in §7 rather than leaving the stale number standing unqualified.

A coincidence in my own data points the same way, and I am not claiming it.
K = 32768 regressed under grouping at 1443.9 TFLOP/s; grouped 16384³ is
1466.3. Two shapes with almost nothing in common landing within 2% is
consistent with a ceiling that is not operand traffic — which would matter far
more than this PR, since it would mean neither a bigger tile nor a store stage
touches what actually binds. Two points measured once each is not a ceiling.
What would test it is the re-fit above plus a third shape reaching the same
number by a third route; a gemm-depth steady state re-fitting near 1470 rather
than 1521 would be the same suspicion from a fourth direction.

Rebase note

Rebased c802627 → 99e5698. #100 and #101 changed .github/, CI.md,
README.md, device-tests/, examples/README.md, modal_app.py and
scripts/ — and nothing under src/ or examples/src/. So every kernel and
harness byte behind the numbers above is unchanged by the rebase and no B200 was
re-booked for it. The one conflict was examples/README.md, where #101's section
and this one both attach after the CLC section; resolved by placing them in
order rather than by either winning, and they now cross-reference each other in
both directions.

::build re-ran green on the rebased tree. ::regcount I started by hand and
then killed — #100 made it a tier-2 CI job, so it runs on this PR
automatically and a local cold sm_100a build was duplicating it. The number it
is expected to report is unchanged from the table above, since the shared plan
and the register count are both untouched; CI is the authority on it.

One note against the brief

gemm::compare (the clc table) now runs at GROUP = 8, so its absolute
milliseconds are no longer comparable to #97's. The difference between its two
columns still is, which is what that table is for, and its docstring now says so.

@feitreim
feitreim force-pushed the l2-aware-traversal branch from 293f25c to 73973a0 Compare July 28, 2026 22:19
@feitreim
feitreim merged commit 7ef5685 into main Jul 28, 2026
4 checks passed
feitreim added a commit that referenced this pull request Jul 28, 2026
…oked stronger (#87) (#104)

* The pair tile was the lever, and the mechanism is not the one that looked stronger (#87)

`gemm_cg2`'s pair tile is `[256, 256]` at three stages, not `[256, 128]`:
**+11.6% at 8192³ and +21.6% at 16384³**, which is 1457.1 and **1622.5
TFLOP/s** — the fastest this kernel has been measured at — and **0.826 and
0.886 of cuBLASLt** against 0.740 and 0.728.

The pair tile and the pipeline depth are const parameters of `Tile` now, and
`bench --case tile` is the sweep, with all four losing rungs kept.

## The re-fit first, because #102 invalidated the fit that ranked this

`gemm-depth` re-run on `7ef5685`, point selections all stated:

| points | fixed ms | per tile | steady TFLOP/s | share of 8192³ |
| --- | ---: | ---: | ---: | ---: |
| K = 8192, 32768 | 0.0865 | 8.6 µs | 1480 | 10.4% |
| K = 2048, 8192, 32768 | 0.1516 | 15.2 µs | 1518 | 18.3% |
| all four | 0.1835 | 18.3 µs | 1538 | 22.1% |

The steady state did not move (1480–1538 against #91's 1515–1527), the ceiling
suspicion got *stronger* — four numbers by three routes in 1437–1480, with the
deepest measured row 2.9% under the asymptote where it used to be 10.6% under —
and the fit got **worse** as an instrument for the item boundary, because #102
put a K-dependent term into the intercept and the answer now moves 10.4% →
22.1% on point selection.

## And the ceiling was a property of the tile

`[256, 256]` measures **1718.5 TFLOP/s** at `K = 32768`, straight through that
band. Three shapes agreeing meant one shared term bound them; every one of the
three had the same tile.

## The sweep, and the control that separates the mechanisms

Min ms over 30, static, `GROUP = 8`, every row checked against the CPU
reference before timing. `vs #102` is against `[256, 128] s3`.

| rung | shared B | CTA/SM | 8192³ TFLOP/s | vs #102 | 16384³ TFLOP/s | vs #102 |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| [256,128] s2 | 49224 | 4 | 844.6 | −35.3% | 934.2 | −30.0% |
| [256,128] s3 | 73816 | 3 | 1305.9 | — | 1333.8 | — |
| [256,128] s4 | 98408 | 2 | 1206.0 | −7.7% | 1361.5 | +2.1% |
| [256,256] s2 | 65608 | 2 | 1285.2 | −1.6% | 1503.8 | +12.7% |
| **[256,256] s3** | 98392 | 2 | **1457.1** | **+11.6%** | **1622.5** | **+21.6%** |

`[256, 128] @ STAGES = 4` is the row #87's table does not have and the reason
the rest is readable: same shared plan and same residency as the winner, at
unchanged tile. It prices the occupancy step alone at −7.7% / +2.1%, leaving
the tile worth **+20.8% / +19.2%** net.

Two things fall out that are not #87's argument. A **fourth pipeline stage
nearly pays for the CTA it costs** — #98 priced that step at 13.6–16.1% on dead
bytes, and with a live stage it is 7.7% and −2.1%. And the reverse trade is a
disaster: `[256, 128] @ STAGES = 2` steps residency *up* to four CTAs an SM,
with the best wave efficiency and the best reuse in the table, and loses 35%.

## Which mechanism: intensity, and boundary count has the wrong sign

The two scale differently with `K`, so `gemm-depth` was re-run on the new tile:

| points | steady TFLOP/s | fixed ms | per tile |
| --- | ---: | ---: | ---: |
| K = 8192, 32768 | 1480 → **1826** | 0.0865 → 0.1504 | 8.6 → 21.5 µs |
| all four | 1538 → **1862** | 0.1835 → 0.1903 | 18.3 → 27.2 µs |

**The slope moved 21–23% and the intercept rose 74%.** Arithmetic intensity is
the whole win, agreeing with the control's +19–21% from different arithmetic.
Boundary count is not a mechanism: halving the tiles does not halve the
boundaries on the critical path, because the occupancy step halves the clusters
too — items per cluster fall 10 → 7 while a boundary draining twice the columns
costs more than twice as much.

This was **pre-registered as the opposite** — that boundary count was the
stronger mechanism and intensity would be worth nothing against a ceiling — and
it was wrong on both halves, in opposite directions.

## Counted, not predicted

`tmem residency census` at all four envelopes, `shared per SM` queried:

| rung | columns | shared B | budget | resident | holding | wait µs |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| [256,128] s2 | 128 | 49224 | 4 | 4 | 4 | 0.9 |
| [256,128] s4 | 128 | 98408 | 2 | 2 | 2 | 0.9 |
| **[256,256] s2** | 256 | 65608 | 2 | **3** | **2** | **100.0** |
| [256,256] s3 | 256 | 98392 | 2 | 2 | 2 | 0.9 |

Predicted and counted agree at all four. The third row is the **first rung in
this repo where the `512 / columns` half of the residency formula binds**, and
the first where `resident` and `holding` differ: a CTA admitted and parked
inside a blocking `tcgen05.alloc`, with the wait column as direct evidence.

## Cost

**168 registers, 0 spill, 528 B frame on every entry point** — unchanged. Not
automatic: a `RegTile<32, 256>` band is 256 fp32 a thread, so the epilogue
drains in two 128-column bands (`DRAIN_N`), which the ladder in the same run
says was necessary. The step also bought **87 registers of headroom**, since
the 2-CTA ceiling is 255 rather than 168.

What it did cost is **generality and the small end**: `n % 256 == 0` where 128
used to do, and 1024³ regresses 18% (98.0 → 79.9 TFLOP/s). Above 2048³ every
row improves. Both are in §7 rather than left to be found.

* rustfmt the examples crate against its own manifest

`cargo fmt` at the root formats the library only — `examples/` and
`device-tests/` are separate manifests, and tier 1 checks all three.
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.

The item to tile map is row-major, and the GEMM already lives in L2

1 participant