-
Notifications
You must be signed in to change notification settings - Fork 89
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 ONNX parsing for SimplifiedLayerNormalization #3129
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f5b1daa
Add simplified_layer_normalization
turneram 545bf19
Formatting
turneram f55617e
Licensing
turneram fb908d8
Merge remote-tracking branch 'origin/develop' into simplified-layernorm
turneram e1ee773
Add code coverage
turneram 116813e
Formatting
turneram 0b7f671
Formatting
turneram 7dca5b4
Merge remote-tracking branch 'origin/develop' into simplified-layernorm
turneram fc0207e
Formatting
turneram 3e96311
Update tests
turneram 8f10e73
Formatting
turneram fb66103
Merge branch 'develop' into simplified-layernorm
umangyadav 6f98953
fix windows build
umangyadav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
#include <migraphx/onnx/op_parser.hpp> | ||
#include <migraphx/ranges.hpp> | ||
#include <migraphx/make_op.hpp> | ||
#include <migraphx/instruction.hpp> | ||
|
||
namespace migraphx { | ||
inline namespace MIGRAPHX_INLINE_NS { | ||
namespace onnx { | ||
|
||
// ONNXRunTime implementation for reference: | ||
// https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/core/providers/cpu/nn/layer_norm_impl.cc | ||
|
||
struct parse_simplified_layer_normalization : op_parser<parse_simplified_layer_normalization> | ||
{ | ||
std::vector<op_desc> operators() const { return {{"SimplifiedLayerNormalization"}}; } | ||
|
||
std::vector<instruction_ref> parse(const op_desc& /*opd*/, | ||
const onnx_parser& parser, | ||
const onnx_parser::node_info& info, | ||
std::vector<instruction_ref> args) const | ||
{ | ||
int64_t axis = -1; | ||
if(contains(info.attributes, "axis")) | ||
{ | ||
axis = parser.parse_value(info.attributes.at("axis")).at<int64_t>(); | ||
} | ||
float epsilon = 1e-5f; | ||
if(contains(info.attributes, "epsilon")) | ||
{ | ||
epsilon = parser.parse_value(info.attributes.at("epsilon")).at<float>(); | ||
} | ||
if(contains(info.attributes, "stash_type")) | ||
{ | ||
std::cerr << "WARNING: SIMPLIFIED_LAYER_NORMALIZATION attribute stash_type is only " | ||
"used for training.\n"; | ||
} | ||
|
||
if(args.size() != 2) | ||
{ | ||
MIGRAPHX_THROW( | ||
"PARSE_SIMPLIFIED_LAYER_NORMALIZATION: invalid input count - expected 2 got " + | ||
std::to_string(args.size())); | ||
} | ||
|
||
auto x = args.at(0); | ||
auto scale = args.at(1); | ||
|
||
auto x_shape = x->get_shape(); | ||
auto x_dtype = x_shape.type(); | ||
int64_t x_rank = x_shape.ndim(); | ||
axis = axis < 0 ? axis + x_rank : axis; | ||
|
||
if(x_rank < 2 or x_rank > 3) | ||
{ | ||
MIGRAPHX_THROW("PARSE_SIMPLIFIED_LAYER_NORMALIZATION: invalid input shape"); | ||
} | ||
|
||
auto x_sq = info.add_common_op("mul", x, x); | ||
auto rms = info.add_instruction(make_op("reduce_mean", {{"axes", {axis}}}), x_sq); | ||
auto mean = rms; | ||
epsilon = | ||
(x_dtype == migraphx::shape::half_type and std::abs(epsilon) < 1e-7) ? 1e-7 : epsilon; | ||
auto eps = info.add_literal(migraphx::literal{migraphx::shape{x_dtype}, {epsilon}}); | ||
rms = info.add_common_op("add", rms, eps); | ||
auto rrms = info.add_instruction(make_op("rsqrt"), rms); | ||
auto result = info.add_common_op("mul", x, rrms); | ||
result = info.add_common_op("mul", result, scale); | ||
|
||
return {result, mean, rrms}; | ||
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. Is this being matched with LayerNorm kernel on the GPU target ? |
||
} | ||
}; | ||
|
||
} // namespace onnx | ||
} // namespace MIGRAPHX_INLINE_NS | ||
} // namespace migraphx |
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
31 changes: 31 additions & 0 deletions
31
test/onnx/parse/simplified_layer_normalization_invalid_input_test.cpp
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <onnx_test.hpp> | ||
|
||
TEST_CASE(simplified_layer_normalization_invalid_input_test) | ||
{ | ||
EXPECT(test::throws( | ||
[&] { migraphx::parse_onnx("simplified_layer_normalization_invalid_input_test.onnx"); })); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/onnx/parse/simplified_layer_normalization_invalid_n_args_test.cpp
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <onnx_test.hpp> | ||
|
||
TEST_CASE(simplified_layer_normalization_invalid_n_args_test) | ||
{ | ||
EXPECT(test::throws( | ||
[&] { migraphx::parse_onnx("simplified_layer_normalization_invalid_n_args_test.onnx"); })); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <onnx_test.hpp> | ||
#include <onnx_test_utils.hpp> | ||
|
||
TEST_CASE(simplified_layer_normalization_test) | ||
{ | ||
migraphx::program p = | ||
make_simplified_layer_norm({2, 2, 4}, {}, {4}, -1, 1e-5f, migraphx::shape::half_type); | ||
|
||
auto prog = optimize_onnx("simplified_layer_normalization_test.onnx"); | ||
EXPECT(p == prog); | ||
} |
23 changes: 23 additions & 0 deletions
23
test/onnx/simplified_layer_normalization_invalid_input_test.onnx
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
1simplified_layer_normalization_invalid_input_test:¯ | ||
+ | ||
x | ||
scaley"SimplifiedLayerNormalization1simplified_layer_normalization_invalid_input_testZ | ||
x | ||
Z | ||
scale | ||
|
||
b | ||
y | ||
B |
28 changes: 28 additions & 0 deletions
28
test/onnx/simplified_layer_normalization_invalid_n_args_test.onnx
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
2simplified_layer_normalization_invalid_n_args_test:Ê | ||
1 | ||
x | ||
scale | ||
biasy"SimplifiedLayerNormalization2simplified_layer_normalization_invalid_n_args_testZ | ||
x | ||
Z | ||
scale | ||
|
||
Z | ||
bias | ||
b | ||
y | ||
B |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#simplified_layer_normalization_test:Õ | ||
g | ||
x | ||
scaley"SimplifiedLayerNormalization* | ||
axisÿÿÿÿÿÿÿÿÿ * | ||
epsilon'7 * | ||
|
||
stash_type #simplified_layer_normalization_testZ | ||
x | ||
Z | ||
scale | ||
|
||
b | ||
y | ||
B |
Oops, something went wrong.
Oops, something went wrong.
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.
Why are we limiting the epsilon for half type? It looks like a user input
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.
That is how we handle epsilon in our regular LayerNorm parser, so I did the same here.