Skip to content

Fix per-sample gradients for LayerNorm without bias - #850

Open
mottopanikeiku wants to merge 1 commit into
meta-pytorch:mainfrom
mottopanikeiku:fix/layer-norm-without-bias
Open

mottopanikeiku wants to merge 1 commit into
meta-pytorch:mainfrom
mottopanikeiku:fix/layer-norm-without-bias

Conversation

@mottopanikeiku

Copy link
Copy Markdown

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Docs change / refactoring / dependency upgrade

Motivation and Context / Related issue

nn.LayerNorm(..., bias=False) (supported since PyTorch 2.1) crashes in backward under GradSampleModule: compute_layer_norm_grad_sample computes the weight's per-sample gradient and then reads layer.bias.requires_grad unconditionally, but layer.bias is None. I added the same layer.bias is not None check 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.

import torch
from opacus import GradSampleModule

layer = torch.nn.LayerNorm(4, bias=False)
model = GradSampleModule(layer, loss_reduction="sum")
x = torch.randn(3, 2, 4, requires_grad=True)
model(x).square().sum().backward()
print(tuple(layer.weight.grad_sample.shape))
LayerNorm(4, bias=False) Before After
backward AttributeError: 'NoneType' object has no attribute 'requires_grad' per-sample weight gradients, shape (3, 4)

How Has This Been Tested (if it applies)

  • New LayerNorm_test.test_no_bias uses the existing microbatch-of-one comparison (run_test) for normalized shapes (4,) and (3, 4), with sum and mean reduction, 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.py and grad_sample_hooks_fast_gradient_clipping_test.py: 81 passed, 2 skipped (the existing multi-GPU tests), on CPU.
  • black, isort (repo flags) and flake8 (.github/workflows/flake8_config.ini) pass on both files.

Checklist

  • The documentation is up-to-date with the changes I made.
  • I have read the CONTRIBUTING document and completed the CLA (see CONTRIBUTING).
  • All tests passed, and additional code has been covered with new tests.

I made this PR and directed its development, with AI assistance from Claude and GPT-6 Astra.

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>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 18, 2026
@meta-codesync

meta-codesync Bot commented Oct 18, 2026

Copy link
Copy Markdown

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant