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.

transformer_utils.cc 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "transformer_utils.h"
  17. #include "external/ge/ge_api_types.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "graph/utils/type_utils.h"
  20. namespace ge {
  21. bool NodeShapeTransUtils::CatchFormatAndShape() {
  22. auto inputs = op_desc_->MutableAllInputName();
  23. auto outputs = op_desc_->MutableAllOutputName();
  24. for (auto &ele : inputs) {
  25. auto tensor_desc_input = op_desc_->MutableInputDesc(ele.first);
  26. if (tensor_desc_input == nullptr) {
  27. continue;
  28. }
  29. auto format = tensor_desc_input->GetFormat();
  30. auto ori_format = tensor_desc_input->GetOriginFormat();
  31. if (format == ori_format) {
  32. GELOGD("Node is %s, input tensor name is %s. ori format: %s, format: %s is same! No need to catch format&shape!",
  33. op_desc_->GetName().c_str(), ele.first.c_str(), TypeUtils::FormatToSerialString(ori_format).c_str(),
  34. TypeUtils::FormatToSerialString(format).c_str());
  35. continue;
  36. }
  37. map_format_in_.insert(std::pair<std::string, Format>(ele.first, format));
  38. map_ori_format_in_.insert(std::pair<std::string, Format>(ele.first, ori_format));
  39. map_dtype_in_.insert(std::pair<std::string, DataType>(ele.first, tensor_desc_input->GetDataType()));
  40. tensor_desc_input->SetFormat(ori_format);
  41. tensor_desc_input->SetShape(tensor_desc_input->GetOriginShape());
  42. }
  43. for (auto &ele : outputs) {
  44. auto tensor_desc_output = op_desc_->MutableOutputDesc(ele.first);
  45. if (tensor_desc_output == nullptr) {
  46. continue;
  47. }
  48. auto format = tensor_desc_output->GetFormat();
  49. auto ori_format = tensor_desc_output->GetOriginFormat();
  50. if (format == ori_format) {
  51. GELOGD("Node is %s, output tensor name is %s. ori format: %s, format: %s is same! No need to catch format&shape!",
  52. op_desc_->GetName().c_str(), ele.first.c_str(), TypeUtils::FormatToSerialString(ori_format).c_str(),
  53. TypeUtils::FormatToSerialString(format).c_str());
  54. continue;
  55. }
  56. map_format_out_.insert(std::pair<std::string, Format>(ele.first, format));
  57. map_ori_format_out_.insert(std::pair<std::string, Format>(ele.first, ori_format));
  58. map_dtype_out_.insert(std::pair<std::string, DataType>(ele.first, tensor_desc_output->GetDataType()));
  59. if (format == ori_format) {
  60. continue;
  61. }
  62. tensor_desc_output->SetFormat(ori_format);
  63. }
  64. return true;
  65. }
  66. bool NodeShapeTransUtils::UpdateFormatAndShape() {
  67. auto inputs = op_desc_->MutableAllInputName();
  68. auto outputs = op_desc_->MutableAllOutputName();
  69. for (auto &ele : inputs) {
  70. auto tensor_desc_input = op_desc_->MutableInputDesc(ele.first);
  71. if (tensor_desc_input == nullptr) {
  72. continue;
  73. }
  74. // if can not find saved info, it says format and origin format is same when catched
  75. if (map_format_in_.find(ele.first) == map_format_in_.end()) {
  76. GELOGD("Node is [%s], input tensor name [%s] is not been catched.Skip update action for it!",
  77. op_desc_->GetName().c_str(), ele.first.c_str());
  78. tensor_desc_input->SetOriginFormat(tensor_desc_input->GetFormat());
  79. tensor_desc_input->SetOriginShape(tensor_desc_input->GetShape());
  80. continue;
  81. }
  82. auto ori_format = tensor_desc_input->GetFormat();
  83. auto ori_shape = tensor_desc_input->GetShape();
  84. auto curr_format = map_format_in_[ele.first];
  85. if (ori_format == curr_format) {
  86. continue;
  87. }
  88. std::unique_ptr<common::transformer::ShapeTransferAccordingToFormat> shape_transfer(new(std::nothrow)
  89. common::transformer::ShapeTransferAccordingToFormat());
  90. if (shape_transfer == nullptr) {
  91. GELOGE(GRAPH_FAILED, "Memory alloc failed");
  92. return false;
  93. }
  94. std::vector<int64_t> ori_shape_dims = ori_shape.GetDims();
  95. std::vector<int64_t> out_dims;
  96. ge::DataType dtype = map_dtype_in_[ele.first];
  97. common::transformer::ShapeAndFormat shape_and_format_info {ori_shape_dims, out_dims, ori_format, curr_format, dtype,
  98. common::transformer::EN_IMPL_CUSTOM_TBE};
  99. shape_transfer->GetShapeAccordingToFormat(shape_and_format_info);
  100. tensor_desc_input->SetFormat(curr_format);
  101. tensor_desc_input->SetShape(GeShape(out_dims));
  102. }
  103. for (auto &ele : outputs) {
  104. auto tensor_desc_output = op_desc_->MutableOutputDesc(ele.first);
  105. if (tensor_desc_output == nullptr) {
  106. continue;
  107. }
  108. // if can not find saved info, it says format and origin format is same when catched
  109. if (map_ori_format_out_.find(ele.first) == map_ori_format_out_.end()) {
  110. GELOGD("Node is [%s], input tensor name [%s] is not been catched.Skip update action for it!",
  111. op_desc_->GetName().c_str(), ele.first.c_str());
  112. tensor_desc_output->SetOriginFormat(tensor_desc_output->GetFormat());
  113. tensor_desc_output->SetOriginShape(tensor_desc_output->GetShape());
  114. continue;
  115. }
  116. auto ori_shape = tensor_desc_output->GetShape();
  117. auto curr_format = tensor_desc_output->GetFormat();
  118. if (curr_format != map_ori_format_out_[ele.first]) {
  119. GELOGE(GRAPH_FAILED, "Node is %s, out tensor name is %s. format: %s, recorded origin format: %s is not same",
  120. op_desc_->GetName().c_str(), ele.first.c_str(), TypeUtils::FormatToSerialString(curr_format).c_str(),
  121. TypeUtils::FormatToSerialString(map_ori_format_out_[ele.first]).c_str());
  122. return GRAPH_FAILED;
  123. }
  124. tensor_desc_output->SetOriginShape(ori_shape);
  125. auto saved_format = map_format_out_[ele.first];
  126. if (curr_format == saved_format) {
  127. GELOGD("Nodeis %s, out tensor name is %s. ori format: %s, recorded format: %s is same! No need to transfer",
  128. op_desc_->GetName().c_str(), ele.first.c_str(), TypeUtils::FormatToSerialString(curr_format).c_str(),
  129. TypeUtils::FormatToSerialString(saved_format).c_str());
  130. continue;
  131. }
  132. tensor_desc_output->SetFormat(saved_format);
  133. std::unique_ptr<common::transformer::ShapeTransferAccordingToFormat> shape_transfer(new(std::nothrow)
  134. common::transformer::ShapeTransferAccordingToFormat());
  135. if (shape_transfer == nullptr) {
  136. GELOGE(GRAPH_FAILED, "Memory alloc failed");
  137. return false;
  138. }
  139. std::vector<int64_t> ori_shape_dims = ori_shape.GetDims();
  140. std::vector<int64_t> out_dims;
  141. ge::DataType dtype = tensor_desc_output->GetDataType();
  142. common::transformer::ShapeAndFormat shape_and_format_info {ori_shape_dims, out_dims, curr_format, saved_format,
  143. dtype, common::transformer::EN_IMPL_CUSTOM_TBE};
  144. shape_transfer->GetShapeAccordingToFormat(shape_and_format_info);
  145. tensor_desc_output->SetShape(GeShape(out_dims));
  146. GELOGD("Node is %s, out tensor name is %s. Update format and shape success,ori format: %s, format: %s",
  147. op_desc_->GetName().c_str(), ele.first.c_str(), TypeUtils::FormatToSerialString(curr_format).c_str(),
  148. TypeUtils::FormatToSerialString(saved_format).c_str());
  149. }
  150. GELOGD("Node is %s. Update format and shape success", op_desc_->GetName().c_str());
  151. return true;
  152. }
  153. } // namespace ge

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