From 4c722e025b86710a1ce9b8252734ede626395690 Mon Sep 17 00:00:00 2001 From: Steve Yoo Date: Mon, 18 Oct 2021 19:20:09 +0900 Subject: [PATCH] Migrate ngraph backend test of condition operations (#7907) * Add test cases for PReLU in cpu plugin * For case when slope is vector * Add Bucketize and NonZero template plugin reference tests * Apply supported types for NonZero * Edit float_t and double_t to float and double * Edit std::vector to std::vector --- .../functional/op_reference/bucketize.cpp | 98 +++++++ .../tests/functional/op_reference/nonzero.cpp | 262 ++++++++++++++++++ ngraph/test/CMakeLists.txt | 2 - ngraph/test/backend/bucketize.in.cpp | 57 ---- ngraph/test/backend/non_zero.in.cpp | 90 ------ 5 files changed, 360 insertions(+), 149 deletions(-) create mode 100644 docs/template_plugin/tests/functional/op_reference/bucketize.cpp create mode 100644 docs/template_plugin/tests/functional/op_reference/nonzero.cpp delete mode 100644 ngraph/test/backend/bucketize.in.cpp delete mode 100644 ngraph/test/backend/non_zero.in.cpp diff --git a/docs/template_plugin/tests/functional/op_reference/bucketize.cpp b/docs/template_plugin/tests/functional/op_reference/bucketize.cpp new file mode 100644 index 00000000000..92fc00bcb5d --- /dev/null +++ b/docs/template_plugin/tests/functional/op_reference/bucketize.cpp @@ -0,0 +1,98 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "openvino/op/bucketize.hpp" +#include "base_reference_test.hpp" + +using namespace reference_tests; +using namespace ov; + +struct BucketizeParams { + template + BucketizeParams(const element::Type& input_type, const PartialShape& input_pshape, const std::vector& input, + const element::Type& bucket_type, const PartialShape& bucket_pshape, const std::vector& buckets, bool with_right_bound, + const element::Type& output_type, const std::vector& expected_output) + : input_type(input_type), + input_pshape(input_pshape), + input(CreateTensor(input_type, input)), + bucket_type(bucket_type), + bucket_pshape(bucket_pshape), + buckets(CreateTensor(bucket_type, buckets)), + with_right_bound(with_right_bound), + output_type(output_type), + expected_output(CreateTensor(output_type, expected_output)) {} + + element::Type input_type; + PartialShape input_pshape; + runtime::Tensor input; + element::Type bucket_type; + PartialShape bucket_pshape; + runtime::Tensor buckets; + bool with_right_bound; + element::Type output_type; + runtime::Tensor expected_output; +}; + +class ReferenceBucketizeLayerTest : public testing::TestWithParam, public CommonReferenceTest { +public: + void SetUp() override { + auto params = GetParam(); + function = CreateFunction(params.input_type, params.input_pshape, params.bucket_type, params.bucket_pshape, + params.with_right_bound, params.output_type); + inputData = {params.input, params.buckets}; + refOutData = {params.expected_output}; + } + + static std::string getTestCaseName(const testing::TestParamInfo& obj) { + auto param = obj.param; + std::ostringstream result; + result << "input_type=" << param.input_type << "_"; + result << "input_pshape=" << param.input_pshape << "_"; + result << "bucket_type=" << param.bucket_type << "_"; + result << "bucket_pshape=" << param.bucket_pshape << "_"; + result << "with_right_bound=" << param.with_right_bound << "_"; + result << "output_type=" << param.output_type; + return result.str(); + } + +private: + static std::shared_ptr CreateFunction(const element::Type& input_type, const PartialShape& input_pshape, + const element::Type& bucket_type, const PartialShape& bucket_pshape, + const bool with_right_bound, const element::Type& output_type) { + auto data = std::make_shared(input_type, input_pshape); + auto buckets = std::make_shared(bucket_type, bucket_pshape); + return std::make_shared(std::make_shared(data, buckets, output_type, with_right_bound), + ParameterVector {data, buckets}); + } +}; + +TEST_P(ReferenceBucketizeLayerTest, CompareWithHardcodedRefs) { + Exec(); +} + +INSTANTIATE_TEST_SUITE_P(smoke_Bucketize_With_Hardcoded_Refs, ReferenceBucketizeLayerTest, + ::testing::Values( + // fp32, int32, with_right_bound + BucketizeParams(element::f32, + PartialShape {10, 1}, + std::vector {8.f, 1.f, 2.f, 1.1f, 8.f, 10.f, 1.f, 10.2f, 0.f, 20.f}, + element::i32, + PartialShape {4}, + std::vector {1, 4, 10, 20}, + true, + element::i32, + std::vector {2, 0, 1, 1, 2, 2, 0, 3, 0, 3}), + // fp32, int32, with_right_bound + BucketizeParams(element::i32, + PartialShape {1, 1, 10}, + std::vector {8, 1, 2, 1, 8, 5, 1, 5, 0, 20}, + element::i32, + PartialShape {4}, + std::vector {1, 4, 10, 20}, + false, + element::i32, + std::vector {2, 1, 1, 1, 2, 2, 1, 2, 0, 4})), + ReferenceBucketizeLayerTest::getTestCaseName); diff --git a/docs/template_plugin/tests/functional/op_reference/nonzero.cpp b/docs/template_plugin/tests/functional/op_reference/nonzero.cpp new file mode 100644 index 00000000000..cc3518fc457 --- /dev/null +++ b/docs/template_plugin/tests/functional/op_reference/nonzero.cpp @@ -0,0 +1,262 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include "openvino/op/non_zero.hpp" +#include "base_reference_test.hpp" + +using namespace reference_tests; +using namespace ov; + +namespace { +struct NonZeroParams { + template + NonZeroParams(const PartialShape& dynamicShape, const PartialShape& inputShape, + const element::Type& inType, const element::Type& refType, + const std::vector& inputData, const std::vector& refData, + const std::string& test_name = "") + : dynamicShape(dynamicShape), + inputShape(inputShape), + inType(inType), + refType(refType), + inputData(CreateTensor(inType, inputData)), + refData(CreateTensor(refType, refData)), + testcaseName(test_name) {} + + PartialShape dynamicShape; + PartialShape inputShape; + element::Type inType; + element::Type refType; + runtime::Tensor inputData; + runtime::Tensor refData; + std::string testcaseName; +}; + +class ReferenceNonZeroLayerTest : public testing::TestWithParam, public CommonReferenceTest { +public: + void SetUp() override { + auto params = GetParam(); + function = CreateFunction(params.dynamicShape, params.inType, params.refType); + inputData = {params.inputData}; + refOutData = {params.refData}; + } + + static std::string getTestCaseName(const testing::TestParamInfo& obj) { + auto param = obj.param; + std::ostringstream result; + result << "dShape=" << param.dynamicShape << "_"; + result << "iShape=" << param.inputShape << "_"; + result << "iType=" << param.inType << "_"; + if (param.testcaseName != "") { + result << "oType=" << param.refType << "_"; + result << param.testcaseName; + } else { + result << "oType=" << param.refType; + } + return result.str(); + } + +private: + static std::shared_ptr CreateFunction(const PartialShape& input_shape, const element::Type& input_type, + const element::Type& output_type) { + const auto in = std::make_shared(input_type, input_shape); + const auto NonZero = std::make_shared(in, output_type); + return std::make_shared(NodeVector {NonZero}, ParameterVector {in}); + } +}; + +TEST_P(ReferenceNonZeroLayerTest, CompareWithHardcodedRefs) { + Exec(); +} + +INSTANTIATE_TEST_SUITE_P(smoke_NonZero_With_Hardcoded_Refs, ReferenceNonZeroLayerTest, + ::testing::Values( + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i32, + std::vector{1, 1, 1, 1, 1, 1}, + std::vector{0, 0, 1, 1, 2, 2, 0, 1, 0, 1, 0, 1}, + "all_1s"), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i32, + std::vector{0, 0, 0, 0, 0, 0}, + std::vector{}, + "all_0s"), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i64, + std::vector{1, 1, 1, 1, 1, 1}, + std::vector{0, 0, 1, 1, 2, 2, 0, 1, 0, 1, 0, 1}, + "all_1s_int64"), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i64, + std::vector{0, 0, 0, 0, 0, 0}, + std::vector{}, + "all_0s_int64"), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::boolean, + element::i32, + std::vector{false, false, false, false, true, true}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i8, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i16, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i64, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u8, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u16, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u32, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u64, + element::i32, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::bf16, + element::i32, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f16, + element::i32, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f32, + element::i32, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f64, + element::i32, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::boolean, + element::i64, + std::vector{false, false, false, false, true, true}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i8, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i16, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i32, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::i64, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u8, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u16, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u32, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::u64, + element::i64, + std::vector{0, 0, 0, 0, 1, 3}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::bf16, + element::i64, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f16, + element::i64, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f32, + element::i64, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1}), + NonZeroParams(PartialShape{3, 2}, + PartialShape{3, 2}, + element::f64, + element::i64, + std::vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}, + std::vector{2, 2, 0, 1})), + ReferenceNonZeroLayerTest::getTestCaseName); +} // namespace diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 5c3cc1d6bd0..ce486c1f378 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -460,7 +460,6 @@ set(MULTI_TEST_SRC backend/batch_norm.in.cpp backend/batch_to_space.in.cpp backend/broadcast.in.cpp - backend/bucketize.in.cpp backend/builder_reduce_ops_opset1.in.cpp backend/ceiling.in.cpp backend/concat.in.cpp @@ -507,7 +506,6 @@ set(MULTI_TEST_SRC backend/node_name.in.cpp backend/normalize_l2.in.cpp backend/non_max_suppression.in.cpp - backend/non_zero.in.cpp backend/one_hot.in.cpp backend/pad.in.cpp backend/parameter_as_output.in.cpp diff --git a/ngraph/test/backend/bucketize.in.cpp b/ngraph/test/backend/bucketize.in.cpp deleted file mode 100644 index f26ffe8a9db..00000000000 --- a/ngraph/test/backend/bucketize.in.cpp +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#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" - -NGRAPH_SUPPRESS_DEPRECATED_START - -using namespace std; -using namespace ngraph; - -static string s_manifest = "${MANIFEST}"; -using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME}); - -NGRAPH_TEST(${BACKEND_NAME}, bucketize_right_edge) { - Shape data_shape{10, 1}; - Shape bucket_shape{4}; - - const auto data = make_shared(element::f32, data_shape); - const auto buckets = make_shared(element::i32, bucket_shape); - const auto bucketize = make_shared(data, buckets, element::i32, true); - const auto f = make_shared(bucketize, ParameterVector{data, buckets}); - - vector data_vect = {8.f, 1.f, 2.f, 1.1f, 8.f, 10.f, 1.f, 10.2f, 0.f, 20.f}; - vector buckets_vect = {1, 4, 10, 20}; - vector expected_vect = {2, 0, 1, 1, 2, 2, 0, 3, 0, 3}; - - auto test_case = test::TestCase(f); - test_case.add_input(data_shape, data_vect); - test_case.add_input(bucket_shape, buckets_vect); - test_case.add_expected_output(data_shape, expected_vect); - test_case.run(); -} - -NGRAPH_TEST(${BACKEND_NAME}, bucketize_left_edge) { - Shape data_shape{1, 1, 10}; - Shape bucket_shape{4}; - - const auto data = make_shared(element::i32, data_shape); - const auto buckets = make_shared(element::f32, bucket_shape); - const auto bucketize = make_shared(data, buckets, element::i32, false); - const auto f = make_shared(bucketize, ParameterVector{data, buckets}); - - vector data_vect = {8, 1, 2, 1, 8, 5, 1, 5, 0, 20}; - vector buckets_vect = {1.f, 4.f, 10.f, 20.f}; - vector expected_vect = {2, 1, 1, 1, 2, 2, 1, 2, 0, 4}; - - auto test_case = test::TestCase(f); - test_case.add_input(data_shape, data_vect); - test_case.add_input(bucket_shape, buckets_vect); - test_case.add_expected_output(data_shape, expected_vect); - test_case.run(); -} diff --git a/ngraph/test/backend/non_zero.in.cpp b/ngraph/test/backend/non_zero.in.cpp deleted file mode 100644 index 5b65d6017af..00000000000 --- a/ngraph/test/backend/non_zero.in.cpp +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include "engines_util/execute_tools.hpp" -#include "gtest/gtest.h" -#include "ngraph/ngraph.hpp" -#include "util/all_close_f.hpp" -#include "util/ndarray.hpp" -#include "util/test_control.hpp" - -using namespace std; -using namespace ngraph; - -static string s_manifest = "${MANIFEST}"; - -NGRAPH_TEST(${BACKEND_NAME}, non_zero) { - PartialShape p_shape = PartialShape::dynamic(); - auto p = make_shared(element::f32, p_shape); - auto non_zero = make_shared(p, element::i32); - auto fun = make_shared(OutputVector{non_zero}, ParameterVector{p}); - - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - auto cfun = backend->compile(fun); - - auto input = backend->create_tensor(element::f32, Shape{3, 2}); - copy_data(input, vector{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 3.0f}); - - std::vector expected_result{2, 2, 0, 1}; - Shape expected_output_shape{2, 2}; - - auto result = make_shared(); - cfun->call_with_validate({result}, {input}); - - EXPECT_EQ(result->get_element_type(), element::i32); - EXPECT_EQ(result->get_shape(), expected_output_shape); - auto result_data = read_vector(result); - ASSERT_EQ(result_data, expected_result); -} - -NGRAPH_TEST(${BACKEND_NAME}, non_zero_all_1s) { - PartialShape p_shape = PartialShape::dynamic(); - auto p = make_shared(element::i32, p_shape); - auto non_zero = make_shared(p, element::i64); - auto fun = make_shared(OutputVector{non_zero}, ParameterVector{p}); - - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - auto cfun = backend->compile(fun); - - Shape input_shape{3, 2}; - vector input_data(shape_size(input_shape), 1); - auto input = backend->create_tensor(element::i32, input_shape); - copy_data(input, input_data); - - std::vector expected_result{0, 0, 1, 1, 2, 2, 0, 1, 0, 1, 0, 1}; - Shape expected_output_shape{2, 6}; - - auto result = make_shared(); - cfun->call_with_validate({result}, {input}); - - EXPECT_EQ(result->get_element_type(), element::i64); - EXPECT_EQ(result->get_shape(), expected_output_shape); - auto result_data = read_vector(result); - ASSERT_EQ(result_data, expected_result); -} - -NGRAPH_TEST(${BACKEND_NAME}, non_zero_all_0s) { - PartialShape p_shape = PartialShape::dynamic(); - auto p = make_shared(element::i32, p_shape); - auto non_zero = make_shared(p, element::i64); - auto fun = make_shared(OutputVector{non_zero}, ParameterVector{p}); - - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - auto cfun = backend->compile(fun); - - Shape input_shape{3, 2}; - vector input_data(shape_size(input_shape), 0); - auto input = backend->create_tensor(element::i32, input_shape); - copy_data(input, input_data); - - Shape expected_output_shape{input_shape.size(), 0}; - - auto result = make_shared(); - cfun->call_with_validate({result}, {input}); - - EXPECT_EQ(result->get_element_type(), element::i64); - EXPECT_EQ(result->get_shape(), expected_output_shape); - auto result_data = read_vector(result); - ASSERT_EQ(result_data.data(), nullptr); -}