Repository navigation
fix(group_norm): keep the backward statistics and reductions in fp32 - #1608
bhargavchintam wants to merge 3 commits into
Conversation
The forward already computes the group mean and rstd in fp32 (linkedin#1538), but group_norm_forward stored them in the input dtype, so a bf16/fp16 backward read rounded statistics. The backward kernel also ran every reduction in the input dtype: in fp16 a channel of 65536 elements can overflow tl.sum when the upstream gradient is large (the input gradient becomes -inf), and in bf16 the sums only keep 8 bits. Store Mean and RSTD in fp32, as layer_norm has done since linkedin#1498, and upcast X, the upstream gradient and W in the backward kernel before the reductions. The fp32 vectors are twice as wide, and at the old 65536 block limit the kernel gets about ten times slower: on a B200 a (16, 32, 256, 256) bf16 backward goes from 139 us to 1518 us. Cap the backward block at 16384, which brings it to 124 us and also takes the fp32 backward at that shape from 2068 us to 134 us.
|
I ran None of the 22 is in |
|
This still merges cleanly on current main ( |
…ckward W is one scalar per channel, so over a channel sum(x_hat * W * dY) is W * sum(x_hat * dY) and sum(W * dY) is W * sum(dY). Those two sums are already taken for dW and dB, so c1 and c2 can be built from them, which drops two of the four reductions per block. With the math in fp32 the backward of benchmark_group_norm.py (bf16, 4096 channels of 512 elements, 4 per group) was slower than main on an H200: 0.337 ms against 0.286 ms at B*T 8192. This brings it to 0.281 ms.
|
I pushed one more commit and re-ran this on an H200 at the new head.
The last two are the slowest of 42 shape and dtype rows; 37 are faster than main. |
Fixes #1606
Summary
_group_norm_backward_kernelruns itstl.sumreductions in the input dtype, andgroup_norm_forwardstoresMeanandRSTDin the input dtype. In fp16 a channel sum can overflow, which turns the whole input gradient into NaN. In bf16 the sums keep only 8 bits, and the backward reads rounded statistics. luca-888 pointed at theMean/RSTDpart in the review of #1538 as a possible follow-up.Two more cases have the same cause. With fp16 input and fp32 parameters, which is what a model under autocast passes in, every bias gradient is
infat (1, 16, 256, 256) with an all-ones upstream gradient. With fp64 input the backward does not compile (Loop-carried variable c1 has initial type fp32 but is re-assigned to fp64 in loop). Both run with this change.Source changes, all in
src/liger_kernel/ops/group_norm.py(the new test is under Testing Done):W,Xand the upstream gradient are upcast to fp32 when they are loaded, in both backward loops.MeanandRSTDare allocated as fp32.MAX_FUSED_SIZE_BACKWARD = 16384limits the backward block. The casts double the vector width, and at the old limit of 65536 the backward gets about ten times slower (table below). My guess is register spills, but I did not measure register use.c1andc2are built from the two channel sums, becauseWis one scalar per channel. This drops two of the four reductions per block. It is the second commit: with the casts alone, the bf16 backward ofbenchmark_group_norm.pywas 18% slower than main on an H200.triton_dtypeanddW.to(dtype)are unchanged because #1253 works on those lines.Details
ops/backends/_ascend/ops/group_norm.py, so it no longer overlaps with this change.F.group_normin fp64 on the same input and upstream gradient, then prints the largest absolute difference):group_norm_backwardlaunched 100 times after a warmup, withtorch.cuda.synchronize()around the loop, mean in µs). "Casts only" is this PR without the block limit:I timed block limits of 65536, 16384, 8192 and 4096. 16384 was the fastest at 256×256, and at the other two shapes it matched 65536. The forward kernel is not changed here.
Testing Done
make testto ensure correctnessmake checkstyleto ensure code style (I ran its two check-only steps, shown at the end, because the other two steps rewrite files)make test-convergenceto ensure convergenceI did not run
make testormake test-convergence. The B200 was rented for this run, and on it I ran only the changed kernel's test file,test/transformers/test_group_norm.py, as below.New test
test_liger_group_norm_low_precision_backwardhas three cases (fp16 and bf16 at (1, 16, 256, 256) with an upstream of ones, and bf16 at (2, 8, 16) with an offset), and compares with fp32 autograd. The bias gradient is only compared where the true value fits in the dtype, since 65536 does not fit in fp16.The two snippets from the issue, run on this branch (on main they print
finite x.grad: 0 / 1048576andmax |x.grad| (expected 0): 466.0):