Skip to content

The backward qkv gradient panel is written packed, not written fp32 and quantized (#91) - #93

Merged
feitreim merged 1 commit into
mainfrom
qkv-join-packed
Aug 5, 2026
Merged

feitreim merged 1 commit into
mainfrom
qkv-join-packed

Conversation

@feitreim

@feitreim feitreim commented Aug 5, 2026

Copy link
Copy Markdown
Owner

Phase 3 of #88, and the last unfused traversal in the attention data path.

The pre-checks first, because one of them moves the premise

#91 says the panel is quantized twice. On current main it is quantized once.
GpuGroupedLinear::backward_into rounds dy into Bf16LinearScratch::rows
one time, and both GEMMs read that one buffer — the weight product MN-major
through row_mn_maps, the input product K-major through row_maps, two
descriptors over the same bytes. #79's "two quantizes in
backward.qkv_proj.weight_gemm" counted the x quantize
(acts.attention_normalized, [N,D]) and the dy quantize ([N,3D]) — two
different tensors, not two roundings of the panel.

Both pre-checks fall out of that:

  1. One layout serves both, and it already exists. No fork of
    LinearScratch or Bf16LinearScratch, no descriptor moves, so o_proj and
    the lm-head are untouched and there is no --kernel gemm.
  2. The rounding count does not change — one before, one now. The join now
    rounds the same fp32 expression in registers instead of storing it and
    reloading it before rounding, which is a no-op. So this is an exact
    substitution like RoPE reads its angles from a table, and the backward rotation folds into the join (#88) #89 and Q/K/V reach flash as one rotate-and-stage pass, and stay staged for the backward (#88) #90, not the widened-tolerance case The backward qkv panel is written fp32 and quantized twice #91 braced
    for, and the gate asserts bit equality against the composition.

The consequence for the expected win: it lands in
backward.qkv_proj.weight_gemm (the deleted quantize) and, much smaller, in
backward.qkv_proj.join (its writes halve). backward.qkv_proj.input_gemm
contains no quantize and should not move — and does not, in any of the six
measurements below. Half of #91's estimate was on the input GEMM; that half was
never there to win.

The change

join_group3_rope_bf16 un-rotates dQ/dK, joins them with dV, and writes the
packed words itself. Per rotated pair: three packed words out instead of six
f32s, at exactly the indices convert_f32_to_bf16_pairs would have written
them to — a group's offset into the row is a multiple of width and a pair
starts at an even column, so each couple is precisely one word.

RowGradient is how a caller says which panel it wrote: Staged when it
filled the row operand itself, Wide for the fp32 fallback whose
register-tiled GEMMs read fp32 panels. GpuGroupedLinear::packed_row_gradient
hands out the buffer under backward_into's own eligibility condition, so the
two cannot disagree. GpuLinear is not touched.

scratch.qkv stays allocated — the forward projection's tcgen05 epilogue
writes it and stage_qkv_heads_bf16 reads it fp32 — so unlike #90 there is no
VRAM change (129.4 GiB used / 49.0 GiB free either side).

Gates

  • --kernel ops — green, including the new check: join_group3_rope_bf16's
    words compared for equality (assert_eq! over Vec<u32>, not a
    tolerance) against rope_backward ×2 + join_group3 run on device and then
    rounded RNE and packed on the host. The existing zero-tolerance
    join_group3_rope-vs-composition check and the rope y / rope dx checks
    are unchanged and still green.
  • --kernel model — green, every check: Dense forward/backward + AdamW,
    Muon, Newton–Schulz, both MoE substitutions, checkpoint v5, and all four
    overfit gates. aligned_tcgen05_linears is what exercises this diff end to
    end (NA=256, DA=512 clears tcgen05_linear_eligible, so the qkv backward
    takes the Staged arm), and it matches the CPU reference under the
    unchanged bf16 tolerances — they did not need widening because the bytes
    did not change.
  • No --kernel gemm: no operand descriptor moved. No flash kernel touched, so
    no KernelBudget moves.

Measurement

Same-container A/B, §13.9 shape, both orders. These containers are the
throttled kind (absolute step ~786 ms where a standalone profile of the same
main commit reads 360.92 ms — the hazard #89 and #90 both hit). Both halves see
it, which is what the same-container rule is for; the row deltas are the claim.

Marked === baseline === / === candidate === pair, main first:

row main 2a8ce1f this branch Δ
backward.qkv_proj.weight_gemm 21.8134 17.3257 −4.4877
backward.qkv_proj.input_gemm 23.9769 23.8626 −0.1143
backward.qkv_proj.join 4.6810 4.5844 −0.0966
full step 785.8889 778.9671 −6.9218

Marked pair with the order reversed (this branch profiled first):

row this branch main 2a8ce1f Δ
backward.qkv_proj.weight_gemm 17.7129 22.0400 −4.3271
backward.qkv_proj.input_gemm 24.4685 24.4750 −0.0065
backward.qkv_proj.join 4.6209 4.6900 −0.0691
full step 787.5536 793.9102 −6.3566

Order does not matter. Each compare_profile run also emits an unmarked pair
before the marked one, and those reproduce it independently: weight_gemm
21.7407 → 17.3452 (−4.3955) and 21.9378 → 17.6348 (−4.3030). Four
same-container measurements of the same row, spread −4.30 to −4.49.

Clean standalone profiles (./run.sh model profile, separate containers,
running ~2.2× faster than the A/B ones):

row main 2a8ce1f this branch Δ
backward.qkv_proj.weight_gemm 18.0159 12.5372 −5.4787
backward.qkv_proj.input_gemm 11.1318 11.0809 −0.0509
backward.qkv_proj.join 5.2560 5.0052 −0.2508
full step 360.9199 345.3640 −15.5559

The named rows agree with the A/B in sign and shape, and on unthrottled silicon
the weight-GEMM row is worth −5.48 ms. I am not claiming the −15.56 full
step
: these are two different containers, and most of the residual is flash
and GEMM rows the diff does not go near drifting the same direction
(flash_kv 29.57 → 27.82, flash_q 21.28 → 19.81). The honest claim is the
~−5.5 ms the named rows account for, ~1.5% of a clean step.

The join row moves less than its halved write traffic suggests (−0.25 ms clean,
−0.07 to −0.10 throttled). It writes three 4-byte packed words per thread where
it used to write three adjacent f32 couples, so the per-thread store width
halved along with the byte count; the pass is now closer to read-bound on its
three [N,D] fp32 inputs. Worth revisiting only if the join becomes the
residual.

Training smoke, --kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100, same shard, two runs per arm:

main A main B this branch A this branch B
step=1 11.106920 11.106920 11.106920 11.106920
step=10 9.004018 9.004664 9.006631 9.001384
step=50 8.130120 8.079599 8.144592 8.130128
step=100 8.100127 8.016968 8.006524 8.102306
tokens/s 73192.2 71117.3 72390.9 71306.8
MFU 32.50% 31.58% 32.14% 31.66%

Step 1 is identical across all four runs — the same 11.106920 #90 recorded
— and from step 10 on, two main runs differ from each other by as much as
main differs from this branch (9.004018 vs 9.004664 within main; 9.001384 vs
9.006631 within this branch). That is the MoE scatter-atomic nondeterminism
#90 documented, and a run that shares its first step exactly and then diverges
only through the atomics is the signature of an exact substitution.

Throughput between separate containers spreads 2.9% within main alone
(73192.2 / 71117.3), so this measurement cannot resolve a 1.5% change: the two
arms' ranges overlap completely and their means differ by 0.4%. The
same-container A/B above is the instrument that can, and it is unambiguous.

Notes

  • join_group3_rope keeps its kernel and its parity check — it is half of the
    oracle the packed join is tested against (§11: the naive kernels are never
    deleted), and it is still what the Wide fallback launches.
  • SPEC §7.1's scratch.qkv row is updated and a new subsection records the
    decision, the one-layout finding, and the rounding-count argument.

This completes #88's plan: #89 gave RoPE its angle table and folded the backward
rotation into the join, #92/#90 made Q/K/V reach flash as one rotate-and-stage
pass, and this removes the last quantize between the join and the GEMMs.

Closes #91, closes #88.

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.

The backward qkv panel is written fp32 and quantized twice The attention data path traverses the same Q/K/V bytes four times

1 participant