Repository navigation
The backward qkv gradient panel is written packed, not written fp32 and quantized (#91) - #93
Merged
Merged
Conversation
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.
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_intoroundsdyintoBf16LinearScratch::rowsone time, and both GEMMs read that one buffer — the weight product MN-major
through
row_mn_maps, the input product K-major throughrow_maps, twodescriptors over the same bytes. #79's "two quantizes in
backward.qkv_proj.weight_gemm" counted thexquantize(
acts.attention_normalized,[N,D]) and thedyquantize ([N,3D]) — twodifferent tensors, not two roundings of the panel.
Both pre-checks fall out of that:
LinearScratchorBf16LinearScratch, no descriptor moves, so o_proj andthe lm-head are untouched and there is no
--kernel gemm.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, inbackward.qkv_proj.join(its writes halve).backward.qkv_proj.input_gemmcontains 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_bf16un-rotates dQ/dK, joins them with dV, and writes thepacked words itself. Per rotated pair: three packed words out instead of six
f32s, at exactly the indices
convert_f32_to_bf16_pairswould have writtenthem to — a group's offset into the row is a multiple of
widthand a pairstarts at an even column, so each couple is precisely one word.
RowGradientis how a caller says which panel it wrote:Stagedwhen itfilled the row operand itself,
Widefor the fp32 fallback whoseregister-tiled GEMMs read fp32 panels.
GpuGroupedLinear::packed_row_gradienthands out the buffer under
backward_into's own eligibility condition, so thetwo cannot disagree.
GpuLinearis not touched.scratch.qkvstays allocated — the forward projection's tcgen05 epiloguewrites it and
stage_qkv_heads_bf16reads it fp32 — so unlike #90 there is noVRAM change (129.4 GiB used / 49.0 GiB free either side).
Gates
--kernel ops— green, including the new check:join_group3_rope_bf16'swords compared for equality (
assert_eq!overVec<u32>, not atolerance) against
rope_backward×2 +join_group3run on device and thenrounded RNE and packed on the host. The existing zero-tolerance
join_group3_rope-vs-composition check and therope y/rope dxchecksare 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_linearsis what exercises this diff end toend (
NA=256, DA=512clearstcgen05_linear_eligible, so the qkv backwardtakes the
Stagedarm), and it matches the CPU reference under theunchanged bf16 tolerances — they did not need widening because the bytes
did not change.
--kernel gemm: no operand descriptor moved. No flash kernel touched, sono 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:2a8ce1fbackward.qkv_proj.weight_gemmbackward.qkv_proj.input_gemmbackward.qkv_proj.joinMarked pair with the order reversed (this branch profiled first):
2a8ce1fbackward.qkv_proj.weight_gemmbackward.qkv_proj.input_gemmbackward.qkv_proj.joinOrder does not matter. Each
compare_profilerun also emits an unmarked pairbefore the marked one, and those reproduce it independently:
weight_gemm21.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):
2a8ce1fbackward.qkv_proj.weight_gemmbackward.qkv_proj.input_gemmbackward.qkv_proj.joinThe 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_kv29.57 → 27.82,flash_q21.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 theresidual.
Training smoke,
--kernel model --bin train --shard /data/wiki-train-00000.tok --steps 100, same shard, two runs per arm: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_ropekeeps its kernel and its parity check — it is half of theoracle the packed join is tested against (§11: the naive kernels are never
deleted), and it is still what the
Widefallback launches.scratch.qkvrow is updated and a new subsection records thedecision, 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.