Repository navigation
perf: fuse weight-gradient operand staging into single-pass quantize+transpose (#43) - #50
Merged
Merged
Conversation
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
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).
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.
Closes #43
What
The weight-gradient GEMM path staged both operands with a separate
convert_f32_to_bf16_pairsfollowed by a fulltranspose_bf16_pairspass onevery call, every block, then ran
tcgen05.f32_accumulateon the transposedpanels. That whole-panel bf16 round-trip is charged against the profiled span,
which is why
gate_up_weight_gemmran at ~18% of bf16 peak vs the forwardexpert GEMM's ~42% — the staging, not the MMA.
This PR removes both explicit
transpose_bf16_pairspasses from the expert andblock-linear weight-grad paths by folding the transpose into the quantize:
convert_f32_transpose_bf16_pairs— the input activation feeds the weightGEMM only as the transposed
lhs_toperand, so quantize + transpose it in onefp32-read pass.
convert_f32_to_bf16_pairs_and_transpose— the output gradient is neededboth non-transposed (
rows, for the input GEMM) and transposed (rhs_t, forthe 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, andGpuGroupedLinear::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
Tcgen05InstructionDescriptortranspose_a/transpose_bbits — consume the native row-major operands withnothing 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_probeon 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_inputare 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 greenBoth 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)backward.experts.gate_up_weight_gemmbackward.experts.down_weight_gemmbackward.qkv_proj.weight_gemmbackward.o_proj.weight_gemmFollow-ups
transpose_a/transpose_b) remains the pathto 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.
transpose_dlogits,transpose_input) onlyfall out of the descriptor route, not this fusion.