You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

prelu_test.cc 9.3 kB

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