Skip to content

The router logits GEMM was staging sixteen columns at a time, and the pin that would have fixed the rest costs 189% of a span - #125

Merged
feitreim merged 7 commits into
mainfrom
ops-kittens-pass2
Aug 7, 2026
Merged

feitreim merged 7 commits into
mainfrom
ops-kittens-pass2

Conversation

@feitreim

@feitreim feitreim commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner

Pass two of the gpu/ops rewrite campaign, on #124's method: compute achieved
GB/s and threads/SM from the shape before touching anything, convert only where
the rewrite changes bytes, geometry or load pipelining, and measure both arms in
one container. Three kernels converted, one converted and reverted with the
numbers, #[launch_bounds] on 42 of the 43 entry points — and the 43rd is
itself one of the largest measured results in here.

Every kernel in lib.rs now has a verdict.

The headline: router_logits_bf16, −44.9%

#124 left it as the standout — 4.228 ms at 431 GB/s, 6% of peak, worst in the
hot set by a factor of three — and guessed it was a blocking problem. It was,
and the blocking constant is ROUTER_GEMM_BK = 16.

At 16 a lane stages two x elements into shared memory, waits on a barrier,
does sixteen FMAs and waits again — 192 times over for dim = 3072. Eight
bytes in flight per lane, 384 barriers a block. Little's law on that is about a
terabyte a second before any of the compute or barrier time, and the kernel
measures 431.

What ships keeps the thread mapping — one lane per output element is the only
one that fills the device, because the output is N * E and E is 8, so a lane
per token leaves an SM 166 threads and anything coarser needs a cross-lane
reduction of the dim axis — and changes everything else about the tile:

  • ROUTER_LOGITS_BK = 128. Sixteen elements a lane as four QUAD_LANES
    vectors, 48 barriers a block instead of 384.
  • Both tiles staged f32, the weight transposed. x widens once per block
    on the way in rather than once per lane that reads it, and a lane's expert
    column becomes contiguous, so both operands of the inner product are one
    16-byte shared load instead of a scalar one.
  • A QUAD_LANES pad on both row strides. The four token rows a warp spans
    and the eight expert columns it spans would otherwise all land on the same
    four banks; one vector of pad rotates each successive row by four banks and
    both reads become conflict-free.
  • Neither staging load is predicated. The address is clamped and the value
    is selected, so the four of them issue back to back instead of down a chain of
    branches — The router weight gradient's loads share a basic block, and 64 partitions buy what 256 did #111's medicine.

The reference twin keeps ROUTER_GEMM_BK = 16 and its own simple blocking.

Shared memory goes 2.5 KiB → 20.6 KiB and the allocation to 52 registers, which
is 4 resident blocks an SM — and the grid is 768 blocks on 148 SMs, so 5.2
blocks' worth of work runs in two waves of four. That is where the rest of it is:
at 833 GB/s the kernel is still 10% of peak. This is −45% on a kernel that
has another 3x in it, and the next lever is the occupancy, not the tile.

fused_classifier_forward_bf16 −10.3%, ..._backward_in_place_bf16 −8.3%

Both walk a padded-vocabulary row pair += CLASSIFIER_THREADS with a runtime
trip count NVVM will not unroll, and the online (max, sum) is a serial
dependence across the whole row — so a lane has exactly one four-byte access
outstanding, for the whole 100 KiB row. 2048 threads/SM × 4 B is 8 KiB in flight
an SM, which is the 1.43 TB/s the forward measures, arithmetically.

Both now walk a QUAD_LANES vector at a time. The scalar walk stays beside it as
the tail, and as the whole of the row when the stride is not a whole number of
vectors — a row base is row * stride_words, so an odd stride puts every other
row off a 16-byte boundary, and classifier_row_quads is where that is decided
rather than in a per-row head alignment.

The backward pays for it: 26 regs / 8 blocks → 44 regs / 48 B frame / 5 blocks.
It wins anyway, which is worth recording next to #124's revert — this is a
kernel where four times the bytes in flight beat five-eighths of the threads.

The reverted one: a QUAD_LANES join_group3_rope_bf16

