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

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