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(pt): fix zero inputs for LayerNorm #4134

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deepmd/pt/model/network/layernorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@
# variance = xx.var(dim=-1, unbiased=False, keepdim=True)
# The following operation is the same as above, but will not raise error when using jit model to inference.
# See https://github.com/pytorch/pytorch/issues/85792
variance, mean = torch.var_mean(xx, dim=-1, unbiased=False, keepdim=True)
yy = (xx - mean) / torch.sqrt(variance + self.eps)
if xx.numel() > 0:
variance, mean = torch.var_mean(xx, dim=-1, unbiased=False, keepdim=True)
yy = (xx - mean) / torch.sqrt(variance + self.eps)

Check warning on line 101 in deepmd/pt/model/network/layernorm.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/network/layernorm.py#L99-L101

Added lines #L99 - L101 were not covered by tests
else:
yy = xx

Check warning on line 103 in deepmd/pt/model/network/layernorm.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/network/layernorm.py#L103

Added line #L103 was not covered by tests
if self.matrix is not None and self.bias is not None:
yy = yy * self.matrix + self.bias
return yy
Expand Down