From 6cee0c1b3ad7e204d16b4c64d9e2aec3081cad25 Mon Sep 17 00:00:00 2001 From: siddhant-0707 Date: Fri, 29 Mar 2024 12:52:50 +0530 Subject: [PATCH] add tests --- .../frontend/src/op/com.microsoft/pad.cpp | 67 +++++-------------- .../frontend/src/op/com.microsoft/pad.hpp | 4 +- .../onnx/frontend/src/ops_bridge.cpp | 3 +- .../tests/models/com.microsoft/pad.prototxt | 61 +++++++++++++++++ .../models/com.microsoft/pad_test.prototxt | 62 +++++++++++++++++ .../tests/onnx_import_com_microsoft.in.cpp | 11 +++ 6 files changed, 156 insertions(+), 52 deletions(-) create mode 100644 src/frontends/onnx/tests/models/com.microsoft/pad.prototxt create mode 100644 src/frontends/onnx/tests/models/com.microsoft/pad_test.prototxt diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/pad.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/pad.cpp index 751b257862fc00..e90716ea8aebfe 100644 --- a/src/frontends/onnx/frontend/src/op/com.microsoft/pad.cpp +++ b/src/frontends/onnx/frontend/src/op/com.microsoft/pad.cpp @@ -4,63 +4,30 @@ #include "op/com.microsoft/pad.hpp" -#include "default_opset.hpp" -#include "ngraph/node.hpp" -#include "ngraph/op/pad.hpp" -#include "ngraph/op/slice.hpp" +#include "exceptions.hpp" +#include "op/pad.hpp" +#include "openvino/op/squeeze.hpp" + +using namespace ov::op; namespace ngraph { namespace onnx_import { namespace op { +namespace custom { namespace set_1 { -OutputVector pad(const Node& node) { - auto inputs = node.get_ng_inputs(); - auto data = inputs.at(0); - auto pads = inputs.at(1); - - std::string pad_mode = node.get_attribute_value("mode", "constant"); - ov::op::PadMode mode; - ov::Output pad_value; - if (pad_mode == "constant") { - mode = ov::op::PadMode::CONSTANT; - if (inputs.size() > 2) { - pad_value = inputs.at(2); - } else { - pad_value = default_opset::Constant::create(data.get_element_type(), Shape{}, {0}); - } - } else if (pad_mode == "edge") { - mode = ov::op::PadMode::EDGE; - } else if (pad_mode == "reflect") { - mode = ov::op::PadMode::REFLECT; - } else { - throw ngraph_error("Unsupported pad_mode in ONNX com.microsoft.Pad operator"); - } - - auto pads_shape = pads.get_shape(); - if (!((pads_shape.size() == 1 && pads_shape[0] == 2 * data.get_shape().size()) || - (pads_shape.size() == 2 && pads_shape[0] == 1 && pads_shape[1] == 2 * data.get_shape().size()))) { - throw ngraph_error("Invalid pads tensor shape in ONNX Pad operator"); - } - - auto pads_begin = std::make_shared( - pads, - default_opset::Constant::create(element::i64, Shape{}, {0}), - default_opset::Constant::create(element::i64, Shape{}, {pads_shape[0] / 2}), - default_opset::Constant::create(element::i64, Shape{}, {1})); - - auto pads_end = std::make_shared( - pads, - default_opset::Constant::create(element::i64, Shape{}, {pads_shape[0] / 2}), - default_opset::Constant::create(element::i64, Shape{}, {pads_shape[0]}), - default_opset::Constant::create(element::i64, Shape{}, {1})); - - if (pad_mode == "constant") { - return {std::make_shared(data, pads_begin, pads_end, pad_value, mode)}; - } else { - return {std::make_shared(data, pads_begin, pads_end, mode)}; - } +ov::OutputVector pad(const Node& node) { + // auto inputs = node.get_ng_inputs(); + // std::string pad_mode = node.get_attribute_value("mode", "constant"); + // node.get_ng_inputs()[1] = std::make_shared(node.get_ng_inputs()[1]); + // auto result = set_11::pad(node).at(0); + auto node_1 = node; + node_1.get_ng_inputs()[1] = std::make_shared(node.get_ng_inputs()[1]); + auto result = set_11::pad(node_1).at(0); + + return {result}; } } // namespace set_1 +} // namespace custom } // namespace op } // namespace onnx_import } // namespace ngraph diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/pad.hpp b/src/frontends/onnx/frontend/src/op/com.microsoft/pad.hpp index 633738f6918128..a65a9dd5156b09 100644 --- a/src/frontends/onnx/frontend/src/op/com.microsoft/pad.hpp +++ b/src/frontends/onnx/frontend/src/op/com.microsoft/pad.hpp @@ -12,9 +12,11 @@ OPENVINO_SUPPRESS_DEPRECATED_START namespace ngraph { namespace onnx_import { namespace op { +namespace custom { namespace set_1 { -OutputVector pad(const Node& node); +ov::OutputVector pad(const Node& node); } // namespace set_1 +} // namespace custom } // namespace op } // namespace onnx_import } // namespace ngraph diff --git a/src/frontends/onnx/frontend/src/ops_bridge.cpp b/src/frontends/onnx/frontend/src/ops_bridge.cpp index d55929a4510a1a..ee9c1939bf0ad0 100644 --- a/src/frontends/onnx/frontend/src/ops_bridge.cpp +++ b/src/frontends/onnx/frontend/src/ops_bridge.cpp @@ -42,6 +42,7 @@ #include "op/com.microsoft/embed_layer_normalization.hpp" #include "op/com.microsoft/fused_conv.hpp" #include "op/com.microsoft/fusedgemm.hpp" +#include "op/com.microsoft/pad.hpp" #include "op/com.microsoft/skip_layer_normalization.hpp" #include "op/compress.hpp" #include "op/concat.hpp" @@ -585,7 +586,6 @@ OperatorsBridge::OperatorsBridge() { REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "FusedConv", 1, fused_conv); REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "FusedGemm", 1, fusedgemm); REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "EmbedLayerNormalization", 1, embed_layer_normalization); - REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "Pad", 1, pad); REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "SkipLayerNormalization", 1, skip_layer_normalization); REGISTER_OPERATOR_WITH_DOMAIN(MICROSOFT_DOMAIN, "Trilu", 1, trilu); @@ -594,6 +594,7 @@ OperatorsBridge::OperatorsBridge() { op::set_13::dequantize_linear, "com.microsoft"); register_operator_in_custom_domain("Gelu", VersionRange::since(1), op::set_1::gelu, "com.microsoft"); + register_operator_in_custom_domain("Pad", VersionRange::since(1), op::custom::set_1::pad, "com.microsoft"); register_operator_in_custom_domain("QuantizeLinear", VersionRange::since(1), op::set_13::quantize_linear, diff --git a/src/frontends/onnx/tests/models/com.microsoft/pad.prototxt b/src/frontends/onnx/tests/models/com.microsoft/pad.prototxt new file mode 100644 index 00000000000000..f01a6ca2c42f0d --- /dev/null +++ b/src/frontends/onnx/tests/models/com.microsoft/pad.prototxt @@ -0,0 +1,61 @@ +ir_version: 3 +producer_name: "nGraph ONNX Importer" +graph { + node { + input: "X" + input: "Y" + output: "out" + name: "Gelu_AddBias_1" + op_type: "BiasGelu" + domain: "com.microsoft" + } + name: "test_graph" + input { + name: "X" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 5 + } + } + } + } + } + input { + name: "Y" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 5 + } + } + } + } + } + output { + name: "out" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 5 + } + } + } + } + } +} +opset_import { + version: 13 +} diff --git a/src/frontends/onnx/tests/models/com.microsoft/pad_test.prototxt b/src/frontends/onnx/tests/models/com.microsoft/pad_test.prototxt new file mode 100644 index 00000000000000..58a4e8e139cc18 --- /dev/null +++ b/src/frontends/onnx/tests/models/com.microsoft/pad_test.prototxt @@ -0,0 +1,62 @@ +ir_version: 6 +producer_name: "OV ONNX Frontend" +graph { + node { + input: "x" + input: "pads" + output: "y" + op_type: "Pad" + domain: "com.microsoft" + attribute { + name: "mode" + s: "constant" + type: STRING + } + } + name: "test_constant_pad_microsoft" + initializer { + dims: 4 + data_type: 7 + int64_data: 0 + int64_data: 2 + int64_data: 0 + int64_data: 0 + name: "pads" + } + input { + name: "x" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 3 + } + dim { + dim_value: 2 + } + } + } + } + } + output { + name: "y" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 3 + } + dim { + dim_value: 4 + } + } + } + } + } +} +opset_import { + domain: "com.microsoft" + version: 1 +} diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp index 94e5ec9d596fef..cfe21a4d0ca738 100644 --- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp +++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp @@ -1262,3 +1262,14 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_trilu_lower) { // clang-format on } + +OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_pad) { + const auto model = convert_model("com.microsoft/pad_test.onnx"); + auto test_case = ov::test::TestCase(model, s_device); + + test_case.add_input({1.f, 1.2f, 2.3f, 3.4f, 4.5f, 5.7f}); + test_case.add_expected_output(Shape{3, 4}, + {0.f, 0.f, 1.f, 1.2f, 0.f, 0.f, 2.3f, 3.4f, 0.f, 0.f, 4.5f, 5.7f}); + + test_case.run(); +}