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/concat.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 TestConcat : public UT::Common {
28 public:
TestConcat()29 TestConcat() {}
SetUp()30 void SetUp() {}
TearDown()31 void TearDown() {}
32 };
33
TEST_F(TestConcat,test_ops_concat1)34 TEST_F(TestConcat, test_ops_concat1) {
35 auto concat = std::make_shared<Concat>();
36 concat->Init(1);
37 auto tensor_x1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 2, 7, 7});
38 auto tensor_x2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 3, 7, 7});
39 auto tensor_x3 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{3, 4, 7, 7});
40 MS_EXCEPTION_IF_NULL(tensor_x1);
41 MS_EXCEPTION_IF_NULL(tensor_x2);
42 MS_EXCEPTION_IF_NULL(tensor_x3);
43 auto input_tuple = std::make_shared<ValueTuple>(std::vector<ValuePtr>{tensor_x1, tensor_x2, tensor_x3});
44 auto abstract = concat->Infer({input_tuple->ToAbstract()});
45 MS_EXCEPTION_IF_NULL(abstract);
46 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
47 auto shape_ptr = abstract->BuildShape();
48 MS_EXCEPTION_IF_NULL(shape_ptr);
49 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
50 auto shape = shape_ptr->cast<abstract::ShapePtr>();
51 MS_EXCEPTION_IF_NULL(shape);
52 auto shape_vec = shape->shape();
53 auto type = abstract->BuildType();
54 MS_EXCEPTION_IF_NULL(type);
55 EXPECT_EQ(type->isa<TensorType>(), true);
56 auto tensor_type = type->cast<TensorTypePtr>();
57 MS_EXCEPTION_IF_NULL(tensor_type);
58 auto data_type = tensor_type->element();
59 MS_EXCEPTION_IF_NULL(data_type);
60 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
61 EXPECT_EQ(shape_vec.size(), 4);
62 EXPECT_EQ(shape_vec[0], 3);
63 EXPECT_EQ(shape_vec[1], 9);
64 EXPECT_EQ(shape_vec[2], 7);
65 EXPECT_EQ(shape_vec[3], 7);
66 }
67
TEST_F(TestConcat,test_ops_concat2)68 TEST_F(TestConcat, test_ops_concat2) {
69 auto concat = std::make_shared<Concat>();
70 concat->Init(2);
71 auto tensor_x1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{3, 4, 5});
72 auto tensor_x2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{3, 4, 2});
73 auto tensor_x3 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{3, 4, 3});
74 MS_EXCEPTION_IF_NULL(tensor_x1);
75 MS_EXCEPTION_IF_NULL(tensor_x2);
76 MS_EXCEPTION_IF_NULL(tensor_x3);
77 auto input_tuple = std::make_shared<ValueTuple>(std::vector<ValuePtr>{tensor_x1, tensor_x2, tensor_x3});
78 auto abstract = concat->Infer({input_tuple->ToAbstract()});
79 MS_EXCEPTION_IF_NULL(abstract);
80 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
81 auto shape_ptr = abstract->BuildShape();
82 MS_EXCEPTION_IF_NULL(shape_ptr);
83 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
84 auto shape = shape_ptr->cast<abstract::ShapePtr>();
85 MS_EXCEPTION_IF_NULL(shape);
86 auto shape_vec = shape->shape();
87 auto type = abstract->BuildType();
88 MS_EXCEPTION_IF_NULL(type);
89 EXPECT_EQ(type->isa<TensorType>(), true);
90 auto tensor_type = type->cast<TensorTypePtr>();
91 MS_EXCEPTION_IF_NULL(tensor_type);
92 auto data_type = tensor_type->element();
93 MS_EXCEPTION_IF_NULL(data_type);
94 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16);
95 EXPECT_EQ(shape_vec.size(), 3);
96 EXPECT_EQ(shape_vec[0], 3);
97 EXPECT_EQ(shape_vec[1], 4);
98 EXPECT_EQ(shape_vec[2], 10);
99 }
100 } // namespace ops
101 } // namespace mindspore
102