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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "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. namespace ge {
  24. namespace {
  25. const char *const kAttrNameType = "type";
  26. } // namespace
  27. Status BitcastPass::Run(NodePtr &node) {
  28. GELOGD("Bitcast running");
  29. if (node == nullptr) {
  30. GELOGE(PARAM_INVALID, "Param [node] must not be null.");
  31. return PARAM_INVALID;
  32. }
  33. if (node->GetType() != BITCAST) {
  34. return SUCCESS;
  35. }
  36. OpDescPtr op_desc = node->GetOpDesc();
  37. if (op_desc == nullptr) {
  38. return PARAM_INVALID;
  39. }
  40. ge::DataType dst_data_type;
  41. if (CheckDstDataType(op_desc, dst_data_type) != SUCCESS) {
  42. return PARAM_INVALID;
  43. }
  44. if (CheckOutputShape(op_desc, dst_data_type) != SUCCESS) {
  45. return PARAM_INVALID;
  46. }
  47. return IsolateAndDeleteNode(node, {0});
  48. }
  49. Status BitcastPass::CheckDstDataType(const OpDescPtr op_desc, ge::DataType &dst_data_type) {
  50. if (!ge::AttrUtils::GetDataType(op_desc, kAttrNameType, dst_data_type)) {
  51. GELOGE(PARAM_INVALID, "Node failed to get attribute type.");
  52. return PARAM_INVALID;
  53. }
  54. if (dst_data_type >= ge::DT_UNDEFINED) {
  55. GELOGE(PARAM_INVALID, "dst_data_type[%s] is not valid.", TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  56. return PARAM_INVALID;
  57. }
  58. if (op_desc->GetOutputDescPtr(0) == nullptr) {
  59. GELOGE(PARAM_INVALID, "Bitcast node outputDesc is null.");
  60. return PARAM_INVALID;
  61. }
  62. if (op_desc->GetOutputDescPtr(0)->GetDataType() != dst_data_type) {
  63. GELOGE(PARAM_INVALID, "dst_data_type[%s] is not equal to output_data_type[%s].",
  64. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  65. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str());
  66. return PARAM_INVALID;
  67. }
  68. return SUCCESS;
  69. }
  70. Status BitcastPass::CheckOutputShape(const OpDescPtr op_desc, const ge::DataType dst_data_type) {
  71. const GeTensorDescPtr &input_tensor_desc = op_desc->MutableInputDesc(0);
  72. const GeTensorDescPtr &output_tensor_desc = op_desc->MutableOutputDesc(0);
  73. if (input_tensor_desc == nullptr) {
  74. GELOGE(PARAM_INVALID, "input_tensor_desc must not be null.");
  75. return PARAM_INVALID;
  76. }
  77. // get origin data_type and shape
  78. ge::DataType ori_data_type = input_tensor_desc->GetDataType();
  79. if (ori_data_type >= ge::DT_UNDEFINED) {
  80. GELOGE(PARAM_INVALID, "ori_data_type[%s] is not valid.", TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  81. return PARAM_INVALID;
  82. }
  83. if (ori_data_type == dst_data_type) {
  84. GELOGW("Origin data type is equal to dest data type.");
  85. return SUCCESS;
  86. }
  87. BitcastPass::kVecInt64 dim_vec(input_tensor_desc->GetShape().GetDims());
  88. if (CalcAndUpdateShape(dim_vec, ori_data_type, dst_data_type) != SUCCESS) {
  89. GELOGE(PARAM_INVALID, "CalcAndUpdateShape failed.");
  90. return PARAM_INVALID;
  91. }
  92. if (dim_vec != output_tensor_desc->GetShape().GetDims()) {
  93. GELOGE(PARAM_INVALID, "out_put_shape is different from expectations.");
  94. return PARAM_INVALID;
  95. }
  96. return SUCCESS;
  97. }
  98. Status BitcastPass::CalcAndUpdateShape(BitcastPass::kVecInt64 &dim_vec, ge::DataType ori_data_type,
  99. ge::DataType dst_data_type) {
  100. if (dim_vec.size() == 0) {
  101. GELOGE(PARAM_INVALID, "Pre node shape size is zero.");
  102. return PARAM_INVALID;
  103. }
  104. int64_t ori_data_size = GetSizeByDataType(ori_data_type);
  105. int64_t dst_data_size = GetSizeByDataType(dst_data_type);
  106. if (ori_data_size == dst_data_size) {
  107. return SUCCESS;
  108. } else if (ori_data_size > dst_data_size) {
  109. if (ori_data_size % dst_data_size != 0) {
  110. GELOGE(PARAM_INVALID, "ori_data_size is not divisible by dst_data_size.");
  111. return PARAM_INVALID;
  112. }
  113. dim_vec.push_back(ori_data_size / dst_data_size);
  114. return SUCCESS;
  115. } else {
  116. if (dst_data_size % ori_data_size != 0) {
  117. GELOGE(PARAM_INVALID, "dst_data_size is not divisible by ori_data_size.");
  118. return PARAM_INVALID;
  119. }
  120. if (dim_vec[dim_vec.size() - 1] != (dst_data_size / ori_data_size)) {
  121. GELOGE(PARAM_INVALID, "The last dim is not equal to dst_data_size / ori_data_size.");
  122. return PARAM_INVALID;
  123. }
  124. dim_vec.pop_back();
  125. }
  126. return SUCCESS;
  127. }
  128. } // namespace ge

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