Skip to content

perf: fuse weight-gradient operand staging into single-pass quantize+transpose (#43) - #50

Merged
feitreim merged 3 commits into
mainfrom
perf/issue-43-wgrad-native-layout
Jul 23, 2026
Merged

feitreim merged 3 commits into
mainfrom
perf/issue-43-wgrad-native-layout

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #43

What

The weight-gradient GEMM path staged both operands with a separate
convert_f32_to_bf16_pairs followed by a full transpose_bf16_pairs pass on
every call, every block, then ran tcgen05.f32_accumulate on the transposed
panels. That whole-panel bf16 round-trip is charged against the profiled span,
which is why gate_up_weight_gemm ran at ~18% of bf16 peak vs the forward
expert GEMM's ~42% — the staging, not the MMA.

This PR removes both explicit transpose_bf16_pairs passes from the expert and
block-linear weight-grad paths by folding the transpose into the quantize:

  • convert_f32_transpose_bf16_pairs — the input activation feeds the weight
    GEMM only as the transposed lhs_t operand, so quantize + transpose it in one
    fp32-read pass.
  • convert_f32_to_bf16_pairs_and_transpose — the output gradient is needed
    both non-transposed (rows, for the input GEMM) and transposed (rhs_t, for
    the weight GEMM), so emit both packed views from one fp32 read, folding the
    standalone transpose into the already-required convert.

Applied in expert_linear_backward, GpuLinear::backward_into, and
GpuGroupedLinear::backward_into. Each operand now stages with a single pass
(fp32 read → bf16 write) instead of convert + transpose.

Why the fallback, not the descriptor route

The issue's preferred full fix is v0.2.1's Tcgen05InstructionDescriptor
transpose_a/transpose_b bits — consume the native row-major operands with
nothing transposed. I assessed it and deferred it: making the 256×256 pair-UMMA
kernel read MN-major operands needs a different TMA box geometry, swizzle atom,
and smem descriptor LBO/SBO, there is no reference for the transpose bits
anywhere in cuda-oxide v0.2.1 (no example exercises them), and the only
verification tool is swizzle_probe on the Modal-only build loop. That is the
"genuine v0.2.1 limitation → land the documented fused single-pass fallback"
branch the issue calls out, so this PR lands the fused kernels and says so
explicitly (see SPEC §13.9). The lm-head transpose_dlogits/transpose_input
are already-bf16 movement with no quantize to fold, so they are left for the
descriptor route.

Gates (./run.sh model, ./run.sh tensor-gpu) — all green

✓ full GPU Dense forward/backward and AdamW (bf16 lm-head) match CPU
✓ aligned tcgen05 expert forward/backward, zero rows, accumulation, and compute sync
✓ aligned tcgen05 substituted MoE model matches CPU with drops and underfull experts
✓ aligned tcgen05 MoE overfits with scheduled aux loss (2.944996 -> 0.000006)
✓ checkpoint v4 resumes bit-identically and rejects E/K/C/L or schedule mismatches
✓ aligned tcgen05 block linears match CPU forward/backward
✓ aligned tcgen05 block linears overfit (3.105286 -> 0.000007)
✓ aligned tcgen05 block linears overfit with Muon (3.105286 -> 0.000000)
✓ tensor-gpu storage, elementwise, reduction, GEMM, and bf16 parity passed

Both fused kernels carry a bit-exact parity check against separate
convert-then-transpose in the tensor-gpu suite.

Profile (BASELINE_REF=main ./run.sh model profile, B200 same container, §13.9 shape B=12)

span main ms this branch ms Δ
backward.experts.gate_up_weight_gemm 59.78 53.62 −10.3%
backward.experts.down_weight_gemm 33.22 28.61 −13.9%
backward.qkv_proj.weight_gemm 21.81 18.46 −15.4%
backward.o_proj.weight_gemm 9.03 7.03 −22.2%
full step 750.85 733.77 −2.3%

Follow-ups

  • The descriptor-transpose route (transpose_a/transpose_b) remains the path
    to forward-parity efficiency on these spans; it needs the pair-UMMA kernel
    taught MN-major operand layout and is worth its own issue once there is a
    cheaper way to iterate on swizzle geometry than full Modal cycles.
  • The lm-head bf16 transposes (transpose_dlogits, transpose_input) only
    fall out of the descriptor route, not this fusion.

feitreim added 3 commits July 23, 2026 15:44
The weight-gradient GEMM path staged its input operand with a separate
convert_f32_to_bf16_pairs followed by a full transpose_bf16_pairs pass on
every call, every block. The input activation is consumed by the weight
GEMM only as the transposed lhs_t operand, so fold the fp32->bf16 rounding
and the transpose into a single global pass (convert_f32_transpose_bf16_pairs),
removing one bf16 write and one bf16 read of the whole [rows, in_width] panel.

Applied in expert_linear_backward, GpuLinear::backward_into, and
GpuGroupedLinear::backward_into. The output-gradient operand keeps its plain
convert (the non-transposed 'rows' buffer still feeds the input GEMM) plus
transpose.

Bit-exact parity check added against separate convert-then-transpose.
The output-gradient operand is consumed by the weight GEMM transposed (rhs_t)
and by the input GEMM non-transposed (rows). The staging previously quantized
it into rows and then ran a full transpose_bf16_pairs to build rhs_t.

Add convert_f32_to_bf16_pairs_and_transpose: a single fp32 read that writes
both the row-major packed matrix and its packed transpose, folding the
standalone transpose into the already-required convert. This removes the last
explicit transpose pass from the expert and block-linear weight-grad paths;
combined with the input-operand fusion, the weight GEMM span now stages both
operands with a single pass each (fp32 read -> bf16 write) instead of
convert + transpose x2.

Bit-exact parity check added for both packed outputs.
@feitreim
feitreim merged commit def9fbe into main Jul 23, 2026
feitreim added a commit that referenced this pull request Jul 25, 2026
The weight-gradient GEMMs materialize both operand transposes in global
memory. Removing them needs the tcgen05 instruction descriptor's
transpose_a/transpose_b bits, but only transpose_b had ever run here
(flash-attn's O = P.V), nothing set transpose_a, and the MN-major smem
descriptor geometry had no reference in cuda-oxide v0.2.1 -- which is
why #43/#50 deferred it as blind Modal iteration.

Adds a standalone single-CTA probe: TMA one A and one B tile, chain four
K=16 MMAs, drain the fp32 TMEM accumulator to global, compare against a
CPU oracle. Every part of the operand walk (smem descriptor LBO/SBO/
swizzle, TMA tile stacking, byte step per K chunk) is a launch parameter,
so one run sweeps a candidate table instead of costing one run per guess.

Five A geometries x two B geometries x four encodings. The encodings
follow the #47 lesson that operands uniform along K hide K-permutation
bugs: random, an exact (m+1)(n+1) outer product mapping the M/N axes, and
two delta-in-K walks that read out which K index each operand actually
paired with.

B200 result (M128_N64, K=64):
- control (both K-major) and transpose_b alone pass every encoding;
- transposed A as two stacked 64-wide SWIZZLE_128B subtiles with LBO =
  subtile stride (8192), SBO = 1024, 2048 bytes per K=16 chunk passes
  every encoding, alone and together with a transposed B;
- the unswizzled 256-byte-pitch reading of transposed A fails;
- M64 fails identically for K-major and MN-major A, so SPEC 7e15's M64
  dead end is a property of the shape, not of the operand layout.
feitreim added a commit that referenced this pull request Jul 25, 2026
With transpose_a/transpose_b validated, the weight-gradient GEMMs read
both operands MN-major straight out of the panels the backward pass
already has, so every transpose they used to need disappears:

- block linears and grouped linears: convert_f32_transpose_bf16_pairs
  and convert_f32_to_bf16_pairs_and_transpose (the #43/#50 fallback) are
  replaced by two plain convert_f32_to_bf16_pairs, and the rhs_t buffer
  is gone entirely -- the input GEMM's own 'rows' operand doubles as the
  weight GEMM's B;
- experts: same, over the stacked [E*C, width] panels;
- lm head: backward.lm_head.transpose_input and transpose_dlogits, two
  whole-panel bf16 transposes of [NP,D] and [NP,VP], are deleted; the
  weight GEMM maps the forward's own head_input and logits buffers.

Each staging buffer now carries a K-major map set and an MN-major one
instead of a transposed copy, so this also drops one N*max_width
scratch buffer per dense scratch and one per expert scratch.

B200: every gpu/model parity gate passes (dense fwd/bwd + AdamW, Muon,
Newton-Schulz, fp32 and tcgen05 experts, MoE substitution with drops and
underfull experts, aux-loss overfit, checkpoint v4 resume, block-linear
parity and both overfits).
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.

Expert weight-gradient GEMMs: per-call transpose staging costs more than the GEMM

1 participant