Repository navigation
Fix per-sample gradients for LayerNorm without bias - #850
Open
mottopanikeiku wants to merge 1 commit into
Open
mottopanikeiku wants to merge 1 commit into
mottopanikeiku wants to merge 1 commit into
Conversation
I found that the LayerNorm grad sampler dereferenced an absent bias when bias=False, causing backward to fail. I now skip bias gradients when no bias is present and test one- and two-dimensional normalization against the microbatch reference. Assisted-by: Claude Assisted-by: GPT-6 Astra Signed-off-by: mottopanikeiku <176798723+mottopanikeiku@users.noreply.github.com>
|
This pull request has been imported. If you are a Meta employee, you can view this in D125987370. (Because this pull request was imported automatically, there will not be any future comments.) |
This branch has not been deployed
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.
Types of changes
Motivation and Context / Related issue
nn.LayerNorm(..., bias=False)(supported since PyTorch 2.1) crashes in backward underGradSampleModule:compute_layer_norm_grad_samplecomputes the weight's per-sample gradient and then readslayer.bias.requires_gradunconditionally, butlayer.biasisNone. I added the samelayer.bias is not Nonecheck the Linear sampler already has. The weight gradients and the trainable/frozen bias cases are unchanged. I found this by reading and testing the LayerNorm sampler; there's no existing issue.LayerNorm(4, bias=False)AttributeError: 'NoneType' object has no attribute 'requires_grad'(3, 4)How Has This Been Tested (if it applies)
LayerNorm_test.test_no_biasuses the existing microbatch-of-one comparison (run_test) for normalized shapes(4,)and(3, 4), withsumandmeanreduction, in hooks, functorch and expanded-weights modes. The 4 hooks-mode subtests fail on main with the error above; all pass with the fix and match the reference gradients.layer_norm_test.py,grad_sample_module_test.py,grad_sample_hooks_test.py,grad_sample_module_fast_gradient_clipping_test.pyandgrad_sample_hooks_fast_gradient_clipping_test.py: 81 passed, 2 skipped (the existing multi-GPU tests), on CPU..github/workflows/flake8_config.ini) pass on both files.Checklist
I made this PR and directed its development, with AI assistance from Claude and GPT-6 Astra.