Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / type_prop / one_hot.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include "gtest/gtest.h"
18 #include "ngraph/ngraph.hpp"
19 #include "util/type_prop.hpp"
20
21 NGRAPH_SUPPRESS_DEPRECATED_START
22
23 using namespace std;
24 using namespace ngraph;
25
26 TEST(type_prop, one_hot_deduce_scalar)
27 {
28     auto param = make_shared<op::Parameter>(element::i32, Shape{});
29     auto oh = make_shared<op::OneHot>(param, Shape{9}, 0);
30     ASSERT_EQ(oh->get_element_type(), element::i32);
31     ASSERT_EQ(oh->get_shape(), (Shape{9}));
32 }
33
34 TEST(type_prop, one_hot_deduce_vector_0)
35 {
36     auto param = make_shared<op::Parameter>(element::i32, Shape{8});
37     auto oh = make_shared<op::OneHot>(param, Shape{9, 8}, 0);
38     ASSERT_EQ(oh->get_element_type(), element::i32);
39     ASSERT_EQ(oh->get_shape(), (Shape{9, 8}));
40 }
41
42 TEST(type_prop, one_hot_deduce_vector_1)
43 {
44     auto param = make_shared<op::Parameter>(element::i32, Shape{8});
45     auto oh = make_shared<op::OneHot>(param, Shape{8, 9}, 1);
46     ASSERT_EQ(oh->get_element_type(), element::i32);
47     ASSERT_EQ(oh->get_shape(), (Shape{8, 9}));
48 }
49
50 TEST(type_prop, one_hot_deduce_matrix_0)
51 {
52     auto param = make_shared<op::Parameter>(element::i32, Shape{12, 24});
53     auto oh = make_shared<op::OneHot>(param, Shape{2, 12, 24}, 0);
54     ASSERT_EQ(oh->get_element_type(), element::i32);
55     ASSERT_EQ(oh->get_shape(), (Shape{2, 12, 24}));
56 }
57
58 TEST(type_prop, one_hot_deduce_matrix_1)
59 {
60     auto param = make_shared<op::Parameter>(element::i32, Shape{12, 24});
61     auto oh = make_shared<op::OneHot>(param, Shape{12, 2, 24}, 1);
62     ASSERT_EQ(oh->get_element_type(), element::i32);
63     ASSERT_EQ(oh->get_shape(), (Shape{12, 2, 24}));
64 }
65
66 TEST(type_prop, one_hot_deduce_matrix_2)
67 {
68     auto param = make_shared<op::Parameter>(element::i32, Shape{12, 24});
69     auto oh = make_shared<op::OneHot>(param, Shape{12, 24, 2}, 2);
70     ASSERT_EQ(oh->get_element_type(), element::i32);
71     ASSERT_EQ(oh->get_shape(), (Shape{12, 24, 2}));
72 }
73
74 TEST(type_prop, one_hot_deduce_et_dynamic)
75 {
76     auto param = make_shared<op::Parameter>(element::dynamic, Shape{12, 24});
77     auto oh = make_shared<op::OneHot>(param, Shape{12, 24, 2}, 2);
78     ASSERT_EQ(oh->get_element_type(), element::dynamic);
79     ASSERT_EQ(oh->get_shape(), (Shape{12, 24, 2}));
80 }
81
82 TEST(type_prop, one_hot_deduce_floating_point)
83 {
84     auto param = make_shared<op::Parameter>(element::f32, Shape{12, 24});
85     try
86     {
87         auto oh = make_shared<op::OneHot>(param, Shape{12, 24, 8}, 3);
88         // Should have thrown, so fail if it didn't
89         FAIL() << "Invalid floating-point element type not detected.";
90     }
91     catch (const NodeValidationFailure& error)
92     {
93         EXPECT_HAS_SUBSTRING(error.what(),
94                              std::string("Argument does not have integral element type."));
95     }
96     catch (...)
97     {
98         FAIL() << "Deduced type check failed for unexpected reason";
99     }
100 }
101
102 TEST(type_prop, one_hot_deduce_axis_oob)
103 {
104     auto param = make_shared<op::Parameter>(element::i32, Shape{12, 24});
105     try
106     {
107         auto oh = make_shared<op::OneHot>(param, Shape{12, 24, 8}, 3);
108         // Should have thrown, so fail if it didn't
109         FAIL() << "One-hot axis out of bounds not detected.";
110     }
111     catch (const NodeValidationFailure& error)
112     {
113         EXPECT_HAS_SUBSTRING(error.what(), std::string("One-hot axis (3) is out of bounds"));
114     }
115     catch (...)
116     {
117         FAIL() << "Deduced type check failed for unexpected reason";
118     }
119 }
120
121 TEST(type_prop, one_hot_deduce_shape_incompatible)
122 {
123     auto param = make_shared<op::Parameter>(element::i32, Shape{12, 24});
124     try
125     {
126         auto oh = make_shared<op::OneHot>(param, Shape{12, 22, 8}, 2);
127         // Should have thrown, so fail if it didn't
128         FAIL() << "Incompatible one-hot output shape not detected.";
129     }
130     catch (const ngraph_error& error)
131     {
132         EXPECT_HAS_SUBSTRING(
133             error.what(), std::string("Argument shape {12,24} does not match the expected shape"));
134     }
135     catch (...)
136     {
137         FAIL() << "Deduced type check failed for unexpected reason";
138     }
139 }
140
141 TEST(type_prop, one_hot_partial_rank_dynamic_rank_dynamic)
142 {
143     PartialShape input_shape{PartialShape::dynamic()};
144     PartialShape requested_shape{PartialShape::dynamic()};
145     size_t one_hot_axis{3000};
146
147     auto param = make_shared<op::Parameter>(element::i32, input_shape);
148     try
149     {
150         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
151         // Should have thrown, so fail if it didn't
152         FAIL() << "Dynamic rank for requested result shape not detected";
153     }
154     catch (const ngraph_error& error)
155     {
156         EXPECT_HAS_SUBSTRING(error.what(), std::string("Requested result shape has dynamic rank"));
157     }
158     catch (...)
159     {
160         FAIL() << "Deduced type check failed for unexpected reason";
161     }
162 }
163
164 TEST(type_prop, one_hot_partial_rank_dynamic_rank_static_dynamic_ok)
165 {
166     PartialShape input_shape{PartialShape::dynamic()};
167     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic()};
168     size_t one_hot_axis{2};
169
170     auto param = make_shared<op::Parameter>(element::i32, input_shape);
171     auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
172
173     ASSERT_EQ(oh->get_output_element_type(0), element::i32);
174     ASSERT_TRUE(oh->get_output_partial_shape(0).same_scheme(
175         PartialShape{Dimension::dynamic(), 2, 3, Dimension::dynamic()}));
176 }
177
178 TEST(type_prop, one_hot_partial_rank_dynamic_rank_static_dynamic_one_hot_dim_dynamic)
179 {
180     PartialShape input_shape{PartialShape::dynamic()};
181     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic()};
182     size_t one_hot_axis{3};
183
184     auto param = make_shared<op::Parameter>(element::i32, input_shape);
185     try
186     {
187         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
188         // Should have thrown, so fail if it didn't
189         FAIL() << "Dynamic one-hot dimension not detected";
190     }
191     catch (const ngraph_error& error)
192     {
193         EXPECT_HAS_SUBSTRING(error.what(),
194                              std::string("Requested result shape ({?,2,3,?}) has dynamic dimension "
195                                          "at the one-hot axis (3)"));
196     }
197     catch (...)
198     {
199         FAIL() << "Deduced type check failed for unexpected reason";
200     }
201 }
202
203 TEST(type_prop, one_hot_partial_rank_dynamic_rank_static_dynamic_one_hot_axis_oob)
204 {
205     PartialShape input_shape{PartialShape::dynamic()};
206     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic()};
207     size_t one_hot_axis{4};
208
209     auto param = make_shared<op::Parameter>(element::i32, input_shape);
210     try
211     {
212         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
213         // Should have thrown, so fail if it didn't
214         FAIL() << "One-hot axis out of bounds not detected (rank-dynamic argument, rank-static "
215                   "dynamic result shape)";
216     }
217     catch (const ngraph_error& error)
218     {
219         EXPECT_HAS_SUBSTRING(
220             error.what(),
221             std::string("One-hot axis (4) is out of bounds (requested result shape: {?,2,3,?})"));
222     }
223     catch (...)
224     {
225         FAIL() << "Deduced type check failed for unexpected reason";
226     }
227 }
228
229 TEST(type_prop, one_hot_partial_rank_static_dynamic_rank_static_dynamic_ok)
230 {
231     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic(), 4};
232     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic(), 4};
233     size_t one_hot_axis{2};
234
235     auto param = make_shared<op::Parameter>(element::i32, input_shape);
236     auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
237
238     ASSERT_EQ(oh->get_output_element_type(0), element::i32);
239     ASSERT_TRUE(oh->get_output_partial_shape(0).same_scheme(
240         PartialShape{3, 2, 3, Dimension::dynamic(), 4}));
241 }
242
243 TEST(type_prop,
244      one_hot_partial_rank_static_dynamic_rank_static_dynamic_incompatible_rank_input_short)
245 {
246     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic()};
247     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic(), 4};
248     size_t one_hot_axis{2};
249
250     auto param = make_shared<op::Parameter>(element::i32, input_shape);
251     try
252     {
253         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
254         // Should have thrown, so fail if it didn't
255         FAIL() << "Incompatible input/output ranks not detected (rank-static dynamic argument, "
256                   "rank-static dynamic result shape)";
257     }
258     catch (const ngraph_error& error)
259     {
260         EXPECT_HAS_SUBSTRING(
261             error.what(),
262             std::string("Argument shape {3,?,?} does not match the expected shape of {?,2,?,4}"));
263     }
264     catch (...)
265     {
266         FAIL() << "Deduced type check failed for unexpected reason";
267     }
268 }
269
270 TEST(type_prop,
271      one_hot_partial_rank_static_dynamic_rank_static_dynamic_incompatible_rank_input_long)
272 {
273     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic(), 4, 5};
274     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic(), 4};
275     size_t one_hot_axis{2};
276
277     auto param = make_shared<op::Parameter>(element::i32, input_shape);
278     try
279     {
280         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
281         // Should have thrown, so fail if it didn't
282         FAIL() << "Incompatible input/output ranks not detected (rank-static dynamic argument, "
283                   "rank-static dynamic result shape)";
284     }
285     catch (const ngraph_error& error)
286     {
287         EXPECT_HAS_SUBSTRING(
288             error.what(),
289             std::string(
290                 "Argument shape {3,?,?,4,5} does not match the expected shape of {?,2,?,4}"));
291     }
292     catch (...)
293     {
294         FAIL() << "Deduced type check failed for unexpected reason";
295     }
296 }
297
298 TEST(type_prop, one_hot_partial_rank_static_dynamic_rank_static_dynamic_incompatible_dim)
299 {
300     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic(), 5};
301     PartialShape requested_shape{Dimension::dynamic(), 2, 3, Dimension::dynamic(), 4};
302     size_t one_hot_axis{2};
303
304     auto param = make_shared<op::Parameter>(element::i32, input_shape);
305     try
306     {
307         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
308         // Should have thrown, so fail if it didn't
309         FAIL() << "Incompatible input/output dimensions not detected (rank-static dynamic "
310                   "argument, rank-static dynamic result shape)";
311     }
312     catch (const ngraph_error& error)
313     {
314         EXPECT_HAS_SUBSTRING(
315             error.what(),
316             std::string("Argument shape {3,?,?,5} does not match the expected shape of {?,2,?,4}"));
317     }
318     catch (...)
319     {
320         FAIL() << "Deduced type check failed for unexpected reason";
321     }
322 }
323
324 TEST(type_prop, one_hot_partial_rank_static_dynamic_rank_static_dynamic_one_hot_dim_dynamic)
325 {
326     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic(), 4};
327     PartialShape requested_shape{
328         Dimension::dynamic(), 2, Dimension::dynamic(), Dimension::dynamic(), 4};
329     size_t one_hot_axis{2};
330
331     auto param = make_shared<op::Parameter>(element::i32, input_shape);
332     try
333     {
334         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
335         // Should have thrown, so fail if it didn't
336         FAIL() << "Dynamic one-hot dimension not detected (rank-static dynamic argument, "
337                   "rank-static dynamic result shape)";
338     }
339     catch (const ngraph_error& error)
340     {
341         EXPECT_HAS_SUBSTRING(error.what(),
342                              std::string("Requested result shape ({?,2,?,?,4}) has dynamic "
343                                          "dimension at the one-hot axis (2)"));
344     }
345     catch (...)
346     {
347         FAIL() << "Deduced type check failed for unexpected reason";
348     }
349 }
350
351 TEST(type_prop, one_hot_partial_rank_static_dynamic_rank_static_dynamic_one_hot_axis_oob)
352 {
353     PartialShape input_shape{3, Dimension::dynamic(), Dimension::dynamic(), 4};
354     PartialShape requested_shape{
355         Dimension::dynamic(), 2, Dimension::dynamic(), Dimension::dynamic(), 4};
356     size_t one_hot_axis{2};
357
358     auto param = make_shared<op::Parameter>(element::i32, input_shape);
359     try
360     {
361         auto oh = make_shared<op::OneHot>(param, requested_shape, one_hot_axis);
362         // Should have thrown, so fail if it didn't
363         FAIL() << "One-hot axis out of bounds not detected (rank-static dynamic argument, "
364                   "rank-static dynamic result shape)";
365     }
366     catch (const ngraph_error& error)
367     {
368         EXPECT_HAS_SUBSTRING(error.what(),
369                              std::string("Requested result shape ({?,2,?,?,4}) has dynamic "
370                                          "dimension at the one-hot axis (2)"));
371     }
372     catch (...)
373     {
374         FAIL() << "Deduced type check failed for unexpected reason";
375     }
376 }
377
378 TEST(type_prop, one_hot_v1_output_shape)
379 {
380     auto indices = make_shared<op::Parameter>(element::i64, Shape{3});
381     auto depth = op::Constant::create(element::i64, Shape{}, {2});
382     auto on_value = op::Constant::create(element::u32, Shape{}, {5});
383     auto off_value = op::Constant::create(element::u32, Shape{}, {10});
384     int64_t axis = -1;
385     auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
386     ASSERT_EQ(ont_hot->get_element_type(), element::u32);
387     ASSERT_EQ(ont_hot->get_shape(), (Shape{3, 2}));
388 }
389
390 TEST(type_prop, one_hot_v1_output_shape_2)
391 {
392     auto indices = make_shared<op::Parameter>(element::i64, Shape{1, 3, 2, 3});
393     auto depth = op::Constant::create(element::i64, Shape{}, {4});
394     auto on_value = op::Constant::create(element::f32, Shape{}, {1.0f});
395     auto off_value = op::Constant::create(element::f32, Shape{}, {0.0f});
396     int64_t axis = 3;
397     auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
398     ASSERT_EQ(ont_hot->get_element_type(), element::f32);
399     ASSERT_EQ(ont_hot->get_shape(), (Shape{1, 3, 2, 4, 3}));
400 }
401
402 TEST(type_prop, one_hot_v1_indices_elem_not_integral)
403 {
404     auto indices = make_shared<op::Parameter>(element::f16, Shape{2, 2});
405     auto depth = make_shared<op::Parameter>(element::i64, Shape{});
406     auto on_value = make_shared<op::Parameter>(element::u32, Shape{});
407     auto off_value = make_shared<op::Parameter>(element::u32, Shape{});
408     int64_t axis = -1;
409     try
410     {
411         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
412         // Should have thrown, so fail if it didn't
413         FAIL() << "Incorrect indices element type not detected";
414     }
415     catch (const ngraph_error& error)
416     {
417         EXPECT_HAS_SUBSTRING(error.what(), std::string("Indices must be integral element type."));
418     }
419     catch (...)
420     {
421         FAIL() << "Deduced type check failed for unexpected reason";
422     }
423 }
424
425 TEST(type_prop, one_hot_v1_depth_elem_not_integral)
426 {
427     auto indices = make_shared<op::Parameter>(element::i64, Shape{2, 2});
428     auto depth = make_shared<op::Parameter>(element::f16, Shape{});
429     auto on_value = make_shared<op::Parameter>(element::u32, Shape{});
430     auto off_value = make_shared<op::Parameter>(element::u32, Shape{});
431     int64_t axis = -1;
432     try
433     {
434         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
435         // Should have thrown, so fail if it didn't
436         FAIL() << "Incorrect depth element type not detected";
437     }
438     catch (const ngraph_error& error)
439     {
440         EXPECT_HAS_SUBSTRING(error.what(), std::string("Depth must be integral element type."));
441     }
442     catch (...)
443     {
444         FAIL() << "Deduced type check failed for unexpected reason";
445     }
446 }
447
448 TEST(type_prop, one_hot_v1_on_off_values_not_compatible)
449 {
450     auto indices = make_shared<op::Parameter>(element::i64, Shape{2, 2});
451     auto depth = make_shared<op::Parameter>(element::i64, Shape{});
452     auto on_value = make_shared<op::Parameter>(element::bf16, Shape{});
453     auto off_value = make_shared<op::Parameter>(element::f16, Shape{});
454     int64_t axis = -1;
455     try
456     {
457         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
458         // Should have thrown, so fail if it didn't
459         FAIL() << "Incompatible on/off element types not detected";
460     }
461     catch (const ngraph_error& error)
462     {
463         EXPECT_HAS_SUBSTRING(
464             error.what(),
465             std::string("on_value element type must be compatible with off_value element type."));
466     }
467     catch (...)
468     {
469         FAIL() << "Deduced type check failed for unexpected reason";
470     }
471 }
472
473 TEST(type_prop, one_hot_v1_depth_not_scalar)
474 {
475     auto indices = make_shared<op::Parameter>(element::i64, Shape{2, 2});
476     auto depth = make_shared<op::Parameter>(element::i64, Shape{1});
477     auto on_value = make_shared<op::Parameter>(element::bf16, Shape{});
478     auto off_value = make_shared<op::Parameter>(element::bf16, Shape{});
479     int64_t axis = -1;
480     try
481     {
482         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
483         // Should have thrown, so fail if it didn't
484         FAIL() << "Not scalar depth input not detected.";
485     }
486     catch (const ngraph_error& error)
487     {
488         EXPECT_HAS_SUBSTRING(error.what(), std::string("depth input must be scalar."));
489     }
490     catch (...)
491     {
492         FAIL() << "Deduced type check failed for unexpected reason";
493     }
494 }
495
496 TEST(type_prop, one_hot_v1_on_value_not_scalar)
497 {
498     auto indices = make_shared<op::Parameter>(element::i64, Shape{2, 2});
499     auto depth = make_shared<op::Parameter>(element::i64, Shape{});
500     auto on_value = make_shared<op::Parameter>(element::bf16, Shape{2});
501     auto off_value = make_shared<op::Parameter>(element::bf16, Shape{});
502     int64_t axis = -1;
503     try
504     {
505         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
506         // Should have thrown, so fail if it didn't
507         FAIL() << "Not scalar on_value input not detected.";
508     }
509     catch (const ngraph_error& error)
510     {
511         EXPECT_HAS_SUBSTRING(error.what(), std::string("on_value input must be scalar."));
512     }
513     catch (...)
514     {
515         FAIL() << "Deduced type check failed for unexpected reason";
516     }
517 }
518
519 TEST(type_prop, one_hot_v1_off_value_not_scalar)
520 {
521     auto indices = make_shared<op::Parameter>(element::i64, Shape{2, 2});
522     auto depth = make_shared<op::Parameter>(element::i64, Shape{});
523     auto on_value = make_shared<op::Parameter>(element::bf16, Shape{});
524     auto off_value = make_shared<op::Parameter>(element::bf16, Shape{3});
525     int64_t axis = -1;
526     try
527     {
528         auto ont_hot = make_shared<op::v1::OneHot>(indices, depth, on_value, off_value, axis);
529         // Should have thrown, so fail if it didn't
530         FAIL() << "Not scalar off_value input not detected.";
531     }
532     catch (const ngraph_error& error)
533     {
534         EXPECT_HAS_SUBSTRING(error.what(), std::string("off_value input must be scalar."));
535     }
536     catch (...)
537     {
538         FAIL() << "Deduced type check failed for unexpected reason";
539     }
540 }