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.

omg_util.cc 11 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "common/omg_util.h"
  17. #include "graph/debug/ge_attr_define.h"
  18. #include "graph/utils/graph_utils.h"
  19. #include "graph/utils/tensor_utils.h"
  20. #include "common/math/math_util.h"
  21. namespace ge {
  22. ///
  23. /// @brief get the Original Type of FrameworkOp
  24. /// @param [in] node
  25. /// @param [out] type
  26. /// @return Status
  27. ///
  28. Status GetOriginalType(const ge::NodePtr &node, string &type) {
  29. GE_CHECK_NOTNULL(node);
  30. type = node->GetType();
  31. GE_IF_BOOL_EXEC(type != FRAMEWORKOP, return SUCCESS);
  32. GE_CHECK_NOTNULL(node->GetOpDesc());
  33. bool ret = ge::AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type);
  34. if (!ret) {
  35. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail from op:%s(%s)", ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
  36. node->GetName().c_str(), node->GetType().c_str());
  37. GELOGE(INTERNAL_ERROR, "[Get][Attr] %s fail from op:%s(%s)", ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
  38. node->GetName().c_str(), node->GetType().c_str());
  39. return INTERNAL_ERROR;
  40. }
  41. GELOGD("Get FrameWorkOp original type [%s]", type.c_str());
  42. return SUCCESS;
  43. }
  44. ///
  45. /// @brief set op stream_label
  46. /// @param [in] node
  47. /// @param [in] label
  48. /// @return Status
  49. ///
  50. Status SetStreamLabel(const ge::NodePtr &node, const std::string &label) {
  51. GE_CHECK_NOTNULL(node);
  52. OpDescPtr tmp_desc = node->GetOpDesc();
  53. GE_CHECK_NOTNULL(tmp_desc);
  54. if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_STREAM_LABEL, label)) {
  55. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_STREAM_LABEL.c_str(),
  56. node->GetName().c_str(), node->GetType().c_str());
  57. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_STREAM_LABEL.c_str(), node->GetName().c_str(),
  58. node->GetType().c_str());
  59. return FAILED;
  60. }
  61. return SUCCESS;
  62. }
  63. ///
  64. /// @brief set op cycle_event flag
  65. /// @param [in] node
  66. /// @return Status
  67. ///
  68. Status SetCycleEvent(const ge::NodePtr &node) {
  69. GE_CHECK_NOTNULL(node);
  70. OpDescPtr tmp_desc = node->GetOpDesc();
  71. GE_CHECK_NOTNULL(tmp_desc);
  72. if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_STREAM_CYCLE_EVENT_FLAG, true)) {
  73. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_STREAM_CYCLE_EVENT_FLAG.c_str(),
  74. node->GetName().c_str(), node->GetType().c_str());
  75. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_STREAM_CYCLE_EVENT_FLAG.c_str(),
  76. node->GetName().c_str(), node->GetType().c_str());
  77. return FAILED;
  78. }
  79. return SUCCESS;
  80. }
  81. ///
  82. /// @brief set op active_label_list
  83. /// @param [in] node
  84. /// @param [in] active_label_list
  85. /// @return Status
  86. ///
  87. Status SetActiveLabelList(const ge::NodePtr &node, const std::vector<std::string> &active_label_list) {
  88. GE_CHECK_NOTNULL(node);
  89. OpDescPtr tmp_desc = node->GetOpDesc();
  90. GE_CHECK_NOTNULL(tmp_desc);
  91. if (!AttrUtils::SetListStr(tmp_desc, ge::ATTR_NAME_ACTIVE_LABEL_LIST, active_label_list)) {
  92. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(),
  93. node->GetName().c_str(), node->GetType().c_str());
  94. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ACTIVE_LABEL_LIST.c_str(), node->GetName().c_str(),
  95. node->GetType().c_str());
  96. return FAILED;
  97. }
  98. return SUCCESS;
  99. }
  100. ///
  101. /// @brief set op branch_label
  102. /// @param [in] node
  103. /// @param [in] branch_label
  104. /// @return Status
  105. ///
  106. Status SetSwitchBranchNodeLabel(const ge::NodePtr &node, const std::string &branch_label) {
  107. GE_CHECK_NOTNULL(node);
  108. OpDescPtr tmp_desc = node->GetOpDesc();
  109. GE_CHECK_NOTNULL(tmp_desc);
  110. if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_SWITCH_BRANCH_NODE_LABEL, branch_label)) {
  111. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_SWITCH_BRANCH_NODE_LABEL.c_str(),
  112. node->GetName().c_str(), node->GetType().c_str());
  113. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_SWITCH_BRANCH_NODE_LABEL.c_str(),
  114. node->GetName().c_str(), node->GetType().c_str());
  115. return FAILED;
  116. }
  117. return SUCCESS;
  118. }
  119. ///
  120. /// @brief set op true_branch flag
  121. /// @param [in] node
  122. /// @param [in] value
  123. /// @return Status
  124. ///
  125. Status SetSwitchTrueBranchFlag(const ge::NodePtr &node, bool value) {
  126. GE_CHECK_NOTNULL(node);
  127. OpDescPtr tmp_desc = node->GetOpDesc();
  128. GE_CHECK_NOTNULL(tmp_desc);
  129. if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG, value)) {
  130. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG.c_str(),
  131. node->GetName().c_str(), node->GetType().c_str());
  132. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG.c_str(),
  133. node->GetName().c_str(), node->GetType().c_str());
  134. return FAILED;
  135. }
  136. return SUCCESS;
  137. }
  138. ///
  139. /// @brief set op original name
  140. /// @param [in] node
  141. /// @param [in] orig_name
  142. /// @return Status
  143. ///
  144. Status SetOriginalNodeName(const ge::NodePtr &node, const std::string &orig_name) {
  145. GE_CHECK_NOTNULL(node);
  146. OpDescPtr tmp_desc = node->GetOpDesc();
  147. GE_CHECK_NOTNULL(tmp_desc);
  148. if (!AttrUtils::SetStr(tmp_desc, ge::ATTR_NAME_ORIG_NODE_NAME, orig_name)) {
  149. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_ORIG_NODE_NAME.c_str(),
  150. node->GetName().c_str(), node->GetType().c_str());
  151. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_ORIG_NODE_NAME.c_str(), node->GetName().c_str(),
  152. node->GetType().c_str());
  153. return FAILED;
  154. }
  155. return SUCCESS;
  156. }
  157. ///
  158. /// @brief set op cyclic_dependence flag
  159. /// @param [in] node
  160. /// @return Status
  161. ///
  162. Status SetCyclicDependenceFlag(const ge::NodePtr &node) {
  163. GE_CHECK_NOTNULL(node);
  164. OpDescPtr tmp_desc = node->GetOpDesc();
  165. GE_CHECK_NOTNULL(tmp_desc);
  166. if (!AttrUtils::SetBool(tmp_desc, ge::ATTR_NAME_CYCLIC_DEPENDENCE_FLAG, true)) {
  167. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_CYCLIC_DEPENDENCE_FLAG.c_str(),
  168. node->GetName().c_str(), node->GetType().c_str());
  169. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_CYCLIC_DEPENDENCE_FLAG.c_str(),
  170. node->GetName().c_str(), node->GetType().c_str());
  171. return FAILED;
  172. }
  173. return SUCCESS;
  174. }
  175. ///
  176. /// @brief set op next_iteration name
  177. /// @param [in] Merge Node
  178. /// @param [in] NextIteration Node
  179. /// @return Status
  180. ///
  181. Status SetNextIteration(const NodePtr &node, const NodePtr &next) {
  182. GE_CHECK_NOTNULL(node);
  183. GE_CHECK_NOTNULL(next);
  184. GE_CHECK_NOTNULL(node->GetOpDesc());
  185. GE_CHECK_NOTNULL(next->GetOpDesc());
  186. const auto SetIterationName = [](const OpDescPtr &op_desc, const std::string &name) {
  187. if (!AttrUtils::SetStr(op_desc, ATTR_NAME_NEXT_ITERATION, name)) {
  188. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_NEXT_ITERATION.c_str(),
  189. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  190. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_NEXT_ITERATION.c_str(), op_desc->GetName().c_str(),
  191. op_desc->GetType().c_str());
  192. return FAILED;
  193. }
  194. return SUCCESS;
  195. };
  196. GE_CHK_STATUS_RET_NOLOG(SetIterationName(node->GetOpDesc(), next->GetName()));
  197. GE_CHK_STATUS_RET_NOLOG(SetIterationName(next->GetOpDesc(), node->GetName()));
  198. return SUCCESS;
  199. }
  200. ///
  201. /// @brief Align the memory
  202. /// @param [in/out] memory size
  203. /// @param [in] alinment
  204. /// @return void
  205. ///
  206. void AlignMemSize(int64_t &mem_size, int64_t align_size) {
  207. if (mem_size <= 0) {
  208. return;
  209. }
  210. mem_size = (mem_size + align_size - 1) / align_size * align_size;
  211. }
  212. ///
  213. /// @brief Get memory size from tensor desc
  214. /// @param [in] node
  215. /// @param [out] memory size
  216. /// @return Status
  217. ///
  218. Status GetMemorySize(const NodePtr &node, int64_t &output_size) {
  219. GE_CHECK_NOTNULL(node->GetOpDesc());
  220. auto output_op_desc = node->GetOpDesc()->GetOutputDescPtr(kBufferPoolNodeOutIndex);
  221. GE_CHECK_NOTNULL(output_op_desc);
  222. int64_t size = 0;
  223. auto ret = ge::TensorUtils::GetSize(*output_op_desc, size);
  224. if (ret != ge::GRAPH_SUCCESS) {
  225. GELOGE(INTERNAL_ERROR, "[Get][Size]Node:%s.", node->GetName().c_str());
  226. REPORT_INNER_ERROR("E19999", "Failed to get output size, node:%s.", node->GetName().c_str());
  227. return INTERNAL_ERROR;
  228. }
  229. FMK_INT64_ADDCHECK(size, kBufferPoolMemAlignSize);
  230. AlignMemSize(size, kBufferPoolMemAlignSize);
  231. // The HCOM operator requires an additional 512 bytes before and after
  232. FMK_INT64_ADDCHECK(size, (kBufferPoolMemAlignSize + kBufferPoolMemAlignSize));
  233. output_size = kBufferPoolMemAlignSize + size + kBufferPoolMemAlignSize;
  234. return SUCCESS;
  235. }
  236. ///
  237. /// @brief Check Is Unknown shape Tensor
  238. /// @param [in] tensor_desc
  239. /// @return true: Unknown / false: Known
  240. ///
  241. bool IsUnknownShapeTensor(const GeTensorDesc &tensor_desc) {
  242. const static int kUnknowShape = -1;
  243. const static int kUnknowRank = -2;
  244. for (auto dim_size : tensor_desc.GetShape().GetDims()) {
  245. if (dim_size == kUnknowShape || dim_size == kUnknowRank) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. ///
  252. /// @brief Set Op _control_flow_group flag
  253. /// @param [in] node
  254. /// @param [in] group, condition group index of node.
  255. /// @return
  256. ///
  257. void SetControlFlowGroup(const NodePtr &node, int64_t group) {
  258. GE_RT_VOID_CHECK_NOTNULL(node);
  259. const auto &op_desc = node->GetOpDesc();
  260. GE_RT_VOID_CHECK_NOTNULL(op_desc);
  261. // op_desc as AttrHolderAdapter valid, Set attribute always success, just log for check.
  262. GELOGD("[%s] Set control flow group index: %ld", node->GetName().c_str(), group);
  263. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_CONTROL_FLOW_GROUP, group)) {
  264. REPORT_INNER_ERROR("E19999", "Set Attr:%s fail for op:%s(%s)", ATTR_NAME_CONTROL_FLOW_GROUP.c_str(),
  265. node->GetName().c_str(), node->GetType().c_str());
  266. GELOGE(FAILED, "[Set][Attr] %s fail for op:%s(%s)", ATTR_NAME_CONTROL_FLOW_GROUP.c_str(), node->GetName().c_str(),
  267. node->GetType().c_str());
  268. }
  269. }
  270. } // namespace ge

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