Skip to content

MoE binning walks in vectors, and the SwiGLU tile arm loses on transaction shape - #128

Merged
feitreim merged 3 commits into
mainfrom
ops-kittens-pass4-moe-swiglu
Aug 7, 2026
Merged

feitreim merged 3 commits into
mainfrom
ops-kittens-pass4-moe-swiglu

Conversation

@feitreim

@feitreim feitreim commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner

Two approved items in gpu/ops, on #124/#125's method. Item 1 ships and is
bit-identical; item 2's gate is loosened, its conversion still loses, and its
kernel is unchanged.
Both verdicts are measurements, and item 2's is the one
worth reading: it is the third independent time this campaign has bought bytes
in flight and been refused.

Baseline is 3392830 (this branch's merge-base, ferro 3ae07a84). B200,
B=12 T=2048 D=3072 FF=4096 E=8 K=2 C=6144 L=12.


Item 1 — how much parallelism relaxing determinism unlocks: 1.66x, and it is not worth taking

moe_bin_assign_parallel was 0.830 ms at ~26 GB/s on a grid of E = 8
blocks (1.026 ms in the profile this campaign started from; same kernel, and the
same-container pair below is what the delta is taken against), and the reason
given for the grid was its deterministic-ordering contract.

The contract was never the thing holding it back. Capacity slots are
first-come-first-served in flattened (token, rank) order, and that is routing
semantics and not numerics — the order slots are handed out in decides which
tokens the capacity drops, so relaxing it moves the loss curve. But it is also,
exactly, a prefix sum, and a prefix sum has as much parallelism as its memory
system will give it. This kernel had none of it, for a reason that has nothing
to do with ordering:

  • E blocks is 2048 threads on 148 SMs, so there are no other warps to hide
    a load behind. What a lane has outstanding is the launch's bandwidth.
  • Both walks were one scalar load at a runtime trip count — four bytes a
    lane, 8 KiB in flight for the whole launch. That is the tens of GB/s it
    measured, arithmetically.
  • The block-wide scan was a 256-lane Hillis-Steele tree: sixteen barriers, on
    a launch with nothing to overlap them with.

(#99's "this reduction's only parallelism is its token-loop depth" is about
moe_aux_terms, the neighbour — checked, and it does not transfer: that kernel
has no scan formulation to dodge with.)

(a) The deterministic route — what ships

QUAD_LANES vector loads, MOE_ASSIGN_FLIGHT = 4 of them in flight per lane
(64 bytes a thread, sixteen times fewer round trips), and a warp-shuffle scan
plus an eight-slot warp-total sum — one barrier where there were sixteen, and
8 shared words where there were 256. Lane ranges are rounded up to whole vectors
so start is QUAD_LANES-aligned and the walk needs no per-lane head; they stay
contiguous, which is all the ordering asks of them.

Bit-identical. check_moe_bin_assign gates the slots and counts against the
serial moe_bin_assign oracle at 5000 pairs — a shape whose lane ranges are 20
pairs, so one whole vector group and a four-pair tail, with a capacity that
four experts fit and the fifth overflows. check_moe_routing's six-token shape
never reached the vector walk at all.

(b) The full relaxation — measured, and not shipped

moe_bin_assign_atomic: pairs / 256 blocks, one thread a pair, one atomic
increment on its expert's counter, first-arrival order. It is the maximally
parallel formulation and it changes which tokens drop, so it breaks
bit-identical resume and needs your explicit sign-off to ship. It is in the tree
as the ceiling, launched by nothing.

The table

bin/bench, 49152 pairs (192 KiB — this launch can never be bandwidth-bound,
only latency-bound), same container:

arm ms/launch vs shipped
serial oracle, one thread an expert 4.6921 227x slower
deterministic, this branch 0.0207 —
relaxed atomics, first-arrival 0.0125 1.66x faster

Composed, BASELINE_REF=origin/main ./run.sh model profile — both arms built
and run back to back in one container:

span main this branch
forward.router.assign 0.8301 0.3571 −57.0%
forward.experts.swiglu 4.1060 4.0007 −2.6%
backward.experts.swiglu 5.3900 5.2069 −3.4%
full step 719.7033 716.8445 −0.4%

A second, earlier pair on the same branch agrees: backward.experts.swiglu
−2.4%, full step −0.39%. The full step is not a claim — the three spans
above account for 0.76 ms of the 2.86 ms it moved, and #124's lesson is that
untouched spans drift further than that on their own.

Verdict: relaxing the contract unlocks 1.66x on a launch that is now 0.02 ms.
Against the deterministic kernel's own 0.357 ms/step that is
0.14 ms/step, or 0.02% of the step, left on the table, for a routing change, a moved loss
curve, and a checkpoint that no longer resumes bit-identically. The parallelism
was never behind the contract; it was behind four bytes a lane.


Item 2 — swiglu to exp2_approx: gate loosened, conversion loses, kernel unchanged

The old blocker was real: check_swiglu_interleaved compares bit-exactly against
libdevice expf, and a RegTile's exp is kittens' Exp — exp2_approx, a
clamped degree-3 minimax. The gate is now loosened, and the answer that comes
out the other side is that it did not matter
, because the tile is the only way
to reach exp2_approx on this family and the tile costs more than the
transcendental saves.

The tolerance, derived rather than picked

rtol = 4.0e-3, atol = 2e-5, on check_swiglu_interleaved_tile. Both are
swept, not guessed — exp2_approx against f64 over the whole ±125 range its
clamp admits:

quantity max relative max absolute
sigmoid(g) 7.66e-5 1.87e-5
silu(g) 7.66e-5 1.69e-5
silu'(g) unbounded 1.88e-5
  • rtol = 2^-8 + 1e-4 = 4.0e-3. The 1e-4 is the transcendental — and
    note the sigmoid attenuates exp2_approx's own 7.5e-5 bound rather than
    amplifying it, since σ_approx ≈ σ·(1 − δ(1−σ)). The 2^-8 = 3.9e-3 is half
    an ulp of a bf16 whose mantissa can be 1.0, and it dominates by fifty
    times
    : these kernels round on the way out, so that is the floor any
    comparison against an fp32 oracle has here, tile or not.
  • atol = 2e-5 is the one that matters. silu' has a zero at g = −1.2785
    where no relative tolerance is finite; 1.9e-5 is the 7.7e-5 times
    max σ(1−σ) = ¼, and it also covers the far tail where the ±125 clamp makes
    exp2_approx disagree with expf by any factor at all on numbers of order
    1e-38. The gate's range is ±8, not the panel's ±1, so the zero and the tail
    are both live in it.

Not 3e-2. Seven and a half times tighter, and every digit is accounted for.

The access pattern, computed before the run

The scalar kernel's accesses are already the ideal shape: two 8-byte loads and
one 8-byte store a thread, a warp's 32 lanes covering one contiguous 256-byte run
with every sector used. A [16, 32] BaseLdtm tile gives a lane 16 elements of
each operand instead of 4 — 64 bytes in flight a thread rather than 16 — and
what it costs is the transaction shape: a BaseLdtm quad is 8 contiguous bf16
(16 bytes) and the next quad is a different row, so one access instruction
touches 8 rows and uses half of each 32-byte sector. The other half is the next
value pair of the same row, so the DRAM traffic is unchanged and only the L1
request count doubles
— ferro-kittens#222's pole. Four times the bytes
outstanding against twice the requests was the trade.

The requests won

bin/bench, 24576 x 4096, same container:

kernel scalar (ships) tile ratio
swiglu_forward_interleaved_packed 0.1661 ms / 3635 GB/s 0.2706 ms / 2232 GB/s 0.61x
swiglu_backward_interleaved_packed 0.2092 ms / 5775 GB/s 0.3867 ms / 3123 GB/s 0.54x

#70's stale 0.70x was the split-layout bf16 forward, and re-measured here on
ferro #180's unrolled mover walks the interleaved pair is worse, not better. The
forward's 1.63x slowdown against a predicted 2x request count is the right order
and a slight overshoot — which is what it should be if the requests are most of
the cost and not all of it.

The tile kernels and their toleranced gate stay in the tree, launched by
nothing
, on moe_bin_assign_atomic's footing: what they establish is the
result, and the next reader should not have to rediscover it.

The third measurement, and what it found

If bytes in flight were the limit, coarsening the scalar walk should have won
where the tile lost — same ideal access shape, four of them outstanding. It
loses too:

shape GB/s vs
one quad a thread, usize divisions 3550 —
[16, 32] BaseLdtm register tiles 2234 0.63x
four quads a thread, grid-strided 2889 0.81x

Three shapes, two of them buying bytes in flight fourfold, both refused. The
kernel was never waiting on memory-level parallelism — which is also, directly,
why a cheaper exp was never going to move it.

What is left is fixed per-thread cost, and the backward beside it is the
evidence: same walk, same row split, twice the bytes an element, 5775 GB/s.
Halving the index math per byte is the only difference. So the one thing that
landed is #125's own open lead, verbatim — the row split's two runtime divisions
by ff / QUAD_LANES are 32-bit now, "about a third of the instructions at no
register cost at all". Same container, that is −2.6% and −3.4% on the two
expert-SwiGLU spans, 0.29 ms/step, and both kernels keep their bit-exact gates
because the math is untouched.

Loss tracking: not run, and why

#108's protocol is for a change to the forward math. There is no change to the
forward math in this PR.
The tile arm that would have carried exp2_approx
into the model is not launched, and the shipped kernels' sigmoid is the same
libdevice expf it was — which their unchanged bit-exact gates prove more
strongly than a few hundred steps against a 0.077 noise floor could.


Registers

kernel regs spill maxntid blocks/SM threads/SM
moe_bin_assign_parallel 40 0 256 6 1536
swiglu_forward_interleaved_packed 23 0 256 8 2048
swiglu_backward_interleaved_packed 32 0 256 8 2048

The binning kernel's 40 registers are the MOE_ASSIGN_GROUP array live across
the walk, and they cost it two resident blocks — which on a grid of E = 8
blocks over 148 SMs costs nothing at all. No spills anywhere.

Gates

./run.sh ops green, including the two new ones: check_moe_bin_assign
(bit-identical slots and counts against the serial oracle; relaxed kernel checked
for the counts and the slot-set it still promises, and deliberately not for the
order it gave up) and check_swiglu_interleaved_tile (the toleranced one).
compare_train is unhealthy, so nothing here is a composed-step claim beyond the
same-container profile pair above.

…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
feitreim force-pushed the ops-kittens-pass4-moe-swiglu branch from f11c1a9 to d03422d Compare August 7, 2026 03:28
@feitreim
feitreim merged commit 3e949cb into main Aug 7, 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.

1 participant