Skip to content

Commit

Permalink
Fix naming for contrib op check, add test for channels last and chang…
Browse files Browse the repository at this point in the history
…e inputs for generated onnx to handle input activation.

TODO Need to finish parser tests
  • Loading branch information
TedThemistokleous committed Dec 23, 2024
1 parent 982392e commit e70ca61
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/onnx/parse_groupnorm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ struct parse_groupnorm : op_parser<parse_groupnorm>
{
std::vector<op_desc> operators() const
{
return {{"GroupNormalization"}, {"GroupNorm"}};
return {{"GroupNormalization", "GroupNormalization"}, {"GroupNorm", "Contrib_GroupNorm"}};
}

instruction_ref parse(const op_desc& opd,
const onnx_parser& parser,
const onnx_parser::node_info& info,
std::vector<instruction_ref> args) const
{
auto is_contrib = opd.op_name == "GroupNorm";
bool is_contrib = (!opd.op_name.compare("Contrib_GroupNorm"));

Check warning on line 45 in src/onnx/parse_groupnorm.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: Use 'not' instead of ! [UseNamedLogicOperator]

Check warning on line 45 in src/onnx/parse_groupnorm.cpp

View workflow job for this annotation

GitHub Actions / tidy

implicit conversion 'int' -> 'bool' [readability-implicit-bool-conversion,-warnings-as-errors]

Check warning on line 45 in src/onnx/parse_groupnorm.cpp

View workflow job for this annotation

GitHub Actions / tidy

do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare,-warnings-as-errors]

float epsilon = 1e-5f;
if(contains(info.attributes, "epsilon"))
{
Expand All @@ -52,7 +53,9 @@ struct parse_groupnorm : op_parser<parse_groupnorm>
if(contains(info.attributes, "num_groups") or contains(info.attributes, "groups"))
{
if (is_contrib)

Check warning on line 55 in src/onnx/parse_groupnorm.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: inconclusive: Found duplicate branches for 'if' and 'else'. [duplicateBranch]
num_groups = parser.parse_value(info.attributes.at("groups")).at<size_t>();
{
num_groups = parser.parse_value(info.attributes.at("num_groups")).at<size_t>();
}
else
num_groups = parser.parse_value(info.attributes.at("num_groups")).at<size_t>();
}
Expand Down Expand Up @@ -90,7 +93,7 @@ struct parse_groupnorm : op_parser<parse_groupnorm>
auto x = args.at(0);
if(is_nhwc and is_contrib)
{
x = info.add_instruction(make_op("transpose", {{"permutation", {0, 3, 2, 1}}}), x);
x = info.add_instruction(make_op("transpose", {{"permutation", {0, 2, 1}}}), x);
}

auto scale = args.at(1); //gamma in the GroupNorm contrib case
Expand Down Expand Up @@ -164,7 +167,7 @@ struct parse_groupnorm : op_parser<parse_groupnorm>
// Convert to NCHW -> NHWC for contrib GroupNorm
if(is_nhwc and is_contrib)
{
output = info.add_instruction(make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), output);
output = info.add_instruction(make_op("transpose", {{"permutation", {0, 2, 1}}}), output);
}
return output;
}
Expand Down
11 changes: 8 additions & 3 deletions test/onnx/gen_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4747,9 +4747,9 @@ def group_norm_contrib_test(x_dims,
beta_dims,
y_dims,
num_groups,
eps_value=1e-5,
activation=0,
channels_last=0,
eps_value=1e-5,
dtype=TensorProto.FLOAT):
x = helper.make_tensor_value_info('x', dtype, x_dims)
gamma = helper.make_tensor_value_info('gamma', dtype, gamma_dims)
Expand All @@ -4759,7 +4759,7 @@ def group_norm_contrib_test(x_dims,
node = onnx.helper.make_node('GroupNorm',
inputs=['x', 'gamma', 'beta'],
outputs=['y'],
activaction=activation,
activation=activation,
channels_last=channels_last,
num_groups=num_groups,
epsilon=eps_value)
Expand All @@ -4769,14 +4769,19 @@ def group_norm_contrib_test(x_dims,

@onnx_test()
def group_norm_contrib_3d_test():
return group_norm_contrib_test([1, 4, 2], [2], [2], [1, 4, 2], 1, 0, 0)
return group_norm_contrib_test([1, 4, 2], [2], [2], [1, 4, 2], 2, 0, 0)


@onnx_test()
def group_norm_contrib_silu_3d_test():
return group_norm_contrib_test([1, 4, 2], [2], [2], [1, 4, 2], 2, 1, 0)


@onnx_test()
def group_norm_contrib_channels_last_3d_test():
return group_norm_contrib_test([1, 4, 2], [2], [2], [1, 4, 2], 2, 0, 1)


@onnx_test()
def group_norm_contrib_no_activation_attr_test():
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [1, 4, 2])
Expand Down
Binary file modified test/onnx/group_norm_contrib_3d_test.onnx
Binary file not shown.
Binary file not shown.
Binary file modified test/onnx/group_norm_contrib_silu_3d_test.onnx
Binary file not shown.
35 changes: 35 additions & 0 deletions test/onnx/parse/group_norm_contrib_channels_last_3d_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 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(group_norm_contrib_channels_last_3d_test)
{
migraphx::program p = make_group_norm(
{1, 4, 2}, {2}, {2}, {1, 2, 2, 2}, {2, 3}, 1e-5f, migraphx::shape::float_type, "gamma", "beta");

auto prog = optimize_onnx("group_norm_contrib_channels_last_3d_test.onnx");
EXPECT(p == prog);
}

0 comments on commit e70ca61

Please sign in to comment.