Repository navigation
Every AdamW write-back reads a pair before writing it, and the router's zeroing walks the dead tail (#99) - #104
Merged
Conversation
…nd the router stops dispatching a block per live bin
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.
Finishes #99. #101 fused the gradient clear and the master transpose into the AdamW write-back and left two things on the table: the surviving AdamW rows sat ~7% above what their byte growth predicted, and the router's zeroing passes still cost ~1 ms outside the optimizer front. The first has exactly the cause #101 named — a store the compiler cannot prove non-aliasing cannot be reordered past a later load, and every store in an AdamW write-back waits on a divide and a square root, so a kernel that interleaves stores with loads turns each element into its own memory round trip. The second turned out to be something else entirely, which is where two of the three negatives below come from.
What changed
Every AdamW write-back reads a whole master pair before writing any of it.
adamw_master_pairwas awhile half < 2loop: it read element2p's gradient and moments, updated them, stored all three, and only then read element2p+1— so half 1's loads waited on half 0's update. It is now a pure register function. The caller loads the pair's gradient, first moment and second moment as three[f32; 2]batches, calls it, and stores;load_pair/store_pairmake the discipline visible at each call site, andadamw_elementis still the one place the update expression exists.The MoE dead-slot pass walks the dead tail instead of scanning every bin.
moe_zero_dead_binsdispatched one block per(expert, slot)— 49152 a layer, 590K a step, per pass — and most of them existed only to load a count, find their slot live, and exit. The expert is now the grid's second dimension, so a block reads its count once and then strides only the slots in[min(count, C), C). Same rows cleared, no scan.The routing-probability reduction stores its sum instead of accumulating into a pre-zeroed buffer, and it gets a full block. One block already owned one expert, so it owned that expert's output slot outright; it was nevertheless combining its lanes with 256 same-address atomic adds into a buffer that a
filllaunch per layer had to zero first. A tree reduction and one store delete thefilland make the reported auxiliary loss independent of atomic arrival order. The block also grows to 1024 threads, which is what actually moved the row:Eblocks is all the parallelism this launch has,Nis 24576, and the token loop's trip count is a runtime value NVVM will not unroll — so the loop's depth is the launch's latency, and a full block is 24 load latencies rather than 96.Measured
--kernel model --bin profile, B200,L=12 B=12 T=2048 D=3072 H=24 HD=128 E=8 K=2 FF=4096 C=6144, 4 386 014 208 parameters.Same-container A/B, both build orders
Both A/B containers were throttled about 2.3× on the compute-bound rows (full step 756 ms against a standalone 331 ms), which is why their absolute numbers sit where they do; the memory-bound rows here barely feel it, and the untouched remainder says the container did not move between the two halves of either pair. The standalone pair's larger delta is the other direction of the same effect — its candidate container was ~7% faster overall — so the A/B figures are the ones to quote.
Per row, from the standalone pair
optimizer.experts.gate_up.adamwoptimizer.experts.down.adamwoptimizer.qkv_proj.adamwoptimizer.embedding.adamwoptimizer.lm_head.adamwoptimizer.o_proj.adamwoptimizer.lm_head.sync_w_tforward.router.aux_probability_sumsbackward.router.zero_dead_binsforward.router.zero_binsforward.router.zero_probability_sumsPer-row bandwidth
Bytes are compulsory: packed-bf16 master read and written (4 B/element), both fp32 moments read and written (16), the fp32 gradient read and cleared (8), and for a master that backs a compute copy its transpose written (2).
experts.gate_up.adamwexperts.down.adamwqkv_proj.adamwo_proj.adamwembedding.adamwlm_head.adamwThe two flat write-backs are now at 6.4–6.6 TB/s, which is the 7 TB/s achievable stream rate #99 measured the front against. The front as a whole moves 131 GB in 22.44 ms — 5.84 TB/s, 1.20× its roofline, against 4.86 TB/s and 1.44× at f15b471 and 3.73 TB/s and 1.88× at phase 0.
Negatives, preserved
Batching the tiled write-back's whole tile is worse than batching one pair at a time.
adamw_bf16_master_transposedgives each thread four master words. Reading all four words' operands — 28 more live values — before any of the four write-backs measured 18.5019 ms against 14.6865 on the gate/up master, 9.2208 against 7.2936 on down and 2.7196 against 2.1699 on qkv: the round trip it saves costs less than the occupancy it spends. Batching within the pair, six more live values, wins on the same rows. The commit is on the branch (2f1e016) and the reason is in the kernel's comment.Striding all
E · Cbins from a fixed grid is worse than the block-per-bin launch it replaced. The first cut at the dead-slot pass kept the flat bin space and gave it 1024 blocks; it measured 0.5658 / 0.5650 ms against 0.4499 / 0.4670. Each block then walked 48 bins and each iteration'sassignment_counts[bin / c]is a dependent global load, so the launch became 48 load latencies deep — the same disease, one level up. Indexing the expert from the grid instead, so a block loads one count and then touches only dead slots, is what wins.Deleting the
fillin front ofmoe_probability_sumsbought nothing on its own. Replacing 256 same-address atomics with a tree reduction moved the row 0.6934 → 0.6903. The atomics were not the cost; the rolled 96-iteration token loop over a single block per expert was, and only widening the block to 1024 threads moved it (→ 0.2345). The fold is kept because it deletes a launch and a nondeterminism, not because it was fast.Numerics
Bit-identical on the AdamW path, and gated as such rather than by tolerance. The two elements of a pair are disjoint, so hoisting their loads and sinking their stores changes which instruction issues when and nothing else:
adamw_elementis untouched, moments stay fp32, andround_master's stochastic stream is still keyed on the global element index. SPEC decision #20 is untouched — no gradient or moment changes dtype.check_adamw_masternow also asserts both moments as words against a host reference, not just the master. From zero moments the update is(1 - beta1) * gand(1 - beta2) * g * g— products only, so no contraction can separate the device's arithmetic from the host's and the comparison is exact rather than approximate. That is what says the batched loads and stores still land each moment on the element it belongs to.check_adamw_master_transposedcontinues to compare the tiled write-back against the flat one, master words and both moments, bit for bit, from non-zero moments.The dead-slot pass clears exactly the rows it cleared before —
[min(count, C), C)per expert — so nothing downstream of it changes. Theopschecks now launch it with one block per expert, deliberately fewer than the capacity, so what they cover is a block striding a dead tail rather than the one-block-per-slot case, and the poisoned-panel checks still require the pass and the scatter to between them rewrite every bin.One value moves, and it is disclosed.
moe_probability_sumssums the same 24576 terms per expert in a different order, so the reported auxiliary loss changes in its last bits. It never reaches a gradient:router_backwardderives the auxiliary term analytically from the assignment counts. The change is in the deterministic direction — the previous order was whatever 256 relaxed atomics arrived in. It is below the loss's print precision: the 100-step smoke's step-1 loss is11.106920on both trees.Gates
--kernel model: 14/14 green, including the stochastic MoE overfit assert (2.949803 → 0.021344), the Muon Newton–Schulz parity, andcheckpoint v5 resumes bit-identically.--kernel ops: green, with the dead-slot checks re-pointed at the strided launch.--kernel tensor-gpu: green, with the parity gate extended to both moments as words.f15b471in both build orders plus a standalone pair, tables above; per-row GB/s for every touched kernel./data/wiki-train-00000.tok: step-1 loss identical at11.106920, step-100 8.095675 against 8.063080 (0.40%, inside the documented ~2.9% smoke noise, and the MoE scatter atomics make some deviation unavoidable). Throughput 78120.6 against 76796.9 tokens/s, +1.7%.KernelBudgetentries exist for the tensor/model kernels, so there is no ratchet to update.What is left on #99
Remedy 4 — the four tiny per-block AdamW launches — is untouched and still costs 0.276 ms for 10 MB across 37 launches, all of it launch latency. Beyond that the front is at 1.20× its roofline and the residual is attributed: the tiled write-back runs at 5.6–6.0 TB/s where the flat one runs at 6.4–6.6, and the difference is its shared-memory staging plus the second global stream it emits. I would close #99 on this.