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

4 years ago
4 years ago
4 years ago
5 years ago
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
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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, "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. return PARAM_INVALID;
  42. }
  43. ge::DataType dst_data_type;
  44. if (CheckDstDataType(op_desc, dst_data_type) != SUCCESS) {
  45. return PARAM_INVALID;
  46. }
  47. if (CheckOutputShape(op_desc, dst_data_type) != SUCCESS) {
  48. return PARAM_INVALID;
  49. }
  50. return IsolateAndDeleteNode(node, {0});
  51. }
  52. Status BitcastPass::CheckDstDataType(const OpDescPtr op_desc, ge::DataType &dst_data_type) {
  53. if (!ge::AttrUtils::GetDataType(op_desc, kAttrNameType, dst_data_type)) {
  54. REPORT_CALL_ERROR("E19999", "Get Attr:%s of op:%s(%s) failed",
  55. kAttrNameType, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  56. GELOGE(PARAM_INVALID, "Node failed to get attribute type.");
  57. return PARAM_INVALID;
  58. }
  59. if (dst_data_type >= ge::DT_UNDEFINED) {
  60. REPORT_INNER_ERROR("E19999", "Param dst_data_type:%d check invalid, op:%s(%s)",
  61. dst_data_type, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  62. GELOGE(PARAM_INVALID, "dst_data_type[%s] is not valid.",
  63. TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  64. return PARAM_INVALID;
  65. }
  66. if (op_desc->GetOutputDescPtr(0) == nullptr) {
  67. REPORT_INNER_ERROR("E19999", "Index 0 ouput desc of op:%s(%s) not exist, check invalid",
  68. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  69. GELOGE(PARAM_INVALID, "Bitcast node outputDesc is null.");
  70. return PARAM_INVALID;
  71. }
  72. if (op_desc->GetOutputDescPtr(0)->GetDataType() != dst_data_type) {
  73. REPORT_INNER_ERROR("E19999", "Index 0 ouput desc of op:%s(%s), it't data type:%s not equal to dst_data_type:%s, "
  74. "check invalid", op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  75. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  76. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str());
  77. GELOGE(PARAM_INVALID, "dst_data_type[%s] is not equal to output_data_type[%s].",
  78. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  79. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str());
  80. return PARAM_INVALID;
  81. }
  82. return SUCCESS;
  83. }
  84. Status BitcastPass::CheckOutputShape(const OpDescPtr op_desc, const ge::DataType dst_data_type) {
  85. const GeTensorDescPtr &input_tensor_desc = op_desc->MutableInputDesc(0);
  86. const GeTensorDescPtr &output_tensor_desc = op_desc->MutableOutputDesc(0);
  87. if (input_tensor_desc == nullptr) {
  88. REPORT_INNER_ERROR("E19999", "Index 0 input desc of op:%s(%s) not exist, check invalid",
  89. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  90. GELOGE(PARAM_INVALID, "input_tensor_desc must not be null.");
  91. return PARAM_INVALID;
  92. }
  93. // get origin data_type and shape
  94. ge::DataType ori_data_type = input_tensor_desc->GetDataType();
  95. if (ori_data_type >= ge::DT_UNDEFINED) {
  96. REPORT_INNER_ERROR("E19999", "ori_data_type:%d of index 0 input desc in op:%s(%s), "
  97. "check invalid",
  98. ori_data_type, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  99. GELOGE(PARAM_INVALID, "ori_data_type[%s] is not valid.",
  100. TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  101. return PARAM_INVALID;
  102. }
  103. if (ori_data_type == dst_data_type) {
  104. GELOGW("Origin data type is equal to dest data type.");
  105. return SUCCESS;
  106. }
  107. BitcastPass::kVecInt64 dim_vec(input_tensor_desc->GetShape().GetDims());
  108. if (CalcAndUpdateShape(dim_vec, ori_data_type, dst_data_type) != SUCCESS) {
  109. GELOGE(PARAM_INVALID, "CalcAndUpdateShape failed.");
  110. return PARAM_INVALID;
  111. }
  112. if (dim_vec != output_tensor_desc->GetShape().GetDims()) {
  113. REPORT_INNER_ERROR("E19999", "Shape:%s of index 0 output desc in op:%s(%s), different from expect shape:%s ,"
  114. "check invalid",
  115. formats::JoinToString(output_tensor_desc->GetShape().GetDims()).c_str(),
  116. op_desc->GetName().c_str(), op_desc->GetType().c_str(), formats::JoinToString(dim_vec).c_str());
  117. GELOGE(PARAM_INVALID, "out_put_shape is different from expectations.");
  118. return PARAM_INVALID;
  119. }
  120. return SUCCESS;
  121. }
  122. Status BitcastPass::CalcAndUpdateShape(BitcastPass::kVecInt64 &dim_vec, ge::DataType ori_data_type,
  123. ge::DataType dst_data_type) {
  124. if (dim_vec.size() == 0) {
  125. REPORT_INNER_ERROR("E19999", "Param dim_vec is empty, check invalid");
  126. GELOGE(PARAM_INVALID, "Pre node shape size is zero.");
  127. return PARAM_INVALID;
  128. }
  129. int64_t ori_data_size = GetSizeByDataType(ori_data_type);
  130. int64_t dst_data_size = GetSizeByDataType(dst_data_type);
  131. if (ori_data_size == dst_data_size) {
  132. return SUCCESS;
  133. } else if (ori_data_size > dst_data_size) {
  134. if (ori_data_size % dst_data_size != 0) {
  135. REPORT_INNER_ERROR("E19999", "size:%ld of ori_data_type:%s is not divisible by size:%ld of dst_data_type:%s ,"
  136. "check invalid",
  137. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str(),
  138. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  139. GELOGE(PARAM_INVALID, "ori_data_size is not divisible by dst_data_size.");
  140. return PARAM_INVALID;
  141. }
  142. dim_vec.push_back(ori_data_size / dst_data_size);
  143. return SUCCESS;
  144. } else {
  145. if (dst_data_size % ori_data_size != 0) {
  146. REPORT_INNER_ERROR("E19999", "size:%ld of dst_data_type:%s is not divisible by size:%ld of ori_data_type:%s ,"
  147. "check invalid",
  148. dst_data_size, TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  149. ori_data_size, TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  150. GELOGE(PARAM_INVALID, "dst_data_size is not divisible by ori_data_size.");
  151. return PARAM_INVALID;
  152. }
  153. if (dim_vec[dim_vec.size() - 1] != (dst_data_size / ori_data_size)) {
  154. REPORT_INNER_ERROR("E19999", "The last dim:%ld in param dim_vec is not equal to "
  155. "dst_data_size:%ld / ori_data_size:%ld, check invalid",
  156. dim_vec[dim_vec.size() - 1], dst_data_size, ori_data_size);
  157. GELOGE(PARAM_INVALID, "The last dim is not equal to dst_data_size / ori_data_size.");
  158. return PARAM_INVALID;
  159. }
  160. dim_vec.pop_back();
  161. }
  162. return SUCCESS;
  163. }
  164. } // namespace ge

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