1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
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 #include <vector>
17 #include <memory>
18 #include "common/common_test.h"
19 #include "ops/addn.h"
20 #include "ir/dtype/type.h"
21 #include "ir/value.h"
22 #include "abstract/dshape.h"
23 #include "utils/tensor_construct_utils.h"
24
25 namespace mindspore {
26 namespace ops {
27 class TestAddN : public UT::Common {
28 public:
TestAddN()29 TestAddN() {}
SetUp()30 void SetUp() {}
TearDown()31 void TearDown() {}
32 };
33
TEST_F(TestAddN,test_ops_addn1)34 TEST_F(TestAddN, test_ops_addn1) {
35 auto addn = std::make_shared<AddN>();
36 auto tensor_x1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 2, 7, 7});
37 auto tensor_x2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 2, 7, 7});
38 MS_EXCEPTION_IF_NULL(tensor_x1);
39 MS_EXCEPTION_IF_NULL(tensor_x2);
40 auto input_tuple = std::make_shared<ValueTuple>(std::vector<ValuePtr>{tensor_x1, tensor_x2});
41 auto abstract = addn->Infer({input_tuple->ToAbstract()});
42 MS_EXCEPTION_IF_NULL(abstract);
43 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
44 auto shape_ptr = abstract->BuildShape();
45 MS_EXCEPTION_IF_NULL(shape_ptr);
46 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
47 auto shape = shape_ptr->cast<abstract::ShapePtr>();
48 MS_EXCEPTION_IF_NULL(shape);
49 auto shape_vec = shape->shape();
50 auto type = abstract->BuildType();
51 MS_EXCEPTION_IF_NULL(type);
52 EXPECT_EQ(type->isa<TensorType>(), true);
53 auto tensor_type = type->cast<TensorTypePtr>();
54 MS_EXCEPTION_IF_NULL(tensor_type);
55 auto data_type = tensor_type->element();
56 MS_EXCEPTION_IF_NULL(data_type);
57 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
58 EXPECT_EQ(shape_vec.size(), 4);
59 EXPECT_EQ(shape_vec[0], 3);
60 EXPECT_EQ(shape_vec[1], 2);
61 EXPECT_EQ(shape_vec[2], 7);
62 EXPECT_EQ(shape_vec[3], 7);
63 }
64
TEST_F(TestAddN,test_ops_addn2)65 TEST_F(TestAddN, test_ops_addn2) {
66 auto addn = std::make_shared<AddN>();
67 auto tensor_x1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeBool, std::vector<int64_t>{3, 4, 5});
68 auto tensor_x2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeBool, std::vector<int64_t>{3, 4, 5});
69 auto tensor_x3 = TensorConstructUtils::CreateOnesTensor(kNumberTypeBool, std::vector<int64_t>{3, 4, 5});
70 MS_EXCEPTION_IF_NULL(tensor_x1);
71 MS_EXCEPTION_IF_NULL(tensor_x2);
72 MS_EXCEPTION_IF_NULL(tensor_x3);
73 auto input_tuple = std::make_shared<ValueTuple>(std::vector<ValuePtr>{tensor_x1, tensor_x2, tensor_x3});
74 auto abstract = addn->Infer({input_tuple->ToAbstract()});
75 MS_EXCEPTION_IF_NULL(abstract);
76 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
77 auto shape_ptr = abstract->BuildShape();
78 MS_EXCEPTION_IF_NULL(shape_ptr);
79 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
80 auto shape = shape_ptr->cast<abstract::ShapePtr>();
81 MS_EXCEPTION_IF_NULL(shape);
82 auto shape_vec = shape->shape();
83 auto type = abstract->BuildType();
84 MS_EXCEPTION_IF_NULL(type);
85 EXPECT_EQ(type->isa<TensorType>(), true);
86 auto tensor_type = type->cast<TensorTypePtr>();
87 MS_EXCEPTION_IF_NULL(tensor_type);
88 auto data_type = tensor_type->element();
89 MS_EXCEPTION_IF_NULL(data_type);
90 EXPECT_EQ(data_type->type_id(), kNumberTypeBool);
91 EXPECT_EQ(shape_vec.size(), 3);
92 EXPECT_EQ(shape_vec[0], 3);
93 EXPECT_EQ(shape_vec[1], 4);
94 EXPECT_EQ(shape_vec[2], 5);
95 }
96 } // namespace ops
97 } // namespace mindspore
98