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.

format_refiner.cc 21 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * Copyright 2019-2020 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 "format_refiner.h"
  17. #include <deque>
  18. #include <iostream>
  19. #include <set>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include "graph/ref_relation.h"
  23. #include "./compute_graph.h"
  24. #include "./ge_error_codes.h"
  25. #include "./graph/ge_tensor.h"
  26. #include "./operator.h"
  27. #include "./operator_factory.h"
  28. #include "debug/ge_log.h"
  29. #include "debug/ge_op_types.h"
  30. #include "debug/ge_util.h"
  31. #include "framework/common/debug/ge_log.h"
  32. #include "utils/node_utils.h"
  33. #include "utils/op_desc_utils.h"
  34. #include "utils/tensor_utils.h"
  35. #include "utils/type_utils.h"
  36. using namespace ge;
  37. using namespace std;
  38. namespace ge {
  39. namespace {
  40. const std::unordered_set<string> kChangeDimNodes = {PERMUTE, EXPANDDIMS, SQUEEZE};
  41. const string kIsGraphInferred = "_is_graph_inferred";
  42. thread_local RefRelations reflection_builder;
  43. } // namespace
  44. graphStatus ReflectionProcess(const std::unordered_set<RefCell, RefCellHash> &reflection,
  45. std::deque<ge::NodePtr> &nodes, ge::Format to_be_set_format) {
  46. for (const auto &cell : reflection) {
  47. auto node = cell.node;
  48. auto in_out_idx = cell.in_out_idx;
  49. GE_CHECK_NOTNULL(node);
  50. GE_CHECK_NOTNULL(node->GetOpDesc());
  51. if (cell.in_out == ge::NODE_IN) {
  52. auto desc = node->GetOpDesc()->GetInputDesc(static_cast<uint32_t>(in_out_idx));
  53. desc.SetOriginFormat(to_be_set_format);
  54. desc.SetFormat(to_be_set_format);
  55. (void)node->GetOpDesc()->UpdateInputDesc(static_cast<uint32_t>(in_out_idx), desc);
  56. } else {
  57. auto desc = node->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(in_out_idx));
  58. desc.SetOriginFormat(to_be_set_format);
  59. desc.SetFormat(to_be_set_format);
  60. (void)node->GetOpDesc()->UpdateOutputDesc(static_cast<uint32_t>(in_out_idx), desc);
  61. }
  62. nodes.push_back(cell.node);
  63. }
  64. return GRAPH_SUCCESS;
  65. }
  66. graphStatus BiasAddFormatFixProcess(ge::NodePtr &node_ptr) {
  67. // 5 meas dim num
  68. if (node_ptr->GetType() != "BiasAdd") {
  69. return GRAPH_SUCCESS;
  70. }
  71. std::unordered_map<string, Format> kTfFormatFix = {{"NHWC", FORMAT_NDHWC}, {"NCHW", FORMAT_NCDHW}};
  72. for (size_t i = 0; i < node_ptr->GetOpDesc()->GetInputsSize(); i++) {
  73. auto in_desc = node_ptr->GetOpDesc()->MutableInputDesc(i);
  74. GE_CHECK_NOTNULL(in_desc);
  75. if (in_desc->MutableShape().GetDimNum() != 5) { // 5 means dim num
  76. continue;
  77. }
  78. auto format = in_desc->GetOriginFormat();
  79. auto key = TypeUtils::FormatToSerialString(format);
  80. auto fixed_format = (kTfFormatFix.count(key) == 0) ? format : kTfFormatFix[key];
  81. in_desc->SetOriginFormat(fixed_format);
  82. in_desc->SetFormat(fixed_format);
  83. GELOGD("fix the %zu'th input of node[%s]. Origin format is %s , after fixed it is %s", i,
  84. node_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(format).c_str(),
  85. TypeUtils::FormatToSerialString(fixed_format).c_str());
  86. }
  87. for (size_t i = 0; i < node_ptr->GetOpDesc()->GetOutputsSize(); i++) {
  88. auto out_desc = node_ptr->GetOpDesc()->MutableOutputDesc(i);
  89. GE_CHECK_NOTNULL(out_desc);
  90. if (out_desc->MutableShape().GetDimNum() != 5) { // 5 means dim num
  91. continue;
  92. }
  93. auto format = out_desc->GetOriginFormat();
  94. auto key = TypeUtils::FormatToSerialString(format);
  95. auto fixed_format = (kTfFormatFix.count(key) == 0) ? format : kTfFormatFix[key];
  96. out_desc->SetOriginFormat(fixed_format);
  97. out_desc->SetFormat(fixed_format);
  98. GELOGD("fix the %zu'th output of node[%s]. Origin format is %s , after fixed it is %s", i,
  99. node_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(format).c_str(),
  100. TypeUtils::FormatToSerialString(fixed_format).c_str());
  101. }
  102. return GRAPH_SUCCESS;
  103. }
  104. graphStatus FormatRefiner::RefreshConstantOutProcess(const ComputeGraphPtr &graph, const OpDescPtr &op_desc) {
  105. GE_CHECK_NOTNULL(graph);
  106. GE_CHECK_NOTNULL(op_desc);
  107. if (op_desc->GetType() == CONSTANTOP && !IsGraphInferred(graph)) {
  108. ConstGeTensorPtr tensor_value;
  109. if (!AttrUtils::GetTensor(op_desc, "value", tensor_value)) {
  110. GELOGE(GRAPH_FAILED, "Get value failed, node name:%s.", op_desc->GetName().c_str());
  111. return GRAPH_FAILED;
  112. }
  113. GE_CHECK_NOTNULL(tensor_value);
  114. (void)op_desc->UpdateOutputDesc(0, tensor_value->GetTensorDesc());
  115. }
  116. return GRAPH_SUCCESS;
  117. }
  118. graphStatus FormatRefiner::GetAnchorPoints(const ge::ComputeGraphPtr &graph, std::vector<ge::NodePtr> &anchor_points,
  119. std::vector<ge::NodePtr> &data_nodes,
  120. std::unordered_map<ge::NodePtr, bool> &node_status) {
  121. if (graph == nullptr) {
  122. GELOGE(GRAPH_FAILED, "input graph is null");
  123. return GRAPH_FAILED;
  124. }
  125. anchor_points.clear();
  126. // Get all anchor point nodes and switch nodes
  127. for (auto &node_ptr : graph->GetAllNodes()) {
  128. if (node_ptr == nullptr) {
  129. return GRAPH_FAILED;
  130. }
  131. auto op_desc = node_ptr->GetOpDesc();
  132. if (op_desc == nullptr) {
  133. return GRAPH_FAILED;
  134. }
  135. graphStatus status = RefreshConstantOutProcess(graph, op_desc);
  136. if (status != GRAPH_SUCCESS) {
  137. GELOGE(GRAPH_FAILED, "refresh constant out process failed!");
  138. return GRAPH_FAILED;
  139. }
  140. // consider special node save process
  141. // get all input desc format
  142. bool node_is_all_nd = false;
  143. auto input_size = static_cast<uint32_t>(op_desc->GetAllInputsSize());
  144. for (uint32_t i = 0; i < input_size; i++) {
  145. // Operator pre-set format but not origin format
  146. GE_IF_BOOL_EXEC(op_desc->MutableInputDesc(i) == nullptr, continue);
  147. auto input_format = op_desc->MutableInputDesc(i)->GetFormat();
  148. // Pre-save data node (only main graph data) and default infer fail
  149. if (node_ptr->GetType() == DATA) {
  150. data_nodes.push_back(node_ptr);
  151. }
  152. if (input_format != FORMAT_ND && input_format != FORMAT_RESERVED) {
  153. node_is_all_nd = true;
  154. }
  155. }
  156. // Get all output desc format
  157. auto output_size = static_cast<uint32_t>(op_desc->GetOutputsSize());
  158. for (uint32_t i = 0; i < output_size; i++) {
  159. GE_IF_BOOL_EXEC(op_desc->MutableOutputDesc(i) == nullptr, continue);
  160. auto output_format = op_desc->MutableOutputDesc(i)->GetFormat();
  161. if (output_format != FORMAT_ND && output_format != FORMAT_RESERVED) {
  162. node_is_all_nd = true;
  163. }
  164. }
  165. // check anchor point valid
  166. if (!node_is_all_nd) {
  167. continue;
  168. }
  169. // special process for biasAdd op
  170. // In tensorflow, biasAdd's format is alwayse NHWC even though set the arg
  171. // "data_format" to NDHWC or NCDHW.It will destroy our format-infer mechanism
  172. // so here do special process
  173. status = BiasAddFormatFixProcess(node_ptr);
  174. if (status != GRAPH_SUCCESS) {
  175. GELOGE(GRAPH_FAILED, "fix biasAdd process failed!");
  176. return GRAPH_FAILED;
  177. }
  178. GELOGD("Node[%s] is anchor point!", node_ptr->GetName().c_str());
  179. anchor_points.push_back(node_ptr);
  180. }
  181. GELOGI("anchor_points number is %zu", anchor_points.size());
  182. return GRAPH_SUCCESS;
  183. }
  184. graphStatus FormatRefiner::AnchorProcess(const ge::NodePtr &anchor_node,
  185. std::unordered_map<ge::NodePtr, bool> &node_status) {
  186. if (anchor_node == nullptr) {
  187. GELOGE(GRAPH_FAILED, "anchor node is null!");
  188. return GRAPH_FAILED;
  189. }
  190. std::deque<ge::NodePtr> nodes;
  191. nodes.push_back(anchor_node);
  192. while (!nodes.empty()) {
  193. ge::NodePtr node = nodes.front();
  194. nodes.pop_front();
  195. graphStatus status = BackInferProcess(nodes, node, node_status);
  196. if (status != GRAPH_SUCCESS && node != nullptr) {
  197. GELOGE(status, "BackInferProcess failed!node name [%s]", node->GetName().c_str());
  198. return status;
  199. }
  200. status = ForwardInferProcess(nodes, node, node_status);
  201. if (status != GRAPH_SUCCESS && node != nullptr) {
  202. GELOGE(status, "ForwardInferProcess failed!node name [%s]", node->GetName().c_str());
  203. return status;
  204. }
  205. }
  206. return GRAPH_SUCCESS;
  207. }
  208. graphStatus FormatRefiner::BackInferProcess(std::deque<ge::NodePtr> &nodes, ge::NodePtr &node,
  209. std::unordered_map<ge::NodePtr, bool> &node_status) {
  210. GE_CHECK_NOTNULL(node);
  211. GE_CHECK_NOTNULL(node->GetOpDesc());
  212. GELOGD("Enter back infer process!Node is [%s]", (node->GetName()).c_str());
  213. for (const auto &in_anchor : node->GetAllInDataAnchors()) {
  214. GELOGD("Node is [%s] [B]", (node->GetName()).c_str());
  215. auto in_data_anchor_idx = in_anchor->GetIdx();
  216. auto input_desc = node->GetOpDesc()->MutableInputDesc(static_cast<uint32_t>(in_data_anchor_idx));
  217. GE_IF_BOOL_EXEC(input_desc == nullptr, continue);
  218. auto to_be_set_format = input_desc->GetOriginFormat();
  219. if (to_be_set_format == FORMAT_ND) {
  220. GELOGD("Node [%s] [B], format is ND", (node->GetName()).c_str());
  221. continue;
  222. }
  223. auto peer_out_data_anchor = in_anchor->GetPeerOutAnchor();
  224. if (peer_out_data_anchor == nullptr) {
  225. GELOGW("Node[%s] %dth in data anchor's peer_out_anchor is null", (node->GetName()).c_str(), in_data_anchor_idx);
  226. continue;
  227. }
  228. auto peer_out_data_node = peer_out_data_anchor->GetOwnerNode();
  229. if (peer_out_data_node == nullptr || peer_out_data_node->GetOpDesc() == nullptr) {
  230. GELOGW("Node[%s]\'s peer_out_data_node or peer_out_data_node desc is null", (node->GetName()).c_str());
  231. continue;
  232. }
  233. // Check format whether have been set
  234. int idx = peer_out_data_anchor->GetIdx();
  235. // do peer_out_node name and index as key to lookup reflections
  236. ge::RefCell key(peer_out_data_node->GetName(), peer_out_data_node, ge::NODE_OUT, idx);
  237. std::unordered_set<RefCell, RefCellHash> reflection;
  238. auto status = reflection_builder.LookUpRefRelations(key, reflection);
  239. if (status != GRAPH_SUCCESS) {
  240. GELOGE(GRAPH_FAILED, "LookUpRefRelations failed!Node is [%s],the %d out edge",
  241. (peer_out_data_node->GetName()).c_str(), idx);
  242. return GRAPH_FAILED;
  243. }
  244. auto ge_tensor_desc = peer_out_data_node->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(idx));
  245. if (ge_tensor_desc.GetOriginFormat() == FORMAT_ND) {
  246. auto dim_num = ge_tensor_desc.GetShape().GetDimNum();
  247. if (dim_num == 0) {
  248. GELOGD("node name:%s idx:%d out is scalar. stop back infer!", peer_out_data_node->GetName().c_str(), idx);
  249. continue;
  250. }
  251. /// Check whether node to change dims ()
  252. /// Because some node will calculate with 5D, C dim maybe multi meaning
  253. auto peer_out_data_node_type = peer_out_data_node->GetType();
  254. auto iter1 = kChangeDimNodes.find(peer_out_data_node_type);
  255. // 4 means dims num
  256. if ((iter1 != kChangeDimNodes.end()) && (dim_num < 4)) {
  257. GELOGD("Node[%s] is change dim node and shape is smaller than 4. do not modify format",
  258. (peer_out_data_node->GetName()).c_str());
  259. continue;
  260. }
  261. if (reflection.empty()) {
  262. ge_tensor_desc.SetOriginFormat(to_be_set_format);
  263. ge_tensor_desc.SetFormat(to_be_set_format);
  264. (void)peer_out_data_node->GetOpDesc()->UpdateOutputDesc(static_cast<uint32_t>(idx), ge_tensor_desc);
  265. // Call operator infer format api (forward) to get out format
  266. GELOGD("call infer format func[Back]!Node is [%s] ", (peer_out_data_node->GetName()).c_str());
  267. status = peer_out_data_node->InferOriginFormat();
  268. if (status != GRAPH_SUCCESS) {
  269. GELOGE(GRAPH_FAILED, "Node[%s] infer format failed", (peer_out_data_node->GetName()).c_str());
  270. return GRAPH_FAILED;
  271. }
  272. nodes.push_back(peer_out_data_node);
  273. } else {
  274. auto status = ReflectionProcess(reflection, nodes, to_be_set_format);
  275. if (status != GRAPH_SUCCESS) {
  276. GELOGE(GRAPH_FAILED, "reflection process failed!");
  277. return GRAPH_FAILED;
  278. }
  279. }
  280. }
  281. }
  282. return GRAPH_SUCCESS;
  283. }
  284. graphStatus FormatRefiner::ForwardInferProcess(std::deque<ge::NodePtr> &nodes, ge::NodePtr &node,
  285. std::unordered_map<ge::NodePtr, bool> &node_status) {
  286. GE_CHECK_NOTNULL(node);
  287. GE_CHECK_NOTNULL(node->GetOpDesc());
  288. GELOGD("Enter forward infer process!Node is [%s]", (node->GetName()).c_str());
  289. for (const auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  290. GELOGD("Node is [%s] [F]", (node->GetName()).c_str());
  291. GE_IF_BOOL_EXEC(out_data_anchor == nullptr, continue);
  292. auto out_data_anchor_idx = out_data_anchor->GetIdx();
  293. auto to_be_set_format =
  294. node->GetOpDesc()->MutableOutputDesc(static_cast<uint32_t>(out_data_anchor_idx))->GetOriginFormat();
  295. if (to_be_set_format == FORMAT_ND) {
  296. GELOGD("Node [%s] format is ND.[F]", (node->GetName()).c_str());
  297. continue;
  298. }
  299. for (const auto &peer_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  300. GE_IF_BOOL_EXEC(peer_in_data_anchor == nullptr, continue);
  301. auto peer_in_data_node = peer_in_data_anchor->GetOwnerNode();
  302. GE_IF_BOOL_EXEC(peer_in_data_node == nullptr, continue);
  303. GE_IF_BOOL_EXEC(peer_in_data_node->GetOpDesc() == nullptr, continue);
  304. // Check format whether have been set
  305. int idx = peer_in_data_anchor->GetIdx();
  306. // do peer_out_node name and index as key to lookup reflections
  307. ge::RefCell key(peer_in_data_node->GetName(), peer_in_data_node, ge::NODE_IN, idx);
  308. std::unordered_set<RefCell, RefCellHash> reflection;
  309. auto status = reflection_builder.LookUpRefRelations(key, reflection);
  310. if (status != GRAPH_SUCCESS) {
  311. GELOGE(GRAPH_FAILED, "LookUpRefRelations failed!Node is [%s],the %d input edge",
  312. (peer_in_data_node->GetName()).c_str(), idx);
  313. return GRAPH_FAILED;
  314. }
  315. auto ge_tensor_desc = peer_in_data_node->GetOpDesc()->GetInputDesc(static_cast<uint32_t>(idx));
  316. if (ge_tensor_desc.GetOriginFormat() == FORMAT_ND) {
  317. auto dim_num = ge_tensor_desc.GetShape().GetDimNum();
  318. if (dim_num == 0) {
  319. GELOGI("node name:%s idx:%d in is scalar. stop forward infer!", peer_in_data_node->GetName().c_str(), idx);
  320. continue;
  321. }
  322. /// Check whether node to change dims ()
  323. /// Because some node will calculate with 5D, C dim maybe multi meaning
  324. auto peer_in_data_node_type = peer_in_data_node->GetType();
  325. auto iter1 = kChangeDimNodes.find(peer_in_data_node_type);
  326. // 4 means dims num
  327. if ((iter1 != kChangeDimNodes.end()) && (dim_num < 4)) {
  328. GELOGD("Node[%s] is change dim node. do not infer origin format", (peer_in_data_node->GetName()).c_str());
  329. continue;
  330. }
  331. if (reflection.empty()) {
  332. ge_tensor_desc.SetOriginFormat(to_be_set_format);
  333. ge_tensor_desc.SetFormat(to_be_set_format);
  334. (void)peer_in_data_node->GetOpDesc()->UpdateInputDesc(static_cast<uint32_t>(idx), ge_tensor_desc);
  335. /// Because netoutput node added before infer format ,so netoutput is end condition
  336. /// must set netoutput format , because saved result depend on format
  337. if (peer_in_data_node_type == NETOUTPUT) {
  338. continue;
  339. }
  340. // Call operator infer format api (forward) to get out format
  341. GELOGD("call infer format func[Back]!Node is [%s] ", (peer_in_data_node->GetName()).c_str());
  342. status = peer_in_data_node->InferOriginFormat();
  343. if (status != GRAPH_SUCCESS) {
  344. GELOGE(GRAPH_FAILED, "Node[%s] infer format failed", (peer_in_data_node->GetName()).c_str());
  345. return GRAPH_FAILED;
  346. }
  347. nodes.push_back(peer_in_data_node);
  348. } else {
  349. auto status = ReflectionProcess(reflection, nodes, to_be_set_format);
  350. if (status != GRAPH_SUCCESS) {
  351. GELOGE(GRAPH_FAILED, "reflection process failed!");
  352. return GRAPH_FAILED;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. return GRAPH_SUCCESS;
  359. }
  360. void FormatRefiner::RefreshOriginFormatOfAnchor(std::vector<ge::NodePtr> &anchor_points) {
  361. for (const auto &node : anchor_points) {
  362. if (node == nullptr || node->GetOpDesc() == nullptr) {
  363. continue;
  364. }
  365. for (const auto &input_desc : node->GetOpDesc()->GetAllInputsDescPtr()) {
  366. if (input_desc != nullptr) {
  367. input_desc->SetOriginFormat(input_desc->GetFormat());
  368. }
  369. }
  370. for (const auto &output_desc : node->GetOpDesc()->GetAllOutputsDescPtr()) {
  371. if (output_desc != nullptr) {
  372. output_desc->SetOriginFormat(output_desc->GetFormat());
  373. }
  374. }
  375. }
  376. }
  377. graphStatus FormatRefiner::DataNodeFormatProcess(const ComputeGraphPtr &graph, std::vector<ge::NodePtr> &data_nodes,
  378. ge::Format data_format,
  379. std::unordered_map<ge::NodePtr, bool> &node_status) {
  380. if (!(IsGraphInferred(graph) && (!TypeUtils::IsInternalFormat(data_format)) && (data_format != FORMAT_ND))) {
  381. GELOGI("no necessary to do DataNodeFormatProcess. is_graph_inferred:%d, data_format:%s", IsGraphInferred(graph),
  382. TypeUtils::FormatToSerialString(data_format).c_str());
  383. return GRAPH_SUCCESS;
  384. }
  385. GELOGD("Enter DataNodeFormatProcess");
  386. std::vector<ge::NodePtr> uninfered_data_nodes;
  387. // Check and renew data nodes format
  388. for (const auto &data_node : data_nodes) {
  389. GE_CHECK_NOTNULL(data_node);
  390. auto op_desc = data_node->GetOpDesc();
  391. GE_CHECK_NOTNULL(op_desc);
  392. GE_CHECK_NOTNULL(op_desc->GetOutputDescPtr(0));
  393. auto curr_format = op_desc->GetOutputDescPtr(0)->GetOriginFormat();
  394. if (curr_format != FORMAT_ND) {
  395. // Data format has been infered , continue
  396. continue;
  397. }
  398. // Set format for un-infered data node
  399. auto input_descs = op_desc->GetAllInputsDescPtr();
  400. auto output_descs = op_desc->GetAllOutputsDescPtr();
  401. for (const auto &input_desc : input_descs) {
  402. if (input_desc != nullptr) {
  403. input_desc->SetOriginFormat(data_format);
  404. input_desc->SetFormat(data_format);
  405. }
  406. }
  407. for (const auto &output_desc : output_descs) {
  408. if (output_desc != nullptr) {
  409. output_desc->SetOriginFormat(data_format);
  410. output_desc->SetFormat(data_format);
  411. }
  412. }
  413. uninfered_data_nodes.push_back(data_node);
  414. }
  415. // Reinfer format from uninfered data nodes
  416. for (const auto &node : uninfered_data_nodes) {
  417. if (node == nullptr) {
  418. continue;
  419. }
  420. GELOGD("data node [%s] start infer format process", node->GetName().c_str());
  421. auto status = AnchorProcess(node, node_status);
  422. if (status != GRAPH_SUCCESS) {
  423. GELOGE(GRAPH_FAILED, "data node [%s] infer format process failed!", node->GetName().c_str());
  424. return GRAPH_FAILED;
  425. }
  426. }
  427. GELOGD("DataNodeFormatProcess success");
  428. return GRAPH_SUCCESS;
  429. }
  430. graphStatus FormatRefiner::InferOrigineFormat(const ge::ComputeGraphPtr &graph) {
  431. GELOGI("Enter InferOrigineFormat process!");
  432. // True: infered false:no-infered
  433. std::unordered_map<ge::NodePtr, bool> node_status;
  434. std::vector<ge::NodePtr> anchor_points;
  435. std::vector<ge::NodePtr> data_nodes;
  436. // global net format
  437. if (graph == nullptr) {
  438. GELOGE(GRAPH_FAILED, "input graph is null");
  439. return GRAPH_FAILED;
  440. }
  441. // build reflection relations of boundary
  442. (void)reflection_builder.Clear();
  443. auto status = reflection_builder.BuildRefRelations(*graph);
  444. if (status != GRAPH_SUCCESS) {
  445. GELOGE(GRAPH_FAILED, "build reflection relations failed for main and subgraph!");
  446. return GRAPH_FAILED;
  447. }
  448. // User set global net format
  449. status = GetAnchorPoints(graph, anchor_points, data_nodes, node_status);
  450. if (status != GRAPH_SUCCESS) {
  451. GELOGE(GRAPH_FAILED, "GetAnchorPoints Process Faild!");
  452. return GRAPH_FAILED;
  453. }
  454. // Refresh origin format of anchor point
  455. RefreshOriginFormatOfAnchor(anchor_points);
  456. // Infer format process
  457. for (const auto &anchor_node : anchor_points) {
  458. if (anchor_node == nullptr) {
  459. continue;
  460. }
  461. status = AnchorProcess(anchor_node, node_status);
  462. if (status != GRAPH_SUCCESS) {
  463. GELOGE(GRAPH_FAILED, "Anchor node [%s] process failed!", anchor_node->GetName().c_str());
  464. return GRAPH_FAILED;
  465. }
  466. }
  467. /// According to discuss with sys-enginer, data node default format is ND.Its format
  468. /// should be set by infered.But if some data-node can not be got by infer, set context's
  469. /// format for these data nodes.
  470. /// Notice: ignore 5D formats
  471. auto data_format = graph->GetDataFormat();
  472. status = DataNodeFormatProcess(graph, data_nodes, data_format, node_status);
  473. (void)AttrUtils::SetBool(graph, kIsGraphInferred, true);
  474. return status;
  475. }
  476. bool FormatRefiner::IsGraphInferred(const ComputeGraphPtr &graph) {
  477. bool is_graph_inferred = false;
  478. return (AttrUtils::GetBool(graph, kIsGraphInferred, is_graph_inferred) && is_graph_inferred);
  479. }
  480. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示