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/merge.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
28 class TestMerge : public UT::Common {
29 public:
TestMerge()30 TestMerge() {}
SetUp()31 void SetUp() {}
TearDown()32 void TearDown() {}
33 };
34
TEST_F(TestMerge,test_ops_merge1)35 TEST_F(TestMerge, test_ops_merge1) {
36 auto merge = std::make_shared<Merge>();
37 merge->Init();
38 auto input_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{2, 4});
39 auto input_y = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{2, 4});
40 MS_EXCEPTION_IF_NULL(input_x);
41 MS_EXCEPTION_IF_NULL(input_y);
42 std::vector<ValuePtr> inputs_ = {input_x, input_y};
43 auto input = std::make_shared<ValueTuple>(inputs_);
44 auto abstract = merge->Infer({input->ToAbstract()});
45 MS_EXCEPTION_IF_NULL(abstract);
46 auto shape_ptr = abstract->BuildShape();
47 MS_EXCEPTION_IF_NULL(shape_ptr);
48 EXPECT_EQ(shape_ptr->isa<abstract::TupleShape>(), true);
49 auto shape = shape_ptr->cast<abstract::TupleShapePtr>();
50 MS_EXCEPTION_IF_NULL(shape);
51 auto shape_vec = shape->shape();
52 EXPECT_EQ(shape_vec.size(), 2);
53 auto shape1 = shape_vec[0]->cast<abstract::ShapePtr>()->shape();
54 EXPECT_EQ(shape1.size(), 2);
55 EXPECT_EQ(shape1[0], 2);
56 EXPECT_EQ(shape1[1], 4);
57 auto shape2 = shape_vec[1]->cast<abstract::ShapePtr>()->shape();
58 EXPECT_EQ(shape2.size(), 1);
59 EXPECT_EQ(shape2[0], 1);
60 auto type_ptr = abstract->BuildType();
61 MS_EXCEPTION_IF_NULL(type_ptr);
62 auto type = type_ptr->cast<TuplePtr>();
63 auto type_vec = type->elements();
64 MS_EXCEPTION_IF_NULL(type_vec[0]);
65 auto data_type1 = type_vec[0]->cast<TensorTypePtr>()->element();
66 MS_EXCEPTION_IF_NULL(data_type1);
67 EXPECT_EQ(data_type1->type_id(), kNumberTypeFloat32);
68 auto data_type2 = type_vec[1]->cast<TensorTypePtr>()->element();
69 MS_EXCEPTION_IF_NULL(data_type2);
70 EXPECT_EQ(data_type2->type_id(), kNumberTypeInt32);
71 }
72
73 } // namespace ops
74 } // namespace mindspore
75