• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/prelu_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 PReLUInfo;
30 using PReLUInfoPtr = std::shared_ptr<PReLUInfo>;
31 PReLUInfoPtr prelu;
32 PReLUInfoPtr prelu_2d;
33 
34 class TestPReLUInfo : public UT::Common {
35  public:
36   TestPReLUInfo() {}
37   void SetUp();
38   void TearDown() {}
39 };
40 
41 void TestPReLUInfo::SetUp() {
42   RankList dev_list;
43 
44   for (int32_t i = 0; i < 1050; i++) {
45     dev_list.push_back(i);
46   }
47 
48   RankList stage_map;
49   stage_map.push_back(1024);
50   stage_map.push_back(26);
51   int32_t local_dev = 0;
52   // create a new g_device_manager
53   g_device_manager = std::make_shared<DeviceManager>();
54   g_device_manager->Init(dev_list, local_dev, stage_map, "hccl");
55   Shapes inputs_shape = {{64, 4, 8, 16}, {4}};
56   Shapes outputs_shape = {{64, 4, 8, 16}};
57   std::unordered_map<std::string, ValuePtr> attr;
58   prelu = std::make_shared<PReLUInfo>("prelu_info", inputs_shape, outputs_shape, attr);
59 
60   Shapes inputs_shape_2d = {{1024, 4}, {4}};
61   Shapes outputs_shape_2d = {{1024, 4}};
62   std::unordered_map<std::string, ValuePtr> attr_2d;
63   prelu_2d = std::make_shared<PReLUInfo>("prelu_info", inputs_shape_2d, outputs_shape_2d, attr_2d);
64 }
65 
66 TEST_F(TestPReLUInfo, InferDevMatrixShape1) {
67   Strategys inputs = {{2, 1, 8, 16}, {1}};
68   StrategyPtr strategy = NewStrategy(0, inputs);
69 
70   prelu->Init(strategy);
71   Shape dev_matrix_shape = prelu->dev_matrix_shape();
72 
73   Shape expect = {2, 1, 8, 16, 4};
74   ASSERT_EQ(dev_matrix_shape, expect);
75 }
76 
77 TEST_F(TestPReLUInfo, InferSliceShape1) {
78   Strategys str = {{2, 1, 8, 16}, {1}};
79   StrategyPtr strategy = NewStrategy(0, str);
80 
81   prelu->Init(strategy);
82   std::vector<TensorInfo> inputs = prelu->inputs_tensor_info();
83   std::vector<TensorInfo> outputs = prelu->outputs_tensor_info();
84 
85   Shape input_slice_shape_expect = {32, 4, 1, 1};
86   Shape param_slice_shape_expect = {4};
87   Shape output_slice_shape_expect = {32, 4, 1, 1};
88 
89   TensorInfo input_tensor_info = inputs.at(0);
90   TensorInfo param_tensor_info = inputs.at(1);
91   TensorInfo output_tensor_info = outputs.at(0);
92 
93   Shape input_slice_shape = input_tensor_info.slice_shape();
94   Shape output_slice_shape = output_tensor_info.slice_shape();
95 
96   ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
97   ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
98 }
99 
100 TEST_F(TestPReLUInfo, GetTensorLayout1) {
101   Strategys str = {{2, 1, 8, 16}, {1}};
102   StrategyPtr strategy = NewStrategy(0, str);
103 
104   prelu->Init(strategy);
105   std::vector<TensorInfo> inputs = prelu->inputs_tensor_info();
106   std::vector<TensorInfo> outputs = prelu->outputs_tensor_info();
107 
108   TensorMap input_expect = {4, 3, 2, 1};
109   TensorMap param_expect = {2};
110   TensorMap output_expect = {4, 3, 2, 1};
111 
112   TensorInfo input_tensor_info = inputs.at(0);
113   TensorInfo param_tensor_info = inputs.at(1);
114   TensorInfo output_tensor_info = outputs.at(0);
115 
116   Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
117   Map param_tensor_map = param_tensor_info.tensor_layout().origin_tensor_map();
118   Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
119 
120   ASSERT_EQ(input_tensor_map.array(), input_expect);
121   ASSERT_EQ(output_tensor_map.array(), output_expect);
122 }
123 
124 TEST_F(TestPReLUInfo, GetMirrorOPs1) {
125   Strategys str = {{2, 1, 2, 2}, {1}};
126   StrategyPtr strategy = NewStrategy(0, str);
127   prelu->Init(strategy);
128   MirrorOps mirror_ops = prelu->mirror_ops();
129   OperatorVector mirror_op = mirror_ops.at(1);
130   OperatorArgs operator_args = mirror_op.at(0).second;
131   std::string arg0_name = operator_args.first.at(0).first;
132   ValuePtr arg0_value = operator_args.first.at(0).second;
133   std::string group = arg0_value->cast<StringImmPtr>()->ToString();
134 
135   ASSERT_EQ(mirror_op.at(0).first, "_MirrorOperator");
136   ASSERT_EQ(mirror_op.size(), 1);
137   ASSERT_EQ(arg0_name, "group");
138 }
139 
140 TEST_F(TestPReLUInfo, CheckStrategy1) {
141   // Success: {{2,1,8,16},{1}}
142   Strategys inputs = {{2, 1, 8, 16}};
143   StrategyPtr strategy = NewStrategy(0, inputs);
144   Status ret = prelu->Init(strategy);
145   ASSERT_EQ(ret, FAILED);
146 }
147 
148 TEST_F(TestPReLUInfo, CheckStrategy2) {
149   Strategys inputs = {{2, 4, 8, 16}, {4}};
150   StrategyPtr strategy = NewStrategy(0, inputs);
151   Status ret = prelu->Init(strategy);
152   ASSERT_EQ(ret, SUCCESS);
153 }
154 
155 TEST_F(TestPReLUInfo, AutoStrategy1) {
156   ASSERT_EQ(prelu->GenerateStrategies(0), Status::SUCCESS);
157   std::vector<std::shared_ptr<StrategyWithCost>> sc = prelu->GetStrategyCost();
158 
159   Shapes splittable_inputs = {{1, 0, 1, 1}, {0}};
160   std::vector<StrategyPtr> sp_vector;
161   Shapes inputs_shape = {{64, 4, 8, 16}, {4}};
162   GenerateStrategiesForIndependentInputs(0, inputs_shape, splittable_inputs, &sp_vector);
163   for (auto stra : sp_vector) {
164     auto stra0 = stra->GetInputDim()[0];
165     auto stra1 = stra->GetInputDim()[1];
166     ASSERT_EQ(stra0[1], 1);
167     ASSERT_EQ(stra1[0], 1);
168   }
169 }
170 
171 TEST_F(TestPReLUInfo, InferDevMatrixShape_2d1) {
172   Strategys inputs = {{128, 1}, {1}};
173   StrategyPtr strategy = NewStrategy(0, inputs);
174 
175   prelu_2d->Init(strategy);
176   Shape dev_matrix_shape = prelu_2d->dev_matrix_shape();
177 
178   Shape expect = {128, 1, 8};
179   ASSERT_EQ(dev_matrix_shape, expect);
180 }
181 
182 TEST_F(TestPReLUInfo, InferSliceShape_2d1) {
183   Strategys str = {{128, 1}, {1}};
184   StrategyPtr strategy = NewStrategy(0, str);
185 
186   prelu_2d->Init(strategy);
187   std::vector<TensorInfo> inputs = prelu_2d->inputs_tensor_info();
188   std::vector<TensorInfo> outputs = prelu_2d->outputs_tensor_info();
189 
190   Shape input_slice_shape_expect = {8, 4};
191   Shape param_slice_shape_expect = {4};
192   Shape output_slice_shape_expect = {8, 4};
193 
194   TensorInfo input_tensor_info = inputs.at(0);
195   TensorInfo param_tensor_info = inputs.at(1);
196   TensorInfo output_tensor_info = outputs.at(0);
197 
198   Shape input_slice_shape = input_tensor_info.slice_shape();
199   Shape output_slice_shape = output_tensor_info.slice_shape();
200 
201   ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
202   ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
203 }
204 
205 TEST_F(TestPReLUInfo, GetTensorLayout_2d1) {
206   Strategys str = {{128, 1}, {1}};
207   StrategyPtr strategy = NewStrategy(0, str);
208 
209   prelu_2d->Init(strategy);
210   std::vector<TensorInfo> inputs = prelu_2d->inputs_tensor_info();
211   std::vector<TensorInfo> outputs = prelu_2d->outputs_tensor_info();
212 
213   TensorMap input_expect = {2, 1};
214   TensorMap param_expect = {0};
215   TensorMap output_expect = {2, 1};
216 
217   TensorInfo input_tensor_info = inputs.at(0);
218   TensorInfo param_tensor_info = inputs.at(1);
219   TensorInfo output_tensor_info = outputs.at(0);
220 
221   Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
222   Map param_tensor_map = param_tensor_info.tensor_layout().origin_tensor_map();
223   Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
224 
225   ASSERT_EQ(input_tensor_map.array(), input_expect);
226   ASSERT_EQ(output_tensor_map.array(), output_expect);
227 }
228 
229 TEST_F(TestPReLUInfo, GetMirrorOPs_2d1) {
230   Strategys str = {{128, 1}, {1}};
231   StrategyPtr strategy = NewStrategy(0, str);
232   prelu_2d->Init(strategy);
233   MirrorOps mirror_ops = prelu_2d->mirror_ops();
234   OperatorVector mirror_op = mirror_ops.at(1);
235   OperatorArgs operator_args = mirror_op.at(0).second;
236   std::string arg0_name = operator_args.first.at(0).first;
237   ValuePtr arg0_value = operator_args.first.at(0).second;
238   std::string group = arg0_value->cast<StringImmPtr>()->ToString();
239 
240   ASSERT_EQ(mirror_op.at(0).first, "_MirrorOperator");
241   ASSERT_EQ(mirror_op.size(), 1);
242   ASSERT_EQ(arg0_name, "group");
243 }
244 
245 TEST_F(TestPReLUInfo, CheckStrategy_2d1) {
246   // Success: {{2,1,8,16},{1}}
247   Strategys inputs = {{128, 1}};
248   StrategyPtr strategy = NewStrategy(0, inputs);
249   Status ret = prelu_2d->Init(strategy);
250   ASSERT_EQ(ret, FAILED);
251 }
252 
253 TEST_F(TestPReLUInfo, CheckStrategy_2d2) {
254   Strategys inputs = {{128, 4}, {4}};
255   StrategyPtr strategy = NewStrategy(0, inputs);
256   Status ret = prelu_2d->Init(strategy);
257   ASSERT_EQ(ret, SUCCESS);
258 }
259 
260 TEST_F(TestPReLUInfo, AutoStrategy_2d1) {
261   ASSERT_EQ(prelu_2d->GenerateStrategies(0), Status::SUCCESS);
262   std::vector<std::shared_ptr<StrategyWithCost>> sc = prelu_2d->GetStrategyCost();
263 
264   Shapes splittable_inputs = {{1, 0}, {0}};
265   std::vector<StrategyPtr> sp_vector;
266   Shapes inputs_shape = {{1024, 4}, {4}};
267   GenerateStrategiesForIndependentInputs(0, inputs_shape, splittable_inputs, &sp_vector);
268   for (auto stra : sp_vector) {
269     auto stra0 = stra->GetInputDim()[0];
270     auto stra1 = stra->GetInputDim()[1];
271     ASSERT_EQ(stra0[1], 1);
272     ASSERT_EQ(stra1[0], 1);
273   }
274 }
275 }  // namespace parallel
276 }  // namespace mindspore
277