Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / type_prop / reshape.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, reshape_deduce_s2v)
27 {
28     auto param = make_shared<op::Parameter>(element::f32, Shape{});
29     auto r = make_shared<op::Reshape>(param, AxisVector{}, Shape{1});
30     ASSERT_EQ(r->get_element_type(), element::f32);
31     ASSERT_EQ(r->get_shape(), (Shape{1}));
32 }
33
34 TEST(type_prop, reshape_deduce_s2m)
35 {
36     auto param = make_shared<op::Parameter>(element::f32, Shape{});
37     auto r = make_shared<op::Reshape>(param, AxisVector{}, Shape{1, 1});
38     ASSERT_EQ(r->get_element_type(), element::f32);
39     ASSERT_EQ(r->get_shape(), (Shape{1, 1}));
40 }
41
42 TEST(type_prop, reshape_deduce_s2t)
43 {
44     auto param = make_shared<op::Parameter>(element::f32, Shape{});
45     auto r = make_shared<op::Reshape>(param, AxisVector{}, Shape{1, 1, 1});
46     ASSERT_EQ(r->get_element_type(), element::f32);
47     ASSERT_EQ(r->get_shape(), (Shape{1, 1, 1}));
48 }
49
50 TEST(type_prop, reshape_deduce_v2s)
51 {
52     auto param = make_shared<op::Parameter>(element::f32, Shape{1});
53     auto r = make_shared<op::Reshape>(param, AxisVector{0}, Shape{});
54     ASSERT_EQ(r->get_element_type(), element::f32);
55     ASSERT_EQ(r->get_shape(), (Shape{}));
56 }
57
58 TEST(type_prop, reshape_deduce_m2s)
59 {
60     auto param = make_shared<op::Parameter>(element::f32, Shape{1, 1});
61     auto r = make_shared<op::Reshape>(param, AxisVector{0, 1}, Shape{});
62     ASSERT_EQ(r->get_element_type(), element::f32);
63     ASSERT_EQ(r->get_shape(), (Shape{}));
64 }
65
66 TEST(type_prop, reshape_deduce_t2s)
67 {
68     auto param = make_shared<op::Parameter>(element::f32, Shape{1, 1, 1});
69     auto r = make_shared<op::Reshape>(param, AxisVector{0, 1, 2}, Shape{});
70     ASSERT_EQ(r->get_element_type(), element::f32);
71     ASSERT_EQ(r->get_shape(), (Shape{}));
72 }
73
74 TEST(type_prop, reshape_deduce_m2v_01)
75 {
76     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4});
77     auto r = make_shared<op::Reshape>(param, AxisVector{0, 1}, Shape{12});
78     ASSERT_EQ(r->get_element_type(), element::f32);
79     ASSERT_EQ(r->get_shape(), (Shape{12}));
80 }
81
82 TEST(type_prop, reshape_deduce_m2v_10)
83 {
84     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4});
85     auto r = make_shared<op::Reshape>(param, AxisVector{1, 0}, Shape{12});
86     ASSERT_EQ(r->get_element_type(), element::f32);
87     ASSERT_EQ(r->get_shape(), (Shape{12}));
88 }
89
90 TEST(type_prop, reshape_deduce_t2v_012)
91 {
92     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
93     auto r = make_shared<op::Reshape>(param, AxisVector{0, 1, 2}, Shape{60});
94     ASSERT_EQ(r->get_element_type(), element::f32);
95     ASSERT_EQ(r->get_shape(), (Shape{60}));
96 }
97
98 TEST(type_prop, reshape_deduce_t2v_120)
99 {
100     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
101     auto r = make_shared<op::Reshape>(param, AxisVector{1, 2, 0}, Shape{60});
102     ASSERT_EQ(r->get_element_type(), element::f32);
103     ASSERT_EQ(r->get_shape(), (Shape{60}));
104 }
105
106 TEST(type_prop, reshape_deduce_not_enough_axes)
107 {
108     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
109     try
110     {
111         auto r = make_shared<op::Reshape>(param, AxisVector{1, 0}, Shape{60});
112         // Should have thrown, so fail if it didn't
113         FAIL() << "Not enough axes not detected";
114     }
115     catch (const NodeValidationFailure& error)
116     {
117         EXPECT_HAS_SUBSTRING(
118             error.what(),
119             std::string("Input axis order is not a permutation of argument's axis indices"));
120     }
121     catch (...)
122     {
123         FAIL() << "Deduced type check failed for unexpected reason";
124     }
125 }
126
127 TEST(type_prop, reshape_deduce_too_many_axes)
128 {
129     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
130     try
131     {
132         auto r = make_shared<op::Reshape>(param, AxisVector{1, 2, 0, 3}, Shape{60});
133         // Should have thrown, so fail if it didn't
134         FAIL() << "Too many axes not detected";
135     }
136     catch (const NodeValidationFailure& error)
137     {
138         EXPECT_HAS_SUBSTRING(
139             error.what(),
140             std::string("Input axis order is not a permutation of argument's axis indices"));
141     }
142     catch (...)
143     {
144         FAIL() << "Deduced type check failed for unexpected reason";
145     }
146 }
147
148 TEST(type_prop, reshape_deduce_duplicate_axes)
149 {
150     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
151     try
152     {
153         auto r = make_shared<op::Reshape>(param, AxisVector{1, 1, 0}, Shape{60});
154         // Should have thrown, so fail if it didn't
155         FAIL() << "Too many axes not detected";
156     }
157     catch (const NodeValidationFailure& error)
158     {
159         EXPECT_HAS_SUBSTRING(
160             error.what(),
161             std::string("Input axis order is not a permutation of argument's axis indices"));
162     }
163     catch (...)
164     {
165         FAIL() << "Deduced type check failed for unexpected reason";
166     }
167 }
168
169 TEST(type_prop, reshape_deduce_wrong_output_shape)
170 {
171     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
172     try
173     {
174         auto r = make_shared<op::Reshape>(param, AxisVector{1, 2, 0}, Shape{3, 3, 3});
175         // Should have thrown, so fail if it didn't
176         FAIL() << "Too many axes not detected";
177     }
178     catch (const NodeValidationFailure& error)
179     {
180         EXPECT_HAS_SUBSTRING(error.what(),
181                              std::string("Product of output shape dimensions does not match "
182                                          "product of argument shape dimensions"));
183     }
184     catch (...)
185     {
186         FAIL() << "Deduced type check failed for unexpected reason";
187     }
188 }
189
190 //
191 // Input shape rank dynamic, so we should set the desired output shape if the axis vector is not
192 // known invalid (invalid means it's not a permutation of {0,...,n-1} for any n).
193 //
194 TEST(type_prop, reshape_partial_rank_dynamic_axisvector_ok)
195 {
196     auto param = make_shared<op::Parameter>(element::f32, PartialShape::dynamic());
197     auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0, 3}, Shape{3, 1, 8, 2});
198     ASSERT_EQ(r->get_element_type(), element::f32);
199     ASSERT_TRUE(r->get_output_partial_shape(0).is_static());
200     ASSERT_EQ(r->get_shape(), (Shape{3, 1, 8, 2}));
201 }
202
203 TEST(type_prop, reshape_partial_rank_dynamic_axisvector_not_ok)
204 {
205     auto param = make_shared<op::Parameter>(element::f32, PartialShape::dynamic());
206     try
207     {
208         auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0, 4}, Shape{3, 1, 8, 2});
209         // Should have thrown, so fail if it didn't
210         FAIL() << "Did not detect malformed AxisVector (input shape rank dynamic)";
211     }
212     catch (const NodeValidationFailure& error)
213     {
214         EXPECT_HAS_SUBSTRING(
215             error.what(),
216             std::string("Input axis order is not a permutation of argument's axis indices"));
217     }
218     catch (...)
219     {
220         FAIL() << "Deduced type check failed for unexpected reason";
221     }
222 }
223
224 //
225 // Input shape rank static but input shape is dynamic, so should set desired output shape if the
226 // axis vector is consistent with the static rank.
227 //
228 TEST(type_prop, reshape_partial_rank_static_dynamic_axisvector_ok)
229 {
230     auto param_shape =
231         PartialShape{Dimension::dynamic(), 6, Dimension::dynamic(), Dimension::dynamic()};
232     auto param = make_shared<op::Parameter>(element::f32, param_shape);
233     auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0, 3}, Shape{3, 1, 8, 2});
234     ASSERT_EQ(r->get_element_type(), element::f32);
235     ASSERT_TRUE(r->get_output_partial_shape(0).is_static());
236     ASSERT_EQ(r->get_shape(), (Shape{3, 1, 8, 2}));
237 }
238
239 TEST(type_prop, reshape_partial_rank_static_dynamic_axisvector_not_ok)
240 {
241     auto param_shape =
242         PartialShape{Dimension::dynamic(), 6, Dimension::dynamic(), Dimension::dynamic()};
243     auto param = make_shared<op::Parameter>(element::f32, param_shape);
244     try
245     {
246         auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0}, Shape{3, 1, 8, 2});
247         // Should have thrown, so fail if it didn't
248         FAIL() << "Did not detect AxisVector inconsistent with rank (rank-static dynamic shape)";
249     }
250     catch (const NodeValidationFailure& error)
251     {
252         EXPECT_HAS_SUBSTRING(
253             error.what(),
254             std::string("Input axis order is not a permutation of argument's axis indices"));
255     }
256     catch (...)
257     {
258         FAIL() << "Deduced type check failed for unexpected reason";
259     }
260 }
261
262 //
263 // Input shape rank static but input shape is dynamic, _but_ one of its static dimensions is zero,
264 // so should set desired output shape only if it also has zero elements.
265 //
266 TEST(type_prop, reshape_partial_rank_static_dynamic_but_zero_ok)
267 {
268     auto param_shape =
269         PartialShape{Dimension::dynamic(), 0, Dimension::dynamic(), Dimension::dynamic()};
270     auto param = make_shared<op::Parameter>(element::f32, PartialShape::dynamic());
271     auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0, 3}, Shape{3, 1, 0, 2});
272     ASSERT_EQ(r->get_element_type(), element::f32);
273     ASSERT_TRUE(r->get_output_partial_shape(0).is_static());
274     ASSERT_EQ(r->get_shape(), (Shape{3, 1, 0, 2}));
275 }
276
277 TEST(type_prop, reshape_partial_rank_static_dynamic_but_zero_not_ok)
278 {
279     auto param_shape =
280         PartialShape{Dimension::dynamic(), 0, Dimension::dynamic(), Dimension::dynamic()};
281     auto param = make_shared<op::Parameter>(element::f32, param_shape);
282     try
283     {
284         auto r = make_shared<op::Reshape>(param, AxisVector{2, 1, 0}, Shape{3, 1, 8, 2});
285         // Should have thrown, so fail if it didn't
286         FAIL() << "Did not detect inconsistent output shape with static-zero-element rank-dynamic"
287                   " static input shape";
288     }
289     catch (const NodeValidationFailure& error)
290     {
291         EXPECT_HAS_SUBSTRING(
292             error.what(),
293             std::string("Input axis order is not a permutation of argument's axis indices"));
294     }
295     catch (...)
296     {
297         FAIL() << "Deduced type check failed for unexpected reason";
298     }
299 }