1 /**
2 * Copyright 2019 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
17 #include <string>
18 #include <list>
19 #include <vector>
20 #include "common/common_test.h"
21 #include "frontend/parallel/strategy.h"
22 #include "frontend/parallel/ops_info/transpose_info.h"
23 #include "frontend/parallel/device_manager.h"
24 #include "frontend/parallel/step_parallel.h"
25
26 namespace mindspore {
27 namespace parallel {
28
29 class TransposeInfo;
30 using TransposeInfoPtr = std::shared_ptr<TransposeInfo>;
31 TransposeInfoPtr transpose;
32
33 class TestTransposeInfo : public UT::Common {
34 public:
TestTransposeInfo()35 TestTransposeInfo() {}
36 void SetUp();
TearDown()37 void TearDown() {}
38 };
39
SetUp()40 void TestTransposeInfo::SetUp() {
41 RankList dev_list;
42
43 for (int32_t i = 0; i < 34; i++) {
44 dev_list.push_back(i);
45 }
46
47 RankList stage_map;
48 stage_map.push_back(32);
49 stage_map.push_back(2);
50
51 int32_t local_dev = 0;
52
53 // create a new g_device_manager
54 g_device_manager = std::make_shared<DeviceManager>();
55 g_device_manager->Init(dev_list, local_dev, stage_map, "hccl");
56
57 std::unordered_map<std::string, ValuePtr> attr;
58
59 Shapes inputs_shape = {{128, 64}};
60 Shapes outputs_shape = {{64, 128}};
61 std::vector<int64_t> axis = {1, 0};
62 ValuePtr val0;
63 ValuePtr val1 = MakeValue(axis);
64 std::vector<ValuePtr> val = {val0, val1};
65
66 transpose = std::make_shared<TransposeInfo>("transpose_info", inputs_shape, outputs_shape, attr);
67 transpose->set_input_value(val);
68 }
69
TEST_F(TestTransposeInfo,InferDevMatrixShape1)70 TEST_F(TestTransposeInfo, InferDevMatrixShape1) {
71 Strategys inputs = {{4, 8}};
72 StrategyPtr strategy = NewStrategy(0, inputs);
73
74 transpose->Init(strategy);
75 Shape dev_matrix_shape = transpose->dev_matrix_shape();
76
77 Shape expect = {4, 8};
78 ASSERT_EQ(dev_matrix_shape, expect);
79 }
80
TEST_F(TestTransposeInfo,InferDevMatrixShape2)81 TEST_F(TestTransposeInfo, InferDevMatrixShape2) {
82 Strategys inputs = {{4, 1}};
83 StrategyPtr strategy = NewStrategy(0, inputs);
84
85 transpose->Init(strategy);
86 Shape dev_matrix_shape = transpose->dev_matrix_shape();
87
88 Shape expect = {4, 1, 8};
89 ASSERT_EQ(dev_matrix_shape, expect);
90 }
91
TEST_F(TestTransposeInfo,InferSliceShape1)92 TEST_F(TestTransposeInfo, InferSliceShape1) {
93 Strategys str = {{4, 8}};
94 StrategyPtr strategy = NewStrategy(0, str);
95
96 transpose->Init(strategy);
97 std::vector<TensorInfo> inputs = transpose->inputs_tensor_info();
98 std::vector<TensorInfo> outputs = transpose->outputs_tensor_info();
99
100 Shape input_slice_shape_expect = {32, 8};
101 Shape output_slice_shape_expect = {8, 32};
102
103 TensorInfo input_tensor_info = inputs.at(0);
104 TensorInfo output_tensor_info = outputs.at(0);
105
106 Shape input_slice_shape = input_tensor_info.slice_shape();
107 Shape output_slice_shape = output_tensor_info.slice_shape();
108
109 ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
110 ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
111 }
112
TEST_F(TestTransposeInfo,GetTensorLayout1)113 TEST_F(TestTransposeInfo, GetTensorLayout1) {
114 Strategys str = {{4, 8}};
115 StrategyPtr strategy = NewStrategy(0, str);
116
117 transpose->Init(strategy);
118 std::vector<TensorInfo> inputs = transpose->inputs_tensor_info();
119 std::vector<TensorInfo> outputs = transpose->outputs_tensor_info();
120
121 TensorMap input_expect = {1, 0};
122 TensorMap output_expect = {0, 1};
123
124 TensorInfo input_tensor_info = inputs.at(0);
125 TensorInfo output_tensor_info = outputs.at(0);
126
127 Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
128 Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
129
130 ASSERT_EQ(input_tensor_map.array(), input_expect);
131 ASSERT_EQ(output_tensor_map.array(), output_expect);
132 }
133
TEST_F(TestTransposeInfo,GetForwardOp1)134 TEST_F(TestTransposeInfo, GetForwardOp1) {
135 Strategys inputs = {{4, 8}};
136 StrategyPtr strategy = NewStrategy(0, inputs);
137
138 transpose->Init(strategy);
139 OperatorVector forward_op = transpose->forward_op();
140 size_t size = forward_op.size();
141
142 ASSERT_EQ(size, 0);
143 }
144
TEST_F(TestTransposeInfo,GetMirrorOPs1)145 TEST_F(TestTransposeInfo, GetMirrorOPs1) {
146 Strategys inputs = {{4, 8}};
147 StrategyPtr strategy = NewStrategy(0, inputs);
148
149 transpose->Init(strategy);
150 MirrorOps mirror_ops = transpose->mirror_ops();
151
152 size_t size = mirror_ops.size();
153
154 ASSERT_EQ(size, 0);
155 }
156
TEST_F(TestTransposeInfo,CheckStrategy1)157 TEST_F(TestTransposeInfo, CheckStrategy1) {
158 Strategys inputs = {{1, 4, 8}};
159 StrategyPtr strategy = NewStrategy(0, inputs);
160
161 Status ret = transpose->Init(strategy);
162 ASSERT_EQ(ret, FAILED);
163 }
164
TEST_F(TestTransposeInfo,CheckStrategy2)165 TEST_F(TestTransposeInfo, CheckStrategy2) {
166 Strategys inputs = {{2, 4, 8}, {2, 4, 8}};
167 StrategyPtr strategy = NewStrategy(0, inputs);
168
169 Status ret = transpose->Init(strategy);
170 ASSERT_EQ(ret, FAILED);
171 }
172
TEST_F(TestTransposeInfo,CheckStrategy3)173 TEST_F(TestTransposeInfo, CheckStrategy3) {
174 Strategys inputs = {{4, 8}};
175 StrategyPtr strategy = NewStrategy(0, inputs);
176
177 Status ret = transpose->Init(strategy);
178 ASSERT_EQ(ret, SUCCESS);
179 }
180
TEST_F(TestTransposeInfo,AutoStrategy1)181 TEST_F(TestTransposeInfo, AutoStrategy1) {
182 ASSERT_EQ(transpose->GenerateStrategies(0), Status::SUCCESS);
183 std::vector<std::shared_ptr<StrategyWithCost>> sc = transpose->GetStrategyCost();
184
185 Shapes splittable_inputs = {{1, 1}};
186 std::vector<StrategyPtr> sp_vector;
187 Shapes inputs_shape = {{128, 64}};
188 GenerateStrategiesForIndependentInputs(0, inputs_shape, splittable_inputs, &sp_vector);
189 ASSERT_EQ(sc.size(), sp_vector.size());
190 }
191
192 } // namespace parallel
193 } // namespace mindspore
194