The arithmetic that motivated it is real and is still in the source as a note:
the index math is five runtime 64-bit divisions — rope_angle's three and
the kernel's own row split — for twelve bytes of output, and four pairs a thread
pays each of them once for four while widening every access from eight bytes to
sixteen.

What it cost is the register file. Six vectors of gradient and three of packed
output live at once: 40 registers against 32, 176 bytes of local frame, six
resident blocks an SM against eight. At 2.97 TB/s over 1.36 GB a launch the
kernel is latency-bound with the same bytes either way, so the threads hiding
that latency were worth more than the divisions saved — ferro-kittens#222's
occupancy pole, reached without a tile in sight
.

backward.qkv_proj.join vs main untouched-span drift relative
own container, run 1 +4.1% +1.0% +3.1%
own container, run 2 +3.0% +2.4% +0.6%

Reverted. The lead still open is the divisions themselves: they are usize and
every operand fits u32, which is about a third of the instructions at no
register cost at all
— the one shape of this fix that does not trade
occupancy.

#[launch_bounds] on gpu/ops (#122) — and what the pin costs

gpu/ops declared none, so every entry point let the driver's JIT derive
.maxntid from whatever allocation it chose. Measured on main: all forty-three
derive 1024, which is safe today only by accident, and #123 records what a
derived block narrower than the launch turns into (701).

Pass two's second result is that the pin is not free, and the block width
alone is not the pin.
A declared .maxntid is an input to ptxas' heuristics,
not only the register budget's divisor.

A bare #[launch_bounds(256)] hands ptxas a 255-register budget where the
derived 1024 gave it 64, and the allocator spends it:

kernel derived #[launch_bounds(256)] backward.router.weight
router_backward_weight_split_bf16 32 regs, 8 blocks/SM 93 regs, 2 blocks/SM +52%

And a resident-block target that is too tight buys a local frame.
(256, 8) caps the budget at the 32 registers the derived allocation already
used, and three kernels still acquired one — the harder unrolling a declared 256
buys has to go somewhere:

kernel frame at (256, 8) span
router_backward_weight_split_bf16 480 B backward.router.weight +189%
join_group3_rope_bf16 200 B
fused_classifier_backward_in_place_bf16 48 B

What ships is (threads, 65536 / (threads * 64)) — (256, 4), (1024, 1),
(128, 6) — the resident-block count that hands ptxas back the 64-register
budget the derived .maxntid gave it
. Under it every kernel keeps its main
allocation, with one exception.

The exception: router_backward_weight_split_bf16 declares nothing

