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

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