From 414a07a85b17be736739133473c1ab341d4b83e1 Mon Sep 17 00:00:00 2001 From: Tracy Sharpe <42477615+tracysh@users.noreply.github.com> Date: Fri, 19 Jul 2019 20:00:59 -0700 Subject: [PATCH] Support Sum opset6 for ONNX 1.2 models (#1447) The NCHWc transform was missing support for the Sum_6 operator from ONNX 1.2. Older models would add unnecessary reorder ops and also would not use the Conv/Add fusion. --- .../core/optimizer/nchwc_transformer.cc | 2 +- .../test/optimizer/nchwc_optimizer_test.cc | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/onnxruntime/core/optimizer/nchwc_transformer.cc b/onnxruntime/core/optimizer/nchwc_transformer.cc index 950cc0adbf..e195afb8eb 100644 --- a/onnxruntime/core/optimizer/nchwc_transformer.cc +++ b/onnxruntime/core/optimizer/nchwc_transformer.cc @@ -637,7 +637,7 @@ void NchwcTransformerImpl::Transform(Node& node) { // needed for correct operation. This avoids doing extra string checks for // nodes unrelated to this transformer. if (graph_utils::IsSupportedOptypeVersionAndDomain(node, "Add", {7}) || - graph_utils::IsSupportedOptypeVersionAndDomain(node, "Sum", {8})) { + graph_utils::IsSupportedOptypeVersionAndDomain(node, "Sum", {6, 8})) { TransformAdd(node); } else if (graph_utils::IsSupportedOptypeVersionAndDomain(node, "Concat", {4})) { TransformConcat(node); diff --git a/onnxruntime/test/optimizer/nchwc_optimizer_test.cc b/onnxruntime/test/optimizer/nchwc_optimizer_test.cc index 7966aa8327..9e99f23917 100644 --- a/onnxruntime/test/optimizer/nchwc_optimizer_test.cc +++ b/onnxruntime/test/optimizer/nchwc_optimizer_test.cc @@ -139,14 +139,17 @@ struct NchwcTestHelper { }; void NchwcOptimizerTester(const std::function& build_test_case, - const std::function& check_nchwc_graph) { + const std::function& check_nchwc_graph, + int opset_version = 10) { // Ignore the test if NCHWc is not supported by the platform. if (MlasNchwcGetBlockSize() <= 1) { return; } // Build the model for this test. - Model model("nchwc"); + std::unordered_map domain_to_version; + domain_to_version[kOnnxDomain] = opset_version; + Model model("nchwc", false, ModelMetaData(), IOnnxRuntimeOpSchemaRegistryList(), domain_to_version); NchwcTestHelper helper(model.MainGraph()); build_test_case(helper); ASSERT_TRUE(model.MainGraph().Resolve().IsOK()); @@ -486,7 +489,7 @@ TEST(NchwcOptimizerTests, ConvGlobalPool) { } TEST(NchwcOptimizerTests, ConvAddFusion) { - auto test_case = [&](const std::string& op_type, bool do_relu) { + auto test_case = [&](const std::string& op_type, int opset_version, bool do_relu) { auto build_test_case = [&](NchwcTestHelper& helper) { auto* input_arg = helper.MakeInput({1, 32, 28, 28}); auto* conv1_output_arg = helper.MakeIntermediate(); @@ -514,15 +517,18 @@ TEST(NchwcOptimizerTests, ConvAddFusion) { EXPECT_EQ(op_to_count["Relu"], 0); }; - NchwcOptimizerTester(build_test_case, check_nchwc_graph); + NchwcOptimizerTester(build_test_case, check_nchwc_graph, opset_version); }; // Verify that Add or Sum can be fused into a preceding NCHWc Conv node, // with an optional Relu node following. std::vector op_types = {"Add", "Sum"}; + static const int opset_versions[] = {7, 10}; for (auto& op_type : op_types) { - test_case(op_type, false); - test_case(op_type, true); + for (auto opset_version : opset_versions) { + test_case(op_type, opset_version, false); + test_case(op_type, opset_version, true); + } } }