Skip to content

The loss tail is one reduction: the mean and every layer's auxiliary loss (#107 A6) - #109

Merged
feitreim merged 2 commits into
mainfrom
fusion-a6-aux-loss-tail
Aug 6, 2026
Merged

feitreim merged 2 commits into
mainfrom
fusion-a6-aux-loss-tail

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Fusion item A6 from #107: the aux-loss / loss tail.

moe_aux_loss was a one-thread grid launched once per layer — twelve launches whose entire cost was the launch — sitting behind twelve moe_probability_sums reductions, a generic sum over the per-token losses and a one-element scale. Inductor writes that whole tail as a single reduction (#107 §1.1 group 3, §1.4 group 25). This does the same.

What changed

moe_probability_sums → moe_aux_terms. The same block-per-expert token reduction at the same 1024 lanes, but it now also takes the assignment counts and the layer index and stores this expert's addend to this layer's auxiliary loss — E · assignment_fraction · mean_probability — into a model-wide [L, E] buffer, instead of storing a raw probability sum into a per-layer [E] scratch. A layer's auxiliary loss is the sum of its E terms; the model's is the sum of the buffer. Keeping a term per (layer, expert) preserves #99's property — no atomics, no dependence on block arrival order — while leaving the whole model's auxiliary loss addressable by one launch.

moe_aux_loss + sum + scale → loss_mean_with_aux. One block reduces the [N] per-token losses grid-stride; lane 0 adds coefficient · Σ aux_terms and stores the scalar. The coefficient stays a host-side per-step argument, so the optim crate's AuxLossSchedule reaches the fused kernel by exactly the path it reached the twelve launches it replaces.

Falling out of that: GpuMoeWorkspace::loss_sum is gone (the intermediate no longer exists), GpuBlockScratch::probability_sums with it, and sum_into / scale_into had no other callers. The dense workspace shares the tail and carries a single zero where the MoE model carries its [L, E] terms.

Launches

MoE forward tail, L = 12:

kernel grid before after
fused_classifier_forward_bf16 N blocks × 1024 1 1
moe_probability_sums → moe_aux_terms E blocks × 1024 12 12
moe_aux_loss 1 thread 12 —
sum 1 block × 256 1 —
scale 1 element 1 —
loss_mean_with_aux 1 block × 1024 — 1
total 27 14

−13 launches per step. Twelve of the fourteen removed were single-thread grids and a thirteenth was a one-element launch; only the sum did work, and it is now the first half of the tail kernel. Against #107's step-wide count that is 1206 → 1193.

Measurements

Direct kernel cost — the measurement that means something

--baseline-ref dc6f533: both binaries built and profiled in one container on one B200, B=12, two replicates each.

label baseline dc6f533 this branch
forward.router.aux_probability_sums → aux_terms (12 launches) 0.2306 / 0.2279 ms 0.2308 / 0.2367 ms
forward.loss.fused_classifier 1.4971 / 1.4974 ms 1.4977 / 1.4979 ms
forward.router.aux_loss (12 × 1 thread) 0.0820 / 0.0796 ms —
forward.loss.reduction (sum) 0.0483 / 0.0476 ms —
forward.loss.mean (scale, then the fused tail) 0.0062 / 0.0066 ms 0.0130 / 0.0128 ms
whole tail 1.8642 / 1.8591 ms 1.7415 / 1.7474 ms
full step 753.4866 / 755.8258 ms 755.5322 / 756.2153 ms

−0.123 / −0.112 ms per step. Three things worth reading off it:

  • Twelve one-thread launches cost 80 µs — 6.7 µs each, for a kernel that does eight multiply-adds. With the one-element scale at 6.4 µs, that is 86 µs of pure launch latency, which is the whole reason A6 exists.
  • The fused tail is cheaper than the sum it absorbed, 0.0128 vs 0.0476 ms, because it reduces at 1024 lanes where reduction_config() gave the generic sum 256. Absorbing twelve auxiliary launches and a scale on top of that is free.
  • Folding the assignment fraction and the layer-indexed store into the per-layer reduction costs nothing that reads above its own noise: +0.0002 ms in one replicate, +0.0088 in the other, against 0.23 ms of reduction.

full step moved +0.27% and +0.05% up, which is the honest way of saying the step total cannot resolve this: 0.12 ms is 0.016% of it, well under its own spread.

Trainer throughput

train.rs at B=16, 12 steps, first discarded; two runs per arm, one container per arm:

arm run 1 run 2 mean
dc6f533 (main) 82,474.5 82,012.9 82,243.7
this branch 83,688.6 83,009.4 83,349.0

Read that as noise, not as +1.3%. Within-arm spread is 0.6% and 0.8%, the arms ran in different containers, and the change is worth 0.03% of a B=16 step by the direct measurement above. A launch-count cleanup of this size cannot produce 1,100 tok/s. What the table shows is that nothing regressed; the profile table is the evidence.

What this does not do

#107 priced A6 at 24 launches, which additionally assumes the twelve moe_aux_terms reductions batch into one. Two routes there, both declined:

  • Make the probability panels contiguous. The twelve [N, E] panels are twelve separate allocations, one per GpuBlockActs. One launch across them needs a workspace-wide [L, N, E] tensor and a per-layer view threaded through GpuBlock::forward_profiled — a signature change to the block forward that several branches in flight are also editing.
  • Fold the reduction into moe_bin_assign_parallel, which already runs one block per expert per layer and already computes the assignment counts the term needs. That kernel runs 256 lanes, and The optimizer front is 11% of the step at 1.9x its roofline, and two of its three passes are deletable #99 (quoted in MOE_AUX_TERMS_THREADS's doc) records that this reduction's only parallelism is the depth of its token loop — which is why it runs at 1024. Trading four times the load latency on twelve 0.019 ms reductions for twelve launches is a net loss.

The 0.23 ms those twelve launches still cost is 0.03% of the step, and at 19 µs each against the 6.7 µs a bare launch costs, the profile says most of it is work rather than latency.

Parity

Existing tolerances, nothing loosened.

modal run modal_app.py::main --kernel model — green, all fifteen checks, including every one that carries the auxiliary loss:

✓ fp32-oracle substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 MoE overfits with scheduled aux loss (2.949761 -> 0.000020)
✓ checkpoint v5 (bf16 masters) resumes bit-identically, rejects v4, and rejects E/K/C/L or schedule mismatches
✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU

modal run modal_app.py::main --kernel ops — green, including a new check_loss_tail.

That new check is the point of this section. The model gate compares the scalar loss at BF16_RTOL = 3e-2 while the auxiliary term is worth about 1.4% of it, so an auxiliary loss that vanished — or that landed in another layer's row of the new [L, E] buffer — would sail through it. check_loss_tail runs moe_aux_terms over three layers with per-layer-distinct assignment counts, then the folded reduction, and compares both the [L, E] terms and the final scalar against a host f64 computation at 1e-5 relative.

Closes nothing; A6 of #107.

@feitreim
feitreim merged commit 2da737a 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.

1 participant