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 22 kB

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

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