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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 "frontend/parallel/step_parallel.h"
  18. #include "frontend/parallel/graph_util/generate_graph.h"
  19. #include "common/py_func_graph_fetcher.h"
  20. #include "debug/draw.h"
  21. #include "frontend/operator/ops.h"
  22. #include "pipeline/jit/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 if (name == "index") {
  310. parse::ConvertData(py::cast<py::object>(item.second), &converted_ret);
  311. ASSERT_EQ(converted_ret->ToString(), "0");
  312. } else {
  313. MS_LOG(EXCEPTION) << "Test failed";
  314. }
  315. }
  316. attributes.emplace(name, converted_ret);
  317. }
  318. }
  319. }
  320. TEST_F(TestStepParallel, CreatOpInstance1) {
  321. OperatorAttrs attrs;
  322. OperatorName op_name = "ABC";
  323. OperatorParams operator_param;
  324. OperatorArgs args = std::make_pair(attrs, operator_param);
  325. EXPECT_THROW({ CreatOpInstance(args.first, op_name, "test"); }, std::runtime_error);
  326. }
  327. TEST_F(TestStepParallel, OperatorInstance) {
  328. Init_Device_Manager();
  329. // creat attrs and prim
  330. PrimitivePtr prim = NewValueNode(prim::kPrimMatMul)->value()->cast<PrimitivePtr>();
  331. ValuePtr transpose_a = MakeValue(false);
  332. ValuePtr transpose_b = MakeValue(false);
  333. prim->set_attr("transpose_a", transpose_a);
  334. prim->set_attr("transpose_b", transpose_b);
  335. auto attrs = prim->attrs();
  336. // creat strategy
  337. std::vector<Dimensions> strategy = {{2, 2}, {2, 4}};
  338. StrategyPtr strategyPtr = parallel::NewStrategy(0, strategy);
  339. // creat shape
  340. Shapes inputs_shape = std::vector<Shape>{{64, 32}, {32, 64}};
  341. Shapes outputs_shape = std::vector<Shape>{{64, 64}};
  342. std::vector<Shapes> shape = {inputs_shape, outputs_shape};
  343. TOTAL_OPS = 0;
  344. OperatorInfoPtr matmul_info = OperatorInstance(prim, attrs, shape);
  345. matmul_info->Init(strategyPtr);
  346. std::string name_expect = "MatMulInfo00";
  347. std::string name_test = matmul_info->name();
  348. ASSERT_EQ(name_expect, name_test);
  349. }
  350. TEST_F(TestStepParallel, ExtractInformation) {
  351. Init_Device_Manager();
  352. FuncGraphManagerPtr manager = Make_Manager();
  353. FuncGraphSet graphs = manager->func_graphs();
  354. FuncGraphPtr graph = *graphs.begin();
  355. auto ret = graph->get_return();
  356. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  357. ExtractInformation(all_nodes);
  358. }
  359. TEST_F(TestStepParallel, ExtractInformation2) {
  360. Init_Device_Manager();
  361. FuncGraphManagerPtr manager = Make_Manager(2);
  362. FuncGraphSet graphs = manager->func_graphs();
  363. FuncGraphPtr graph = *graphs.begin();
  364. auto ret = graph->get_return();
  365. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  366. EXPECT_THROW({ ExtractInformation(all_nodes); }, std::runtime_error);
  367. }
  368. TEST_F(TestStepParallel, ExtractInformation3) {
  369. Init_Device_Manager();
  370. FuncGraphManagerPtr manager = Make_Manager(3);
  371. FuncGraphSet graphs = manager->func_graphs();
  372. FuncGraphPtr graph = *graphs.begin();
  373. auto ret = graph->get_return();
  374. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  375. EXPECT_THROW({ ExtractInformation(all_nodes); }, std::runtime_error);
  376. }
  377. TEST_F(TestStepParallel, ForwardCommunication1) {
  378. Init_Device_Manager();
  379. ValuePtr attr0_value = MakeValue(REDUCE_OP_SUM);
  380. ValuePtr attr1_value = MakeValue("0-1-2");
  381. Attr attr0 = std::make_pair("op", attr0_value);
  382. Attr attr1 = std::make_pair("group", attr1_value);
  383. OperatorAttrs attrs = {attr0, attr1};
  384. OperatorName op_name = "AllReduce";
  385. OperatorParams operator_param;
  386. OperatorArgs args = std::make_pair(attrs, operator_param);
  387. Operator op = std::make_pair(op_name, args);
  388. OperatorVector op_list = {op, op};
  389. FuncGraphManagerPtr manager = Make_Manager();
  390. FuncGraphSet graphs = manager->func_graphs();
  391. FuncGraphPtr graph = *graphs.begin();
  392. auto ret = graph->get_return();
  393. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  394. ExtractInformation(all_nodes);
  395. for (auto &node : all_nodes) {
  396. if (!node->isa<CNode>()) {
  397. continue;
  398. }
  399. auto cnode = node->cast<CNodePtr>();
  400. FuncGraphPtr func_graph = node->func_graph();
  401. PrimitivePtr prim = cnode->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  402. if (prim->name() == "MatMul") {
  403. ForwardCommunication(op_list, cnode);
  404. draw::Draw("./forwardcommunication.dot", func_graph);
  405. }
  406. }
  407. AnfNodeSet after_nodes = manager->all_nodes();
  408. for (auto &node : after_nodes) {
  409. if (!node->isa<CNode>()) {
  410. continue;
  411. }
  412. auto &inputs = node->cast<CNodePtr>()->inputs();
  413. PrimitivePtr prim = inputs[0]->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  414. if (prim->name() == "return" || prim->name() == "MatMul") {
  415. if (!inputs[1]->isa<Parameter>()) {
  416. CNodePtr pre_node = inputs[1]->cast<CNodePtr>();
  417. PrimitivePtr pre_prim = pre_node->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  418. CNodePtr pre_node2 = pre_node->input(1)->cast<CNodePtr>();
  419. PrimitivePtr pre_prim2 = pre_node2->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  420. ASSERT_EQ("AllReduce", pre_prim->name());
  421. ASSERT_EQ("AllReduce", pre_prim2->name());
  422. }
  423. }
  424. }
  425. }
  426. TEST_F(TestStepParallel, ForwardCommunication2) {
  427. OperatorVector op_list;
  428. FuncGraphManagerPtr manager = Make_Manager();
  429. FuncGraphSet graphs = manager->func_graphs();
  430. FuncGraphPtr graph = *graphs.begin();
  431. auto ret = graph->get_return();
  432. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  433. ExtractInformation(all_nodes);
  434. for (auto &node : all_nodes) {
  435. if (!node->isa<CNode>()) {
  436. continue;
  437. }
  438. auto cnode = node->cast<CNodePtr>();
  439. FuncGraphPtr func_graph = node->func_graph();
  440. func_graph->set_manager(nullptr);
  441. PrimitivePtr prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  442. if (prim->name() == "MatMul") {
  443. EXPECT_THROW({ ForwardCommunication(op_list, cnode); }, std::runtime_error);
  444. break;
  445. }
  446. }
  447. }
  448. TEST_F(TestStepParallel, ForwardCommunication3) {
  449. OperatorVector op_list;
  450. FuncGraphManagerPtr manager = Make_Manager();
  451. FuncGraphSet graphs = manager->func_graphs();
  452. FuncGraphPtr graph = *graphs.begin();
  453. auto ret = graph->get_return();
  454. std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
  455. ExtractInformation(all_nodes);
  456. for (auto &node : all_nodes) {
  457. if (!node->isa<CNode>()) {
  458. continue;
  459. }
  460. auto cnode = node->cast<CNodePtr>();
  461. FuncGraphPtr func_graph = node->func_graph();
  462. PrimitivePtr prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  463. if (prim->name() == "MatMul") {
  464. OperatorAttrs attrs;
  465. OperatorParams operator_param;
  466. OperatorArgs args = std::make_pair(attrs, operator_param);
  467. Operator op = std::make_pair("ABC", args);
  468. OperatorVector op_list = {op};
  469. EXPECT_THROW({ ForwardCommunication(op_list, cnode); }, std::runtime_error);
  470. break;
  471. }
  472. }
  473. }
  474. TEST_F(TestStepParallel, GetTensorInLayout) {
  475. Init_Device_Manager();
  476. // creat attrs and prim
  477. FuncGraphPtr func_graph = std::make_shared<FuncGraph>();
  478. Shape inputs_x_dims = {64, 32};
  479. Shape inputs_y_dims = {32, 64};
  480. Shape outputs_dims = {64, 64};
  481. CNodePtr node = Make_Node(inputs_x_dims, inputs_y_dims, outputs_dims);
  482. std::vector<AnfNodePtr> inputs(node->inputs());
  483. CNodePtr node1 = func_graph->NewCNode(inputs);
  484. PrimitivePtr prim = node1->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>();
  485. ValuePtr transpose_a = MakeValue(false);
  486. ValuePtr transpose_b = MakeValue(false);
  487. prim->set_attr("transpose_a", transpose_a);
  488. prim->set_attr("transpose_b", transpose_b);
  489. auto attrs = prim->attrs();
  490. // creat strategy
  491. std::vector<Dimensions> strategy = {{2, 2}, {2, 4}};
  492. StrategyPtr strategyPtr = parallel::NewStrategy(0, strategy);
  493. // creat shape
  494. Shapes inputs_shape = std::vector<Shape>{{64, 32}, {32, 64}};
  495. Shapes outputs_shape = std::vector<Shape>{{64, 64}};
  496. std::vector<Shapes> shape = {inputs_shape, outputs_shape};
  497. OperatorInfoPtr matmul_info = OperatorInstance(prim, attrs, shape);
  498. matmul_info->Init(strategyPtr);
  499. node->set_operator_info(matmul_info);
  500. OperatorInfoPtr distribute_operator_pre = node->operator_info();
  501. TensorLayout tensorlayout_e;
  502. std::vector<int32_t> array = {64, 64};
  503. TensorLayout tensorlayout = GetTensorInLayout(node1, prim, distribute_operator_pre);
  504. std::vector<int32_t> tensor_shape_test = tensorlayout.tensor_shape().array();
  505. ASSERT_EQ(array, tensor_shape_test);
  506. }
  507. } // namespace parallel
  508. } // namespace mindspore