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.

infershape_pass.cc 21 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "graph/passes/infershape_pass.h"
  17. #include "common/util/error_manager/error_manager.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "analyzer/analyzer.h"
  20. #include "framework/common/util.h"
  21. #include "graph/common/omg_util.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "graph/debug/ge_util.h"
  24. #include "graph/operator_factory_impl.h"
  25. #include "graph/utils/graph_utils.h"
  26. #include "graph/utils/node_utils.h"
  27. #include "graph/utils/tensor_utils.h"
  28. #include "graph/utils/type_utils.h"
  29. namespace ge {
  30. namespace {
  31. const char *const kPreOpInputShapeRange = "_pre_op_in_range";
  32. thread_local std::unordered_map<NodePtr, InferenceContextPtr> context_map;
  33. }
  34. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY void InferShapePass::ClearContextMap() { context_map.clear(); }
  35. InferenceContextPtr CreateInferenceContextPtr(const std::unordered_map<NodePtr, InferenceContextPtr> &context_map,
  36. const NodePtr &node) {
  37. if (node == nullptr) {
  38. GELOGE(GRAPH_FAILED, "node is null");
  39. return nullptr;
  40. }
  41. InferenceContextPtr inference_context = std::shared_ptr<InferenceContext>(InferenceContext::Create());
  42. if (inference_context == nullptr) {
  43. REPORT_CALL_ERROR("E19999", "Failed to alloc InferenceContext, node:%s", node->GetName().c_str());
  44. GELOGE(GRAPH_FAILED, "[Alloc][InferenceContext] failed.");
  45. return nullptr;
  46. }
  47. auto all_in_data_anchors = node->GetAllInDataAnchors();
  48. std::vector<std::vector<ShapeAndType>> input_shapes_and_types(all_in_data_anchors.size());
  49. std::vector<std::string> marks;
  50. bool has_input_shapes_and_types = false;
  51. for (const auto &in_anchor : all_in_data_anchors) {
  52. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  53. if (out_anchor == nullptr) {
  54. continue;
  55. }
  56. auto input_node = out_anchor->GetOwnerNode();
  57. if (input_node == nullptr) {
  58. continue;
  59. }
  60. auto iter = context_map.find(input_node);
  61. if (iter != context_map.end()) {
  62. const auto &src_context = iter->second;
  63. GE_IF_BOOL_EXEC(src_context == nullptr, REPORT_INNER_ERROR("E19999", "src_context is null.");
  64. GELOGE(GRAPH_FAILED, "[Check][Param] src_context is null."); return nullptr);
  65. GELOGD("node:%s get %ld marks from node:%s", node->GetName().c_str(), src_context->GetMarks().size(),
  66. input_node->GetName().c_str());
  67. for (auto mark : src_context->GetMarks()) {
  68. marks.push_back(mark);
  69. }
  70. auto output_idx = out_anchor->GetIdx();
  71. auto input_idx = in_anchor->GetIdx();
  72. auto output_shape_and_type = src_context->GetOutputHandleShapesAndTypes();
  73. if (output_idx < static_cast<int>(output_shape_and_type.size())) {
  74. GELOGI("Add shape and type from %s:%d to %s:%d", input_node->GetName().c_str(), output_idx,
  75. node->GetName().c_str(), input_idx);
  76. input_shapes_and_types[input_idx] = output_shape_and_type[output_idx];
  77. has_input_shapes_and_types = true;
  78. } else {
  79. GELOGI("[%s] Output out of range. index = %d, size = %zu", node->GetName().c_str(), output_idx,
  80. output_shape_and_type.size());
  81. }
  82. }
  83. }
  84. if (has_input_shapes_and_types) {
  85. inference_context->SetInputHandleShapesAndTypes(std::move(input_shapes_and_types));
  86. }
  87. inference_context->SetMarks(marks);
  88. return inference_context;
  89. }
  90. void SerialShapeRange(const GeTensorDescPtr &desc, std::string &desc_str) {
  91. desc_str += "[";
  92. std::vector<std::pair<int64_t, int64_t>> shape_range;
  93. (void)desc->GetShapeRange(shape_range);
  94. for (const auto &pair : shape_range) {
  95. desc_str += "{";
  96. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  97. desc_str += "},";
  98. }
  99. desc_str += "]";
  100. shape_range.clear();
  101. (void)desc->GetOriginShapeRange(shape_range);
  102. for (const auto &pair : shape_range) {
  103. desc_str += ",{";
  104. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  105. desc_str += "},";
  106. }
  107. }
  108. std::string GetInTensorInfoWithString(const ge::NodePtr &node) {
  109. ge::OpDescPtr op_desc = node->GetOpDesc();
  110. std::stringstream ss;
  111. ss << "{";
  112. int32_t in_idx = 0;
  113. for (const auto &input_desc : op_desc->GetAllInputsDescPtr()) {
  114. if (input_desc == nullptr) {
  115. in_idx++;
  116. continue;
  117. }
  118. if (in_idx > 0) {
  119. ss << " ";
  120. }
  121. ss << "input_" << in_idx << " "
  122. << "tensor: [";
  123. ss << "(shape:[" << input_desc->MutableShape().ToString() << "]),";
  124. ss << "(format:" << TypeUtils::FormatToSerialString(input_desc->GetFormat()) << "),";
  125. ss << "(dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetDataType()) << "),";
  126. ss << "(origin_shape:" << input_desc->GetOriginShape().ToString() << "),";
  127. ss << "(origin_format:" << TypeUtils::FormatToSerialString(input_desc->GetOriginFormat()) << "),";
  128. ss << "(origin_dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetOriginDataType()) << "),";
  129. string range_str;
  130. SerialShapeRange(input_desc, range_str);
  131. ss << "(shape_range:" << range_str << ")]";
  132. in_idx++;
  133. }
  134. return ss.str();
  135. }
  136. void InferShapePass::AnalyzeFailedInfo(const NodePtr &node) {
  137. auto graph = node->GetOwnerComputeGraph();
  138. if (graph == nullptr) {
  139. GELOGW("Owner compute graph of node %s is nullptr", node->GetName().c_str());
  140. }
  141. auto root_graph = ge::GraphUtils::FindRootGraph(graph);
  142. if (root_graph == nullptr) {
  143. GELOGW("Root compute graph of node %s is nullptr", node->GetName().c_str());
  144. }
  145. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(), analyzer::INFER_SHAPE, node,
  146. "InferShapeFailed!"};
  147. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  148. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(), root_graph->GetGraphID());
  149. REPORT_CALL_ERROR("E19999", "Call InferShapeAndType for node:%s(%s) failed, input_tensor:%s", node->GetName().c_str(),
  150. node->GetType().c_str(), GetInTensorInfoWithString(node).c_str());
  151. GELOGE(GE_GRAPH_INFERSHAPE_FAILED, "[Call][InferShapeAndType] for node:%s(%s) failed, input_tensor:%s",
  152. node->GetName().c_str(), node->GetType().c_str(), GetInTensorInfoWithString(node).c_str());
  153. }
  154. bool InferShapePass::TensorDescChanged(const GeTensorDescPtr &src, const GeTensorDescPtr &dst) {
  155. bool changed = false;
  156. const auto &dst_dims = dst->GetShape().GetDims();
  157. const auto &src_dims = src->GetShape().GetDims();
  158. if (dst_dims != src_dims) {
  159. changed = true;
  160. }
  161. return changed;
  162. }
  163. graphStatus InferShapePass::UpdateInputDescAttr(const GeTensorDescPtr &src, GeTensorDescPtr &dst, bool &changed) {
  164. dst->SetOriginShape(src->GetOriginShape());
  165. dst->SetShape(src->MutableShape());
  166. dst->SetDataType(src->GetDataType());
  167. dst->SetOriginDataType(src->GetOriginDataType());
  168. if (src->MutableShape().GetDims() != UNKNOWN_RANK) {
  169. std::vector<std::pair<int64_t, int64_t>> shape_range;
  170. (void)src->GetShapeRange(shape_range);
  171. dst->SetShapeRange(shape_range);
  172. }
  173. std::vector<int64_t> pre_op_in_range;
  174. if (ge::AttrUtils::GetListInt(*src, kPreOpInputShapeRange, pre_op_in_range)) {
  175. (void)ge::AttrUtils::SetListInt(*dst, kPreOpInputShapeRange, pre_op_in_range);
  176. }
  177. ge::TensorUtils::SetRealDimCnt(*dst, static_cast<uint32_t>(src->MutableShape().GetDims().size()));
  178. return GRAPH_SUCCESS;
  179. }
  180. graphStatus InferShapePass::Infer(NodePtr &node) {
  181. bool is_unknown_graph = node->GetOwnerComputeGraph()->GetGraphUnknownFlag();
  182. auto opdesc = node->GetOpDesc();
  183. if (node->Verify() != GRAPH_SUCCESS) {
  184. REPORT_CALL_ERROR("E19999", "Verifying %s failed.", node->GetName().c_str());
  185. GELOGE(GRAPH_FAILED, "[Call][Verify] Verifying %s failed.", node->GetName().c_str());
  186. return GRAPH_FAILED;
  187. }
  188. PrintInOutTensorShape(node, "before_infershape");
  189. Operator op = OpDescUtils::CreateOperatorFromNode(node);
  190. if (!is_unknown_graph) {
  191. auto inference_context = CreateInferenceContextPtr(context_map, node);
  192. GE_CHECK_NOTNULL(inference_context);
  193. GELOGD("create context for node:%s, marks %zu", node->GetName().c_str(), inference_context->GetMarks().size());
  194. op.SetInferenceContext(inference_context);
  195. }
  196. graphStatus status = CallInferShapeFunc(node, op);
  197. if (status != GRAPH_PARAM_INVALID && status != GRAPH_SUCCESS) {
  198. REPORT_CALL_ERROR("E19999", "%s call infer function failed.", node->GetName().c_str());
  199. GELOGE(GRAPH_FAILED, "[Call][InferFunction] failed, node:%s.", node->GetName().c_str());
  200. return GRAPH_FAILED;
  201. }
  202. if (!is_unknown_graph) {
  203. auto ctx_after_infer = op.GetInferenceContext();
  204. if (ctx_after_infer != nullptr) {
  205. GELOGD("[%s] after infershape. mark:%zu", node->GetName().c_str(), ctx_after_infer->GetMarks().size());
  206. if (!ctx_after_infer->GetOutputHandleShapesAndTypes().empty() || !ctx_after_infer->GetMarks().empty()) {
  207. GELOGD("[%s] set inference context after. mark:%zu", node->GetName().c_str(),
  208. ctx_after_infer->GetMarks().size());
  209. (void)context_map.emplace(node, ctx_after_infer);
  210. }
  211. }
  212. }
  213. return GRAPH_SUCCESS;
  214. }
  215. graphStatus InferShapePass::CallInferShapeFunc(NodePtr &node, Operator &op) {
  216. auto op_desc = node->GetOpDesc();
  217. const auto &op_type = op_desc->GetType();
  218. auto ret = op_desc->CallInferFunc(op);
  219. if (ret == GRAPH_PARAM_INVALID) {
  220. // Op ir no infer func, try to get infer func from operator factory
  221. auto node_op = ge::OperatorFactory::CreateOperator("node_op", op_desc->GetType());
  222. if (node_op.IsEmpty()) {
  223. GELOGW("get op from OperatorFactory fail. opType: %s", op_type.c_str());
  224. return ret;
  225. }
  226. GELOGD("get op from OperatorFactory success. opType: %s", op_type.c_str());
  227. auto temp_op_desc = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  228. node_op.BreakConnect();
  229. if (temp_op_desc == nullptr) {
  230. REPORT_CALL_ERROR("E19999", "GetOpDescFromOperator failed, return nullptr.");
  231. GELOGE(GRAPH_FAILED, "[Get][OpDesc] temp op desc is null");
  232. return GRAPH_FAILED;
  233. }
  234. if (!op_desc->UpdateInputName(temp_op_desc->GetAllInputName())) {
  235. GELOGW("InferShapeAndType UpdateInputName failed");
  236. for (const auto &out_desc : op_desc->GetAllOutputsDescPtr()) {
  237. if (out_desc != nullptr && out_desc->GetShape().GetDims().empty()) {
  238. break;
  239. }
  240. return GRAPH_SUCCESS;
  241. }
  242. }
  243. if (!op_desc->UpdateOutputName(temp_op_desc->GetAllOutputName())) {
  244. GELOGW("InferShapeAndType UpdateOutputName failed");
  245. }
  246. op_desc->AddInferFunc(temp_op_desc->GetInferFunc());
  247. ret = op_desc->CallInferFunc(op);
  248. GELOGI("op CallInferFunc second. ret: %u", ret);
  249. }
  250. return ret;
  251. }
  252. graphStatus InferShapePass::UpdatePeerInputs(NodePtr &node) {
  253. bool is_unknown_graph = node->GetOwnerComputeGraph()->GetGraphUnknownFlag();
  254. if (is_unknown_graph) {
  255. PrintInOutTensorShape(node, "after_infershape when running");
  256. return GRAPH_SUCCESS;
  257. }
  258. UpdateInputOutputOriginAttr(node);
  259. if (NodeUtils::UpdatePeerNodeInputDesc(node) != SUCCESS) {
  260. return GRAPH_FAILED;
  261. }
  262. PrintInOutTensorShape(node, "after_infershape");
  263. return GRAPH_SUCCESS;
  264. }
  265. void InferShapePass::UpdateInputOutputOriginAttr(NodePtr &node) {
  266. auto op_desc = node->GetOpDesc();
  267. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  268. auto output_tensor = op_desc->MutableOutputDesc(out_anchor->GetIdx());
  269. if (output_tensor == nullptr) {
  270. continue;
  271. }
  272. if (output_tensor->MutableShape().GetDims().empty()) {
  273. output_tensor->SetOriginShape(output_tensor->GetShape());
  274. }
  275. ge::TensorUtils::SetRealDimCnt(*output_tensor,
  276. static_cast<uint32_t>(output_tensor->GetOriginShape().GetDims().size()));
  277. output_tensor->SetOriginDataType(output_tensor->GetDataType());
  278. // set output origin shape range
  279. std::vector<std::pair<int64_t, int64_t>> range;
  280. (void)output_tensor->GetShapeRange(range);
  281. output_tensor->SetOriginShapeRange(range);
  282. GELOGD("node name is %s, origin shape is %ld, origin format is %s, origin data type is %s", node->GetName().c_str(),
  283. output_tensor->GetOriginShape().GetShapeSize(),
  284. TypeUtils::FormatToSerialString(output_tensor->GetOriginFormat()).c_str(),
  285. TypeUtils::DataTypeToSerialString(output_tensor->GetOriginDataType()).c_str());
  286. }
  287. for (const auto &in_anchor : node->GetAllInDataAnchors()) {
  288. auto input_tensor = op_desc->MutableInputDesc(in_anchor->GetIdx());
  289. if (input_tensor == nullptr) {
  290. continue;
  291. }
  292. // set input origin shape range
  293. std::vector<std::pair<int64_t, int64_t>> range;
  294. (void)input_tensor->GetShapeRange(range);
  295. input_tensor->SetOriginShapeRange(range);
  296. }
  297. }
  298. Status InferShapePass::DoRepassForLoopNode(NodePtr &node) {
  299. GE_CHK_STATUS_RET_NOLOG(RePassLoopNode(node));
  300. bool need_repass = false;
  301. auto has_attr = AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, need_repass);
  302. if (has_attr) {
  303. if (!OptionExists(kOptimizeAfterSubGraph)) {
  304. return SUCCESS;
  305. }
  306. if (need_repass) {
  307. AddImmediateRePassNode(node);
  308. GELOGD("Node %s need repass immediately.", node->GetName().c_str());
  309. } else {
  310. // clear attr on while
  311. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  312. }
  313. }
  314. return SUCCESS;
  315. }
  316. Status InferShapePass::RePassLoopNode(const NodePtr &node) {
  317. const auto RePassNode = [&](const std::set<std::string> &re_pass_types) {
  318. for (auto &n : node->GetOutDataNodes()) {
  319. GE_CHECK_NOTNULL(n);
  320. std::string node_type;
  321. GE_CHK_STATUS_RET(GetOriginalType(n, node_type), "[Get][OriginalType] of node:%s failed.", n->GetName().c_str());
  322. if (re_pass_types.count(node_type) > 0) {
  323. AddImmediateRePassNode(n);
  324. (void)AttrUtils::SetBool(n->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, false);
  325. GELOGD("Node %s need repass immediately after %s.", n->GetName().c_str(), node->GetName().c_str());
  326. }
  327. }
  328. return SUCCESS;
  329. };
  330. const auto ExProcNode = [&](const std::set<std::string> &proc_types,
  331. const std::function<void(InferShapePass *, NodePtr)> &proc_func,
  332. const std::string &info) {
  333. for (auto &n : node->GetOutDataNodes()) {
  334. GE_CHECK_NOTNULL(n);
  335. std::string node_type;
  336. GE_CHK_STATUS_RET(GetOriginalType(n, node_type), "[Get][OriginalType] of node:%s failed.", n->GetName().c_str());
  337. if (proc_types.count(node_type) > 0) {
  338. proc_func(this, n);
  339. GELOGD("Node %s %s after %s.", n->GetName().c_str(), info.c_str(), node->GetName().c_str());
  340. }
  341. }
  342. return SUCCESS;
  343. };
  344. std::string node_type;
  345. GE_CHK_STATUS_RET(GetOriginalType(node, node_type),
  346. "[Get][OriginalType] of node:%s failed.", node->GetName().c_str());
  347. if (kNextIterationOpTypes.count(node_type) > 0) {
  348. return RePassNode(kMergeOpTypes); // Re-Pass Merge
  349. }
  350. if (kMergeOpTypes.count(node_type) > 0) {
  351. if (node->GetOpDesc()->HasAttr(ATTR_NAME_NEED_INFER_AGAIN)) {
  352. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  353. return RePassNode(kSwitchOpTypes); // Re-Pass Switch
  354. }
  355. return SUCCESS;
  356. }
  357. if (kSwitchOpTypes.count(node_type) > 0) {
  358. if (node->GetOpDesc()->HasAttr(ATTR_NAME_NEED_INFER_AGAIN)) {
  359. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  360. return ExProcNode(kExitOpTypes, &InferShapePass::AddNodeResume, "need resume"); // Resume Exit
  361. } else {
  362. return ExProcNode(kExitOpTypes, &InferShapePass::AddNodeSuspend, "need suspend"); // Suspend Exit
  363. }
  364. }
  365. return SUCCESS;
  366. }
  367. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY
  368. graphStatus InferShapePass::InferShapeAndType(NodePtr &node) {
  369. GE_CHECK_NOTNULL(node);
  370. GE_CHECK_NOTNULL(node->GetOpDesc());
  371. InferShapePass pass;
  372. std::set<NodePtr> unused_changed_nodes;
  373. return pass.InferAndUpdate(node, true, unused_changed_nodes);
  374. }
  375. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY
  376. graphStatus InferShapePass::InferShapeAndType(NodePtr &node, bool before_subgraph) {
  377. GE_CHECK_NOTNULL(node);
  378. GE_CHECK_NOTNULL(node->GetOpDesc());
  379. InferShapePass pass;
  380. std::set<NodePtr> unused_changed_nodes;
  381. return pass.InferAndUpdate(node, before_subgraph, unused_changed_nodes);
  382. }
  383. graphStatus InferShapeForRunning::Infer(NodePtr &node) {
  384. auto opdesc = node->GetOpDesc();
  385. vector<ge::DataType> temp_dtype;
  386. for (auto &tensor_desc : opdesc->GetAllOutputsDescPtr()) {
  387. temp_dtype.emplace_back(tensor_desc->GetDataType());
  388. }
  389. PrintInOutTensorShape(node, "before_infershape when running");
  390. Operator op = OpDescUtils::CreateOperatorFromNode(node);
  391. graphStatus status = CallInferShapeFuncForRunning(node, op);
  392. if (status == GRAPH_PARAM_INVALID || status == GRAPH_SUCCESS) {
  393. // ensure the dtype is not changed after infershape in running
  394. auto after_opdesc = node->GetOpDesc();
  395. GE_IF_BOOL_EXEC(after_opdesc == nullptr, REPORT_INNER_ERROR("E19999", "param node has no opdesc, check invalid.");
  396. GELOGE(GRAPH_FAILED, "[Get][OpDesc] after_opdesc is null."); return GRAPH_FAILED);
  397. auto all_output_tensor = after_opdesc->GetAllOutputsDescPtr();
  398. for (size_t i = 0; i < all_output_tensor.size(); ++i) {
  399. if (all_output_tensor.at(i)->GetDataType() != temp_dtype[i]) {
  400. GELOGD("Op %s output %zu need reset dtype,original dtype is %s, new dtype is %s", node->GetName().c_str(), i,
  401. TypeUtils::DataTypeToSerialString(all_output_tensor.at(i)->GetDataType()).c_str(),
  402. TypeUtils::DataTypeToSerialString(temp_dtype[i]).c_str());
  403. all_output_tensor.at(i)->SetDataType(temp_dtype[i]);
  404. }
  405. }
  406. PrintInOutTensorShape(node, "after_infershape when running");
  407. return GRAPH_SUCCESS;
  408. } else {
  409. REPORT_CALL_ERROR("E19999", "%s call infer function failed.", node->GetName().c_str());
  410. GELOGE(GRAPH_FAILED, "[Call][InferFunction] failed, node:%s.", node->GetName().c_str());
  411. return GRAPH_FAILED;
  412. }
  413. }
  414. graphStatus InferShapeForRunning::CallInferShapeFuncForRunning(NodePtr &node, Operator &op) {
  415. auto op_desc = node->GetOpDesc();
  416. const auto &op_type = op_desc->GetType();
  417. // Create InferenceContext to avoid null pointer access.
  418. const static std::set<std::string> force_context_op_types{"Enter", "Switch", "RefSwitch"};
  419. if (force_context_op_types.count(op_type) > 0) {
  420. GELOGD("Set InferenceContext for node [%s]", op_desc->GetName().c_str());
  421. op.SetInferenceContext(std::shared_ptr<InferenceContext>(InferenceContext::Create()));
  422. }
  423. // Get infer func and execute
  424. auto ret = op_desc->CallInferFunc(op);
  425. if (ret == GRAPH_PARAM_INVALID) {
  426. GELOGD("NodeUtils::GetNodeType return value is: [%s]", NodeUtils::GetNodeType(*node).c_str());
  427. auto origin_type = NodeUtils::GetNodeType(*node);
  428. auto infer_func = ge::OperatorFactoryImpl::GetInferShapeFunc(origin_type);
  429. if (infer_func == nullptr) {
  430. REPORT_INNER_ERROR("E19999", "Failed to Get InferFunc. type is %s", origin_type.c_str());
  431. GELOGE(GRAPH_FAILED, "[Get][InferFunc] failed. type is %s", origin_type.c_str());
  432. return GRAPH_FAILED;
  433. }
  434. op_desc->AddInferFunc(infer_func);
  435. ret = op_desc->CallInferFunc(op);
  436. GELOGI("op CallInferFunc second. ret: %u", ret);
  437. }
  438. return ret;
  439. }
  440. bool InferShapeForRunning::TensorDescChanged(const GeTensorDescPtr &src, const GeTensorDescPtr &dst) {
  441. bool changed = false;
  442. const auto &dst_dims = dst->GetShape().GetDims();
  443. const auto &src_dims = src->GetShape().GetDims();
  444. if (dst_dims != src_dims) {
  445. changed = true;
  446. }
  447. return changed;
  448. }
  449. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY
  450. graphStatus InferShapeForRunning::InferShapeAndTypeForRunning(NodePtr &node, bool before_subgraph) {
  451. GE_CHECK_NOTNULL(node);
  452. GE_CHECK_NOTNULL(node->GetOpDesc());
  453. InferShapeForRunning pass;
  454. std::set<NodePtr> unused_changed_nodes;
  455. return pass.InferAndUpdate(node, before_subgraph, unused_changed_nodes);
  456. }
  457. } // namespace ge

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