From 719797326b5dd39f34e982f07e99f4c35e35f7e4 Mon Sep 17 00:00:00 2001 From: Liubov Batanina Date: Mon, 24 Aug 2020 14:16:29 +0300 Subject: [PATCH] Added test for Pad (#1902) * Addded test for Pad * Add builder --- .../single_layer_tests/pad.cpp | 97 +++++++++++++++++++ .../shared/include/single_layer_tests/pad.hpp | 36 +++++++ .../shared/src/single_layer_tests/pad.cpp | 56 +++++++++++ .../include/ngraph_functions/builders.hpp | 6 ++ .../ngraph_functions/utils/ngraph_helpers.hpp | 8 ++ .../tests/ngraph_functions/src/pad.cpp | 43 ++++++++ .../src/utils/ngraph_helpers.cpp | 18 ++++ 7 files changed, 264 insertions(+) create mode 100644 inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/pad.cpp create mode 100644 inference-engine/tests/functional/plugin/shared/include/single_layer_tests/pad.hpp create mode 100644 inference-engine/tests/functional/plugin/shared/src/single_layer_tests/pad.cpp create mode 100644 inference-engine/tests/ngraph_functions/src/pad.cpp diff --git a/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/pad.cpp b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/pad.cpp new file mode 100644 index 00000000000..fb975e808c6 --- /dev/null +++ b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/pad.cpp @@ -0,0 +1,97 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "single_layer_tests/pad.hpp" + +using namespace LayerTestsDefinitions; + +namespace { +const std::vector netPrecisions = { + InferenceEngine::Precision::FP32, + InferenceEngine::Precision::FP16 +}; + +const std::vector> padsBegin2D = {{0, 0}, {1, 1}, {2, 0}, {0, 3}}; +const std::vector> padsEnd2D = {{0, 0}, {1, 1}, {0, 1}, {3, 2}}; +const std::vector argPadValue = {0.f, 1.f, 2.f, -1.f}; + +const std::vector padMode = { + ngraph::helpers::PadMode::EDGE, + ngraph::helpers::PadMode::REFLECT, +}; + +const auto pad2DConstparams = testing::Combine( + testing::ValuesIn(padsBegin2D), + testing::ValuesIn(padsEnd2D), + testing::ValuesIn(argPadValue), + testing::Values(ngraph::helpers::PadMode::CONSTANT), + testing::ValuesIn(netPrecisions), + testing::Values(std::vector{13, 5}), + testing::Values(CommonTestUtils::DEVICE_CPU) +); + +INSTANTIATE_TEST_CASE_P( + Pad2DConst, + PadLayerTest, + pad2DConstparams, + PadLayerTest::getTestCaseName +); + +const auto pad2Dparams = testing::Combine( + testing::ValuesIn(padsBegin2D), + testing::ValuesIn(padsEnd2D), + testing::Values(0), + testing::ValuesIn(padMode), + testing::ValuesIn(netPrecisions), + testing::Values(std::vector{13, 5}), + testing::Values(CommonTestUtils::DEVICE_CPU) +); + +INSTANTIATE_TEST_CASE_P( + Pad2D, + PadLayerTest, + pad2Dparams, + PadLayerTest::getTestCaseName +); + +const std::vector> padsBegin4D = {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 0, 1, 0}, {0, 3, 0, 1}}; +const std::vector> padsEnd4D = {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 0, 0, 1}, {1, 3, 2, 0}}; + +const auto pad4DConstparams = testing::Combine( + testing::ValuesIn(padsBegin4D), + testing::ValuesIn(padsEnd4D), + testing::ValuesIn(argPadValue), + testing::Values(ngraph::helpers::PadMode::CONSTANT), + testing::ValuesIn(netPrecisions), + testing::Values(std::vector{3, 5, 10, 11}), + testing::Values(CommonTestUtils::DEVICE_CPU) +); + +INSTANTIATE_TEST_CASE_P( + Pad4DConst, + PadLayerTest, + pad4DConstparams, + PadLayerTest::getTestCaseName +); + +const auto pad4Dparams = testing::Combine( + testing::ValuesIn(padsBegin4D), + testing::ValuesIn(padsEnd4D), + testing::Values(0), + testing::ValuesIn(padMode), + testing::ValuesIn(netPrecisions), + testing::Values(std::vector{3, 5, 10, 11}), + testing::Values(CommonTestUtils::DEVICE_CPU) +); + +INSTANTIATE_TEST_CASE_P( + Pad4D, + PadLayerTest, + pad4Dparams, + PadLayerTest::getTestCaseName +); + +} // namespace diff --git a/inference-engine/tests/functional/plugin/shared/include/single_layer_tests/pad.hpp b/inference-engine/tests/functional/plugin/shared/include/single_layer_tests/pad.hpp new file mode 100644 index 00000000000..117392660b0 --- /dev/null +++ b/inference-engine/tests/functional/plugin/shared/include/single_layer_tests/pad.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include +#include +#include + +#include "functional_test_utils/layer_test_utils.hpp" +#include "ngraph_functions/builders.hpp" + +typedef std::tuple< + InferenceEngine::SizeVector, // padsBegin + InferenceEngine::SizeVector, // padsEnd + float, // argPadValue + ngraph::helpers::PadMode, // padMode + InferenceEngine::Precision, // Net precision + InferenceEngine::SizeVector, // Input shapes + LayerTestsUtils::TargetDevice // Target device name +> padLayerTestParamsSet; + +namespace LayerTestsDefinitions { + +class PadLayerTest : public testing::WithParamInterface, + public LayerTestsUtils::LayerTestsCommon { +public: + static std::string getTestCaseName(testing::TestParamInfo obj); + +protected: + void SetUp() override; +}; + +} // namespace LayerTestsDefinitions \ No newline at end of file diff --git a/inference-engine/tests/functional/plugin/shared/src/single_layer_tests/pad.cpp b/inference-engine/tests/functional/plugin/shared/src/single_layer_tests/pad.cpp new file mode 100644 index 00000000000..96855904e93 --- /dev/null +++ b/inference-engine/tests/functional/plugin/shared/src/single_layer_tests/pad.cpp @@ -0,0 +1,56 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include +#include +#include +#include + +#include "single_layer_tests/pad.hpp" + + +namespace LayerTestsDefinitions { + +std::string PadLayerTest::getTestCaseName(testing::TestParamInfo obj) { + InferenceEngine::Precision netPrecision; + InferenceEngine::SizeVector inputShapes, padsBegin, padsEnd; + ngraph::helpers::PadMode padMode; + float argPadValue; + std::string targetDevice; + std::tie(padsBegin, padsEnd, argPadValue, padMode, netPrecision, inputShapes, targetDevice) = obj.param; + + std::ostringstream result; + result << "IS=" << CommonTestUtils::vec2str(inputShapes) << "_"; + result << "padsBegin=" << CommonTestUtils::vec2str(padsBegin) << "_"; + result << "padsEnd=" << CommonTestUtils::vec2str(padsEnd) << "_"; + if (padMode == ngraph::helpers::PadMode::CONSTANT) { + result << "Value=" << argPadValue << "_"; + } + result << "PadMode=" << padMode << "_"; + result << "netPRC=" << netPrecision.name() << "_"; + result << "targetDevice=" << targetDevice; + return result.str(); +} + +void PadLayerTest::SetUp() { + InferenceEngine::SizeVector inputShape, padsBegin, padsEnd; + float argPadValue; + ngraph::helpers::PadMode padMode; + InferenceEngine::Precision netPrecision; + std::tie(padsBegin, padsEnd, argPadValue, padMode, netPrecision, inputShape, targetDevice) = this->GetParam(); + auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision); + auto params = ngraph::builder::makeParams(ngPrc, {inputShape}); + auto paramOuts = ngraph::helpers::convert2OutputVector( + ngraph::helpers::castOps2Nodes(params)); + auto pad = ngraph::builder::makePad(paramOuts[0], padsBegin, padsEnd, argPadValue, padMode); + ngraph::ResultVector results{std::make_shared(pad)}; + function = std::make_shared(results, params, "pad"); +} + +TEST_P(PadLayerTest, CompareWithRefs) { + Run(); +} + +} // namespace LayerTestsDefinitions \ No newline at end of file diff --git a/inference-engine/tests/ngraph_functions/include/ngraph_functions/builders.hpp b/inference-engine/tests/ngraph_functions/include/ngraph_functions/builders.hpp index 4f6bcdf8981..142280e0e6e 100644 --- a/inference-engine/tests/ngraph_functions/include/ngraph_functions/builders.hpp +++ b/inference-engine/tests/ngraph_functions/include/ngraph_functions/builders.hpp @@ -328,5 +328,11 @@ std::shared_ptr makeFullyConnected(const ngraph::Output& in, std::shared_ptr makeConcat(const std::vector>& in, const int& axis); +std::shared_ptr makePad(const ngraph::Output& data, + const std::vector& padsBegin, + const std::vector& padsEnd, + float argPadValue, + ngraph::helpers::PadMode padMode); + } // namespace builder } // namespace ngraph diff --git a/inference-engine/tests/ngraph_functions/include/ngraph_functions/utils/ngraph_helpers.hpp b/inference-engine/tests/ngraph_functions/include/ngraph_functions/utils/ngraph_helpers.hpp index aa8c56fd724..11baba4ab60 100644 --- a/inference-engine/tests/ngraph_functions/include/ngraph_functions/utils/ngraph_helpers.hpp +++ b/inference-engine/tests/ngraph_functions/include/ngraph_functions/utils/ngraph_helpers.hpp @@ -165,7 +165,15 @@ enum class InputLayerType { PARAMETER, }; +enum class PadMode { + CONSTANT, + EDGE, + REFLECT, + SYMMETRIC, +}; + std::ostream &operator<<(std::ostream &os, const ReductionType &m); +std::ostream &operator<<(std::ostream &os, const PadMode &m); inline std::string quantizationGranularityToString(const QuantizationGranularity &granularity) { static std::map names = { diff --git a/inference-engine/tests/ngraph_functions/src/pad.cpp b/inference-engine/tests/ngraph_functions/src/pad.cpp new file mode 100644 index 00000000000..3ea39c58d02 --- /dev/null +++ b/inference-engine/tests/ngraph_functions/src/pad.cpp @@ -0,0 +1,43 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include + +#include "ngraph_functions/builders.hpp" + +namespace ngraph { +namespace builder { +std::shared_ptr makePad(const ngraph::Output& data, + const std::vector& padsBegin, + const std::vector& padsEnd, + float argPadValue, + ngraph::helpers::PadMode padMode) { + ngraph::op::PadMode pad_mode; + switch (padMode) { + case ngraph::helpers::PadMode::CONSTANT: + pad_mode = ngraph::op::PadMode::CONSTANT; + break; + case ngraph::helpers::PadMode::EDGE: + pad_mode = ngraph::op::PadMode::EDGE; + break; + case ngraph::helpers::PadMode::REFLECT: + pad_mode = ngraph::op::PadMode::REFLECT; + break; + case ngraph::helpers::PadMode::SYMMETRIC: + pad_mode = ngraph::op::PadMode::SYMMETRIC; + break; + default: + throw std::runtime_error("Can't create layer for this pad mode"); + } + + auto pads_begin = std::make_shared(ngraph::element::i64, + ngraph::Shape{padsBegin.size()}, padsBegin.data()); + auto pads_end = std::make_shared(ngraph::element::i64, + ngraph::Shape{padsEnd.size()}, padsEnd.data()); + auto arg_pad_value = std::make_shared(data.get_element_type(), ngraph::Shape{}, &argPadValue); + return std::make_shared(data, pads_begin, pads_end, arg_pad_value, pad_mode); +} +} // namespace builder +} // namespace ngraph diff --git a/inference-engine/tests/ngraph_functions/src/utils/ngraph_helpers.cpp b/inference-engine/tests/ngraph_functions/src/utils/ngraph_helpers.cpp index 2c282218f13..7b7db85a411 100644 --- a/inference-engine/tests/ngraph_functions/src/utils/ngraph_helpers.cpp +++ b/inference-engine/tests/ngraph_functions/src/utils/ngraph_helpers.cpp @@ -47,6 +47,24 @@ std::ostream &operator<<(std::ostream &os, const ReductionType &m) { return os; } +std::ostream &operator<<(std::ostream &os, const PadMode &m) { + switch (m) { + case PadMode::CONSTANT: + os << "CONSTANT"; + break; + case PadMode::EDGE: + os << "EDGE"; + break; + case PadMode::REFLECT: + os << "REFLECT"; + break; + case PadMode::SYMMETRIC: + os << "SYMMETRIC"; + break; + } + return os; +} + OutputVector convert2OutputVector(const std::vector> &nodes) { OutputVector outs; std::for_each(nodes.begin(), nodes.end(), [&outs](const std::shared_ptr &n) {