Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhant-0707 committed Mar 29, 2024
1 parent c63e240 commit 6cee0c1
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 52 deletions.
67 changes: 17 additions & 50 deletions src/frontends/onnx/frontend/src/op/com.microsoft/pad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("mode", "constant");
ov::op::PadMode mode;
ov::Output<ov::Node> 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<default_opset::Slice>(
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<default_opset::Slice>(
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<default_opset::Pad>(data, pads_begin, pads_end, pad_value, mode)};
} else {
return {std::make_shared<default_opset::Pad>(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<std::string>("mode", "constant");
// node.get_ng_inputs()[1] = std::make_shared<v0::Squeeze>(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<v0::Squeeze>(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
4 changes: 3 additions & 1 deletion src/frontends/onnx/frontend/src/op/com.microsoft/pad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/frontends/onnx/frontend/src/ops_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions src/frontends/onnx/tests/models/com.microsoft/pad.prototxt
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions src/frontends/onnx/tests/models/com.microsoft/pad_test.prototxt
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>({1.f, 1.2f, 2.3f, 3.4f, 4.5f, 5.7f});
test_case.add_expected_output<float>(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();
}

0 comments on commit 6cee0c1

Please sign in to comment.