From a4957ab22e99cf03b0892861b26f01de4988b6a3 Mon Sep 17 00:00:00 2001 From: Chris Austen Date: Wed, 20 Sep 2023 19:59:46 -0400 Subject: [PATCH 1/2] Update Constant parsing to support more attributes #2141 (#2216) Add parsing support for value_float, value_floats, value_int, value_ints attributes Disable failing tests Resolves Test failures due to IndexError: _Map_base::at migraphx-benchmark/AMDMIGraphX#76 --- src/onnx/parse_constant.cpp | 29 +++++++- .../constant_multiple_attributes_test.onnx | Bin 0 -> 187 bytes test/onnx/constant_no_attributes_test.onnx | 3 + test/onnx/constant_value_float_test.onnx | Bin 0 -> 97 bytes test/onnx/constant_value_floats_test.onnx | Bin 0 -> 110 bytes test/onnx/constant_value_int_test.onnx | 3 + test/onnx/constant_value_ints_test.onnx | 4 + test/onnx/gen_onnx.py | 70 ++++++++++++++++++ test/onnx/onnx_test.cpp | 52 +++++++++++++ test/py/onnx_backend_test.py | 34 +++++++++ 10 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 test/onnx/constant_multiple_attributes_test.onnx create mode 100644 test/onnx/constant_no_attributes_test.onnx create mode 100644 test/onnx/constant_value_float_test.onnx create mode 100644 test/onnx/constant_value_floats_test.onnx create mode 100644 test/onnx/constant_value_int_test.onnx create mode 100644 test/onnx/constant_value_ints_test.onnx diff --git a/src/onnx/parse_constant.cpp b/src/onnx/parse_constant.cpp index e4ea726f9a1..a598b56bbb4 100644 --- a/src/onnx/parse_constant.cpp +++ b/src/onnx/parse_constant.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { @@ -39,16 +40,38 @@ struct parse_constant : op_parser onnx_parser::node_info info, const std::vector& /*args*/) const { - literal v = parser.parse_value(info.attributes.at("value")); + static const std::vector attributes = { + "value", "value_float", "value_floats", "value_int", "value_ints"}; + + std::vector present_attributes; + std::copy_if(attributes.begin(), + attributes.end(), + std::back_inserter(present_attributes), + [&](const std::string& a) { return contains(info.attributes, a); }); + + if(present_attributes.empty()) + { + MIGRAPHX_THROW("Constant node does not contain any supported attribute"); + } + + if(present_attributes.size() > 1) + { + MIGRAPHX_THROW("Constant contains multiple attributes: " + + join_strings(std::move(present_attributes), ", ")); + } + + // cppcheck-suppress accessMoved + auto&& attr = info.attributes[present_attributes[0]]; + literal v = parser.parse_value(attr); + // return empty literal if(v.get_shape().elements() == 0) { return info.add_literal(literal{v.get_shape().type()}); } - auto dim_size = info.attributes.at("value").t().dims_size(); // if dim_size is 0, it is a scalar - if(dim_size == 0) + if(attr.has_t() and attr.t().dims_size() == 0) { migraphx::shape scalar_shape{v.get_shape().type()}; return info.add_literal(migraphx::literal{scalar_shape, v.data()}); diff --git a/test/onnx/constant_multiple_attributes_test.onnx b/test/onnx/constant_multiple_attributes_test.onnx new file mode 100644 index 0000000000000000000000000000000000000000..d22b735adc03efb1ffffa33b3b799f0dc5c50f86 GIT binary patch literal 187 zcmdIe&nqrT%qxk{EzK#(EXYZXPb?`Z%1kOPNiB{C;u5QVMy@O+4ri!3Egde_ zvc#OyR4oM#W&uVe9tJRIum@5OPCQ`afco-^^NSWRvS>+j@qkswr{&})mK57EFf`Z$ i2?mD+jBHxMTwD;z%)F9f2Sx{`1&r)OxX6h~fFA(Dzc}9j literal 0 HcmV?d00001 diff --git a/test/onnx/constant_no_attributes_test.onnx b/test/onnx/constant_no_attributes_test.onnx new file mode 100644 index 00000000000..2c6ffad0455 --- /dev/null +++ b/test/onnx/constant_no_attributes_test.onnx @@ -0,0 +1,3 @@ +constant_no_attributes_test:) + +"Constantconstant_no_attributes_testB \ No newline at end of file diff --git a/test/onnx/constant_value_float_test.onnx b/test/onnx/constant_value_float_test.onnx new file mode 100644 index 0000000000000000000000000000000000000000..da91f06641e631f1d6b05f6416af0d015d8f4ba1 GIT binary patch literal 97 zcmdadd_literal(migraphx::literal{migraphx::shape{migraphx::shape::float_type, {1}}, {1.0f}}); + auto prog = optimize_onnx("constant_value_float_test.onnx"); + + EXPECT(p == prog); +} + +TEST_CASE(constant_value_floats_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + mm->add_literal( + migraphx::literal{migraphx::shape{migraphx::shape::float_type, {3}}, {1.0f, 2.0f, 3.0f}}); + auto prog = optimize_onnx("constant_value_floats_test.onnx"); + + EXPECT(p == prog); +} + +TEST_CASE(constant_value_int_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {1}}, {1}}); + auto prog = optimize_onnx("constant_value_int_test.onnx"); + + EXPECT(p == prog); +} + +TEST_CASE(constant_value_ints_test) +{ + migraphx::program p; + auto* mm = p.get_main_module(); + mm->add_literal( + migraphx::literal{migraphx::shape{migraphx::shape::int64_type, {3}}, {1, 2, 3}}); + auto prog = optimize_onnx("constant_value_ints_test.onnx"); + + EXPECT(p == prog); +} + +TEST_CASE(constant_no_attributes_test) +{ + EXPECT(test::throws([&] { optimize_onnx("constant_no_attributes_test.onnx"); })); +} + +TEST_CASE(constant_multiple_attributes_test) +{ + EXPECT(test::throws([&] { optimize_onnx("constant_multiple_attributes_test.onnx"); })); +} + TEST_CASE(constant_fill_test) { migraphx::program p; diff --git a/test/py/onnx_backend_test.py b/test/py/onnx_backend_test.py index 4ae7203b6da..b7f26608472 100644 --- a/test/py/onnx_backend_test.py +++ b/test/py/onnx_backend_test.py @@ -108,6 +108,34 @@ def disabled_tests_onnx_1_12_0(backend_test): backend_test.exclude(r'test_scatter_elements_with_duplicate_indices_cpu') +def disabled_tests_onnx_1_13_0(backend_test): + # The following tests fail due to the CastLike operator being unsupported + backend_test.exclude(r'test_elu_default_expanded_ver18_cpu') + backend_test.exclude(r'test_elu_example_expanded_ver18_cpu') + backend_test.exclude(r'test_elu_expanded_ver18_cpu') + backend_test.exclude(r'test_hardsigmoid_default_expanded_ver18_cpu') + backend_test.exclude(r'test_hardsigmoid_example_expanded_ver18_cpu') + backend_test.exclude(r'test_hardsigmoid_expanded_ver18_cpu') + backend_test.exclude(r'test_leakyrelu_default_expanded_cpu') + backend_test.exclude(r'test_leakyrelu_example_expanded_cpu') + backend_test.exclude(r'test_leakyrelu_expanded_cpu') + backend_test.exclude(r'test_selu_default_expanded_ver18_cpu') + backend_test.exclude(r'test_selu_example_expanded_ver18_cpu') + backend_test.exclude(r'test_selu_expanded_ver18_cpu') + backend_test.exclude(r'test_thresholdedrelu_default_expanded_ver18_cpu') + backend_test.exclude(r'test_thresholdedrelu_example_expanded_ver18_cpu') + backend_test.exclude(r'test_thresholdedrelu_expanded_ver18_cpu') + backend_test.exclude(r'test_relu_expanded_ver18_cpu') + backend_test.exclude(r'test_softsign_example_expanded_ver18_cpu') + backend_test.exclude(r'test_softsign_expanded_ver18_cpu') + + +def disabled_tests_onnx_1_14_0(backend_test): + # The following tests fail due to the CastLike operator being unsupported + backend_test.exclude(r'test_softplus_example_expanded_ver18_cpu') + backend_test.exclude(r'test_softplus_expanded_ver18_cpu') + + def create_backend_test(testname=None, target_device=None): if target_device is not None: c2.set_device(target_device) @@ -334,6 +362,12 @@ def create_backend_test(testname=None, target_device=None): if version.parse(onnx.__version__) >= version.parse("1.12.0"): disabled_tests_onnx_1_12_0(backend_test) + if version.parse(onnx.__version__) >= version.parse("1.13.0"): + disabled_tests_onnx_1_13_0(backend_test) + + if version.parse(onnx.__version__) >= version.parse("1.14.0"): + disabled_tests_onnx_1_14_0(backend_test) + # import all test cases at global scope to make # them visible to python.unittest. From 4637621adb447bcf35e8bb59d5520929a1bae763 Mon Sep 17 00:00:00 2001 From: Chris Austen Date: Thu, 21 Sep 2023 00:19:01 -0400 Subject: [PATCH 2/2] Rectify flipped coordinate_transformation_mode logic in ROIAlign #2159 (#2214) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rectify flipped coordinate_transformation_mode logic in ROIAlign * Handle both opset 10 and 16 versions * Fix version check and clang tidy warning Co-authored-by: Dino Musić --- src/include/migraphx/op/roialign.hpp | 2 +- src/onnx/parse_roialign.cpp | 11 +++++++---- src/targets/gpu/jit/roialign.cpp | 2 +- test/onnx/onnx_test.cpp | 8 +++++++- test/ref/roialign.cpp | 8 ++++---- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/include/migraphx/op/roialign.hpp b/src/include/migraphx/op/roialign.hpp index 814c2aa4170..1dcb13ee4a5 100644 --- a/src/include/migraphx/op/roialign.hpp +++ b/src/include/migraphx/op/roialign.hpp @@ -124,7 +124,7 @@ struct roialign { xy[ii] = roi_start[ii] + p[ii] * bin_size[ii] + (i[ii] + .5f) * bin_size[ii] / bin_grid_size[ii]; - xy[ii] = (coord_trans_mode == "output_half_pixel") ? (xy[ii] - 0.5f) : xy[ii]; + xy[ii] = (coord_trans_mode == "half_pixel") ? (xy[ii] - 0.5f) : xy[ii]; if(xy[ii] < -1.0 or xy[ii] > dims[ii]) { results[index] = pos_weight{}; diff --git a/src/onnx/parse_roialign.cpp b/src/onnx/parse_roialign.cpp index 88ccbde6105..d397df608e3 100644 --- a/src/onnx/parse_roialign.cpp +++ b/src/onnx/parse_roialign.cpp @@ -37,15 +37,18 @@ struct parse_roialign : op_parser std::vector operators() const { return {{"RoiAlign"}}; } instruction_ref parse(const op_desc& /*opd*/, - const onnx_parser& /*parser*/, + const onnx_parser& parser, onnx_parser::node_info info, const std::vector& args) const { - std::string coord_trans_mode = "half_pixel"; - if(contains(info.attributes, "coordinate_transformation_mode")) + std::string coord_trans_mode = + parser.opset_version >= 16 ? "half_pixel" : "output_half_pixel"; + + if(const auto* a = "coordinate_transformation_mode"; contains(info.attributes, a)) { - coord_trans_mode = info.attributes.at("coordinate_transformation_mode").s(); + coord_trans_mode = info.attributes.at(a).s(); } + if(not contains({"half_pixel", "output_half_pixel"}, coord_trans_mode)) { MIGRAPHX_THROW("coordinate_transformation_mode \"" + coord_trans_mode + diff --git a/src/targets/gpu/jit/roialign.cpp b/src/targets/gpu/jit/roialign.cpp index 5da90c26621..c78ad1aeed9 100644 --- a/src/targets/gpu/jit/roialign.cpp +++ b/src/targets/gpu/jit/roialign.cpp @@ -81,7 +81,7 @@ struct roialign_compiler : compiler // coord_trans_mode auto ctm = v.at("coordinate_transformation_mode").to(); - float rois_offset = (ctm == "output_half_pixel") ? -0.5f : 0.0f; + float rois_offset = (ctm == "half_pixel") ? -0.5f : 0.0f; options.params += " -DROIS_OFFSET=" + std::to_string(rois_offset); // spatial_scale diff --git a/test/onnx/onnx_test.cpp b/test/onnx/onnx_test.cpp index 0dde3764b37..b0e6255a976 100644 --- a/test/onnx/onnx_test.cpp +++ b/test/onnx/onnx_test.cpp @@ -5951,7 +5951,13 @@ TEST_CASE(roialign_default_test) auto rois = mm->add_parameter("rois", srois); auto bi = mm->add_parameter("batch_ind", sbi); - auto r = mm->add_instruction(migraphx::make_op("roialign"), x, rois, bi); + // Due to the onnx model using opset 12, the coordinate_transformation_mode should be set to + // output_half_pixel + auto r = mm->add_instruction( + migraphx::make_op("roialign", {{"coordinate_transformation_mode", "output_half_pixel"}}), + x, + rois, + bi); mm->add_return({r}); auto prog = migraphx::parse_onnx("roialign_default_test.onnx"); diff --git a/test/ref/roialign.cpp b/test/ref/roialign.cpp index a93962c5046..0896f2202ca 100644 --- a/test/ref/roialign.cpp +++ b/test/ref/roialign.cpp @@ -73,7 +73,7 @@ TEST_CASE(roialign_out_of_bound_test) }; { - auto p = create_program("output_half_pixel"); + auto p = create_program("half_pixel"); p.compile(migraphx::make_target("ref")); auto result = p.eval({}).back(); std::vector results_vector; @@ -130,7 +130,7 @@ TEST_CASE(roialign_test) }; { - auto p = create_program(); + auto p = create_program("output_half_pixel"); p.compile(migraphx::make_target("ref")); auto result = p.eval({}).back(); std::vector results_vector; @@ -154,7 +154,7 @@ TEST_CASE(roialign_test) } { - auto p = create_program("output_half_pixel"); + auto p = create_program("half_pixel"); p.compile(migraphx::make_target("ref")); auto result = p.eval({}).back(); std::vector results_vector; @@ -175,7 +175,7 @@ TEST_CASE(roialign_test) } { - auto p = create_program("output_half_pixel", migraphx::op::pooling_mode::max, 0); + auto p = create_program("half_pixel", migraphx::op::pooling_mode::max, 0); p.compile(migraphx::make_target("ref")); auto result = p.eval({}).back(); std::vector results_vector;