It is the kernel whose own note (#111) says the .local depot is supposed to
be there, L1-resident, and that the registers it saves are what keep the warps
hiding the loads. Declaring the block moves that balance, and every target a
256-thread block has moves it the wrong way
— same container, against main's
derived allocation, both passes of each run:

declaration regs frame blocks/SM backward.router.weight
none (ships) 32 0 8 —
(256) 93 0 2 +52%
(256, 4) 64 192 B 4 +8.1% / +8.6%
(256, 6) 40 408 B 6 +126% / +120%
(256, 8) 32 480 B 8 +189%

Four targets, four losses, and the two that keep the occupancy are the two that
pay for it in frame. What #122 buys here — a declared block instead of a derived
one — is not worth 8% of the span, and a derived 1024 is in no danger of being
narrower than the 256 this kernel launches in. 42 of 43 pinned, and the
43rd carries the table.

The survey: every kernel in lib.rs has a verdict

Baseline 2eb1359, ./run.sh model profile on B200, B=12 T=2048 D=3072 FF=4096
E=8 K=2 L=12; full step 296.858 ms. GB/s from the shapes each span launches,
% peak against 8 TB/s. threads/SM is bin/profile's new report (below).

Converted in pass two

span ms GB/s % peak kernel verdict
forward.router.logits 4.228 431 5% router_logits_bf16 converted, −44.9%
backward.loss.fused_classifier 3.351 2219 28% fused_classifier_backward_in_place_bf16 converted, −8.3%
forward.loss.fused_classifier 1.735 1429 18% fused_classifier_forward_bf16 converted, −10.3%
backward.qkv_proj.join 5.498 2966 37% join_group3_rope_bf16 reverted — measured worse

Carried from #124

span ms GB/s % peak kernel verdict
backward.ffn_norm / backward.attention_norm 7.001 / 6.338 1802 / 1886 23% rms_norm_backward_fused_tile_bf16 converted in #124, neutral
backward.experts.swiglu 6.223 4523 57% swiglu_backward_interleaved_packed already at bandwidth
forward.experts.swiglu 5.060 3019 38% swiglu_forward_interleaved_packed numerics: the gate is bit-exact, a tile arm needs a tolerance
backward.router.weight 3.075 617 8% router_backward_weight_split_bf16 #111 measured the depot as the faster arrangement
forward.router.scatter 2.971 2537 32% moe_scatter_packed_quad indexed — no rectangle
backward.router.gather_dx 2.678 2651 33% moe_gather_dx_add_quad indexed
forward.router.gather 2.758 2753 34% moe_gather_combine_add_quad_packed indexed
backward.router.scatter_dy 2.196 3232 40% moe_scatter_dy_packed indexed
forward.attention_residual_norm 2.554 3681 46% rms_norm_forward_residual_bf16 the shared depot saves the second pass' traffic
backward.router.input 1.776 2046 26% router_backward_input 128 B frame on main and on this branch — see below
forward.attention_norm / forward.final_norm 1.853 / 0.160 1956 / 1886 24% rms_norm_forward_fast_bf16 tile arm reverted in #124 (+7–14%)

New verdicts in pass two

span ms GB/s % peak kernel verdict
forward.router.assign 1.026 21 0.3% moe_bin_assign_parallel open, and the structural one left: the grid is E = 8 blocks, 5% of the device. The deterministic-ordering contract is what pins it, not the memory.
backward.embedding 0.400 755 9% embedding_backward_scatter atomic-bound on colliding tokens, not bandwidth
backward.final_norm.input 0.318 2374 30% rms_norm_backward_x_fast_bf16 near-saturated, once a step
backward.router.weight_merge 0.240 319 4% router_backward_weight_merge 96 blocks, under one wave; 0.08% of the step
forward.router.zero_bins / backward.router.zero_dead_bins 0.251 / 0.240 — — moe_zero_dead_bins_bf16 writes only the dead capacity tail; traffic is routing-dependent
forward.router.aux_terms 0.250 378 5% moe_aux_terms E = 8 blocks again, 0.08% of the step
backward.router.softmax 0.198 167 2% router_backward 96 blocks; 0.07% of the step
forward.embedding_norm 0.182 2493 31% embedding_forward_norm_bf16 near-saturated for a gather
backward.final_norm.weight 0.182 2483 31% rms_norm_backward_weight_fast_bf16 near-saturated
forward.router.topk 0.154 153 2% router_softmax_topk 96 blocks and a k * e serial scan a token; 0.05%
forward.loss.mean 0.015 — — loss_mean_with_aux one block, by construction

Not on the step's path — pinned only

rms_norm_backward_fused_bf16 (the arm a shape that does not divide the tile
takes), swiglu_forward_interleaved, swiglu_backward_interleaved,
split_group3, join_group3_rope, moe_scatter_packed_wide,
moe_gather_combine_add_bf16, moe_gather_combine_add_quad_bf16,
moe_scatter_dy, moe_zero_dead_bins, moe_gather_dx_add, rope_forward,
attention_probabilities, attention_output, attention_backward_q,
attention_backward_k, attention_backward_v — each is either the fp32 or
unpacked arm the training shape does not take, or is launched only by the
gpu/ops gate. All carry the declaration; none carry a conversion.

The instrument: bin/profile reports all of gpu/ops

report_norm_kernels became report_dense_kernels: registers, frame, the
declared .maxntid
, blocks/SM and threads/SM for every kernel the step
launches, at the block its call site builds. It is what produced the whole
launch_bounds section above, and maxntid is the column that makes #122
legible — 1024 in that column is a block the tree never asked for.

It also surfaced a pre-existing finding pass one did not have the instrument to
see: router_backward_input carries a 128-byte local frame on main, at 62
registers and 4 blocks/SM. Unchanged here, reported not fixed.

Parity

router_logits_bf16 was among the lib.rs kernels the gate never compared —
check_moe_routing builds its logits with reference.router_logits, so the
shipped kernel's only coverage was whole-model parity.
check_router_logits_bf16 now compares it against that reference at
5e-6 / 5e-6: both arms see the same bf16-rounded values and sum them in the
same ascending dim order, so what is left to differ is the tiling. Two shapes —
whole tiles, then one ragged on every axis the kernel walks (token rows past the
last full band, a dim tail shorter than a reduction tile, fewer experts than
the tile is wide).

check_classifier_bf16 gets a third case whose row stride is not a whole
number of QUAD_LANES vectors — the arm that puts the whole row on the scalar
walk, which nothing else reaches.

check_rope is now two shapes: HD = 4 as before, and HD = 8, a head that is
a whole number of vectors — the shape the training run has, and the one the
reverted arm needed. It stays after the revert because it was covered by nothing.

Nothing was loosened. ./run.sh ops (15 checks) is green.

The composed A/B

Three same-container BASELINE_REF=main ./run.sh model profile runs, each
reporting its build-and-run pass (A) and its measured pass (C). The
three converted spans hold their sign and their magnitude in all six:

span run 1 A / C run 2 A / C run 3 A / C
forward.router.logits −44.1% / −44.9% −44.6% / −44.0% −44.2%¹
forward.loss.fused_classifier −10.3% / −10.3% −10.4% / −10.8% −8.9%¹
backward.loss.fused_classifier −8.2% / −8.3% −8.1% / −8.4% −6.2%¹
untouched-span median drift +0.69% / −0.37% +0.46% / +0.45% +2.40%¹

¹ run 3 is a plain ./run.sh model profile against the 2eb1359 baseline in a
different container, so its column carries that container's +2.4% drift; the
three spans are −46%, −11% and −8% relative to it.

At the shipping operating point (./run.sh model profile, full step
296.858 ms), the three spans go 9.314 ms → 6.830 ms, which is
−2.48 ms, −0.84% of the step.

compare_profile's own full-step number is not that, and #124 already recorded
why: its binary compiles the tcgen05 GEMMs at a 4224-byte-spill operating point
the shipping binary never reaches, so its step is 718 ms rather than 297 and a
2.5 ms saving is 0.35% of it — inside the ±0.7% the untouched spans move on
their own. Its pairs are what this branch is quoted from; its total is not.

bin/train.rs, B=12, one container, back to back: main 92,748.4 tok/s
(MFU 41.18%)
, this branch 93,230.6 tok/s (MFU 41.40%) — +0.52%, which
is what a −0.84% step predicts. That is compare_train's warmup pair rather
than its measured rounds. The rounds are the one measurement in this PR that
did not land
: the two-round run was started three times — once beside a
concurrent profile run, twice alone — and every time the Modal container stopped
producing output partway through. It stalls in /tmp/train-baseline, running
main's own binary with none of this branch in it, so what it is not is a hang
this diff introduced. The pair above is reported as what it is, and the rounds
are worth re-running when the harness is producing output again.

Nulls, with the number

  • join_group3_rope_bf16's vector arm — above. Two measurements, both
    non-negative, register-file mechanism, u32 index math is the open lead.
  • fused_classifier_* on tiles. The fused RMSNorm backward on register tiles, and the forward that measured worse #124 named this "the clearest tile shape
    left", and the API is all there — RegTile::row_max, sub_row, exp2,
    online_rescale, right_fill are exactly the online softmax over a padded
    row. It is not an API gap that stopped it: block-per-row holds 8 blocks of
    256
    here and the natural tile shape holds 8 of 128, which is The fused RMSNorm backward on register tiles, and the forward that measured worse #124's reverted
    forward exactly, on a kernel whose diagnosed problem was bytes in flight rather
    than barriers. The vector walk buys the same four times the bytes without
    paying the thread shape for it.
  • router_logits_bf16 on tiles. The output is [N, 8]; the expert width is
    half a RegTile's minimum N, the operands are mixed dtype (packed bf16 x
    against an fp32 router weight that is fp32 by design), and the only mapping
    with enough threads is one lane per output element — which is not a tile at
    all. Recorded, not attempted.

