1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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 //*****************************************************************************
17 #include "gtest/gtest.h"
18 #include "ngraph/ngraph.hpp"
19 #include "util/type_prop.hpp"
21 NGRAPH_SUPPRESS_DEPRECATED_START
24 using namespace ngraph;
26 TEST(type_prop, sum_deduce)
28 auto param_0 = make_shared<op::Parameter>(element::f32, Shape{2, 4});
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}));
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}));
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{}));
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}));
47 TEST(type_prop, sum_axis_oob)
49 auto param_0 = make_shared<op::Parameter>(element::f32, Shape{2, 4});
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";
57 catch (const NodeValidationFailure& error)
59 EXPECT_HAS_SUBSTRING(error.what(), std::string("Reduction axis (2) is out of bounds"));
63 FAIL() << "Deduced type check failed for unexpected reason";
67 TEST(type_prop, sum_dynamic_axes)
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);
73 EXPECT_EQ(sum->get_output_element_type(0), element::f32);
74 EXPECT_TRUE(sum->get_output_partial_shape(0).rank().is_dynamic());
77 TEST(type_prop, sum_partial_rank_dynamic)
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);
83 EXPECT_EQ(sum->get_output_element_type(0), element::f32);
84 EXPECT_TRUE(sum->get_output_partial_shape(0).is_dynamic());
87 TEST(type_prop, sum_partial_rank_static_dynamic_ok_result_static)
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);
94 EXPECT_EQ(sum->get_output_element_type(0), element::f32);
95 EXPECT_EQ(sum->get_shape(), (Shape{1, 2, 5}));
98 TEST(type_prop, sum_partial_rank_static_dynamic_ok_result_dynamic)
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);
105 EXPECT_EQ(sum->get_output_element_type(0), element::f32);
107 sum->get_output_partial_shape(0).same_scheme(PartialShape{1, 2, Dimension::dynamic()}));
110 TEST(type_prop, sum_partial_rank_static_dynamic_axes_oob)
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};
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)";
122 catch (const NodeValidationFailure& error)
124 EXPECT_HAS_SUBSTRING(error.what(), std::string("Reduction axis (5) is out of bounds"));
128 FAIL() << "Deduced type check failed for unexpected reason";
132 TEST(type_prop, sum_partial_negative_axes)
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);
139 EXPECT_EQ(sum->get_output_element_type(0), element::f32);
140 EXPECT_EQ(sum->get_shape(), (Shape{1, 2, 5}));