Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LayerNorm(bias=False) error #108060

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion torch/nn/modules/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def __init__(self, normalized_shape: _shape_t, eps: float = 1e-5, elementwise_af
def reset_parameters(self) -> None:
if self.elementwise_affine:
init.ones_(self.weight)
init.zeros_(self.bias)
if self.bias is not None:
init.zeros_(self.bias)

def forward(self, input: Tensor) -> Tensor:
return F.layer_norm(
Expand Down
9 changes: 7 additions & 2 deletions torch/testing/_internal/common_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,10 @@ def module_inputs_torch_nn_LayerNorm(module_info, device, dtype, requires_grad,
constructor_input=FunctionInput([5], 1e-3),
forward_input=FunctionInput(make_input((0, 5))),
desc='1d_empty_elementwise_affine'),
ModuleInput(
constructor_input=FunctionInput([2, 2, 5], 1e-3, elementwise_affine=True, bias=False),
forward_input=FunctionInput(make_input((4, 2, 2, 5))),
desc='3d_elementwise_affine_no_bias'),
]


Expand Down Expand Up @@ -1809,15 +1813,16 @@ def module_inputs_torch_nn_Transformer(module_info, device, dtype, requires_grad
# Samples below are for validating the no-batch-dim support.
key_padding_masks = (None, torch.tensor([False, False, True], device=device, dtype=torch.bool))
attn_masks = (None, torch.tensor([False, False, True], device=device, dtype=torch.bool).expand((3, 3)))
for mask, key_padding_mask, norm_first in itertools.product(attn_masks, key_padding_masks, (True, False)):
for mask, key_padding_mask, norm_first, bias in \
itertools.product(attn_masks, key_padding_masks, (True, False), (True, False)):
# Using same mask for tgt and memory
src_mask , tgt_mask = (mask,) * 2
src_key_padding_mask, tgt_key_padding_mask = (key_padding_mask,) * 2
samples.append(
ModuleInput(
constructor_input=FunctionInput(d_model=4, nhead=2, dim_feedforward=8,
num_encoder_layers=1, num_decoder_layers=1,
dropout=0.0, batch_first=True, norm_first=norm_first),
dropout=0.0, batch_first=True, norm_first=norm_first, bias=bias),
forward_input=FunctionInput(
make_input((3, 4)), make_input((3, 4)), tgt_mask=tgt_mask, src_mask=src_mask,
tgt_key_padding_mask=tgt_key_padding_mask, src_key_padding_mask=src_key_padding_mask
Expand Down