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 22 kB

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