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.

permute_kernel.cc 6.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "host_kernels/permute_kernel.h"
  17. #include <memory>
  18. #include <vector>
  19. #include "common/debug/log.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "common/op/ge_op_utils.h"
  22. #include "common/types.h"
  23. #include "common/util.h"
  24. #include "graph/common/bcast.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.h"
  27. #include "common/formats/formats.h"
  28. #include "common/formats/format_transfers/format_transfer_transpose.h"
  29. #include "common/formats/utils/formats_trans_utils.h"
  30. #include "host_kernels/kernel_utils.h"
  31. #include "framework/common/ge_inner_error_codes.h"
  32. namespace ge {
  33. namespace {
  34. const char *const kAttrOrder = "order";
  35. const char *const kAttrPerm = "perm";
  36. const size_t kTbePermuteInputSize = 2;
  37. } // namespace
  38. Status PermuteKernel::ValidateInput(const OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input) {
  39. if (input.empty()) {
  40. GELOGE(PARAM_INVALID, "Input tensor vector is empty");
  41. return PARAM_INVALID;
  42. }
  43. ConstGeTensorPtr const_weight_ptr = input[0];
  44. if (const_weight_ptr == nullptr) {
  45. GELOGE(PARAM_INVALID, "Input const_weight_ptr is nullptr.");
  46. return PARAM_INVALID;
  47. }
  48. const uint8_t *src_data = const_weight_ptr->GetData().data();
  49. if ((op_desc_ptr == nullptr) || (src_data == nullptr)) {
  50. GELOGW("Input opDescPtr is nullptr.");
  51. return PARAM_INVALID;
  52. }
  53. if (op_desc_ptr->GetInputsSize() >= kTbePermuteInputSize) {
  54. GELOGW("trans_op has more than 1 input_size.");
  55. return NOT_CHANGED;
  56. }
  57. return SUCCESS;
  58. }
  59. Status PermuteKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  60. std::vector<GeTensorPtr> &v_output) {
  61. GELOGD("PermuteKernel begin.");
  62. Status status = ValidateInput(op_desc_ptr, input);
  63. if (status != SUCCESS) {
  64. return status;
  65. }
  66. ConstGeTensorPtr const_weight_ptr = input[0];
  67. GeTensorDesc op_desc = op_desc_ptr->GetOutputDesc(0);
  68. GeTensorDesc op_desc_in = op_desc_ptr->GetInputDesc(0);
  69. auto src_format = op_desc_in.GetFormat();
  70. auto src_shape = op_desc_in.GetShape().GetDims();
  71. auto src_data_type = op_desc_in.GetDataType();
  72. auto data_shape = op_desc.GetShape().GetDims();
  73. auto data_format = op_desc.GetFormat();
  74. auto data_type = op_desc.GetDataType();
  75. GELOGD(
  76. "current node %s, format %s, input shape %s, data type %s, weight format %s, shape %s, data type %s. "
  77. "output format %s, shape %s, data type %s",
  78. op_desc_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
  79. formats::ShapeToString(src_shape).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  80. TypeUtils::FormatToSerialString(const_weight_ptr->GetTensorDesc().GetFormat()).c_str(),
  81. formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str(),
  82. TypeUtils::DataTypeToSerialString(const_weight_ptr->GetTensorDesc().GetDataType()).c_str(),
  83. TypeUtils::FormatToSerialString(data_format).c_str(), formats::ShapeToString(data_shape).c_str(),
  84. TypeUtils::DataTypeToSerialString(data_type).c_str());
  85. vector<int64_t> perm_list;
  86. if (!AttrUtils::GetListInt(op_desc_ptr, kAttrOrder, perm_list) &&
  87. !AttrUtils::GetListInt(op_desc_ptr, kAttrPerm, perm_list)) {
  88. GELOGW("Get perm_list failed, Transpose from shape %s to %s is not supported, ",
  89. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str());
  90. return NOT_CHANGED;
  91. }
  92. GELOGD("Transpose from %s to %s, shape %s to %s, perm_list %s, data type %s",
  93. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  94. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  95. formats::ShapeToString(perm_list).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  96. if ((data_shape.empty()) || (src_data_type != data_type)) {
  97. GELOGW("Transpose is not supported. Invalid shape (src: %s, dst: %s) or inconsistent datatype (src: %s, dst: %s)",
  98. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  99. TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  100. TypeUtils::DataTypeToSerialString(data_type).c_str());
  101. return NOT_CHANGED;
  102. }
  103. if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
  104. GELOGW("CheckSize failed, input size is not equal to weight size");
  105. return NOT_CHANGED;
  106. }
  107. const uint8_t *src_data = const_weight_ptr->GetData().data();
  108. formats::TransResult trans_result;
  109. auto ret = formats::TransposeWithShapeCheck(src_data, src_shape, data_shape, src_data_type, perm_list, trans_result);
  110. if (ret != SUCCESS) {
  111. GELOGW("Failed to Transpose from %s to %s, shape %s to %s, perm_list %s, data type %s",
  112. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  113. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  114. formats::ShapeToString(perm_list).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  115. return NOT_CHANGED;
  116. }
  117. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  118. GE_CHECK_NOTNULL(output_ptr);
  119. GE_CHK_STATUS_RET(output_ptr->SetData(trans_result.data.get(), trans_result.length));
  120. v_output.push_back(output_ptr);
  121. return SUCCESS;
  122. }
  123. REGISTER_KERNEL(PERMUTE, PermuteKernel);
  124. REGISTER_KERNEL(TRANSPOSED, PermuteKernel);
  125. } // namespace ge

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