Skip to content

Commit

Permalink
Support NonConst pads_begin and pads_end in Pad op (openvinotoolkit#8697
Browse files Browse the repository at this point in the history
)

* Support pads_begin & pads_end as dynamic ops in Pad

* Extend Pad template test w/ NonConst PB & PE cases

* Remove xfails for 69443 after issue was fixed
  • Loading branch information
vurusovs authored Nov 23, 2021
1 parent 2518830 commit 3b88682
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 18 deletions.
67 changes: 67 additions & 0 deletions docs/template_plugin/tests/functional/op_reference/pad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,64 @@ TEST_P(ReferencePadTestParamsOk, CompareWithRefs) {
EXPECT_NO_THROW(Exec());
}

class ReferencePadTestNonConstPadsBeginPadsEnd : public ReferencePadTest {
public:
void SetUp() override {
SKIP_IF_CURRENT_TEST_IS_DISABLED();
auto params = GetParam();
function = CreateFunction(params);
inputData = {params.inputData.data, params.padsBegin.data, params.padsEnd.data};
refOutData = {params.expectedOutput.data};
}

private:
static std::shared_ptr<Function> CreateFunction(const PadParams& params) {
const auto data = std::make_shared<op::v0::Parameter>(params.inputData.type,
params.inputData.shape);
const auto padsBegin = std::make_shared<op::v0::Parameter>(params.padsBegin.type,
params.padsBegin.shape);
const auto padsEnd = std::make_shared<op::v0::Parameter>(params.padsEnd.type,
params.padsEnd.shape);
const auto f = [&] {
if (params.useConstValue) {
// pad_value should be used only in CONSTANT mode
const auto padVal = op::v0::Constant::create(params.constantValue.type,
params.constantValue.shape,
params.constantValue.data.data());
return std::make_shared<Function>(std::make_shared<op::v1::Pad>(data,
padsBegin,
padsEnd,
padVal,
params.padMode),
ParameterVector{data, padsBegin, padsEnd});
}

return std::make_shared<Function>(std::make_shared<op::v1::Pad>(data,
padsBegin,
padsEnd,
params.padMode),
ParameterVector{data, padsBegin, padsEnd});
}();
return f;
}
};

TEST_P(ReferencePadTestNonConstPadsBeginPadsEnd, CompareWithRefs) {
Exec();
}

class ReferencePadTestNonConstPadsBeginPadsEndTooLarge : public ReferencePadTestNonConstPadsBeginPadsEnd {};

TEST_P(ReferencePadTestNonConstPadsBeginPadsEndTooLarge, CompareWithRefs) {
EXPECT_ANY_THROW(Exec());
}

class ReferencePadTestNonConstPadsBeginPadsEndParamsOk : public ReferencePadTestNonConstPadsBeginPadsEnd {};

TEST_P(ReferencePadTestNonConstPadsBeginPadsEndParamsOk, CompareWithRefs) {
EXPECT_NO_THROW(Exec());
}

template <element::Type_t ET, element::Type_t ET_INT>
std::vector<PadParams> generateParams() {
using T = typename element_type_traits<ET>::value_type;
Expand Down Expand Up @@ -1005,6 +1063,9 @@ std::vector<PadParams> generateCombinedParams() {
INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTest,
testing::ValuesIn(generateCombinedParams()), ReferencePadTest::getTestCaseName);

INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTestNonConstPadsBeginPadsEnd,
testing::ValuesIn(generateCombinedParams()), ReferencePadTest::getTestCaseName);

template <element::Type_t ET, element::Type_t ET_INT>
std::vector<PadParams> generateParamsTooLarge() {
using T = typename element_type_traits<ET>::value_type;
Expand Down Expand Up @@ -1052,6 +1113,9 @@ std::vector<PadParams> generateCombinedParamsTooLarge() {
INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTestParamsTooLarge,
testing::ValuesIn(generateCombinedParamsTooLarge()), ReferencePadTest::getTestCaseName);

INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTestNonConstPadsBeginPadsEndTooLarge,
testing::ValuesIn(generateCombinedParamsTooLarge()), ReferencePadTest::getTestCaseName);

template <element::Type_t ET, element::Type_t ET_INT>
std::vector<PadParams> generateParamsOk() {
using T = typename element_type_traits<ET>::value_type;
Expand Down Expand Up @@ -1098,4 +1162,7 @@ std::vector<PadParams> generateCombinedParamsOk() {

INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTestParamsOk,
testing::ValuesIn(generateCombinedParamsOk()), ReferencePadTest::getTestCaseName);

INSTANTIATE_TEST_SUITE_P(smoke_Pad_With_Hardcoded_Refs, ReferencePadTestNonConstPadsBeginPadsEndParamsOk,
testing::ValuesIn(generateCombinedParamsOk()), ReferencePadTest::getTestCaseName);
} // namespace
21 changes: 19 additions & 2 deletions ngraph/core/src/op/pad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,33 @@ bool op::v1::Pad::evaluate_pad(const HostTensorVector& outputs, const HostTensor
} else {
pad_value = pad_zero_value.data();
}

