Skip to content

The optimizer front is 11% of the step at 1.9x its roofline, and two of its three passes are deletable #99

Closed

Description

@feitreim

The optimizer block has never been measured. This is the phase-0 decomposition of ./run.sh model profile at d6b99ea, standalone container, 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, full step 336.74 ms.

What the optimizer actually is

GpuDenseAdamW::update_profiled (gpu/model/src/lib.rs) walks the parameter list and launches, per tensor, one AdamW kernel; every tensor that carries a tcgen05 compute copy then gets a second launch that rebuilds the transposed operand from the master. Separately, GpuDense::zero_grad runs a fill/fill_u32 over every gradient buffer at the top of the step. Those three kernels are the entire optimizer front:

kernel source what it touches
adamw_bf16_master gpu/tensor-gpu/src/lib.rs fp32 grad (r), packed-bf16 master (rw), fp32 m (rw), fp32 v (rw)
adamw_bf16_master_packed_grad same packed-bf16 grad (r), packed-bf16 master (rw), fp32 m (rw), fp32 v (rw) — lm-head only
adamw same fp32 grad (r), fp32 param (rw), fp32 m (rw), fp32 v (rw) — norms + router (decision #22)
transpose_bf16_pairs same packed master (r), packed transpose (w)
fill / fill_u32 same gradient (w)

Dtypes, confirmed in source rather than assumed: master weights are packed bf16 (two per u32 word, GpuBf16Tensor), both moments are fp32 (GpuAdamWMoments), weight gradients arrive fp32 for every tensor except the lm-head, whose dw is packed bf16 straight out of accumulate_transposed. There is no separate bf16 re-cast pass — the AdamW write-back rounds in place via round_master. Bytes/element therefore: 24 for a bf16 master with an fp32 grad, 22 for the lm-head, 28 for an fp32 parameter, 4 for the transpose, 4 (2 for lm-head) for the zeroing.

Measured decomposition

Per-block rows are the sum over all 12 blocks, as the profiler reports them.

row elements B/el GB ms GB/s % step
optimizer.experts.gate_up.adamw 2 415.9 M 24 57.98 10.8209 5358 3.21%
optimizer.experts.down.adamw 1 208.0 M 24 28.99 5.4425 5327 1.62%
optimizer.experts.sync_compute 3 623.9 M 4 14.50 5.1576 2811 1.53%
optimizer.qkv_proj.adamw 339.7 M 24 8.15 1.6097 5065 0.48%
optimizer.embedding.adamw 154.4 M 24 3.71 0.7460 4967 0.22%
optimizer.lm_head.adamw 154.9 M 22 3.41 0.6764 5039 0.20%
optimizer.o_proj.adamw 113.2 M 24 2.72 0.6058 4486 0.18%
optimizer.qkv_proj.sync_compute 339.7 M 4 1.36 0.5723 2375 0.17%
optimizer.o_proj.sync_compute 113.2 M 4 0.45 0.2483 1824 0.07%
optimizer.lm_head.sync_w_t 154.9 M 4 0.62 0.2235 2773 0.07%
optimizer.router.adamw 0.29 M 28 0.008 0.1014 81 0.03%
optimizer.ffn_norm.adamw 0.037 M 28 0.001 0.1005 10 0.03%
optimizer.attention_norm.adamw 0.037 M 28 0.001 0.0982 11 0.03%
optimizer.final_norm.adamw 0.003 M 28 0.000 0.0085 10 0.00%
optimizer total 121.90 26.412 4615 7.84%

zero_grad is the same front by another name — it exists only because AdamW consumes a gradient it does not clear, and its tensor list is exactly 1:1 with the AdamW list:

row elements B/el GB ms GB/s % step
zero_grad.experts.gate_up 2 415.9 M 4 9.66 5.8762 1645 1.75%
zero_grad.experts.down 1 208.0 M 4 4.83 2.9750 1624 0.88%
zero_grad.qkv_proj 339.7 M 4 1.36 0.8880 1530 0.26%
zero_grad.embedding 154.4 M 4 0.62 0.3773 1637 0.11%
zero_grad.o_proj 113.2 M 4 0.45 0.3458 1310 0.10%
zero_grad.lm_head 154.9 M 2 0.31 0.1927 1608 0.06%
zero_grad.router 0.29 M 4 0.001 0.0778 15 0.02%
zero_grad.ffn_norm 0.037 M 4 0.000 0.0767 2 0.02%
zero_grad.attention_norm 0.037 M 4 0.000 0.0757 2 0.02%
zero_grad.final_norm 0.003 M 4 0.000 0.0064 2 0.00%
zero_grad total 17.24 10.892 1583 3.24%

Combined optimizer front: 139.13 GB, 37.30 ms, 11.08% of the step, 3730 GB/s average.

Roofline

Every byte is compulsory HBM traffic — nothing in this front is reused across launches, and no tensor fits L2 (126 MB vs 9.7 GB for the gate/up gradient alone). Against B200 HBM3e:

bytes @ 8.0 TB/s (spec) @ 7.0 TB/s (achievable stream) measured measured / roof
optimizer only 121.90 GB 15.24 ms 17.41 ms 26.41 ms 1.52×
+ zero_grad 139.13 GB 17.39 ms 19.88 ms 37.30 ms 1.88×

Verdict: the AdamW kernels themselves are respectable (5.0–5.4 TB/s, 63–67% of spec peak). The two supporting passes are not: the transposes run at 1.8–2.8 TB/s and the fills at 1.3–1.6 TB/s, 16–35% of peak, and together they are 17.09 ms — 46% of the front for 24% of the bytes.

Why they are slow is not a mystery. fill and the AdamW kernels are launched through LaunchConfig::for_num_elems, which is one element (or one packed pair) per thread with a 256-thread block and no grid-stride loop. Zeroing the gate/up gradient dispatches 9 437 184 blocks that each store 1 KiB; the AdamW over the same tensor dispatches 4 718 592. At a GigaThread dispatch rate of roughly one block per clock, 9.4 M blocks is ~5 ms of pure dispatch on its own — which is the whole measured 5.88 ms. The transpose kernels do use a 64×64 tile with 256 threads, but their inner walk is while local < TRANSPOSE_WORDS { …; local += TRANSPOSE_THREADS }, whose trip count NVVM cannot prove, so it stays rolled: eight serially dependent 4-byte accesses per thread and no memory-level parallelism. (Same failure mode as ferro-kittens #166/#181.)

Remedies, in leverage order

The house pattern here is deletion, not acceleration — #81's routing fusion and #89/#92's staging fusion. Both supporting passes are deletable, because the AdamW kernel already has, in registers, exactly what each of them goes back to global memory to fetch.

  1. Fold grad-zeroing into the AdamW write-back (~10.9 ms → ~3.3 ms, net ≈ −7.6 ms). AdamW already reads every gradient word. Have it store zero over the word it just consumed and delete zero_grad from the step. Byte-neutral in the abstract — the same 17.24 GB is written either way — but it moves those writes out of a 1.58 TB/s kernel into one running at 5.2 TB/s, and deletes ~100 launches and 15.7 M blocks. Preconditions hold: every buffer zero_grad clears has exactly one AdamW consumer, gradient buffers are allocated zeroed, and accumulation micro-batching (if added) still zeroes once per optimizer step.

  2. Fold the compute-copy transpose into the AdamW write-back (~6.2 ms → ~1.3 ms, net ≈ −4.9 ms). sync_compute re-reads the master that AdamW wrote microseconds earlier purely to transpose it. Reshape AdamW into the existing 64×64 tile of transpose_bf16_pairs, stage the rounded bf16 result in shared memory, and emit both the row-major master and the transpose from one pass. This deletes the 8.46 GB master re-read outright and removes 49 launches. convert_f32_to_bf16_pairs_and_transpose is the precedent — same shape, one read, two views. Shapes all satisfy the 64-multiple contract (qkv 3072×9216, o_proj 3072×3072, gate/up 24576×8192, down 32768×3072, lm-head 3072×50432).

  3. Give the remaining flat elementwise kernels a compile-time-bounded, vectorized walk (est. −2 to −4 ms). Replace the one-element-per-thread launches with a fixed grid and a for i in 0..PER_THREAD walk over 128-bit accesses, so the trip count is static and the loads are independent. This is what lifts AdamW itself off its 5.2 TB/s plateau toward the 7 TB/s stream rate, and it also covers the fill users outside the optimizer (forward.router.zero_bins 0.4449, backward.router.zero_dead_bins 0.4633, forward.router.zero_probability_sums 0.0735 — 0.98 ms more).

  4. Batch the four tiny per-block launches (est. −0.25 ms, low priority). attention_norm, ffn_norm, router and final_norm together move 10 MB and cost 0.31 ms across 37 launches — pure launch latency at 8 µs each. A single grouped launch over a descriptor array would recover most of it. Small, but it is 100% overhead.

Ceiling if 1–3 land: 37.30 ms → ~20 ms, i.e. the front reaches its 7 TB/s roofline and ~5% comes off the step.

Numerics

Nothing here changes the update expression or its precision. Moments stay fp32, accumulation order per element is unchanged (the update is a pure per-element map), and the stochastic-rounding stream is keyed on the global element index, which a 64×64 tile walk reproduces exactly — so round_master sees the same (seed, element) pairs and the write-back is bit-identical. Decision #20 and the campaign precedent are untouched: no gradient or moment is downgraded. The gate for any PR here is assert_eq on the master words, m, and v against the pre-change code on a fixed input.

Activity

  1. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Remedies 1 and 2 are measured and up as #101: 37.303 → 27.285 ms standalone (−10.02 ms), −8.548 / −8.559 ms in same-container A/B in both build orders with the untouched remainder within 0.35%. Full step 336.742 → 327.674 ms. The front is now at 1.4× its roofline rather than 1.9×.

    Both estimates in the plan held: fusing the zeroing deleted 10.89 ms of fill for ~2.9 ms added to the AdamW rows, and fusing the transpose deleted 5.98 ms of transpose_bf16_pairs for ~1.3 ms.

    Three findings from doing it that were not in the plan, worth keeping:

    1. A store the compiler cannot prove non-aliasing must go last. The first version of the packed-gradient write-back cleared its gradient word before reading the moments, and measured 2.83 ms against 0.66. Nothing tells NVVM the gradient does not alias the moments, so a store ahead of their loads orders every one of them behind it and the update loses its memory-level parallelism. Moving the clear after the loop restored it (0.6864 vs 0.6567 baseline, absorbing the 0.16 ms zero_grad.lm_head). This is a 4× effect on a kernel whose byte count only grew 9% — anyone fusing a clear into another kernel should know it.

    2. The lm-head will not take the fused transpose. Its [3072, 50432] master has a 98 KiB row stride, so a fused 64×64 tile's four operand streams each touch 64 rows spread over 6.4 MiB. Measured 3.9292 ms against 0.6764 + 0.2235 for the split pair. Every other master's row stride is 6–18 KiB and every other one won. The head keeps sync_compute; the packed-gradient transposed kernel is deleted rather than kept unused.

    3. Remedy 3's value inside the optimizer is now small, because the tiled write-back already got the statically bounded four-words-per-thread walk for 96% of the parameters. What is left of it is outside this front: fill still runs at ~1.6 TB/s in forward.router.zero_bins (0.4449), backward.router.zero_dead_bins (0.4633) and forward.router.zero_probability_sums (0.0735) — 0.98 ms. Remedy 4 (the four tiny per-block AdamW launches, 0.30 ms for 10 MB) is untouched.

    One residual inside the front worth a follow-up: the surviving AdamW rows sit ~7% above what their byte growth predicts (experts.gate_up +4.04 ms measured against +2.71 predicted for 24 → 30 B/element). The same aliasing constraint as finding 1 applies within the pair loop — half 0's moment stores are ordered ahead of half 1's gradient load — so batching all six per-pair loads ahead of all six stores should recover most of it. Not attempted here; it would be worth ~1.5–2 ms.

  2. added a commit that references this issue on Aug 5, 2026
  3. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Remedies 1 and 2 of the follow-up are measured and up as #104: −4.868 / −4.911 ms on the touched rows in same-container A/B in both build orders (untouched remainder 1.0027 / 1.0017), −5.53 ms in a standalone pair. The front is at 1.20× its roofline — 131 GB in 22.44 ms, 5.84 TB/s — against 1.44× at f15b471 and 1.88× at phase 0.

    The load-batching estimate held and then some. The plan called it worth ~1.5–2 ms; batching a whole master pair's six loads ahead of its six stores took the front from 26.971 to 22.441 ms, and the two flat write-backs to 6.4–6.6 TB/s, which is the 7 TB/s stream rate this issue measured against. Per row: gate/up 14.6865 → 12.0953, down 7.2936 → 6.1148, qkv 2.1699 → 1.8130, embedding 0.7972 → 0.6521, lm-head 0.7088 → 0.5839, o_proj 0.7914 → 0.6802.

    The router passes were not fill and were not what the plan thought. Three findings, two of them negatives:

    1. forward.router.zero_bins and backward.router.zero_dead_bins have been dead-slot passes since MoE routing fusion: interleaved SwiGLU kills split/join, gathers fold their adds (-46 ms/step) #81, not fill. Their 0.92 ms was dispatch: one block per (expert, slot) is 49152 blocks a layer, 590K a step per pass, and a balanced router leaves almost all of those slots live, so most blocks existed to load a count and exit. Indexing the expert from the grid's second dimension — one count load per block, then a stride over [min(count, C), C) only — takes them to 0.2256 / 0.2231.

    2. Striding the flat E · C bin space from a fixed 1024-block grid is worse than the launch it replaced: 0.5658 / 0.5650 against 0.4499 / 0.4670. Each block then walked 48 bins and each iteration's assignment_counts[bin / c] is a dependent global load, so the pass became 48 load latencies deep. It is the same aliasing-and-latency disease one level up, and it is why the fix has to be indexed by expert rather than by bin.

    3. Only forward.router.zero_probability_sums (0.0724) was a real fill, and folding it away — moe_probability_sums stores its expert's sum from a tree reduction rather than accumulating into a pre-zeroed slot with 256 same-address atomics — bought nothing on its own (0.6934 → 0.6903 on the reduction row). The atomics were not the cost. E blocks is all the parallelism that launch has, N is 24576, and its token loop's trip count is a runtime value NVVM will not unroll, so its depth is its latency: widening the block from 256 to 1024 threads is what moved it, to 0.2345. The fold is kept for the deleted launch and the deleted nondeterminism, not for speed.

    One more negative inside the front. Batching the whole tile — all four of a thread's master words' operands ahead of all four write-backs — costs more occupancy than the round trip it saves: 18.5019 ms against 14.6865 on gate/up, 9.2208 against 7.2936 on down, 2.7196 against 2.1699 on qkv. Six more live values per thread wins; 28 more loses. Both negatives are preserved as commits on the branch.

    Recommend closing. What is left is remedy 4 — the four tiny per-block AdamW launches, 0.276 ms for 10 MB across 37 launches, all launch latency — and an attributed residual: 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. Neither is the 11%-of-the-step problem this issue opened on.

  4. added a commit that references this issue on Aug 5, 2026
  5. feitreim commented on Aug 5, 2026

    @feitreim
    OwnerAuthor

    Ledger at close (auto-closed by #104, main fa9ad06): front 37.30 ms (1.88× roof) → 27.29 (#101, 1.44×) → 22.44 ms (#104, 1.20× roof at 5.84 TB/s). Remaining terms attributed and small: 0.276 ms of pure launch latency across 37 tiny AdamW launches, and the tiled write-back's shared-staging + second-stream cost (5.6–6.0 vs 6.4–6.6 TB/s flat). Root mechanism for the aliasing fences is cuda-oxide's DisjointSlice lacking noalias — upstream question, nothing filed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions