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

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