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.

step_parallel_test.cc 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "common/common_test.h"
  17. #include "parallel/step_parallel.h"
  18. #include "parallel/graph_util/generate_graph.h"
  19. #include "common/py_func_graph_fetcher.h"
  20. #include "debug/draw.h"
  21. #include "operator/ops.h"
  22. #include "pipeline/static_analysis/static_analysis.h"
  23. namespace mindspore {
  24. namespace parallel {
  25. extern size_t TOTAL_OPS;
  26. class TestStepParallel : public UT::Common {
  27. public:
  28. TestStepParallel() {}
  29. void SetUp();
  30. void TearDown() {}
  31. };
  32. void TestStepParallel::SetUp() { UT::InitPythonPath(); }
  33. void Init_Device_Manager() {
  34. std::vector<int32_t> dev_list;
  35. for (int32_t i = 0; i < 20; i++) {
  36. dev_list.push_back(i);
  37. }
  38. std::vector<int32_t> stage_map;
  39. stage_map.push_back(16);
  40. stage_map.push_back(4);
  41. int32_t local_dev = 0;
  42. // create a new g_device_manager
  43. g_device_manager = std::make_shared<DeviceManager>();
  44. g_device_manager->Init(dev_list, local_dev, stage_map, "hccl");
  45. }
  46. CNodePtr Make_Node(Shape x, Shape y, Shape out, int condition = 0) {
  47. FuncGraphPtr func_graph = std::make_shared<FuncGraph>();
  48. ParameterPtr param1 = func_graph->add_parameter();
  49. ParameterPtr param2 = func_graph->add_parameter();
  50. param1->set_name("x");
  51. param2->set_name("y");
  52. BaseShapePtr shape1 = std::make_shared<abstract::Shape>(x);
  53. BaseShapePtr shape2 = std::make_shared<abstract::Shape>(y);
  54. BaseShapePtr shape3 = std::make_shared<abstract::Shape>(out);
  55. std::shared_ptr<tensor::Tensor> inputs_x = std::make_shared<tensor::Tensor>(kNumberTypeInt32, x);
  56. std::shared_ptr<tensor::Tensor> inputs_y = std::make_shared<tensor::Tensor>(kNumberTypeInt32, y);
  57. std::shared_ptr<tensor::Tensor> inputs_out = std::make_shared<tensor::Tensor>(kNumberTypeInt32, out);
  58. AbstractBasePtr abstract1 = abstract::FromValue(inputs_x, true);
  59. AbstractBasePtr abstract2 = abstract::FromValue(inputs_y, true);
  60. AbstractBasePtr abstract3 = abstract::FromValue(inputs_out, true);
  61. switch (condition) {
  62. case 0: {
  63. abstract1->set_shape(shape1);
  64. abstract2->set_shape(shape2);
  65. abstract3->set_shape(shape3);
  66. param1->set_abstract(abstract1);
  67. param2->set_abstract(abstract2);
  68. break;
  69. }
  70. case 1: {
  71. abstract1->set_shape(nullptr);
  72. param1->set_abstract(abstract1);
  73. param2->set_abstract(abstract2);
  74. break;
  75. }
  76. case 2: {
  77. abstract1->set_shape(shape1);
  78. abstract2->set_shape(shape2);
  79. param1->set_abstract(abstract1);
  80. param2->set_abstract(abstract2);
  81. abstract3 = abstract::FromValue(1, false);
  82. break;
  83. }
  84. case 3: {
  85. std::vector<BaseShapePtr> shape_o = {std::make_shared<abstract::Shape>(x), std::make_shared<abstract::Shape>(y)};
  86. BaseShapePtr shape4 = std::make_shared<abstract::TupleShape>(shape_o);
  87. abstract1->set_shape(shape1);
  88. abstract2->set_shape(shape2);
  89. abstract3->set_shape(shape4);
  90. param1->set_abstract(abstract1);
  91. param2->set_abstract(abstract2);
  92. break;
  93. }
  94. default:
  95. MS_LOG(INFO) << "Do Nothing!";
  96. }
  97. std::vector<AnfNodePtr> inputs;
  98. inputs.push_back(NewValueNode(prim::kPrimMatMul));
  99. inputs.push_back(param1);
  100. inputs.push_back(param2);
  101. CNodePtr node = func_graph->NewCNode(inputs);
  102. node->set_abstract(abstract3);
  103. return node;
  104. }
  105. FuncGraphManagerPtr Make_Manager(int condition = 0) {
  106. Shape inputs_x = {64, 32};
  107. Shape inputs_y = {32, 64};
  108. Shape inputs_z = {64, 128};
  109. Shape outputs_1 = {64, 64};
  110. Shape outputs_2 = {64, 128};
  111. FuncGraphPtr func_graph = std::make_shared<FuncGraph>();
  112. ParameterPtr param1 = func_graph->add_parameter();
  113. ParameterPtr param2 = func_graph->add_parameter();
  114. ParameterPtr param3 = func_graph->add_parameter();
  115. std::shared_ptr<tensor::Tensor> inputs_x_dim = std::make_shared<tensor::Tensor>(kNumberTypeInt32, inputs_x);
  116. std::shared_ptr<tensor::Tensor> inputs_y_dim = std::make_shared<tensor::Tensor>(kNumberTypeInt32, inputs_y);
  117. std::shared_ptr<tensor::Tensor> inputs_z_dim = std::make_shared<tensor::Tensor>(kNumberTypeInt32, inputs_z);
  118. std::shared_ptr<tensor::Tensor> inputs_out1_dim = std::make_shared<tensor::Tensor>(kNumberTypeInt32, outputs_1);
  119. std::shared_ptr<tensor::Tensor> inputs_out2_dim = std::make_shared<tensor::Tensor>(kNumberTypeInt32, outputs_2);
  120. AbstractBasePtr abstract_x = abstract::FromValue(inputs_x_dim, true);
  121. AbstractBasePtr abstract_y = abstract::FromValue(inputs_y_dim, true);
  122. AbstractBasePtr abstract_z = abstract::FromValue(inputs_z_dim, true);
  123. AbstractBasePtr abstract_out1 = abstract::FromValue(inputs_out1_dim, true);
  124. AbstractBasePtr abstract_out2 = abstract::FromValue(inputs_out2_dim, true);
  125. param1->set_abstract(abstract_x);
  126. param2->set_abstract(abstract_y);
  127. param3->set_abstract(abstract_z);
  128. std::vector<int> v1 = {2, 2};
  129. std::vector<int> v2 = {2, 4};
  130. std::vector<ValuePtr> elements = {MakeValue(v1), MakeValue(v2)};
  131. ValueTuplePtr var = std::make_shared<ValueTuple>(elements);
  132. std::vector<AnfNodePtr> inputs;
  133. inputs.push_back(NewValueNode(prim::kPrimMatMul));
  134. inputs.push_back(param1);
  135. inputs.push_back(param2);
  136. CNodePtr node1 = func_graph->NewCNode(inputs);
  137. node1->set_in_forward_flag(true);
  138. node1->set_abstract(abstract_out1);
  139. PrimitivePtr prim1 = node1->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  140. ValuePtr transpose_a = MakeValue(false);
  141. ValuePtr transpose_b = MakeValue(false);
  142. prim1->AddAttr("transpose_a", transpose_a);
  143. prim1->AddAttr("transpose_b", transpose_b);
  144. prim1->AddAttr("instance_name", MakeValue("matmul1"));
  145. prim1->AddAttr("strategy", var);
  146. inputs.clear();
  147. std::vector<int> v3 = {2, 2};
  148. std::vector<int> v4 = {2, 4};
  149. std::vector<ValuePtr> elements2 = {MakeValue(v3), MakeValue(v4)};
  150. ValueTuplePtr var2 = std::make_shared<ValueTuple>(elements2);
  151. inputs.push_back(NewValueNode(prim::kPrimMatMul));
  152. inputs.push_back(node1);
  153. inputs.push_back(param3);
  154. CNodePtr node2 = func_graph->NewCNode(inputs);
  155. node2->set_in_forward_flag(true);
  156. node2->set_abstract(abstract_out2);
  157. inputs.clear();
  158. inputs.push_back(NewValueNode(prim::kPrimReturn));
  159. inputs.push_back(node2);
  160. CNodePtr cnode_return = func_graph->NewCNode(inputs);
  161. cnode_return->set_in_forward_flag(true);
  162. func_graph->set_return(cnode_return);
  163. PrimitivePtr prim2 = node2->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  164. prim2->AddAttr("transpose_a", transpose_a);
  165. prim2->AddAttr("transpose_b", transpose_b);
  166. prim2->AddAttr("instance_name", MakeValue("matmul2"));
  167. prim2->AddAttr("strategy", var2);
  168. switch (condition) {
  169. case 1: {
  170. prim1->set_attr("strategy", MakeValue(0));
  171. break;
  172. }
  173. case 2: {
  174. std::vector<ValuePtr> elements_t = {MakeValue(0)};
  175. ValueTuplePtr var_t = std::make_shared<ValueTuple>(elements_t);
  176. prim1->set_attr("strategy", var_t);
  177. break;
  178. }
  179. case 3: {
  180. std::vector<int> vt1 = {2, 4};
  181. std::vector<int> vt2 = {2, 4};
  182. std::vector<ValuePtr> elements_t2 = {MakeValue(vt1), MakeValue(vt2)};
  183. ValueTuplePtr var_t2 = std::make_shared<ValueTuple>(elements_t2);
  184. prim1->set_attr("strategy", var_t2);
  185. break;
  186. }
  187. }
  188. std::vector<FuncGraphPtr> func_graphs{func_graph};
  189. FuncGraphManagerPtr manager = std::make_shared<FuncGraphManager>(func_graphs, true);
  190. manager->Init();
  191. return manager;
  192. }
  193. TEST_F(TestStepParallel, GetPythonPath1) {
  194. OperatorName operator_name = "AllReduce";
  195. const std::string expect = "mindspore.ops.operations";
  196. auto temp = parallel::GetOpPythonPath(operator_name);
  197. ASSERT_EQ(temp, expect);
  198. }
  199. TEST_F(TestStepParallel, GetPythonPath2) {
  200. OperatorName operator_name = "TensorAdd";
  201. const std::string expect = "mindspore.ops.operations";
  202. auto temp = parallel::GetOpPythonPath(operator_name);
  203. ASSERT_EQ(temp, expect);
  204. }
  205. TEST_F(TestStepParallel, ExtractStrategy) {
  206. Dimensions v1 = {2, 2};
  207. Dimensions v2 = {4, 4};
  208. std::unordered_map<std::string, ValuePtr> attrs;
  209. // stage
  210. ValuePtr val1 = MakeValue(v1);
  211. ValuePtr val2 = MakeValue(v2);
  212. std::vector<ValuePtr> elements = {val1, val2};
  213. ValueTuplePtr strategy_tuple = std::make_shared<ValueTuple>(elements);
  214. attrs["strategy"] = strategy_tuple;
  215. std::vector<Dimensions> strategy_expect = {v1, v2};
  216. StrategyPtr strategy = ExtractStrategy(attrs);
  217. std::vector<Dimensions> strategy_test = strategy->GetInputDim();
  218. ASSERT_EQ(strategy_expect, strategy_test);
  219. }
  220. TEST_F(TestStepParallel, ExtractShape) {
  221. Shape inputs_x_dims = {64, 32};
  222. Shape inputs_y_dims = {32, 64};
  223. Shape outputs_dims = {64, 64};
  224. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims, 4);
  225. EXPECT_THROW({ ExtractShape(node); }, std::runtime_error);
  226. }
  227. TEST_F(TestStepParallel, ExtractShape1) {
  228. Shape inputs_x_dims = {64, 32};
  229. Shape inputs_y_dims = {32, 64};
  230. Shape outputs_dims = {64, 64};
  231. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims);
  232. std::vector<Shapes> shape_test = ExtractShape(node);
  233. Shapes inputs_shape = std::vector<Shape>{inputs_x_dims, inputs_y_dims};
  234. Shapes outputs_shape = std::vector<Shape>{outputs_dims};
  235. std::vector<Shapes> shape_expect = {inputs_shape, outputs_shape};
  236. ASSERT_EQ(shape_test, shape_expect);
  237. }
  238. TEST_F(TestStepParallel, ExtractShape2) {
  239. Shape inputs_x_dims = {64, 32};
  240. Shape inputs_y_dims = {32, 64};
  241. Shape outputs_dims = {64, 64};
  242. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims, 1);
  243. EXPECT_THROW({ ExtractShape(node); }, std::runtime_error);
  244. }
  245. TEST_F(TestStepParallel, ExtractShape3) {
  246. Shape inputs_x_dims = {64, 32};
  247. Shape inputs_y_dims = {32, 64};
  248. Shape outputs_dims = {64, 64};
  249. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims, 3);
  250. Shapes inputs_shape = std::vector<Shape>{inputs_x_dims, inputs_y_dims};
  251. std::vector<Shapes> shape_expect = {inputs_shape, inputs_shape};
  252. std::vector<Shapes> shape_test = ExtractShape(node);
  253. ASSERT_EQ(shape_test, shape_expect);
  254. }
  255. TEST_F(TestStepParallel, ExtractShape4) {
  256. Shape inputs_x_dims = {64, 32};
  257. Shape inputs_y_dims = {32, 64};
  258. Shape outputs_dims = {64, 64};
  259. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims, 2);
  260. Shapes inputs_shape = std::vector<Shape>{inputs_x_dims, inputs_y_dims};
  261. EXPECT_THROW({ ExtractShape(node); }, std::runtime_error);
  262. }
  263. TEST_F(TestStepParallel, CreatOpInstance) {
  264. ValuePtr attr0_value = MakeValue(REDUCE_OP_SUM);
  265. ValuePtr attr1_value = MakeValue("0-1-2");
  266. Attr attr0 = std::make_pair("op", attr0_value);
  267. Attr attr1 = std::make_pair("group", attr1_value);
  268. OperatorAttrs attrs = {attr0, attr1};
  269. OperatorName op_name = "AllReduce";
  270. OperatorParams operator_param;
  271. OperatorArgs args = std::make_pair(attrs, operator_param);
  272. auto op_instance = CreatOpInstance(args.first, op_name, "test");
  273. ASSERT_TRUE(op_instance);
  274. PrimitivePyPtr allreduce_ptr = dyn_cast<PrimitivePy>(op_instance);
  275. ASSERT_TRUE(allreduce_ptr);
  276. if (nullptr != allreduce_ptr) {
  277. MS_LOG(INFO) << "Get PrimitivePyPtr: " << allreduce_ptr->name();
  278. auto func = allreduce_ptr->GetComputeFunction();
  279. if (py::isinstance<py::none>(func)) {
  280. MS_LOG(EXCEPTION) << "" << allreduce_ptr->name() << "'s compute function is not implemented";
  281. }
  282. std::vector<py::object> arglist;
  283. (void)std::transform(attrs.begin(), attrs.end(), std::back_inserter(arglist),
  284. [](Attr attr) { return ValuePtrToPyData(attr.second); });
  285. py::object allreduce_pyobj = parse::python_adapter::CallPyFn(
  286. "mindspore.parallel._utils", "_get_python_op", "AllReduce", "mindspore.ops.operations", "test", arglist);
  287. py::dict opAttr = py::getattr(allreduce_pyobj, "attrs");
  288. std::unordered_map<std::string, ValuePtr> attributes{};
  289. for (auto item : opAttr) {
  290. if (!py::isinstance<py::str>(item.first)) {
  291. MS_LOG(EXCEPTION) << "type error in py dict convert";
  292. }
  293. std::string name = py::cast<std::string>(item.first);
  294. MS_LOG(INFO) << "Attr name: " << name;
  295. ValuePtr converted_ret;
  296. if (name == "op") {
  297. parse::ConvertData(py::cast<py::object>(item.second), &converted_ret);
  298. ASSERT_EQ(converted_ret->ToString(), "sum");
  299. } else {
  300. if (name == "group") {
  301. parse::ConvertData(py::cast<py::object>(item.second), &converted_ret);
  302. ASSERT_EQ(converted_ret->ToString(), "0-1-2");
  303. } else if (name == "fusion") {
  304. parse::ConvertData(py::cast<py::object>(item.second), &converted_ret);
  305. ASSERT_EQ(converted_ret->ToString(), "0");
  306. } else if (name == "instance_name") {
  307. parse::ConvertData(py::cast<py::object>(item.second), &converted_ret);
  308. ASSERT_EQ(converted_ret->ToString(), "test");
  309. } else {
  310. MS_LOG(EXCEPTION) << "Test failed";
  311. }
  312. }
  313. attributes.emplace(name, converted_ret);
  314. }
  315. }
  316. }
  317. TEST_F(TestStepParallel, CreatOpInstance1) {
  318. OperatorAttrs attrs;
  319. OperatorName op_name = "ABC";
  320. OperatorParams operator_param;
  321. OperatorArgs args = std::make_pair(attrs, operator_param);
  322. EXPECT_THROW({ CreatOpInstance(args.first, op_name, "test"); }, std::runtime_error);
  323. }
  324. TEST_F(TestStepParallel, OperatorInstance) {
  325. Init_Device_Manager();
  326. // creat attrs and prim
  327. PrimitivePtr prim = NewValueNode(prim::kPrimMatMul)->value()->cast<PrimitivePtr>();
  328. ValuePtr transpose_a = MakeValue(false);
  329. ValuePtr transpose_b = MakeValue(false);
  330. prim->set_attr("transpose_a", transpose_a);
  331. prim->set_attr("transpose_b", transpose_b);
  332. auto attrs = prim->attrs();
  333. // creat strategy
  334. std::vector<Dimensions> strategy = {{2, 2}, {2, 4}};
  335. StrategyPtr strategyPtr = parallel::NewStrategy(0, strategy);
  336. // creat shape
  337. Shapes inputs_shape = std::vector<Shape>{{64, 32}, {32, 64}};
  338. Shapes outputs_shape = std::vector<Shape>{{64, 64}};
  339. std::vector<Shapes> shape = {inputs_shape, outputs_shape};
  340. TOTAL_OPS = 0;
  341. OperatorInfoPtr matmul_info = OperatorInstance(prim, attrs, shape);
  342. matmul_info->Init(strategyPtr);
  343. std::string name_expect = "MatMulInfo00";
  344. std::string name_test = matmul_info->name();
  345. ASSERT_EQ(name_expect, name_test);
  346. }
  347. TEST_F(TestStepParallel, ExtractInformation) {
  348. Init_Device_Manager();
  349. FuncGraphManagerPtr manager = Make_Manager();
  350. FuncGraphSet graphs = manager->func_graphs();
  351. FuncGraphPtr graph = *graphs.begin();
  352. auto ret = graph->get_return();
  353. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  354. ExtractInformation(all_nodes);
  355. }
  356. TEST_F(TestStepParallel, ExtractInformation2) {
  357. Init_Device_Manager();
  358. FuncGraphManagerPtr manager = Make_Manager(2);
  359. FuncGraphSet graphs = manager->func_graphs();
  360. FuncGraphPtr graph = *graphs.begin();
  361. auto ret = graph->get_return();
  362. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  363. EXPECT_THROW({ ExtractInformation(all_nodes); }, std::runtime_error);
  364. }
  365. TEST_F(TestStepParallel, ExtractInformation3) {
  366. Init_Device_Manager();
  367. FuncGraphManagerPtr manager = Make_Manager(3);
  368. FuncGraphSet graphs = manager->func_graphs();
  369. FuncGraphPtr graph = *graphs.begin();
  370. auto ret = graph->get_return();
  371. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  372. EXPECT_THROW({ ExtractInformation(all_nodes); }, std::runtime_error);
  373. }
  374. TEST_F(TestStepParallel, ForwardCommunication1) {
  375. Init_Device_Manager();
  376. ValuePtr attr0_value = MakeValue(REDUCE_OP_SUM);
  377. ValuePtr attr1_value = MakeValue("0-1-2");
  378. Attr attr0 = std::make_pair("op", attr0_value);
  379. Attr attr1 = std::make_pair("group", attr1_value);
  380. OperatorAttrs attrs = {attr0, attr1};
  381. OperatorName op_name = "AllReduce";
  382. OperatorParams operator_param;
  383. OperatorArgs args = std::make_pair(attrs, operator_param);
  384. Operator op = std::make_pair(op_name, args);
  385. OperatorVector op_list = {op, op};
  386. FuncGraphManagerPtr manager = Make_Manager();
  387. FuncGraphSet graphs = manager->func_graphs();
  388. FuncGraphPtr graph = *graphs.begin();
  389. auto ret = graph->get_return();
  390. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  391. ExtractInformation(all_nodes);
  392. for (auto &node : all_nodes) {
  393. if (!node->isa<CNode>()) {
  394. continue;
  395. }
  396. auto cnode = node->cast<CNodePtr>();
  397. FuncGraphPtr func_graph = node->func_graph();
  398. PrimitivePtr prim = cnode->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  399. if (prim->name() == "MatMul") {
  400. ForwardCommunication(op_list, cnode);
  401. draw::Draw("./forwardcommunication.dot", func_graph);
  402. }
  403. }
  404. AnfNodeSet after_nodes = manager->all_nodes();
  405. for (auto &node : after_nodes) {
  406. if (!node->isa<CNode>()) {
  407. continue;
  408. }
  409. auto &inputs = node->cast<CNodePtr>()->inputs();
  410. PrimitivePtr prim = inputs[0]->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  411. if (prim->name() == "return" || prim->name() == "MatMul") {
  412. if (!inputs[1]->isa<Parameter>()) {
  413. CNodePtr pre_node = inputs[1]->cast<CNodePtr>();
  414. PrimitivePtr pre_prim = pre_node->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  415. CNodePtr pre_node2 = pre_node->input(1)->cast<CNodePtr>();
  416. PrimitivePtr pre_prim2 = pre_node2->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  417. ASSERT_EQ("AllReduce", pre_prim->name());
  418. ASSERT_EQ("AllReduce", pre_prim2->name());
  419. }
  420. }
  421. }
  422. }
  423. TEST_F(TestStepParallel, ForwardCommunication2) {
  424. OperatorVector op_list;
  425. FuncGraphManagerPtr manager = Make_Manager();
  426. FuncGraphSet graphs = manager->func_graphs();
  427. FuncGraphPtr graph = *graphs.begin();
  428. auto ret = graph->get_return();
  429. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  430. ExtractInformation(all_nodes);
  431. for (auto &node : all_nodes) {
  432. if (!node->isa<CNode>()) {
  433. continue;
  434. }
  435. auto cnode = node->cast<CNodePtr>();
  436. FuncGraphPtr func_graph = node->func_graph();
  437. func_graph->set_manager(nullptr);
  438. PrimitivePtr prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  439. if (prim->name() == "MatMul") {
  440. EXPECT_THROW({ ForwardCommunication(op_list, cnode); }, std::runtime_error);
  441. break;
  442. }
  443. }
  444. }
  445. TEST_F(TestStepParallel, ForwardCommunication3) {
  446. OperatorVector op_list;
  447. FuncGraphManagerPtr manager = Make_Manager();
  448. FuncGraphSet graphs = manager->func_graphs();
  449. FuncGraphPtr graph = *graphs.begin();
  450. auto ret = graph->get_return();
  451. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  452. ExtractInformation(all_nodes);
  453. for (auto &node : all_nodes) {
  454. if (!node->isa<CNode>()) {
  455. continue;
  456. }
  457. auto cnode = node->cast<CNodePtr>();
  458. FuncGraphPtr func_graph = node->func_graph();
  459. PrimitivePtr prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  460. if (prim->name() == "MatMul") {
  461. OperatorAttrs attrs;
  462. OperatorParams operator_param;
  463. OperatorArgs args = std::make_pair(attrs, operator_param);
  464. Operator op = std::make_pair("ABC", args);
  465. OperatorVector op_list = {op};
  466. EXPECT_THROW({ ForwardCommunication(op_list, cnode); }, std::runtime_error);
  467. break;
  468. }
  469. }
  470. }
  471. TEST_F(TestStepParallel, GetTensorInLayout) {
  472. Init_Device_Manager();
  473. // creat attrs and prim
  474. FuncGraphPtr func_graph = std::make_shared<FuncGraph>();
  475. Shape inputs_x_dims = {64, 32};
  476. Shape inputs_y_dims = {32, 64};
  477. Shape outputs_dims = {64, 64};
  478. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims);
  479. std::vector<AnfNodePtr> inputs(node->inputs());
  480. CNodePtr node1 = func_graph->NewCNode(inputs);
  481. PrimitivePtr prim = node1->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  482. ValuePtr transpose_a = MakeValue(false);
  483. ValuePtr transpose_b = MakeValue(false);
  484. prim->set_attr("transpose_a", transpose_a);
  485. prim->set_attr("transpose_b", transpose_b);
  486. auto attrs = prim->attrs();
  487. // creat strategy
  488. std::vector<Dimensions> strategy = {{2, 2}, {2, 4}};
  489. StrategyPtr strategyPtr = parallel::NewStrategy(0, strategy);
  490. // creat shape
  491. Shapes inputs_shape = std::vector<Shape>{{64, 32}, {32, 64}};
  492. Shapes outputs_shape = std::vector<Shape>{{64, 64}};
  493. std::vector<Shapes> shape = {inputs_shape, outputs_shape};
  494. OperatorInfoPtr matmul_info = OperatorInstance(prim, attrs, shape);
  495. matmul_info->Init(strategyPtr);
  496. node->set_operator_info(matmul_info);
  497. OperatorInfoPtr distribute_operator_pre = node->operator_info();
  498. TensorLayout tensorlayout_e;
  499. std::vector<int32_t> array = {64, 64};
  500. TensorLayout tensorlayout = GetTensorInLayout(node1, prim, distribute_operator_pre);
  501. std::vector<int32_t> tensor_shape_test = tensorlayout.tensor_shape().array();
  502. ASSERT_EQ(array, tensor_shape_test);
  503. }
  504. } // namespace parallel
  505. } // namespace mindspore