From 67f64f7e8aa789eef5b6fbc5d71fa725262339cf Mon Sep 17 00:00:00 2001 From: Oleg Pipikin Date: Tue, 12 Sep 2023 11:46:30 +0200 Subject: [PATCH] Refactor ComparisonLayerTest, ClampLayerTest (#19681) * Refactor ClampLayerTest * Refactor ComparisonLayerTest --- .../single_layer_tests/clamp.cpp | 33 ++--- .../single_layer_tests/comparison.cpp | 44 ++++--- .../shared/include/single_op_tests/clamp.hpp | 15 +++ .../include/single_op_tests/comparison.hpp | 15 +++ .../shared_test_classes/single_op/clamp.hpp | 30 +++++ .../single_op/comparison.hpp | 37 ++++++ .../src/single_op/clamp.cpp | 50 ++++++++ .../src/single_op/comparison.cpp | 113 ++++++++++++++++++ 8 files changed, 303 insertions(+), 34 deletions(-) create mode 100644 src/tests/functional/plugin/shared/include/single_op_tests/clamp.hpp create mode 100644 src/tests/functional/plugin/shared/include/single_op_tests/comparison.hpp create mode 100644 src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/clamp.hpp create mode 100644 src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/comparison.hpp create mode 100644 src/tests/functional/shared_test_classes/src/single_op/clamp.cpp create mode 100644 src/tests/functional/shared_test_classes/src/single_op/comparison.cpp diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp index 0bf4d29c8155e0..15e66eb2978574 100644 --- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp +++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/clamp.cpp @@ -4,17 +4,19 @@ #include -#include "single_layer_tests/clamp.hpp" +#include "single_op_tests/clamp.hpp" #include "common_test_utils/test_constants.hpp" -using namespace LayerTestsDefinitions; +namespace { +using ov::test::ClampLayerTest; -const std::vector> inShapes = { - {50}, - {10, 10}, - {1, 20, 20} +const std::vector> input_shapes_static = { + {{ 50 }}, + {{ 10, 10 }}, + {{ 1, 20, 20 }} }; + const std::vector> intervals = { {-20.1, -10.5}, {-10.0, 10.0}, @@ -27,26 +29,27 @@ const std::vector> intervals_unsigned = { {10.6, 20.6} }; -const std::vector netPrc = { - InferenceEngine::Precision::FP32, - InferenceEngine::Precision::FP16, - InferenceEngine::Precision::I64, - InferenceEngine::Precision::I32 +const std::vector model_type = { + ov::element::f32, + ov::element::f16, + ov::element::i64, + ov::element::i32 }; const auto test_Clamp_signed = ::testing::Combine( - ::testing::ValuesIn(inShapes), + ::testing::ValuesIn(ov::test::static_shapes_to_test_representation(input_shapes_static)), ::testing::ValuesIn(intervals), - ::testing::ValuesIn(netPrc), + ::testing::ValuesIn(model_type), ::testing::Values(ov::test::utils::DEVICE_CPU) ); const auto test_Clamp_unsigned = ::testing::Combine( - ::testing::ValuesIn(inShapes), + ::testing::ValuesIn(ov::test::static_shapes_to_test_representation(input_shapes_static)), ::testing::ValuesIn(intervals_unsigned), - ::testing::Values(InferenceEngine::Precision::U64), + ::testing::Values(ov::element::u64), ::testing::Values(ov::test::utils::DEVICE_CPU) ); INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_signed, ClampLayerTest, test_Clamp_signed, ClampLayerTest::getTestCaseName); INSTANTIATE_TEST_SUITE_P(smoke_TestsClamp_unsigned, ClampLayerTest, test_Clamp_unsigned, ClampLayerTest::getTestCaseName); +} // namespace diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/comparison.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/comparison.cpp index 572d595e39852b..0b68b33e2074a9 100644 --- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/comparison.cpp +++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/single_layer_tests/comparison.cpp @@ -3,15 +3,13 @@ // #include -#include "single_layer_tests/comparison.hpp" +#include "single_op_tests/comparison.hpp" #include "common_test_utils/test_constants.hpp" -using namespace LayerTestsDefinitions; -using namespace LayerTestsDefinitions::ComparisonParams; - namespace { +using ov::test::ComparisonLayerTest; -std::map, std::vector>> inputShapes = { +std::map> input_shapes_combinations = { {{1}, {{1}, {17}, {1, 1}, {2, 18}, {1, 1, 2}, {2, 2, 3}, {1, 1, 2, 3}}}, {{5}, {{1}, {1, 1}, {2, 5}, {1, 1, 1}, {2, 2, 5}}}, {{2, 200}, {{1}, {200}, {1, 200}, {2, 200}, {2, 2, 200}}}, @@ -20,11 +18,23 @@ std::map, std::vector>> inputShapes = { {{2, 1, 1, 3, 1}, {{1}, {1, 3, 4}, {2, 1, 3, 4}, {1, 1, 1, 1, 1}}}, }; -std::vector inputsPrecisions = { - InferenceEngine::Precision::FP32, - InferenceEngine::Precision::FP16, - InferenceEngine::Precision::I32, - InferenceEngine::Precision::BOOL, +auto input_shapes_pair_vector = ov::test::utils::combineParams(input_shapes_combinations); + +auto converter = [] (const std::vector>& shapes) { + std::vector> result; + for (const auto& shape : shapes) { + result.push_back({shape.first, shape.second}); + } + return result; +}; + +auto input_shapes_static = converter(input_shapes_pair_vector); + +std::vector model_type = { + ov::element::f32, + ov::element::f16, + ov::element::i32, + ov::element::boolean, }; std::vector comparisonOpTypes = { @@ -44,19 +54,17 @@ std::vector secondInputTypes = { std::map additional_config = {}; const auto ComparisonTestParams = ::testing::Combine( - ::testing::ValuesIn(ov::test::utils::combineParams(inputShapes)), - ::testing::ValuesIn(inputsPrecisions), + ::testing::ValuesIn(ov::test::static_shapes_to_test_representation(input_shapes_static)), ::testing::ValuesIn(comparisonOpTypes), ::testing::ValuesIn(secondInputTypes), - ::testing::Values(InferenceEngine::Precision::UNSPECIFIED), - ::testing::Values(InferenceEngine::Precision::UNSPECIFIED), + ::testing::ValuesIn(model_type), ::testing::Values(ov::test::utils::DEVICE_CPU), ::testing::Values(additional_config)); INSTANTIATE_TEST_SUITE_P(smoke_CompareWithRefs, ComparisonLayerTest, ComparisonTestParams, ComparisonLayerTest::getTestCaseName); -std::vector inputShapesIsOps = { +std::vector> input_shapes_is_ops_static = { {{1}, {1}}, {{1, 2}, {1}}, {{3, 1}, {1}}, @@ -79,12 +87,10 @@ std::vector comparisonOpTypesIs = { }; const auto ComparisonTestParamsIs = ::testing::Combine( - ::testing::ValuesIn(inputShapesIsOps), - ::testing::Values(InferenceEngine::Precision::FP32), + ::testing::ValuesIn(ov::test::static_shapes_to_test_representation(input_shapes_is_ops_static)), ::testing::ValuesIn(comparisonOpTypesIs), ::testing::Values(ngraph::helpers::InputLayerType::CONSTANT), - ::testing::Values(InferenceEngine::Precision::UNSPECIFIED), - ::testing::Values(InferenceEngine::Precision::UNSPECIFIED), + ::testing::Values(ov::element::f32), ::testing::Values(ov::test::utils::DEVICE_CPU), ::testing::Values(additional_config)); diff --git a/src/tests/functional/plugin/shared/include/single_op_tests/clamp.hpp b/src/tests/functional/plugin/shared/include/single_op_tests/clamp.hpp new file mode 100644 index 00000000000000..c3d848fea3d0ab --- /dev/null +++ b/src/tests/functional/plugin/shared/include/single_op_tests/clamp.hpp @@ -0,0 +1,15 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "shared_test_classes/single_op/clamp.hpp" + +namespace ov { +namespace test { +TEST_P(ClampLayerTest, Inference) { + run(); +} +} // namespace test +} // namespace ov diff --git a/src/tests/functional/plugin/shared/include/single_op_tests/comparison.hpp b/src/tests/functional/plugin/shared/include/single_op_tests/comparison.hpp new file mode 100644 index 00000000000000..65e36f84c152e0 --- /dev/null +++ b/src/tests/functional/plugin/shared/include/single_op_tests/comparison.hpp @@ -0,0 +1,15 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +namespace ov { +namespace test { +TEST_P(ComparisonLayerTest, Inference) { + run(); +} +} // namespace test +} // namespace ov diff --git a/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/clamp.hpp b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/clamp.hpp new file mode 100644 index 00000000000000..eadded62f9ff9c --- /dev/null +++ b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/clamp.hpp @@ -0,0 +1,30 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include +#include + +#include "shared_test_classes/base/ov_subgraph.hpp" + +namespace ov { +namespace test { + +using clampParamsTuple = std::tuple< + std::vector, // Input shape + std::pair, // Interval [min, max] + ov::element::Type, // Model precision + std::string>; // Device name + +class ClampLayerTest : public testing::WithParamInterface, + virtual public ov::test::SubgraphBaseTest { +public: + static std::string getTestCaseName(const testing::TestParamInfo& obj); +protected: + void SetUp() override; +}; + +} // namespace test +} // namespace ov diff --git a/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/comparison.hpp b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/comparison.hpp new file mode 100644 index 00000000000000..0e29e65f22c122 --- /dev/null +++ b/src/tests/functional/shared_test_classes/include/shared_test_classes/single_op/comparison.hpp @@ -0,0 +1,37 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + + +#include + +#include "gtest/gtest.h" +#include "shared_test_classes/base/ov_subgraph.hpp" +#include "common_test_utils/test_constants.hpp" +#include "ngraph_functions/utils/ngraph_helpers.hpp" + +namespace ov { +namespace test { + +typedef std::tuple< + std::vector, // Input shapes tuple + ngraph::helpers::ComparisonTypes, // Comparison op type + ngraph::helpers::InputLayerType, // Second input type + ov::element::Type, // Model type + std::string, // Device name + std::map // Additional network configuration +> ComparisonTestParams; + +class ComparisonLayerTest : public testing::WithParamInterface, + virtual public ov::test::SubgraphBaseTest { + ngraph::helpers::ComparisonTypes comparison_op_type; +protected: + void SetUp() override; + void generate_inputs(const std::vector& targetInputStaticShapes) override; +public: + static std::string getTestCaseName(const testing::TestParamInfo &obj); +}; +} // namespace test +} // namespace ov diff --git a/src/tests/functional/shared_test_classes/src/single_op/clamp.cpp b/src/tests/functional/shared_test_classes/src/single_op/clamp.cpp new file mode 100644 index 00000000000000..307c913fd68a3d --- /dev/null +++ b/src/tests/functional/shared_test_classes/src/single_op/clamp.cpp @@ -0,0 +1,50 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "shared_test_classes/single_op/clamp.hpp" + +namespace ov { +namespace test { +std::string ClampLayerTest::getTestCaseName(const testing::TestParamInfo& obj) { + std::vector shapes; + std::pair interval; + ov::element::Type model_type; + std::string target_device; + + std::tie(shapes, interval, model_type, target_device) = obj.param; + + std::ostringstream result; + result << "IS=("; + for (size_t i = 0lu; i < shapes.size(); i++) { + result << ov::test::utils::partialShape2str({shapes[i].first}) << (i < shapes.size() - 1lu ? "_" : ""); + } + result << ")_TS="; + for (size_t i = 0lu; i < shapes.front().second.size(); i++) { + result << "{"; + for (size_t j = 0lu; j < shapes.size(); j++) { + result << ov::test::utils::vec2str(shapes[j].second[i]) << (j < shapes.size() - 1lu ? "_" : ""); + } + result << "}_"; + } + result << "min=" << interval.first << "_"; + result << "max=" << interval.second << "_"; + result << "netPrc=" << model_type.get_type_name() << "_"; + result << "trgDev=" << target_device; + return result.str(); +} + +void ClampLayerTest::SetUp() { + std::vector shapes; + std::pair interval; + ov::element::Type model_type; + std::tie(shapes, interval, model_type, targetDevice) = this->GetParam(); + init_input_shapes(shapes); + + auto input = std::make_shared(model_type, inputDynamicShapes.front()); + auto clamp = std::make_shared(input, interval.first, interval.second); + auto result = std::make_shared(clamp); + function = std::make_shared(result, ngraph::ParameterVector{input}); +} +} // namespace test +} // namespace ov diff --git a/src/tests/functional/shared_test_classes/src/single_op/comparison.cpp b/src/tests/functional/shared_test_classes/src/single_op/comparison.cpp new file mode 100644 index 00000000000000..7dacd12bc9d74c --- /dev/null +++ b/src/tests/functional/shared_test_classes/src/single_op/comparison.cpp @@ -0,0 +1,113 @@ +// Copyright (C) 2018-2023 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "shared_test_classes/single_op/comparison.hpp" + +#include "ngraph_functions/builders.hpp" +#include "common_test_utils/ov_tensor_utils.hpp" + +namespace ov { +namespace test { +using ngraph::helpers::ComparisonTypes; +using ngraph::helpers::InputLayerType; + +std::string ComparisonLayerTest::getTestCaseName(const testing::TestParamInfo &obj) { + std::vector shapes; + ComparisonTypes comparison_op_type; + InputLayerType second_input_type; + ov::element::Type model_type; + std::string device_name; + std::map additional_config; + std::tie(shapes, + comparison_op_type, + second_input_type, + model_type, + device_name, + additional_config) = obj.param; + + std::ostringstream result; + result << "IS=("; + for (size_t i = 0lu; i < shapes.size(); i++) { + result << ov::test::utils::partialShape2str({shapes[i].first}) << (i < shapes.size() - 1lu ? "_" : ""); + } + result << ")_TS="; + for (size_t i = 0lu; i < shapes.front().second.size(); i++) { + result << "{"; + for (size_t j = 0lu; j < shapes.size(); j++) { + result << ov::test::utils::vec2str(shapes[j].second[i]) << (j < shapes.size() - 1lu ? "_" : ""); + } + result << "}_"; + } + result << "comparisonOpType=" << comparison_op_type << "_"; + result << "secondInputType=" << second_input_type << "_"; + result << "in_type=" << model_type.get_type_name() << "_"; + result << "targetDevice=" << device_name; + return result.str(); +} + +void ComparisonLayerTest::SetUp() { + std::vector shapes; + InputLayerType second_input_type; + std::map additional_config; + ov::element::Type model_type; + std::tie(shapes, + comparison_op_type, + second_input_type, + model_type, + targetDevice, + additional_config) = this->GetParam(); + configuration.insert(additional_config.begin(), additional_config.end()); + init_input_shapes(shapes); + + ov::ParameterVector inputs {std::make_shared(model_type, inputDynamicShapes[0])}; + + std::shared_ptr second_input; + if (second_input_type == InputLayerType::PARAMETER) { + second_input = std::make_shared(model_type, inputDynamicShapes[1]); + inputs.push_back(std::dynamic_pointer_cast(second_input)); + } else { + ov::Tensor tensor = ov::test::utils::create_and_fill_tensor(model_type, targetStaticShapes.front()[1]); + second_input = std::make_shared(tensor); + } + + auto comparisonNode = ngraph::builder::makeComparison(inputs[0], second_input, comparison_op_type); + function = std::make_shared(comparisonNode, inputs, "Comparison"); +} + +void ComparisonLayerTest::generate_inputs(const std::vector& target_input_static_shapes) { + if (comparison_op_type == ComparisonTypes::IS_FINITE || comparison_op_type == ComparisonTypes::IS_NAN) { + inputs.clear(); + auto params = function->get_parameters(); + OPENVINO_ASSERT(target_input_static_shapes.size() >= params.size()); + for (int i = 0; i < params.size(); i++) { + ov::Tensor tensor(params[i]->get_element_type(), target_input_static_shapes[i]); + auto data_ptr = static_cast(tensor.data()); + auto data_ptr_int = static_cast(tensor.data()); + auto range = tensor.get_size(); + auto start = -static_cast(range) / 2.f; + testing::internal::Random random(1); + for (size_t i = 0; i < range; i++) { + if (i % 7 == 0) { + data_ptr[i] = std::numeric_limits::infinity(); + } else if (i % 7 == 1) { + data_ptr[i] = -std::numeric_limits::infinity(); + } else if (i % 7 == 2) { + data_ptr_int[i] = 0x7F800000 + random.Generate(range); + } else if (i % 7 == 3) { + data_ptr[i] = std::numeric_limits::quiet_NaN(); + } else if (i % 7 == 5) { + data_ptr[i] = -std::numeric_limits::quiet_NaN(); + } else { + data_ptr[i] = start + static_cast(random.Generate(range)); + } + } + inputs.insert({params[i], tensor}); + } + } else { + SubgraphBaseTest::generate_inputs(target_input_static_shapes); + } +} + +} // namespace test +} // namespace ov