No ferro-kittens issue is filed by this pass. Neither shape that could have
gone to tiles was blocked by the library: one has the whole API it needs and lost
on occupancy, which is #222 already, and the other has no tile shape to begin
with. The one genuinely new mechanism found here — that .maxntid is an input to
ptxas' heuristics and not only a divisor, so declaring it can cost a kernel
half its occupancy — is a cuda-oxide/ptxas fact and belongs in this tree's
#[launch_bounds] notes, which is where it now is.

Part of #70's campaign; the #[launch_bounds] half closes the gpu/ops side of
#122.

@feitreim
feitreim merged commit d7d3728 into main Aug 7, 2026
feitreim added a commit that referenced this pull request Aug 7, 2026
The coarsened walk (0.81x) and the register tile (0.63x) both bought bytes in
flight and both lost, which says the kernel was never short of them. What
separates the forward from the backward beside it is fixed per-thread cost, and
#125 already named it: two 64-bit divisions by a runtime value.
feitreim added a commit that referenced this pull request Aug 7, 2026
The coarsened walk (0.81x) and the register tile (0.63x) both bought bytes in
flight and both lost, which says the kernel was never short of them. What
separates the forward from the backward beside it is fixed per-thread cost, and
#125 already named it: two 64-bit divisions by a runtime value.
feitreim added a commit that referenced this pull request Aug 7, 2026
…ction shape

