-
Notifications
You must be signed in to change notification settings - Fork 26.3k
torch::nn::LayerNorm #28032
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
torch::nn::LayerNorm #28032
Conversation
… var names, updated get var calls
…ruct a module which has no default constructor
|
|
||
| ASSERT_TRUE(torch::allclose(output, expected)); | ||
| } | ||
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.
Thanks for fixing the extra spaces :D
| if (options.elementwise_affine()) { | ||
| torch::nn::init::ones_(weight); | ||
| torch::nn::init::zeros_(bias); | ||
| } |
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.
This code block
if (options.elementwise_affine()) {
torch::nn::init::ones_(weight);
torch::nn::init::zeros_(bias);
}should be at the end the reset() function, because the expectation is that all initialization logic lives in the reset() function :D
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.
alright
|
@anjali411 This PR has some conflicts with master, and it also contains changes to |
|
@anjali411 The code conflict and submodule issue should be fixed :D |
| inline Tensor layer_norm(const Tensor& input, | ||
| const LayerNormOptions& options, | ||
| const Tensor& weight, | ||
| const Tensor& bias) { |
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.
In the Python version, weight and bias can take None as default value, and in the C++ version I think we should take Tensor() as default value to match the Python version :D
test/cpp/api/functional.cpp
Outdated
| const auto input = torch::randn({2, 2}); | ||
| const auto weight = torch::randn({2, 2}); | ||
| const auto bias = torch::randn({2, 2}); | ||
| auto y = F::layer_norm(input, LayerNormOptions({2, 2}).eps(2e-5), weight, bias); |
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.
In order to test the "no weights and no bias" case for F::layer_norm, let's probably change this to
| auto y = F::layer_norm(input, LayerNormOptions({2, 2}).eps(2e-5), weight, bias); | |
| auto y = F::layer_norm(input, LayerNormOptions({2, 2}).eps(2e-5)); |
and remove weight and bias for this test.
test/cpp/api/functional.cpp
Outdated
| const auto weight = torch::randn({2, 2}); | ||
| const auto bias = torch::randn({2, 2}); | ||
| auto y = F::layer_norm(input, LayerNormOptions({2, 2}).eps(2e-5), weight, bias); | ||
| auto y_exp = torch::layer_norm(input, {2, 2}, weight, bias, 2e-5); |
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.
and we can pass Tensor() as weight and bias here.
…ion and updated the test in functional.cpp
yf225
left a comment
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.
@anjali411 Thanks a lot for the update! I think we are almost there :D
| namespace torch { | ||
| namespace nn { | ||
|
|
||
| LayerNormImpl::LayerNormImpl(const LayerNormOptions& options_) : options(options_) { |
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.
based on clang-tidy:
{
path: 'torch/csrc/api/src/nn/modules/normalization.cpp',
start_line: 15,
end_line: 15,
start_column: 30,
end_column: 30,
annotation_level: 'failure',
message: '[[modernize-pass-by-value]] warning: pass by value and use std::move'
},
we should probably add // NOLINT(modernize-pass-by-value) at the end of this line.
| namespace torch { | ||
| namespace nn { | ||
|
|
||
| LayerNormOptions::LayerNormOptions(std::vector<int64_t> normalized_shape) : normalized_shape_(normalized_shape) {} |
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.
clang-tidy complains:
{
path: 'torch/csrc/api/src/nn/options/normalization.cpp',
start_line: 6,
end_line: 6,
start_column: 36,
end_column: 36,
annotation_level: 'failure',
message: '[[modernize-pass-by-value]] warning: pass by value and use std::move'
}
for this one I think we can do normalized_shape_(std::move(normalized_shape)).
facebook-github-bot
left a comment
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.
@anjali411 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@anjali411 merged this pull request in 7b59174. |
Summary: Pull Request resolved: pytorch#28032 Differential Revision: D18047371 Pulled By: anjali411 fbshipit-source-id: fb61aea52d6622a67ec1d84950e17e85686461ae
Summary: Since now we have merged pytorch#28032 (thanks anjali411!) Pull Request resolved: pytorch#28484 Differential Revision: D18085844 Pulled By: yf225 fbshipit-source-id: 4be972687addea8f57f48dfe9707837196593062
No description provided.