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
add type annotations to torch.nn.modules.normalization #49035
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4cfa0ac
add type annotations to torch.nn.modules.normalization
guilhermeleobas 3d9b430
address comments
guilhermeleobas fa5cc8c
closes gh-49034
guilhermeleobas 58d987d
Merge remote-tracking branch 'upstream/master' into normalization
guilhermeleobas 1c2b04d
revert change from List to Sequence
guilhermeleobas 59aba29
Merge remote-tracking branch 'upstream/master' into normalization
guilhermeleobas File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
from .. import init | ||
|
||
from torch import Tensor, Size | ||
from typing import Union, List | ||
from typing import Union, List, Tuple | ||
|
||
|
||
class LocalResponseNorm(Module): | ||
|
@@ -141,20 +141,21 @@ class LayerNorm(Module): | |
>>> output = m(input) | ||
""" | ||
__constants__ = ['normalized_shape', 'eps', 'elementwise_affine'] | ||
normalized_shape: _shape_t | ||
normalized_shape: Tuple[int, ...] | ||
eps: float | ||
elementwise_affine: bool | ||
|
||
def __init__(self, normalized_shape: _shape_t, eps: float = 1e-5, elementwise_affine: bool = True) -> None: | ||
super(LayerNorm, self).__init__() | ||
if isinstance(normalized_shape, numbers.Integral): | ||
normalized_shape = (normalized_shape,) | ||
self.normalized_shape = tuple(normalized_shape) | ||
# mypy error: incompatible types in assignment | ||
normalized_shape = (normalized_shape,) # type: ignore[assignment] | ||
self.normalized_shape = tuple(normalized_shape) # type: ignore[arg-type] | ||
self.eps = eps | ||
self.elementwise_affine = elementwise_affine | ||
if self.elementwise_affine: | ||
self.weight = Parameter(torch.Tensor(*normalized_shape)) | ||
self.bias = Parameter(torch.Tensor(*normalized_shape)) | ||
self.weight = Parameter(torch.Tensor(*self.normalized_shape)) | ||
self.bias = Parameter(torch.Tensor(*self.normalized_shape)) | ||
else: | ||
self.register_parameter('weight', None) | ||
self.register_parameter('bias', None) | ||
|
@@ -169,7 +170,7 @@ def forward(self, input: Tensor) -> Tensor: | |
return F.layer_norm( | ||
input, self.normalized_shape, self.weight, self.bias, self.eps) | ||
|
||
def extra_repr(self) -> Tensor: | ||
def extra_repr(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder why it was not caught before. |
||
return '{normalized_shape}, eps={eps}, ' \ | ||
'elementwise_affine={elementwise_affine}'.format(**self.__dict__) | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalized_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also wondering do we even need the second
tuple()
wrapper?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the second
tuple()
is to make sureself.normalized_shape
is a tuple and not a list, for instance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I do not think the suggested change works as expected. If the input arg
normalized_shape
is a list, then the if block will never be executed andnormalized_shape_
will not be defined.