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

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