Fix group norm backward for inputs with more than three dimensions - #1376
Open
truong-v wants to merge 1 commit into
Open
Fix group norm backward for inputs with more than three dimensions#1376truong-v wants to merge 1 commit into
truong-v wants to merge 1 commit into
Conversation
group_norm_backward measured hidden_size on the unflattened gradient, so for an (N, C, H, W) input it got W instead of the per-channel element count H*W. The gradient buffer was then allocated too small but addressed with the input's real strides: the kernel wrote past it, reduced over one row per channel, and the host raised on the final view. Measure hidden_size after the flatten, the way the forward pass does, and cover 4-D and 5-D inputs in test_group_norm.py.
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.
Summary
group_norm_backwardreadshidden_size = dY.shape[-1]before flattening the gradient whilegroup_norm_forwardflattens first and then measures, so for an(N, C, H, W)input the backward pass usesWwhere it means the per-channel element countH*W: it allocates the input-gradient buffer too small, addresses it with the input's real strides (so the kernel writes past the allocation), reducesdW/dBover one row per channel, and finally raisesRuntimeError: shape '[N, C, H, W]' is invalid for input of size ...on the closingview.LigerGroupNormaccepts any input withdim() >= 3and its forward pass is correct for 4-D and 5-D, so this hits the standard convolutional case at the first backward pass; 3-D inputs are unaffected, since theredY.shape[-1]already equals the per-channel element count.Fixes #1375
Details
src/liger_kernel/ops/group_norm.py: move thehidden_sizecomputation below the flatten and divide bychannels_per_group, matching the forward pass. For 3-D inputs the kernel receives exactly the same arguments as before, so that path is unchanged; this is host-side scalar arithmetic, no kernel change and nothing to benchmark.test/transformers/test_group_norm.py: factor the existing test body into_test_liger_group_norm(shape, ...)and addtest_liger_group_norm_spatial_dimscovering(2, 6, 4, 8),(4, 32, 16, 16)and(2, 8, 2, 4, 4). The existing cases keep their parameters.The NPU backend's own
group_norm_backward(ops/backends/_ascend/ops/group_norm.py) already flattens first and then derives its per-channel count ashidden_size // channels_per_group, so this brings the default path in line with it.(2, 6, 4, 8), 3 groups, fp32 on H100, againsttorch.nn.GroupNorm:RuntimeErrorRuntimeErrorRuntimeErrorUnder
compute-sanitizer --tool memcheckwithPYTORCH_NO_CUDA_MEMORY_CACHING=1, the same 4-D backward reports 28 invalid global writes at theDXstore (30 errors total) before the fix and 0 after.Testing Done
test/transformers/test_group_norm.py: 8 passed (5 pre-existing + 3 new).Reverting only the
group_norm.pychange makes the 3 new cases fail and leaves the 5 pre-existing ones passing.test_group_norm.pyis the only file in the suite that exercises this op, so nothing else inmake testis affected. I ranruff check .andruff format --check .over the repo, but not the full suite or the convergence suite locally — boxes below reflect that.Hardware Type: H100 NVL
run
make testto ensure correctnessrun
make checkstyleto ensure code stylerun
make test-convergenceto ensure convergenceEnvironment
a5d795efd2c1436549e70118ef519134e9c27833(main) + this branch, editable install