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

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Copyright 2020-2021 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/shape_refiner.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "common/omg_util.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/utils/tensor_utils.h"
  27. #include "graph/utils/type_utils.h"
  28. #include "external/graph/operator_factory.h"
  29. namespace ge {
  30. namespace {
  31. constexpr int kSwitchExitAnchorIndex = 0;
  32. constexpr int kSwitchPredAnchorIndex = 1;
  33. void SerialShapeRange(const GeTensorDescPtr &desc, std::string &desc_str) {
  34. desc_str += "[";
  35. std::vector<std::pair<int64_t, int64_t>> shape_range;
  36. (void)desc->GetShapeRange(shape_range);
  37. for (const auto &pair : shape_range) {
  38. desc_str += "{";
  39. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  40. desc_str += "},";
  41. }
  42. desc_str += "]";
  43. shape_range.clear();
  44. (void)desc->GetOriginShapeRange(shape_range);
  45. for (const auto &pair : shape_range) {
  46. desc_str += ",{";
  47. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  48. desc_str += "},";
  49. }
  50. }
  51. void UpdateShapeAndDType(const GeTensorDescPtr &src, GeTensorDescPtr &dst) {
  52. dst->SetOriginShape(src->GetOriginShape());
  53. dst->SetShape(src->GetShape());
  54. dst->SetDataType(src->GetDataType());
  55. dst->SetOriginDataType(src->GetOriginDataType());
  56. vector<pair<int64_t, int64_t>> src_shape_range;
  57. src->GetShapeRange(src_shape_range);
  58. dst->SetShapeRange(src_shape_range);
  59. dst->SetOriginShapeRange(src_shape_range);
  60. ge::TensorUtils::SetRealDimCnt(*dst, static_cast<uint32_t>(src->GetShape().GetDims().size()));
  61. }
  62. } // namespace
  63. std::string InferShapePass::SerialTensorInfo(const GeTensorDescPtr &tensor_desc) const {
  64. std::stringstream ss;
  65. ss << "(shape:[" << tensor_desc->MutableShape().ToString() << "]),";
  66. ss << "(format:" << TypeUtils::FormatToSerialString(tensor_desc->GetFormat()) << "),";
  67. ss << "(dtype:" << TypeUtils::DataTypeToSerialString(tensor_desc->GetDataType()) << "),";
  68. ss << "(origin_shape:" << tensor_desc->GetOriginShape().ToString() << "),";
  69. ss << "(origin_format:" << TypeUtils::FormatToSerialString(tensor_desc->GetOriginFormat()) << "),";
  70. ss << "(origin_dtype:" << TypeUtils::DataTypeToSerialString(tensor_desc->GetOriginDataType()) << "),";
  71. string range_str;
  72. SerialShapeRange(tensor_desc, range_str);
  73. ss << "(shape_range:" << range_str << ")";
  74. return ss.str();
  75. }
  76. Status InferShapePass::SuspendV1LoopExitNodes(const NodePtr &node) {
  77. if (node->GetType() != SWITCH) {
  78. return SUCCESS;
  79. }
  80. auto pred_node = NodeUtils::GetInDataNodeByIndex(*node, kSwitchPredAnchorIndex);
  81. GE_CHECK_NOTNULL(pred_node);
  82. if (pred_node->GetType() != LOOPCOND) {
  83. return SUCCESS;
  84. }
  85. for (const auto &anchor_2_node : NodeUtils::GetOutDataNodesWithAnchorByIndex(*node, kSwitchExitAnchorIndex)) {
  86. GELOGI("Found v1 loop when infershape, suspend Exit node %s, type %s.", anchor_2_node.second->GetName().c_str(),
  87. anchor_2_node.second->GetType().c_str());
  88. auto &suspend_nodes = graphs_2_suspend_nodes_[GetCurrentGraphName()];
  89. if (suspend_nodes.nodes_set.insert(anchor_2_node.second).second) {
  90. suspend_nodes.nodes.push(anchor_2_node.second);
  91. AddNodeSuspend(anchor_2_node.second);
  92. }
  93. }
  94. return SUCCESS;
  95. }
  96. Status InferShapePass::Infer(NodePtr &node) {
  97. auto ret = InferShapeAndType(node);
  98. if (ret != GRAPH_SUCCESS) {
  99. auto graph = node->GetOwnerComputeGraph();
  100. GE_CHECK_NOTNULL(graph);
  101. auto root_graph = ge::GraphUtils::FindRootGraph(graph);
  102. GE_CHECK_NOTNULL(root_graph);
  103. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(),
  104. analyzer::INFER_SHAPE, node, "InferShapeFailed!"};
  105. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  106. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(),
  107. root_graph->GetGraphID());
  108. REPORT_CALL_ERROR("E19999", "Call InferShapeAndType for node:%s(%s) failed", node->GetName().c_str(),
  109. node->GetType().c_str());
  110. GELOGE(GE_GRAPH_INFERSHAPE_FAILED, "[Call][InferShapeAndType] for node:%s(%s) failed", node->GetName().c_str(),
  111. node->GetType().c_str());
  112. return GE_GRAPH_INFERSHAPE_FAILED;
  113. }
  114. return SUCCESS;
  115. }
  116. graphStatus InferShapePass::InferShapeAndType(NodePtr &node) {
  117. auto ret = SuspendV1LoopExitNodes(node);
  118. if (ret != SUCCESS) {
  119. GELOGE(ret, "Suspend V1 loop exit nodes failed.");
  120. return ret;
  121. }
  122. bool is_unknown_graph = node->GetOwnerComputeGraph()->GetGraphUnknownFlag();
  123. auto opdesc = node->GetOpDesc();
  124. if (node->Verify() != GRAPH_SUCCESS) {
  125. REPORT_CALL_ERROR("E19999", "Verifying %s failed.", node->GetName().c_str());
  126. GELOGE(GRAPH_FAILED, "[Call][Verify] Verifying %s failed.", node->GetName().c_str());
  127. return GRAPH_FAILED;
  128. }
  129. Operator op = OpDescUtils::CreateOperatorFromNode(node);
  130. if (!is_unknown_graph) {
  131. auto inference_context = ShapeRefiner::CreateInferenceContext(node);
  132. GE_CHECK_NOTNULL(inference_context);
  133. std::vector<AscendString> marks;
  134. inference_context->GetMarks(marks);
  135. GELOGD("create context for node:%s, marks %zu", node->GetName().c_str(), marks.size());
  136. op.SetInferenceContext(inference_context);
  137. }
  138. graphStatus status = CallInferShapeFunc(node, op);
  139. if (status != GRAPH_NODE_NEED_REPASS && status != GRAPH_PARAM_INVALID && status != GRAPH_SUCCESS) {
  140. // node like netoutput return param_invalid, but valid ?
  141. return GE_GRAPH_INFERSHAPE_FAILED;
  142. }
  143. UpdateCurNodeOutputDesc(node);
  144. if (!is_unknown_graph) {
  145. auto ctx_after_infer = op.GetInferenceContext();
  146. if (ctx_after_infer != nullptr) {
  147. std::vector<AscendString> marks;
  148. ctx_after_infer->GetMarks(marks);
  149. GELOGD("[%s] after infershape. mark:%zu", node->GetName().c_str(), marks.size());
  150. if (!ctx_after_infer->GetOutputHandleShapesAndTypes().empty() || !marks.empty()) {
  151. GELOGD("[%s] set inference context after. mark:%zu", node->GetName().c_str(),
  152. marks.size());
  153. ShapeRefiner::PushToContextMap(node, ctx_after_infer);
  154. }
  155. }
  156. }
  157. return (status == GRAPH_NODE_NEED_REPASS) ? GRAPH_NODE_NEED_REPASS : GRAPH_SUCCESS;
  158. }
  159. void InferShapePass::UpdateCurNodeOutputDesc(NodePtr &node) {
  160. auto op_desc = node->GetOpDesc();
  161. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  162. auto output_tensor = op_desc->MutableOutputDesc(out_anchor->GetIdx());
  163. GE_IF_BOOL_EXEC(output_tensor == nullptr, continue);
  164. GE_IF_BOOL_EXEC(output_tensor->MutableShape().GetDims().empty(),
  165. output_tensor->SetOriginShape(output_tensor->GetShape()));
  166. ge::TensorUtils::SetRealDimCnt(*output_tensor, static_cast<uint32_t>(output_tensor->GetOriginShape().GetDims()
  167. .size()));
  168. output_tensor->SetOriginDataType(output_tensor->GetDataType());
  169. // set output origin shape range
  170. std::vector<std::pair<int64_t, int64_t>> range;
  171. (void)output_tensor->GetShapeRange(range);
  172. output_tensor->SetOriginShapeRange(range);
  173. GELOGD("node name is %s, origin shape is %ld, origin format is %s, origin data type is %s",
  174. node->GetName().c_str(), output_tensor->GetOriginShape().GetShapeSize(),
  175. TypeUtils::FormatToSerialString(output_tensor->GetOriginFormat()).c_str(),
  176. TypeUtils::DataTypeToSerialString(output_tensor->GetOriginDataType()).c_str());
  177. }
  178. }
  179. bool InferShapePass::SameTensorDesc(const GeTensorDescPtr &src, const GeTensorDescPtr &dst) {
  180. // check shape range
  181. vector<std::pair<int64_t, int64_t>> src_shape_range;
  182. vector<std::pair<int64_t, int64_t>> dst_shape_range;
  183. src->GetShapeRange(src_shape_range);
  184. dst->GetShapeRange(dst_shape_range);
  185. if (src_shape_range.size() != dst_shape_range.size()) {
  186. GELOGI("Src shape range size is %zu, dst shape range size is %zu, not same.", src_shape_range.size(),
  187. dst_shape_range.size());
  188. return false;
  189. }
  190. for (size_t i = 0; i < src_shape_range.size(); ++i) {
  191. if (src_shape_range[i].first != dst_shape_range[i].first ||
  192. src_shape_range[i].second != dst_shape_range[i].second) {
  193. GELOGI("Current dim %zu. Src shape range is [%lu-%lu], dst shape range is [%lu-%lu], not same.",
  194. i, src_shape_range[i].first, src_shape_range[i].second, dst_shape_range[i].first, dst_shape_range[i].second);
  195. return false;
  196. }
  197. }
  198. // check shape
  199. auto src_shape = src->GetShape();
  200. auto dst_shape = dst->GetShape();
  201. if (src_shape.GetDims() != dst_shape.GetDims() || src->GetOriginShape().GetDims() != dst->GetOriginShape().GetDims() ||
  202. src->GetDataType() != dst->GetDataType() || src->GetOriginDataType() != dst->GetOriginDataType()) {
  203. GELOGD(
  204. "Src shape is %s, origin_shape is %s, data_type is %s, origin data_type is %s; "
  205. "Dst shape is %s, origin_shape is %s, data_type is %s, original data_type is %s, not same.",
  206. src_shape.ToString().c_str(), src->GetOriginShape().ToString().c_str(),
  207. TypeUtils::DataTypeToSerialString(src->GetDataType()).c_str(),
  208. TypeUtils::DataTypeToSerialString(src->GetOriginDataType()).c_str(), dst_shape.ToString().c_str(),
  209. dst->GetOriginShape().ToString().c_str(), TypeUtils::DataTypeToSerialString(dst->GetDataType()).c_str(),
  210. TypeUtils::DataTypeToSerialString(dst->GetOriginDataType()).c_str());
  211. return false;
  212. }
  213. return true;
  214. }
  215. graphStatus InferShapePass::UpdateTensorDesc(const GeTensorDescPtr &src, GeTensorDescPtr &dst, bool &changed) {
  216. changed = !SameTensorDesc(src, dst);
  217. // refresh src itself
  218. src->SetOriginShape(src->GetShape());
  219. src->SetOriginDataType(src->GetDataType());
  220. TensorUtils::SetRealDimCnt(*src, static_cast<uint32_t>(src->GetOriginShape().GetDims().size()));
  221. vector<pair<int64_t, int64_t>> src_shape_range;
  222. src->GetShapeRange(src_shape_range);
  223. src->SetOriginShapeRange(src_shape_range);
  224. if (!changed) {
  225. GELOGD("Peer dst tensor_desc is same as src tensor_desc. No need update.");
  226. return SUCCESS;
  227. }
  228. UpdateShapeAndDType(src, dst);
  229. GELOGD(
  230. "UpdatePeerInputDesc from src Node: shape: [%s], datatype: %s, original datatype is %s."
  231. "To dst Node: shape: [%s], datatype: %s, original datatype is %s.",
  232. src->GetShape().ToString().c_str(), TypeUtils::DataTypeToSerialString(src->GetDataType()).c_str(),
  233. TypeUtils::DataTypeToSerialString(src->GetOriginDataType()).c_str(), dst->GetShape().ToString().c_str(),
  234. TypeUtils::DataTypeToSerialString(dst->GetDataType()).c_str(),
  235. TypeUtils::DataTypeToSerialString(dst->GetOriginDataType()).c_str());
  236. return SUCCESS;
  237. }
  238. graphStatus InferShapePass::CallInferShapeFunc(NodePtr &node, Operator &op) {
  239. auto op_desc = node->GetOpDesc();
  240. const auto &op_type = op_desc->GetType();
  241. auto ret = op_desc->CallInferFunc(op);
  242. if (ret == GRAPH_PARAM_INVALID) {
  243. // Op ir no infer func, try to get infer func from operator factory
  244. auto node_op = ge::OperatorFactory::CreateOperator("node_op", op_desc->GetType().c_str());
  245. if (node_op.IsEmpty()) {
  246. GELOGW("get op from OperatorFactory fail. opType: %s", op_type.c_str());
  247. return ret;
  248. }
  249. GELOGD("get op from OperatorFactory success. opType: %s", op_type.c_str());
  250. auto temp_op_desc = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  251. node_op.BreakConnect();
  252. if (temp_op_desc == nullptr) {
  253. REPORT_CALL_ERROR("E19999", "GetOpDescFromOperator failed, return nullptr.");
  254. GELOGE(GRAPH_FAILED, "[Get][OpDesc] temp op desc is null");
  255. return GRAPH_FAILED;
  256. }
  257. if (!op_desc->UpdateInputName(temp_op_desc->GetAllInputName())) {
  258. GELOGW("InferShapeAndType UpdateInputName failed");
  259. for (const auto &out_desc : op_desc->GetAllOutputsDescPtr()) {
  260. if (out_desc != nullptr && out_desc->GetShape().GetDims().empty()) {
  261. break;
  262. }
  263. return GRAPH_SUCCESS;
  264. }
  265. }
  266. if (!op_desc->UpdateOutputName(temp_op_desc->GetAllOutputName())) {
  267. GELOGW("InferShapeAndType UpdateOutputName failed");
  268. }
  269. op_desc->AddInferFunc(temp_op_desc->GetInferFunc());
  270. ret = op_desc->CallInferFunc(op);
  271. GELOGI("op CallInferFunc second. ret: %u", ret);
  272. }
  273. return ret;
  274. }
  275. graphStatus InferShapePass::UpdateOutputFromSubgraphs(const std::vector<GeTensorDescPtr> &src, GeTensorDescPtr &dst) {
  276. GELOGD("Enter update parent node shape for class branch op process");
  277. // check sub_graph shape.If not same ,do unknown shape process
  278. auto ref_out_tensor = src.at(0);
  279. ge::GeShape &ref_out_tensor_shape = ref_out_tensor->MutableShape();
  280. for (auto &tensor : src) {
  281. if (ref_out_tensor->GetDataType() != tensor->GetDataType()) {
  282. REPORT_INNER_ERROR("E19999", "Does not support diff dtype among all ref output, shape:%s",
  283. ref_out_tensor_shape.ToString().c_str());
  284. GELOGE(GRAPH_FAILED, "[Check][Param] node does not support diff dtype output");
  285. return GRAPH_FAILED;
  286. }
  287. auto shape = tensor->MutableShape();
  288. if (shape.GetDims().size() != ref_out_tensor_shape.GetDims().size()) {
  289. GELOGD("Shape from subgraph size: %lu, ref_out_tensor_shape size: %lu", shape.GetShapeSize(),
  290. ref_out_tensor_shape.GetShapeSize());
  291. ref_out_tensor_shape = GeShape(UNKNOWN_RANK);
  292. break;
  293. }
  294. for (size_t j = 0; j < ref_out_tensor_shape.GetDims().size(); j++) {
  295. if (ref_out_tensor_shape.GetDim(j) == shape.GetDim(j)) {
  296. continue;
  297. }
  298. GELOGD("j: %zu ,shape from subgraph size: %lu, ref_out_tensor_shape size: %lu", j, shape.GetShapeSize(),
  299. ref_out_tensor_shape.GetShapeSize());
  300. (void)ref_out_tensor_shape.SetDim(j, UNKNOWN_DIM);
  301. }
  302. }
  303. UpdateShapeAndDType(ref_out_tensor, dst);
  304. return GRAPH_SUCCESS;
  305. }
  306. graphStatus InferShapePass::UpdateOutputFromSubgraphsForMultiDims(const std::vector<GeTensorDescPtr> &src,
  307. GeTensorDescPtr &dst) {
  308. // check sub_graph shape. Get max for update.
  309. if (src.empty()) {
  310. GELOGI("Src subgraph shape is empty.");
  311. return SUCCESS;
  312. }
  313. int64_t max_size = 0;
  314. size_t max_shape_index = 0;
  315. auto &ref_out_tensor = src.at(0);
  316. for (size_t j = 0; j < src.size(); ++j) {
  317. auto &tensor = src.at(j);
  318. if (ref_out_tensor->GetDataType() != tensor->GetDataType()) {
  319. REPORT_INNER_ERROR("E19999", "node does not support diff dtype among all ref output");
  320. GELOGE(GRAPH_FAILED, "[Check][Param] node does not support diff dtype among all ref output");
  321. return GRAPH_FAILED;
  322. }
  323. auto shape = tensor->MutableShape();
  324. int64_t size = 1;
  325. for (auto dim : shape.GetDims()) {
  326. if (dim != 0 && INT64_MAX / dim < size) {
  327. REPORT_INNER_ERROR("E19999", "The shape:%s size overflow", shape.ToString().c_str());
  328. GELOGE(PARAM_INVALID, "[Check][Overflow] The shape size overflow");
  329. return PARAM_INVALID;
  330. }
  331. size *= dim;
  332. }
  333. if (size > max_size) {
  334. max_size = size;
  335. max_shape_index = j;
  336. }
  337. }
  338. UpdateShapeAndDType(src.at(max_shape_index), dst);
  339. return GRAPH_SUCCESS;
  340. }
  341. Status InferShapePass::OnSuspendNodesLeaked() {
  342. auto iter = graphs_2_suspend_nodes_.find(GetCurrentGraphName());
  343. if (iter == graphs_2_suspend_nodes_.end()) {
  344. GELOGI("Current graph %s no suspend node.", GetCurrentGraphName().c_str());
  345. return SUCCESS;
  346. }
  347. if (!iter->second.nodes.empty()) {
  348. AddNodeResume(iter->second.PopSuspendedNode());
  349. }
  350. return SUCCESS;
  351. }
  352. } // namespace ge

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