// compute pads_begin and pads_end CoordinateDiffs from pads_begin
// and pads_end shapes and reshape output to determine shape
// (in case pads_begin and pads_end are Parameters, output is dynamic with static rank).

op::v0::Constant pads_begin_const(inputs[1]);
CoordinateDiff pads_begin_coord(pads_begin_const.cast_vector<ptrdiff_t>());
op::v0::Constant pads_end_const(inputs[2]);
CoordinateDiff pads_end_coord(pads_end_const.cast_vector<ptrdiff_t>());

auto data_shape = data->get_shape();
ov::Shape padded_shape(data_shape.size());
for (size_t i = 0; i < data_shape.size(); ++i) {
padded_shape[i] = data_shape[i] + pads_begin_coord[i] + pads_end_coord[i];
}

const auto& out = outputs[0];
out->set_shape(padded_shape);

ngraph::runtime::reference::pad(data->get_data_ptr<char>(),
pad_value,
out->get_data_ptr<char>(),
elem_size,
data->get_shape(),
out->get_shape(),
get_pads_begin(),
get_pads_end(),
pads_begin_coord,
pads_end_coord,
get_pad_mode());

return true;
Expand Down
1 change: 0 additions & 1 deletion runtime/bindings/python/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def xfail_test(reason="Mark the test as expected to fail", strict=True):
xfail_accuracy = xfail_test(reason="Accuracy")
xfail_issue_FLOAT_LIKE = xfail_test(reason="Use of bfloat16 or float16")
xfail_issue_69444 = xfail_test(reason="failed with accuracy issue")
xfail_issue_69443 = xfail_test(reason="Error in ref. implementation due to the empty pads_begin, pads_end")
skip_issue_67415 = pytest.mark.skip(reason="RuntimeError: Unsupported data type for when filling blob!")
xfail_issue_67415 = xfail_test(reason="RuntimeError: Unsupported data type for when filling blob!")
xfail_issue_33488 = xfail_test(reason="RuntimeError: nGraph does not support the following ONNX operations: "
Expand Down
7 changes: 0 additions & 7 deletions runtime/bindings/python/tests/test_onnx/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
xfail_issue_63136,
xfail_issue_63137,
xfail_issue_63138,
xfail_issue_69443,
xfail_issue_69444,
)
from tests.test_onnx.utils.onnx_backend import OpenVinoTestBackend
Expand Down Expand Up @@ -126,12 +125,6 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_lstm_with_initial_bias_cpu",
"OnnxBackendNodeModelTest.test_lstm_with_peepholes_cpu",
),
(
xfail_issue_69443,
"OnnxBackendNodeModelTest.test_constant_pad_cpu",
"OnnxBackendNodeModelTest.test_edge_pad_cpu",
"OnnxBackendNodeModelTest.test_reflect_pad_cpu",
),
(
xfail_issue_39658,
"OnnxBackendNodeModelTest.test_tile_cpu",
Expand Down
1 change: 0 additions & 1 deletion runtime/bindings/python/tests_compatibility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def xfail_test(reason="Mark the test as expected to fail", strict=True):
skip_segfault = pytest.mark.skip(reason="Segmentation fault error")
xfail_accuracy = xfail_test(reason="Accuracy")
xfail_issue_69444 = xfail_test(reason="failed with accuracy issue")
xfail_issue_69443 = xfail_test(reason="Segmentation fault due to empty pads_begin, pads_end")
xfail_issue_67415 = xfail_test(reason="RuntimeError: Unsupported data type for when filling blob!")
xfail_issue_33488 = xfail_test(reason="RuntimeError: nGraph does not support the following ONNX operations: "
"MaxUnpool")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
xfail_issue_63136,
xfail_issue_63137,
xfail_issue_63138,
xfail_issue_69443,
xfail_issue_69444,
)
from tests_compatibility.test_onnx.utils.onnx_backend import OpenVinoTestBackend
Expand Down Expand Up @@ -111,12 +110,6 @@ def expect_fail(test_case_path, xfail): # type: (str) -> None
"OnnxBackendNodeModelTest.test_lstm_with_initial_bias_cpu",
"OnnxBackendNodeModelTest.test_lstm_with_peepholes_cpu",
),
(
xfail_issue_69443,
"OnnxBackendNodeModelTest.test_constant_pad_cpu",
"OnnxBackendNodeModelTest.test_edge_pad_cpu",
"OnnxBackendNodeModelTest.test_reflect_pad_cpu",
),
(
xfail_issue_39658,
"OnnxBackendNodeModelTest.test_tile_cpu",
Expand Down

0 comments on commit 3b88682

Please sign in to comment.