**Item 1.** `moe_bin_assign_parallel`'s contract was never the thing holding it
back. Capacity slots are first-come-first-served in flattened `(token, rank)`
order — routing semantics, not numerics, since the order decides *which* tokens
the capacity drops — but that order is a prefix sum, and a prefix sum has as
much parallelism as its memory system will give it. The kernel had none: `E`
blocks is 2048 threads on 148 SMs and a scalar walk at a runtime trip count is
four bytes a lane, so the entire launch had 8 KiB in flight. It now walks
`QUAD_LANES` vectors, `MOE_ASSIGN_FLIGHT` of them at once, and scans with warp
shuffles behind one barrier where a 256-lane Hillis-Steele tree needed sixteen.
Bit-identical slots and counts, gated against the serial oracle.

`moe_bin_assign_atomic` is the full relaxation — one atomic per pair,
first-arrival order — measured as the ceiling and **not launched by anything**.

**Item 2.** The gate is loosened, the conversion still loses, and the kernel is
unchanged. `swiglu_{forward,backward}_interleaved_tile` and the toleranced
`check_swiglu_interleaved_tile` that gates them are in the tree because what
they establish is the result: reaching `exp2_approx` on this family means
reaching it through a `RegTile`, and a `RegTile` costs more here than the
transcendental saves. The one thing that did land is #125's own open lead — the
row split's two runtime divisions are 32-bit now, at no register cost.
feitreim added a commit that referenced this pull request Aug 7, 2026
…ction shape

**Item 1.** `moe_bin_assign_parallel`'s contract was never the thing holding it
back. Capacity slots are first-come-first-served in flattened `(token, rank)`
order — routing semantics, not numerics, since the order decides *which* tokens
the capacity drops — but that order is a prefix sum, and a prefix sum has as
much parallelism as its memory system will give it. The kernel had none: `E`
blocks is 2048 threads on 148 SMs and a scalar walk at a runtime trip count is
four bytes a lane, so the entire launch had 8 KiB in flight. It now walks
`QUAD_LANES` vectors, `MOE_ASSIGN_FLIGHT` of them at once, and scans with warp
shuffles behind one barrier where a 256-lane Hillis-Steele tree needed sixteen.
Bit-identical slots and counts, gated against the serial oracle.

`moe_bin_assign_atomic` is the full relaxation — one atomic per pair,
first-arrival order — measured as the ceiling and **not launched by anything**.

**Item 2.** The gate is loosened, the conversion still loses, and the kernel is
unchanged. `swiglu_{forward,backward}_interleaved_tile` and the toleranced
`check_swiglu_interleaved_tile` that gates them are in the tree because what
they establish is the result: reaching `exp2_approx` on this family means
reaching it through a `RegTile`, and a `RegTile` costs more here than the
transcendental saves. The one thing that did land is #125's own open lead — the
row split's two runtime divisions are 32-bit now, at no register cost.
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.

1 participant