From e9777a6da0c60becaa70d684d16ce50ebac577a4 Mon Sep 17 00:00:00 2001 From: David Nam Date: Thu, 21 Oct 2021 15:27:56 +0900 Subject: [PATCH] Migrate range1,4 to template plugin test (#8023) * Migrate range1,4 to template plugin test * Fix cpplint issue * Return NULL in default case in range test * Fix cpplint error --- .../tests/functional/op_reference/range.cpp | 410 ++++++++++++++++++ ngraph/test/CMakeLists.txt | 1 - ngraph/test/backend/range.in.cpp | 148 ------- 3 files changed, 410 insertions(+), 149 deletions(-) create mode 100644 docs/template_plugin/tests/functional/op_reference/range.cpp delete mode 100644 ngraph/test/backend/range.in.cpp diff --git a/docs/template_plugin/tests/functional/op_reference/range.cpp b/docs/template_plugin/tests/functional/op_reference/range.cpp new file mode 100644 index 00000000000..1ab030433f5 --- /dev/null +++ b/docs/template_plugin/tests/functional/op_reference/range.cpp @@ -0,0 +1,410 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include +#include "base_reference_test.hpp" +#include "openvino/op/range.hpp" +#include "openvino/op/constant.hpp" + +using namespace ov; +using namespace reference_tests; + +namespace { + +struct RangeParams { + template + RangeParams(const Shape& iShape, + const Shape& oShape, + const element::Type& iType, + const element::Type& oType, + const element::Type& nodeType, + const std::vector& oValues, + float start, + float stop, + float step) + : inShape(iShape), + outShape(oShape), + inType(iType), + outType(oType), + nodeType(nodeType), + outData(CreateTensor(oType, oValues)), + start(start), + stop(stop), + step(step) {} + + Shape inShape; + Shape outShape; + element::Type inType; + element::Type outType; + element::Type nodeType; + runtime::Tensor inData; + runtime::Tensor outData; + float start; + float stop; + float step; +}; + +static std::shared_ptr CreateConstant(Shape& ishape, element::Type ntype, float input) { + switch (ntype) { + case element::Type_t::f64: + return std::make_shared(ntype, ishape, std::vector{input}); + case element::Type_t::f32: + return std::make_shared(ntype, ishape, std::vector{input}); + case element::Type_t::f16: + return std::make_shared(ntype, ishape, std::vector{input}); + case element::Type_t::bf16: + return std::make_shared(ntype, ishape, std::vector{input}); + case element::Type_t::i64: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::i32: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::i16: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::i8: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::u64: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::u32: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::u16: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + case element::Type_t::u8: + return std::make_shared(ntype, ishape, std::vector{static_cast(input)}); + default: + return NULL; + } +} + +class ReferenceRangeV0LayerTest : public testing::TestWithParam, public CommonReferenceTest { +public: + void SetUp() override { + auto params = GetParam(); + function = CreateFunction(params.inShape, + params.outShape, + params.inType, + params.outType, + params.nodeType, + params.start, + params.stop, + params.step); + refOutData = {params.outData}; + } + + static std::string getTestCaseName(const testing::TestParamInfo& obj) { + auto param = obj.param; + std::ostringstream result; + result << "iShape=" << param.inShape << "_"; + result << "oShape=" << param.outShape << "_"; + result << "iType=" << param.inType << "_"; + result << "oType=" << param.outType << "_"; + result << "nType=" << param.nodeType; + return result.str(); + } + +private: + static std::shared_ptr CreateFunction(Shape& ishape, + Shape& oshape, + element::Type& itype, + element::Type& otype, + element::Type& ntype, + float fstart, + float fstop, + float fstep) { + auto start = CreateConstant(ishape, ntype, fstart); + auto stop = CreateConstant(ishape, ntype, fstop); + auto step = CreateConstant(ishape, ntype, fstep); + auto range = std::make_shared(start, stop, step); + return std::make_shared(NodeVector{range}, ParameterVector{}); + } +}; + +class ReferenceRangeV4LayerTest : public testing::TestWithParam, public CommonReferenceTest { +public: + void SetUp() override { + auto params = GetParam(); + function = CreateFunction(params.inShape, + params.outShape, + params.inType, + params.outType, + params.nodeType, + params.start, + params.stop, + params.step); + refOutData = {params.outData}; + } + + static std::string getTestCaseName(const testing::TestParamInfo& obj) { + auto param = obj.param; + std::ostringstream result; + result << "iShape=" << param.inShape << "_"; + result << "oShape=" << param.outShape << "_"; + result << "iType=" << param.inType << "_"; + result << "oType=" << param.outType << "_"; + result << "nType=" << param.nodeType; + return result.str(); + } + +private: + static std::shared_ptr CreateFunction(Shape& ishape, + Shape& oshape, + element::Type& itype, + element::Type& otype, + element::Type& ntype, + float fstart, + float fstop, + float fstep) { + auto start = CreateConstant(ishape, ntype, fstart); + auto stop = CreateConstant(ishape, ntype, fstop); + auto step = CreateConstant(ishape, ntype, fstep); + auto range = std::make_shared(start, stop, step, otype); + return std::make_shared(NodeVector{range}, ParameterVector{}); + } +}; + +TEST_P(ReferenceRangeV0LayerTest, RangeWithHardcodedRefs) { + Exec(); +} + +TEST_P(ReferenceRangeV4LayerTest, RangeWithHardcodedRefs) { + Exec(); +} + +template +std::vector generateParamsForRangeV0Int() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams( + ov::Shape{}, + ov::Shape{4}, + IN_ET, + IN_ET, + IN_ET, + std::vector{-5, -2, 1, 4}, + -5, + 6, + 3), + RangeParams( + ov::Shape{}, + ov::Shape{2}, + IN_ET, + IN_ET, + IN_ET, + std::vector{10, 7}, + 10, + 5, + -3)}; + return params; +} + +template +std::vector generateParamsForRangeV0UnsignedInt() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams( + ov::Shape{}, + ov::Shape{10}, + IN_ET, + IN_ET, + IN_ET, + std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + 0, + 10, + 1) + }; + return params; +} + +template +std::vector generateParamsForRangeV0Float() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams(ov::Shape{}, + ov::Shape{4}, + IN_ET, + IN_ET, + IN_ET, + std::vector{0.0f, 0.25f, 0.5f, 0.75}, + 0.0f, + 1.0f, + 0.25f), + RangeParams(ov::Shape{}, + ov::Shape{10}, + IN_ET, + IN_ET, + IN_ET, + std::vector{-1.0f, -0.8f, -0.6f, -0.4f, -0.2f, 0.0f, 0.2f, 0.4f, 0.6f, 0.8f}, + -1.0f, + 0.875f, + 0.2f), + RangeParams(ov::Shape{}, + ov::Shape{8}, + IN_ET, + IN_ET, + IN_ET, + std::vector{2.0f, 1.75f, 1.5f, 1.25f, 1.0f, 0.75f, 0.5f, 0.25}, + 2.0f, + 0.0f, + -0.25f)}; + return params; +} + +template +std::vector generateParamsForRangeV4Int() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams( + ov::Shape{}, + ov::Shape{4}, + IN_ET, + IN_ET, + IN_ET, + std::vector{-5, -2, 1, 4}, + -5, + 6, + 3), + RangeParams( + ov::Shape{}, + ov::Shape{2}, + IN_ET, + IN_ET, + IN_ET, + std::vector{10, 7}, + 10, + 5, + -3) + }; + + return params; +} + +template +std::vector generateParamsForRangeV4UnsignedInt() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams( + ov::Shape{}, + ov::Shape{10}, + IN_ET, + IN_ET, + element::Type_t::f32, + std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + 1.2f, + 11.3f, + 1.6f), + RangeParams( + ov::Shape{}, + ov::Shape{10}, + IN_ET, + IN_ET, + IN_ET, + std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + 0, + 10, + 1) + }; + + return params; +} + +template +std::vector generateParamsForRangeV4Float() { + using T = typename element_type_traits::value_type; + + std::vector params{ + RangeParams(ov::Shape{}, + ov::Shape{4}, + IN_ET, + IN_ET, + IN_ET, + std::vector{0.0f, 0.25f, 0.5f, 0.75f}, + 0.0f, + 1.0f, + 0.25f), + RangeParams(ov::Shape{}, + ov::Shape{10}, + IN_ET, + IN_ET, + IN_ET, + std::vector{-1.0f, -0.8f, -0.6f, -0.4f, -0.2f, 0.0f, 0.2f, 0.4f, 0.6f, 0.8f}, + -1.0f, + 0.875f, + 0.2f), + RangeParams(ov::Shape{}, + ov::Shape{8}, + IN_ET, + IN_ET, + IN_ET, + std::vector{2.0f, 1.75f, 1.5f, 1.25f, 1.0f, 0.75f, 0.5f, 0.25f}, + 2, + 0, + -0.25) + }; + + return params; +} + +std::vector generateCombinedParamsForRangeV0() { + const std::vector> allTypeParams{ + generateParamsForRangeV0Float(), + generateParamsForRangeV0Float(), + generateParamsForRangeV0Float(), + generateParamsForRangeV0Int(), + generateParamsForRangeV0Int(), + generateParamsForRangeV0Int(), + generateParamsForRangeV0Int(), + generateParamsForRangeV0UnsignedInt(), + generateParamsForRangeV0UnsignedInt(), + generateParamsForRangeV0UnsignedInt(), + generateParamsForRangeV0UnsignedInt(), + }; + + std::vector combinedParams; + + for (const auto& params : allTypeParams) { + combinedParams.insert(combinedParams.end(), params.begin(), params.end()); + } + + return combinedParams; +} + +std::vector generateCombinedParamsForRangeV4() { + const std::vector> allTypeParams{ + generateParamsForRangeV4Float(), + generateParamsForRangeV4Float(), + generateParamsForRangeV4Float(), + generateParamsForRangeV4Int(), + generateParamsForRangeV4Int(), + generateParamsForRangeV4Int(), + generateParamsForRangeV4UnsignedInt(), + generateParamsForRangeV4UnsignedInt(), + generateParamsForRangeV4UnsignedInt(), + }; + + std::vector combinedParams; + + for (const auto& params : allTypeParams) { + combinedParams.insert(combinedParams.end(), params.begin(), params.end()); + } + + return combinedParams; +} + +INSTANTIATE_TEST_SUITE_P( + smoke_Range_V0_With_Hardcoded_Refs, + ReferenceRangeV0LayerTest, + ::testing::ValuesIn(generateCombinedParamsForRangeV0()), + ReferenceRangeV0LayerTest::getTestCaseName); + +INSTANTIATE_TEST_SUITE_P( + smoke_Range_V4_With_Hardcoded_Refs, + ReferenceRangeV4LayerTest, + ::testing::ValuesIn(generateCombinedParamsForRangeV4()), + ReferenceRangeV4LayerTest::getTestCaseName); + +} // namespace diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 7c73f6447a6..d5ddfc8434b 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -499,7 +499,6 @@ set(MULTI_TEST_SRC backend/prior_box.in.cpp backend/proposal.in.cpp backend/psroi_pooling.in.cpp - backend/range.in.cpp backend/recurrent_cells.in.cpp backend/region_yolo.in.cpp backend/reorg_yolo.in.cpp diff --git a/ngraph/test/backend/range.in.cpp b/ngraph/test/backend/range.in.cpp deleted file mode 100644 index af93b2afee4..00000000000 --- a/ngraph/test/backend/range.in.cpp +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include "engines_util/execute_tools.hpp" -#include "engines_util/test_case.hpp" -#include "engines_util/test_engines.hpp" -#include "gtest/gtest.h" -#include "ngraph/ngraph.hpp" -#include "util/test_control.hpp" - -using namespace std; -using namespace ngraph; -using namespace ngraph::test; - -static string s_manifest = "${MANIFEST}"; -using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME}); - -template -struct RangeTest { - T start; - T stop; - T step; - Shape expected_result_shape; - std::vector expected_result; -}; - -// ------------------------------ V0 ------------------------------ - -// TODO(amprocte): We should test this with more than just int32, but there is a bug in the -// handling of element type-changing that is currently blocking doing that easily. -NGRAPH_TEST(${BACKEND_NAME}, range_v0_int32) { - element::Type_t et = element::i32; - std::vector> int32_tests = { - RangeTest{0, 10, 1, Shape{10}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, - RangeTest{-5, 6, 3, Shape{4}, {-5, -2, 1, 4}}, - RangeTest{10, 5, -3, Shape{2}, {10, 7}}}; - - for (auto& test : int32_tests) { - // Create a graph for f(start,stop,step) = Range(start,stop,step). - auto start = make_shared(et, Shape{}, std::vector{test.start}); - auto stop = make_shared(et, Shape{}, std::vector{test.stop}); - auto step = make_shared(et, Shape{}, std::vector{test.step}); - auto range = make_shared(start, stop, step); - auto pshape_out = range->get_output_partial_shape(0); - ASSERT_TRUE(pshape_out.rank().is_static() && pshape_out.rank() == Dimension{1}); - auto f = make_shared(NodeVector{range}, ParameterVector{}); - - auto test_case = test::TestCase(f); - - test_case.add_expected_output(test.expected_result_shape, test.expected_result); - test_case.run(); - } -} - -NGRAPH_TEST(${BACKEND_NAME}, range_v0_float32) { - element::Type_t et = element::f32; - std::vector> float32_tests = { - RangeTest{0, 1, 0.25, Shape{4}, {0.0f, 0.25f, 0.5f, 0.75f}}, - RangeTest{-1, 0.875, 0.2, Shape{10}, {-1.0f, -0.8f, -0.6f, -0.4f, -0.2f, 0.0f, 0.2f, 0.4f, 0.6f, 0.8f}}, - RangeTest{2, 0, -0.25, Shape{8}, {2.0f, 1.75f, 1.5f, 1.25f, 1.0f, 0.75f, 0.5f, 0.25f}}}; - - for (auto& test : float32_tests) { - // Create a graph for f(start,stop,step) = Range(start,stop,step). - auto start = make_shared(et, Shape{}, std::vector{test.start}); - auto stop = make_shared(et, Shape{}, std::vector{test.stop}); - auto step = make_shared(et, Shape{}, std::vector{test.step}); - auto range = make_shared(start, stop, step); - auto pshape_out = range->get_output_partial_shape(0); - ASSERT_TRUE(pshape_out.rank().is_static() && pshape_out.rank() == Dimension{1}); - auto f = make_shared(NodeVector{range}, ParameterVector{}); - - auto test_case = test::TestCase(f); - - test_case.add_expected_output(test.expected_result_shape, test.expected_result); - test_case.run_with_tolerance_as_fp(1.0e-4f); - } -} - -// ------------------------------ V4 ------------------------------ - -NGRAPH_TEST(${BACKEND_NAME}, range_v4_trunc_inputs) { - auto start = make_shared(element::f32, Shape{}); - auto stop = make_shared(element::f32, Shape{}); - auto step = make_shared(element::f32, Shape{}); - - auto range = make_shared(start, stop, step, element::i32); - auto f = make_shared(range, ParameterVector{start, stop, step}); - - std::vector start_vect{1.2}; - std::vector stop_vect{11.3}; - std::vector step_vect{1.6f}; - - auto test_case = test::TestCase(f); - test_case.add_input(Shape{}, start_vect); - test_case.add_input(Shape{}, stop_vect); - test_case.add_input(Shape{}, step_vect); - test_case.add_expected_output(Shape{10}, std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); - test_case.run(); -} - -NGRAPH_TEST(${BACKEND_NAME}, range_v4_int32) { - element::Type_t et = element::i32; - std::vector> int32_tests = { - RangeTest{0, 10, 1, Shape{10}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, - RangeTest{-5, 6, 3, Shape{4}, {-5, -2, 1, 4}}, - RangeTest{10, 0, 1, Shape{0}, {}}, - RangeTest{10, 5, -3, Shape{2}, {10, 7}}}; - - for (auto& test : int32_tests) { - auto start = make_shared(et, Shape{}, std::vector{test.start}); - auto stop = make_shared(et, Shape{}, std::vector{test.stop}); - auto step = make_shared(et, Shape{}, std::vector{test.step}); - auto range = make_shared(start, stop, step, et); - auto pshape_out = range->get_output_partial_shape(0); - ASSERT_TRUE(pshape_out.rank().is_static() && pshape_out.rank() == Dimension{1}); - auto f = make_shared(NodeVector{range}, ParameterVector{}); - - auto test_case = test::TestCase(f); - - test_case.add_expected_output(test.expected_result_shape, test.expected_result); - test_case.run(); - } -} - -NGRAPH_TEST(${BACKEND_NAME}, range_v4_float32) { - element::Type_t et = element::f32; - std::vector> float32_tests = { - RangeTest{0, 1, 0.25, Shape{4}, {0.0f, 0.25f, 0.5f, 0.75f}}, - RangeTest{-1, 0.875, 0.2, Shape{10}, {-1.0f, -0.8f, -0.6f, -0.4f, -0.2f, 0.0f, 0.2f, 0.4f, 0.6f, 0.8f}}, - RangeTest{10, 0, 1, Shape{0}, {}}, - RangeTest{2, 0, -0.25, Shape{8}, {2.0f, 1.75f, 1.5f, 1.25f, 1.0f, 0.75f, 0.5f, 0.25f}}}; - - for (auto& test : float32_tests) { - auto start = make_shared(et, Shape{}, std::vector{test.start}); - auto stop = make_shared(et, Shape{}, std::vector{test.stop}); - auto step = make_shared(et, Shape{}, std::vector{test.step}); - auto range = make_shared(start, stop, step, et); - auto pshape_out = range->get_output_partial_shape(0); - ASSERT_TRUE(pshape_out.rank().is_static() && pshape_out.rank() == Dimension{1}); - auto f = make_shared(NodeVector{range}, ParameterVector{}); - - auto test_case = test::TestCase(f); - - test_case.add_expected_output(test.expected_result_shape, test.expected_result); - test_case.run_with_tolerance_as_fp(1.0e-4f); - } -}