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

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