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

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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.",
  56. TypeUtils::DataTypeToSerialString(dst_data_type).c_str());
  57. return PARAM_INVALID;
  58. }
  59. if (op_desc->GetOutputDescPtr(0) == nullptr) {
  60. GELOGE(PARAM_INVALID, "Bitcast node outputDesc is null.");
  61. return PARAM_INVALID;
  62. }
  63. if (op_desc->GetOutputDescPtr(0)->GetDataType() != dst_data_type) {
  64. GELOGE(PARAM_INVALID, "dst_data_type[%s] is not equal to output_data_type[%s].",
  65. TypeUtils::DataTypeToSerialString(dst_data_type).c_str(),
  66. TypeUtils::DataTypeToSerialString(op_desc->GetOutputDescPtr(0)->GetDataType()).c_str());
  67. return PARAM_INVALID;
  68. }
  69. return SUCCESS;
  70. }
  71. Status BitcastPass::CheckOutputShape(const OpDescPtr op_desc, const ge::DataType dst_data_type) {
  72. const GeTensorDescPtr &input_tensor_desc = op_desc->MutableInputDesc(0);
  73. const GeTensorDescPtr &output_tensor_desc = op_desc->MutableOutputDesc(0);
  74. if (input_tensor_desc == nullptr) {
  75. GELOGE(PARAM_INVALID, "input_tensor_desc must not be null.");
  76. return PARAM_INVALID;
  77. }
  78. // get origin data_type and shape
  79. ge::DataType ori_data_type = input_tensor_desc->GetDataType();
  80. if (ori_data_type >= ge::DT_UNDEFINED) {
  81. GELOGE(PARAM_INVALID, "ori_data_type[%s] is not valid.",
  82. TypeUtils::DataTypeToSerialString(ori_data_type).c_str());
  83. return PARAM_INVALID;
  84. }
  85. if (ori_data_type == dst_data_type) {
  86. GELOGW("Origin data type is equal to dest data type.");
  87. return SUCCESS;
  88. }
  89. BitcastPass::kVecInt64 dim_vec(input_tensor_desc->GetShape().GetDims());
  90. if (CalcAndUpdateShape(dim_vec, ori_data_type, dst_data_type) != SUCCESS) {
  91. GELOGE(PARAM_INVALID, "CalcAndUpdateShape failed.");
  92. return PARAM_INVALID;
  93. }
  94. if (dim_vec != output_tensor_desc->GetShape().GetDims()) {
  95. GELOGE(PARAM_INVALID, "out_put_shape is different from expectations.");
  96. return PARAM_INVALID;
  97. }
  98. return SUCCESS;
  99. }
  100. Status BitcastPass::CalcAndUpdateShape(BitcastPass::kVecInt64 &dim_vec, ge::DataType ori_data_type,
  101. ge::DataType dst_data_type) {
  102. if (dim_vec.size() == 0) {
  103. GELOGE(PARAM_INVALID, "Pre node shape size is zero.");
  104. return PARAM_INVALID;
  105. }
  106. int64_t ori_data_size = GetSizeByDataType(ori_data_type);
  107. int64_t dst_data_size = GetSizeByDataType(dst_data_type);
  108. if (ori_data_size == dst_data_size) {
  109. return SUCCESS;
  110. } else if (ori_data_size > dst_data_size) {
  111. if (ori_data_size % dst_data_size != 0) {
  112. GELOGE(PARAM_INVALID, "ori_data_size is not divisible by dst_data_size.");
  113. return PARAM_INVALID;
  114. }
  115. dim_vec.push_back(ori_data_size / dst_data_size);
  116. return SUCCESS;
  117. } else {
  118. if (dst_data_size % ori_data_size != 0) {
  119. GELOGE(PARAM_INVALID, "dst_data_size is not divisible by ori_data_size.");
  120. return PARAM_INVALID;
  121. }
  122. if (dim_vec[dim_vec.size() - 1] != (dst_data_size / ori_data_size)) {
  123. GELOGE(PARAM_INVALID, "The last dim is not equal to dst_data_size / ori_data_size.");
  124. return PARAM_INVALID;
  125. }
  126. dim_vec.pop_back();
  127. }
  128. return SUCCESS;
  129. }
  130. } // namespace ge

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