22// SPDX-License-Identifier: Apache-2.0
33//
44
5+ #include " openvino/op/one_hot.hpp"
6+
57#include < gtest/gtest.h>
68
79#include " common_test_utils/test_assertions.hpp"
810#include " custom_shape_infer.hpp"
911#include " openvino/op/constant.hpp"
1012#include " openvino/op/parameter.hpp"
11- #include " openvino/op/one_hot.hpp"
1213namespace ov {
1314namespace intel_cpu {
1415namespace unit_test {
@@ -18,97 +19,117 @@ using namespace ov;
1819using namespace ov ::intel_cpu;
1920using namespace testing ;
2021
21- using OneHotTestParams = std::tuple<unit_test::ShapeVector, // Input shapes
22- int64_t , // depth
23- int32_t , // on_value
24- int32_t , // off_value
25- StaticShape // Expected shape
22+ using OneHotTestParams = std::tuple<unit_test::ShapeVector, // Input shapes
23+ int64_t , // depth
24+ int32_t , // on_value
25+ int32_t , // off_value
26+ StaticShape // Expected shape
2627 >;
28+ // Parameters for typed test used test case internal loop.
29+ const auto OneHotTestData =
30+ std::vector<OneHotTestParams>{make_tuple (unit_test::ShapeVector{{3 }, {}, {}, {}}, 2 , 5 , 10 , StaticShape ({3 , 2 })),
31+ make_tuple (unit_test::ShapeVector{{3 }, {}, {}, {}}, 2 , 1 , 0 , StaticShape ({3 , 2 }))};
2732
28- class OneHotCpuShapeInferenceTest : public unit_test ::OpCpuShapeInferenceTest<op::v1::OneHot>,
29- public WithParamInterface<OneHotTestParams> {
30- public:
31- static std::string getTestCaseName (const testing::TestParamInfo<OneHotTestParams>& obj) {
32- const auto & [tmp_input_shapes, tmp_depth, tmp_on, tmp_off, tmp_exp_shape] = obj.param ;
33- std::ostringstream result;
34- result << " IS" << ov::test::utils::vec2str (tmp_input_shapes) << " _" ;
35- result << " depth" << tmp_depth << " _" ;
36- result << " on" << tmp_on << " _" ;
37- result << " off" << tmp_off << " _" ;
38- result << " exp_shape" << tmp_exp_shape;
39- return result.str ();
40- }
41-
33+ template <typename TOp>
34+ class OneHotCpuShapeInferenceTest : public unit_test ::OpCpuShapeInferenceTest<TOp> {
4235protected:
4336 void SetUp () override {
44- std::tie (input_shapes, m_depth, m_on, m_off, exp_shape) = GetParam ();
45- output_shapes = unit_test::ShapeVector (0 );
46- output_shapes.push_back (exp_shape);
47- arg = std::make_shared<op::v0::Parameter>(element::i64 , input_shapes.front ().get_shape ());
37+ this ->output_shapes .resize (0 );
4838 }
4939
50- int64_t m_depth;
51- int32_t m_on;
52- int32_t m_off;
53- std::shared_ptr<op::v0::Parameter> arg;
40+ template <class ... Args>
41+ std::shared_ptr<TOp> make_one_hot (Args&&... args) {
42+ if constexpr (std::is_same_v<TOp, ov::op::v16::OneHot>) {
43+ return this ->make_op (std::forward<Args>(args)...,
44+ ov::op::v16::OneHot::NegativeIndicesMode::IGNORE_NEGATIVE);
45+ }
46+ return this ->make_op (std::forward<Args>(args)...);
47+ }
5448};
5549
56- TEST_P (OneHotCpuShapeInferenceTest , shape_inference_empty_const_map) {
57- const auto depth = op::v0::Constant::create (element::i64 , ov::Shape{}, {m_depth});
58- const auto on_value = op::v0::Constant::create (element::i32 , ov::Shape{}, {m_on});
59- const auto off_value = op::v0::Constant::create (element::i32 , ov::Shape{}, {m_off});
60- int64_t axis = -1 ;
61- const auto op = make_op (arg, depth, on_value, off_value, axis);
62- unit_test::cpu_test_shape_infer (op.get (), input_shapes, output_shapes);
50+ TYPED_TEST_SUITE_P (OneHotCpuShapeInferenceTest);
51+
52+ TYPED_TEST_P (OneHotCpuShapeInferenceTest, shape_inference_empty_const_map) {
53+ for (auto && data : OneHotTestData) {
54+ int64_t depth;
55+ int32_t on;
56+ int32_t off;
57+ std::tie (this ->input_shapes , depth, on, off, this ->exp_shape ) = data;
58+ this ->output_shapes = {this ->exp_shape };
59+ const auto depth_op = op::v0::Constant::create (element::i64 , ov::Shape{}, {depth});
60+ const auto on_value_op = op::v0::Constant::create (element::i32 , ov::Shape{}, {on});
61+ const auto off_value_op = op::v0::Constant::create (element::i32 , ov::Shape{}, {off});
62+ const auto input = std::make_shared<op::v0::Parameter>(element::i64 , this ->input_shapes .front ().get_shape ());
63+ int64_t axis = -1 ;
64+ const auto op = this ->make_one_hot (input, depth_op, on_value_op, off_value_op, axis);
65+ unit_test::cpu_test_shape_infer (op.get (), this ->input_shapes , this ->output_shapes );
66+ }
6367}
6468
65- TEST_P (OneHotCpuShapeInferenceTest , shape_inference_with_const_map) {
66- const auto depth = std::make_shared<op::v0::Parameter>(element::i64 , ov::Shape{});
67- const auto on = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
68- const auto off = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
69- int64_t axis = -1 ;
70- const auto op = make_op (arg, depth, on, off, axis);
71-
72- const auto depth_tensor = ov::Tensor (element::i64 , ov::Shape{}, &m_depth);
73- const auto on_tensor = ov::Tensor (element::i32 , ov::Shape{}, &m_on);
74- const auto off_tensor = ov::Tensor (element::i32 , ov::Shape{}, &m_off);
75- const std::unordered_map<size_t , ov::Tensor> constant_data = {{1 , depth_tensor}, {2 , on_tensor}, {3 , off_tensor}};
76-
77- unit_test::cpu_test_shape_infer (op.get (), input_shapes, output_shapes, constant_data);
69+ TYPED_TEST_P (OneHotCpuShapeInferenceTest, shape_inference_with_const_map) {
70+ for (auto && data : OneHotTestData) {
71+ int64_t depth;
72+ int32_t on;
73+ int32_t off;
74+ std::tie (this ->input_shapes , depth, on, off, this ->exp_shape ) = data;
75+ this ->output_shapes = {this ->exp_shape };
76+ const auto depth_op = std::make_shared<op::v0::Parameter>(element::i64 , ov::Shape{});
77+ const auto on_value_op = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
78+ const auto off_value_op = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
79+ const auto input = std::make_shared<op::v0::Parameter>(element::i64 , this ->input_shapes .front ().get_shape ());
80+ int64_t axis = -1 ;
81+ const auto op = this ->make_one_hot (input, depth_op, on_value_op, off_value_op, axis);
82+
83+ const auto depth_tensor = ov::Tensor (element::i64 , ov::Shape{}, &depth);
84+ const auto on_tensor = ov::Tensor (element::i32 , ov::Shape{}, &on);
85+ const auto off_tensor = ov::Tensor (element::i32 , ov::Shape{}, &off);
86+ const std::unordered_map<size_t , ov::Tensor> constant_data = {{1 , depth_tensor},
87+ {2 , on_tensor},
88+ {3 , off_tensor}};
89+
90+ unit_test::cpu_test_shape_infer (op.get (), this ->input_shapes , this ->output_shapes , constant_data);
91+ }
7892}
7993
80- INSTANTIATE_TEST_SUITE_P (
81- CpuShapeInfer,
82- OneHotCpuShapeInferenceTest ,
83- Values (make_tuple(unit_test::ShapeVector{{3 }, {}, {}, {}}, 2 , 5 , 10 , StaticShape({3 , 2 })),
84- make_tuple(unit_test::ShapeVector{{3 }, {}, {}, {}}, 2 , 1 , 0 , StaticShape({3 , 2 }))),
85- OneHotCpuShapeInferenceTest::getTestCaseName);
86-
87- using OneHotCpuShapeInferenceThrowExceptionTest = OneHotCpuShapeInferenceTest;
88- TEST_P (OneHotCpuShapeInferenceThrowExceptionTest, wrong_pattern) {
89- const auto depth = std::make_shared<op::v0::Parameter>(element::i64 , ov::Shape{});
90- const auto on = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
91- const auto off = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
94+ REGISTER_TYPED_TEST_SUITE_P (OneHotCpuShapeInferenceTest,
95+ shape_inference_empty_const_map,
96+ shape_inference_with_const_map);
97+ using OneHotTypes = Types<op::v1::OneHot, op::v16::OneHot>;
98+ INSTANTIATE_TYPED_TEST_SUITE_P (CpuShapeInfer, OneHotCpuShapeInferenceTest, OneHotTypes);
99+
100+ template <typename TOp>
101+ using OneHotCpuShapeInferenceThrowExceptionTest = OneHotCpuShapeInferenceTest<TOp>;
102+
103+ TYPED_TEST_SUITE_P (OneHotCpuShapeInferenceThrowExceptionTest);
104+ TYPED_TEST_P (OneHotCpuShapeInferenceThrowExceptionTest, wrong_pattern) {
105+ this ->input_shapes = unit_test::ShapeVector{{3 }, {}, {}, {}};
106+ this ->exp_shape = StaticShape ({});
107+ this ->output_shapes = {this ->exp_shape };
108+ const auto depth_op = std::make_shared<op::v0::Parameter>(element::i64 , ov::Shape{});
109+ const auto on_op = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
110+ const auto off_op = std::make_shared<op::v0::Parameter>(element::i32 , ov::Shape{});
111+ const auto input = std::make_shared<op::v0::Parameter>(element::i64 , this ->input_shapes .front ().get_shape ());
92112 int64_t axis = -1 ;
93- const auto op = make_op (arg, depth, on, off, axis);
94113
95- const auto depth_tensor = ov::Tensor (element::i64 , ov::Shape{}, &m_depth);
96- const auto on_tensor = ov::Tensor (element::i32 , ov::Shape{}, &m_on);
97- const auto off_tensor = ov::Tensor (element::i32 , ov::Shape{}, &m_off);
98- const std::unordered_map<size_t , ov::Tensor> constant_data = {{1 , depth_tensor}, {2 , on_tensor}, {3 , off_tensor}};
114+ const auto op = this ->make_one_hot (input, depth_op, on_op, off_op, axis);
99115
100- OV_EXPECT_THROW (unit_test::cpu_test_shape_infer (op.get (), input_shapes, output_shapes, constant_data),
116+ const int64_t depth = -2 ;
117+ const int32_t on = 1 ;
118+ const int32_t off = 0 ;
119+
120+ const auto depth_tensor = ov::Tensor (element::i64 , ov::Shape{}, &depth);
121+ const auto on_tensor = ov::Tensor (element::i32 , ov::Shape{}, &on);
122+ const auto off_tensor = ov::Tensor (element::i32 , ov::Shape{}, &off);
123+ const std::unordered_map<size_t , ov::Tensor> constant_data = {{1 , depth_tensor}, {2 , on_tensor}, {3 , off_tensor}};
124+ OV_EXPECT_THROW (unit_test::cpu_test_shape_infer (op.get (), this ->input_shapes , this ->output_shapes , constant_data),
101125 ov::Exception,
102126 testing::HasSubstr (" OneHot depth value can't be negative" ));
103127}
104128
105- INSTANTIATE_TEST_SUITE_P (
106- CpuShapeInfer,
107- OneHotCpuShapeInferenceThrowExceptionTest,
108- Values (make_tuple(unit_test::ShapeVector{{3 }, {}, {}, {}}, -2 , 1 , 0 , StaticShape({}))),
109- OneHotCpuShapeInferenceThrowExceptionTest::getTestCaseName);
129+ REGISTER_TYPED_TEST_SUITE_P (OneHotCpuShapeInferenceThrowExceptionTest, wrong_pattern);
130+ INSTANTIATE_TYPED_TEST_SUITE_P (CpuShapeInfer, OneHotCpuShapeInferenceThrowExceptionTest, OneHotTypes);
110131
111- } // namespace cpu_shape_infer
112- } // namespace unit_test
113- } // namespace intel_cpu
114- } // namespace ov
132+ } // namespace cpu_shape_infer
133+ } // namespace unit_test
134+ } // namespace intel_cpu
135+ } // namespace ov
0 commit comments