Repository navigation
The loss tail is one reduction: the mean and every layer's auxiliary loss (#107 A6) - #109
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fusion item A6 from #107: the aux-loss / loss tail.
moe_aux_losswas a one-thread grid launched once per layer — twelve launches whose entire cost was the launch — sitting behind twelvemoe_probability_sumsreductions, a genericsumover the per-token losses and a one-elementscale. 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 itsEterms; 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 addscoefficient · Σ aux_termsand stores the scalar. The coefficient stays a host-side per-step argument, so the optim crate'sAuxLossSchedulereaches the fused kernel by exactly the path it reached the twelve launches it replaces.Falling out of that:
GpuMoeWorkspace::loss_sumis gone (the intermediate no longer exists),GpuBlockScratch::probability_sumswith it, andsum_into/scale_intohad 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:fused_classifier_forward_bf16Nblocks × 1024moe_probability_sums→moe_aux_termsEblocks × 1024moe_aux_losssumscaleloss_mean_with_aux−13 launches per step. Twelve of the fourteen removed were single-thread grids and a thirteenth was a one-element launch; only the
sumdid 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.dc6f533forward.router.aux_probability_sums→aux_terms(12 launches)forward.loss.fused_classifierforward.router.aux_loss(12 × 1 thread)forward.loss.reduction(sum)forward.loss.mean(scale, then the fused tail)full step−0.123 / −0.112 ms per step. Three things worth reading off it:
scaleat 6.4 µs, that is 86 µs of pure launch latency, which is the whole reason A6 exists.sumit absorbed, 0.0128 vs 0.0476 ms, because it reduces at 1024 lanes wherereduction_config()gave the genericsum256. Absorbing twelve auxiliary launches and ascaleon top of that is free.full stepmoved +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.rsat B=16, 12 steps, first discarded; two runs per arm, one container per arm:dc6f533(main)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_termsreductions batch into one. Two routes there, both declined:[N, E]panels are twelve separate allocations, one perGpuBlockActs. One launch across them needs a workspace-wide[L, N, E]tensor and a per-layer view threaded throughGpuBlock::forward_profiled— a signature change to the block forward that several branches in flight are also editing.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 inMOE_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:modal run modal_app.py::main --kernel ops— green, including a newcheck_loss_tail.That new check is the point of this section. The model gate compares the scalar loss at
BF16_RTOL = 3e-2while 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_tailrunsmoe_aux_termsover 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 at1e-5relative.Closes nothing; A6 of #107.