• 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 "common/common_test.h"
18 #include "frontend/parallel/strategy.h"
19 #include "frontend/parallel/device_manager.h"
20 #include "frontend/parallel/ops_info/operator_info.h"
21 #include "frontend/parallel/ops_info/tmp_identity_info.h"
22 #include "frontend/parallel/step_parallel.h"
23 
24 namespace mindspore {
25 namespace parallel {
26 
27 class TmpIdentityInfo;
28 using TmpIdentityInfoPtr = std::shared_ptr<TmpIdentityInfo>;
29 TmpIdentityInfoPtr identity_ptr;
30 
31 class TestTmpIdentityInfo : public UT::Common {
32  public:
TestTmpIdentityInfo()33   TestTmpIdentityInfo() { identity_ptr2 = nullptr; }
34   void SetUp();
TearDown()35   void TearDown() {}
36 
37   TmpIdentityInfoPtr identity_ptr2;
38 };
39 
SetUp()40 void TestTmpIdentityInfo::SetUp() {
41   RankList dev_list;
42 
43   for (int32_t i = 0; i < 1050; i++) {
44     dev_list.push_back(i);
45   }
46 
47   RankList stage_map;
48   stage_map.push_back(1024);
49   stage_map.push_back(26);
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   Shapes inputs_shape = {{2, 4, 8, 16}};
59   Shapes outputs_shape = {{2, 4, 8, 16}};
60   identity_ptr = std::make_shared<TmpIdentityInfo>(inputs_shape, outputs_shape, attr);
61 
62   Shapes inputs_shape2 = {{4, 16, 8, 16}};
63   Shapes outputs_shape2 = {{4, 16, 8, 16}};
64   identity_ptr2 = std::make_shared<TmpIdentityInfo>(inputs_shape2, outputs_shape2, attr);
65 }
66 
TEST_F(TestTmpIdentityInfo,InferDevMatrixShape1)67 TEST_F(TestTmpIdentityInfo, InferDevMatrixShape1) {
68   Strategys inputs = {{2, 4, 8, 16}};
69   StrategyPtr strategy = NewStrategy(0, inputs);
70 
71   identity_ptr->Init(strategy);
72   Shape dev_matrix_shape = identity_ptr->dev_matrix_shape();
73 
74   Shape expect = {2, 4, 8, 16};
75   ASSERT_EQ(dev_matrix_shape, expect);
76 }
77 
TEST_F(TestTmpIdentityInfo,InferSliceShape1)78 TEST_F(TestTmpIdentityInfo, InferSliceShape1) {
79   Strategys str = {{2, 4, 8, 16}};
80   StrategyPtr strategy = NewStrategy(0, str);
81 
82   identity_ptr->Init(strategy);
83   std::vector<TensorInfo> inputs = identity_ptr->inputs_tensor_info();
84   std::vector<TensorInfo> outputs = identity_ptr->outputs_tensor_info();
85 
86   Shape input_slice_shape_expect = {1, 1, 1, 1};
87   Shape output_slice_shape_expect = {1, 1, 1, 1};
88 
89   TensorInfo input_tensor_info = inputs.at(0);
90   TensorInfo output_tensor_info = outputs.at(0);
91 
92   Shape input_slice_shape = input_tensor_info.slice_shape();
93   Shape output_slice_shape = output_tensor_info.slice_shape();
94 
95   ASSERT_EQ(input_slice_shape, input_slice_shape_expect);
96   ASSERT_EQ(output_slice_shape, output_slice_shape_expect);
97 }
98 
TEST_F(TestTmpIdentityInfo,GetTensorLayout1)99 TEST_F(TestTmpIdentityInfo, GetTensorLayout1) {
100   Strategys str = {{2, 4, 8, 16}};
101   StrategyPtr strategy = NewStrategy(0, str);
102 
103   identity_ptr->Init(strategy);
104   std::vector<TensorInfo> inputs = identity_ptr->inputs_tensor_info();
105   std::vector<TensorInfo> outputs = identity_ptr->outputs_tensor_info();
106 
107   TensorMap input_expect = {3, 2, 1, 0};
108   TensorMap output_expect = {3, 2, 1, 0};
109 
110   TensorInfo input_tensor_info = inputs.at(0);
111   TensorInfo output_tensor_info = outputs.at(0);
112 
113   Map input_tensor_map = input_tensor_info.tensor_layout().origin_tensor_map();
114   Map output_tensor_map = output_tensor_info.tensor_layout().origin_tensor_map();
115 
116   ASSERT_EQ(input_tensor_map.array(), input_expect);
117   ASSERT_EQ(output_tensor_map.array(), output_expect);
118 }
119 
TEST_F(TestTmpIdentityInfo,CheckStrategy1)120 TEST_F(TestTmpIdentityInfo, CheckStrategy1) {
121   // Success: {{2,4,8,16}}
122   Strategys inputs = {{2, 2, 8, 16}, {2, 4, 16, 1}};
123   StrategyPtr strategy = NewStrategy(0, inputs);
124 
125   Status ret = identity_ptr->Init(strategy);
126   ASSERT_EQ(ret, FAILED);
127 }
128 
TEST_F(TestTmpIdentityInfo,CheckStrategy2)129 TEST_F(TestTmpIdentityInfo, CheckStrategy2) {
130   // Success: {{2,4,8,16}}
131   Strategys inputs = {{2, 4, 8}};
132   StrategyPtr strategy = NewStrategy(0, inputs);
133 
134   Status ret = identity_ptr->Init(strategy);
135   ASSERT_EQ(ret, FAILED);
136 }
137 
TEST_F(TestTmpIdentityInfo,test_generate_strategies)138 TEST_F(TestTmpIdentityInfo, test_generate_strategies) {
139   ASSERT_EQ(identity_ptr->GenerateStrategies(0), Status::SUCCESS);
140   std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr->GetStrategyCost();
141   for (const auto& swc : sc) {
142     StrategyPtr sp = swc->strategy_ptr;
143     Cost cost = *(swc->cost_list[0]);
144 
145     identity_ptr->Init(sp);
146     std::vector<TensorInfo> inputs_info = identity_ptr->inputs_tensor_info();
147     std::vector<TensorInfo> outputs_info = identity_ptr->outputs_tensor_info();
148     ASSERT_DOUBLE_EQ(identity_ptr->operator_cost()->GetComputationCost(inputs_info, outputs_info, sp->GetInputStage()),
149                      cost.computation_cost_);
150     ASSERT_DOUBLE_EQ(identity_ptr->operator_cost()->GetCommCost(inputs_info, outputs_info, sp->GetInputStage()),
151                      cost.communication_cost_);
152   }
153 }
154 
TEST_F(TestTmpIdentityInfo,test_generate_strategies_base)155 TEST_F(TestTmpIdentityInfo, test_generate_strategies_base) {
156   ASSERT_EQ(identity_ptr->GenerateStrategies(0), Status::SUCCESS);
157   std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr->GetStrategyCost();
158 
159   Shapes splittable_inputs = {{1, 1, 1, 1}};
160   std::vector<StrategyPtr> sp_vector;
161   Shapes inputs_shape = {{2, 4, 8, 16}};
162   GenerateStrategiesForIndependentInputs(0, inputs_shape, splittable_inputs, &sp_vector);
163   ASSERT_EQ(sc.size(), sp_vector.size());
164 }
165 
TEST_F(TestTmpIdentityInfo,test_generate_strategies_base2)166 TEST_F(TestTmpIdentityInfo, test_generate_strategies_base2) {
167   ASSERT_EQ(identity_ptr2->GenerateStrategies(0), Status::SUCCESS);
168   std::vector<std::shared_ptr<StrategyWithCost>> sc = identity_ptr2->GetStrategyCost();
169 
170   Shapes splittable_inputs = {{1, 1, 1, 1}};
171   std::vector<StrategyPtr> sp_vector;
172   Shapes inputs_shape2 = {{4, 16, 8, 16}};
173   GenerateStrategiesForIndependentInputs(0, inputs_shape2, splittable_inputs, &sp_vector);
174   ASSERT_EQ(sc.size(), sp_vector.size());
175 }
176 }  // namespace parallel
177 }  // namespace mindspore
178