Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / type_prop / sum.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, sum_deduce)
27 {
28     auto param_0 = make_shared<op::Parameter>(element::f32, Shape{2, 4});
29
30     auto r0 = make_shared<op::Sum>(param_0, AxisSet{0});
31     ASSERT_EQ(r0->get_element_type(), element::f32);
32     ASSERT_EQ(r0->get_shape(), (Shape{4}));
33
34     auto r1 = make_shared<op::Sum>(param_0, AxisSet{1});
35     ASSERT_EQ(r1->get_element_type(), element::f32);
36     ASSERT_EQ(r1->get_shape(), (Shape{2}));
37
38     auto r01 = make_shared<op::Sum>(param_0, AxisSet{0, 1});
39     ASSERT_EQ(r01->get_element_type(), element::f32);
40     ASSERT_EQ(r01->get_shape(), (Shape{}));
41
42     auto r_none = make_shared<op::Sum>(param_0, AxisSet{});
43     ASSERT_EQ(r_none->get_element_type(), element::f32);
44     ASSERT_EQ(r_none->get_shape(), (Shape{2, 4}));
45 }
46
47 TEST(type_prop, sum_axis_oob)
48 {
49     auto param_0 = make_shared<op::Parameter>(element::f32, Shape{2, 4});
50
51     try
52     {
53         auto r = make_shared<op::Sum>(param_0, AxisSet{0, 2, 1});
54         // Should have thrown, so fail if it didn't
55         FAIL() << "Did not detect out-of-bound axis for sum";
56     }
57     catch (const NodeValidationFailure& error)
58     {
59         EXPECT_HAS_SUBSTRING(error.what(), std::string("Reduction axis (2) is out of bounds"));
60     }
61     catch (...)
62     {
63         FAIL() << "Deduced type check failed for unexpected reason";
64     }
65 }
66
67 TEST(type_prop, sum_dynamic_axes)
68 {
69     auto param = make_shared<op::Parameter>(element::f32, Shape{3, 4, 5});
70     auto summation_axes = make_shared<op::Parameter>(element::i64, Shape{2});
71     auto sum = make_shared<op::Sum>(param, summation_axes);
72
73     EXPECT_EQ(sum->get_output_element_type(0), element::f32);
74     EXPECT_TRUE(sum->get_output_partial_shape(0).rank().is_dynamic());
75 }
76
77 TEST(type_prop, sum_partial_rank_dynamic)
78 {
79     auto param = make_shared<op::Parameter>(element::f32, PartialShape::dynamic());
80     auto summation_axes = AxisSet{2385, 0, 4404}; // arbitrary
81     auto sum = make_shared<op::Sum>(param, summation_axes);
82
83     EXPECT_EQ(sum->get_output_element_type(0), element::f32);
84     EXPECT_TRUE(sum->get_output_partial_shape(0).is_dynamic());
85 }
86
87 TEST(type_prop, sum_partial_rank_static_dynamic_ok_result_static)
88 {
89     auto param =
90         make_shared<op::Parameter>(element::f32, PartialShape{1, 2, Dimension::dynamic(), 4, 5});
91     auto summation_axes = AxisSet{2, 3};
92     auto sum = make_shared<op::Sum>(param, summation_axes);
93
94     EXPECT_EQ(sum->get_output_element_type(0), element::f32);
95     EXPECT_EQ(sum->get_shape(), (Shape{1, 2, 5}));
96 }
97
98 TEST(type_prop, sum_partial_rank_static_dynamic_ok_result_dynamic)
99 {
100     auto param = make_shared<op::Parameter>(
101         element::f32, PartialShape{1, 2, Dimension::dynamic(), 4, Dimension::dynamic()});
102     auto summation_axes = AxisSet{2, 3};
103     auto sum = make_shared<op::Sum>(param, summation_axes);
104
105     EXPECT_EQ(sum->get_output_element_type(0), element::f32);
106     EXPECT_TRUE(
107         sum->get_output_partial_shape(0).same_scheme(PartialShape{1, 2, Dimension::dynamic()}));
108 }
109
110 TEST(type_prop, sum_partial_rank_static_dynamic_axes_oob)
111 {
112     auto param = make_shared<op::Parameter>(
113         element::f32, PartialShape{1, 2, Dimension::dynamic(), 4, Dimension::dynamic()});
114     auto summation_axes = AxisSet{2, 5, 1};
115
116     try
117     {
118         auto sum = make_shared<op::Sum>(param, summation_axes);
119         // Should have thrown, so fail if it didn't
120         FAIL() << "Did not detect out-of-bound axis for sum (rank-static dynamic input)";
121     }
122     catch (const NodeValidationFailure& error)
123     {
124         EXPECT_HAS_SUBSTRING(error.what(), std::string("Reduction axis (5) is out of bounds"));
125     }
126     catch (...)
127     {
128         FAIL() << "Deduced type check failed for unexpected reason";
129     }
130 }
131
132 TEST(type_prop, sum_partial_negative_axes)
133 {
134     auto param =
135         make_shared<op::Parameter>(element::f32, PartialShape{1, 2, Dimension::dynamic(), 4, 5});
136     auto summation_axes = op::Constant::create(element::i64, Shape{2}, {-3, -2});
137     auto sum = make_shared<op::Sum>(param, summation_axes);
138
139     EXPECT_EQ(sum->get_output_element_type(0), element::f32);
140     EXPECT_EQ(sum->get_shape(), (Shape{1, 2, 5}));
141 }