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.

bitcast_pass.cc 9.4 kB

4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/bitcast_pass.h"
  17. #include <vector>
  18. #include <string>
  19. #include "common/ge/ge_util.h"
  20. #include "graph/utils/type_utils.h"
  21. #include "framework/common/debug/log.h"
  22. #include "framework/common/ge_inner_error_codes.h"
  23. #include "common/formats/utils/formats_trans_utils.h"
  24. namespace ge {
  25. namespace {
  26. const char *const kAttrNameType = "type";
  27. } // namespace
  28. Status BitcastPass::Run(NodePtr &node) {
  29. GELOGD("Bitcast running");
  30. if (node == nullptr) {
  31. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  32. GELOGE(PARAM_INVALID, "[Check][Param] Param [node] must not be null.");
  33. return PARAM_INVALID;
  34. }
  35. if (node->GetType() != BITCAST) {
  36. return SUCCESS;
  37. }
  38. OpDescPtr op_desc = node->GetOpDesc();
  39. if (op_desc == nullptr) {
  40. REPORT_INNER_ERROR("E19999", "Param op_desc of node is nullptr, check invalid");
  41. GELOGE(PARAM_INVALID, "[Get][OpDesc] failed, op_desc of node is nullptr");
  42. return PARAM_INVALID;
  43. }
  44. ge::DataType dst_data_type;
  45. if (CheckDstDataType(op_desc, dst_data_type) != SUCCESS) {
  46. return PARAM_INVALID;
  47. }
  48. if (CheckOutputShape(op_desc, dst_data_type) != SUCCESS) {
  49. return PARAM_INVALID;
  50. }
  51. return IsolateAndDeleteNode(node, {0});
  52. }
  53. Status BitcastPass::CheckDstDataType(const OpDescPtr op_desc, ge::DataType &dst_data_type) {
  54. if (!ge::AttrUtils::GetDataType(op_desc, kAttrNameType, dst_data_type)) {
  55. REPORT_CALL_ERROR("E19999", "Get Attr:%s of op:%s(%s) failed",
  56. kAttrNameType, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  57. GELOGE(PARAM_INVALID, "[Get][Attr] %s of op:%s(%s) failed",
  58. kAttrNameType, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  59. return PARAM_INVALID;
  60. }
  61. if (dst_data_type >= ge::DT_UNDEFINED) {
  62. REPORT_INNER_ERROR("E19999", "Param dst_data_type:%d check invalid, op:%s(%s)",
  63. dst_data_type, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  64. GELOGE(PARAM_INVALID, "[Check][Param] dst_data_type[%s] is not valid, op:%s(%s)",
  65. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  66. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  67. return PARAM_INVALID;
  68. }
  69. if (op_desc->GetOutputDescPtr(0) == nullptr) {
  70. REPORT_INNER_ERROR("E19999", "Index 0 ouput desc of op:%s(%s) not exist, check invalid",
  71. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  72. GELOGE(PARAM_INVALID, "[Get][OutputDescPtr] Index 0 ouput desc of op:%s(%s) not exist.",
  73. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  74. return PARAM_INVALID;
  75. }
  76. if (op_desc->GetOutputDescPtr(0)->GetDataType() != dst_data_type) {
  77. REPORT_INNER_ERROR("E19999", "Index 0 ouput desc of op:%s(%s), it't data type:%s not equal to dst_data_type:%s, "
  78. "check invalid", op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  79. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  80. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str());
  81. GELOGE(PARAM_INVALID, "[Check][Param] dst_data_type[%s] is not equal to output_data_type[%s], op:%s(%s).",
  82. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  83. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str(),
  84. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  85. return PARAM_INVALID;
  86. }
  87. return SUCCESS;
  88. }
  89. Status BitcastPass::CheckOutputShape(const OpDescPtr op_desc, const ge::DataType dst_data_type) {
  90. const GeTensorDescPtr &input_tensor_desc = op_desc->MutableInputDesc(0);
  91. const GeTensorDescPtr &output_tensor_desc = op_desc->MutableOutputDesc(0);
  92. if (input_tensor_desc == nullptr) {
  93. REPORT_INNER_ERROR("E19999", "Index 0 input desc of op:%s(%s) not exist, check invalid",
  94. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  95. GELOGE(PARAM_INVALID, "[Check][Param] input_tensor_desc(index:0) of op:%s(%s) must not be null.",
  96. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  97. return PARAM_INVALID;
  98. }
  99. // get origin data_type and shape
  100. ge::DataType ori_data_type = input_tensor_desc->GetDataType();
  101. if (ori_data_type >= ge::DT_UNDEFINED) {
  102. REPORT_INNER_ERROR("E19999", "ori_data_type:%d of index 0 input desc in op:%s(%s), "
  103. "check invalid",
  104. ori_data_type, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  105. GELOGE(PARAM_INVALID, "[Check][Param] ori_data_type[%s] of input desc(index 0) in op:%s(%s) is not valid.",
  106. TypeUtils::DataTypeToSerialString(ori_data_type).c_str(),
  107. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  108. return PARAM_INVALID;
  109. }
  110. if (ori_data_type == dst_data_type) {
  111. GELOGW("Origin data type is equal to dest data type.");
  112. return SUCCESS;
  113. }
  114. BitcastPass::kVecInt64 dim_vec(input_tensor_desc->GetShape().GetDims());
  115. if (CalcAndUpdateShape(dim_vec, ori_data_type, dst_data_type) != SUCCESS) {
  116. GELOGE(PARAM_INVALID, "[Call][CalcAndUpdateShape] failed.");
  117. return PARAM_INVALID;
  118. }
  119. if (dim_vec != output_tensor_desc->GetShape().GetDims()) {
  120. REPORT_INNER_ERROR("E19999", "Shape:%s of index 0 output desc in op:%s(%s), different from expect shape:%s ,"
  121. "check invalid",
  122. formats::JoinToString(output_tensor_desc->GetShape().GetDims()).c_str(),
  123. op_desc->GetName().c_str(), op_desc->GetType().c_str(), formats::JoinToString(dim_vec).c_str());
  124. GELOGE(PARAM_INVALID, "[Check][Param] Shape:%s of index 0 output desc in op:%s(%s), different from expect shape:%s",
  125. formats::JoinToString(output_tensor_desc->GetShape().GetDims()).c_str(),
  126. op_desc->GetName().c_str(), op_desc->GetType().c_str(), formats::JoinToString(dim_vec).c_str());
  127. return PARAM_INVALID;
  128. }
  129. return SUCCESS;
  130. }
  131. Status BitcastPass::CalcAndUpdateShape(BitcastPass::kVecInt64 &dim_vec, ge::DataType ori_data_type,
  132. ge::DataType dst_data_type) {
  133. if (dim_vec.size() == 0) {
  134. REPORT_INNER_ERROR("E19999", "Param dim_vec is empty, check invalid");
  135. GELOGE(PARAM_INVALID, "[Check][Param] Param dim_vec is empty.");
  136. return PARAM_INVALID;
  137. }
  138. int64_t ori_data_size = GetSizeByDataType(ori_data_type);
  139. int64_t dst_data_size = GetSizeByDataType(dst_data_type);
  140. if (ori_data_size == dst_data_size) {
  141. return SUCCESS;
  142. } else if (ori_data_size > dst_data_size) {
  143. if (ori_data_size % dst_data_size != 0) {
  144. REPORT_INNER_ERROR("E19999", "size:%ld of ori_data_type:%s is not divisible by size:%ld of dst_data_type:%s ,"
  145. "check invalid",
  146. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str(),
  147. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  148. GELOGE(PARAM_INVALID,
  149. "[Check][Param] size:%ld of ori_data_type:%s is not divisible by size:%ld of dst_data_type:%s.",
  150. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str(),
  151. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  152. return PARAM_INVALID;
  153. }
  154. dim_vec.push_back(ori_data_size / dst_data_size);
  155. return SUCCESS;
  156. } else {
  157. if (dst_data_size % ori_data_size != 0) {
  158. REPORT_INNER_ERROR("E19999", "size:%ld of dst_data_type:%s is not divisible by size:%ld of ori_data_type:%s ,"
  159. "check invalid",
  160. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  161. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  162. GELOGE(PARAM_INVALID,
  163. "[Check][Param] size:%ld of dst_data_type:%s is not divisible by size:%ld of ori_data_type:%s.",
  164. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  165. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  166. return PARAM_INVALID;
  167. }
  168. if (dim_vec[dim_vec.size() - 1] != (dst_data_size / ori_data_size)) {
  169. REPORT_INNER_ERROR("E19999", "The last dim:%ld in param dim_vec is not equal to "
  170. "dst_data_size:%ld / ori_data_size:%ld, check invalid",
  171. dim_vec[dim_vec.size() - 1], dst_data_size, ori_data_size);
  172. GELOGE(PARAM_INVALID, "[Check][Param] The last dim:%ld in param dim_vec is not equal to "
  173. "dst_data_size:%ld / ori_data_size:%ld",
  174. dim_vec[dim_vec.size() - 1], dst_data_size, ori_data_size);
  175. return PARAM_INVALID;
  176. }
  177. dim_vec.pop_back();
  178. }
  179. return SUCCESS;
  180. }
  181. } // namespace ge

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