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.

transdata_kernel.cc 6.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/transdata_kernel.h"
  17. #include <memory>
  18. #include <vector>
  19. #include "common/debug/log.h"
  20. #include "common/formats/formats.h"
  21. #include "common/formats/utils/formats_trans_utils.h"
  22. #include "common/fp16_t.h"
  23. #include "common/op/ge_op_utils.h"
  24. #include "common/types.h"
  25. #include "common/util.h"
  26. #include "framework/common/debug/ge_log.h"
  27. #include "framework/common/ge_inner_error_codes.h"
  28. #include "graph/common/bcast.h"
  29. #include "host_kernels/kernel_utils.h"
  30. #include "graph/utils/type_utils.h"
  31. #include "inc/kernel_factory.h"
  32. namespace ge {
  33. namespace {
  34. const size_t kTransdataInputSize = 1;
  35. }
  36. Status TransdataKernel::ValidateInput(const OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input) {
  37. if (input.empty()) {
  38. GELOGE(PARAM_INVALID, "Input tensor vector is empty");
  39. return PARAM_INVALID;
  40. }
  41. ConstGeTensorPtr const_weight_ptr = input[0];
  42. if (const_weight_ptr == nullptr) {
  43. GELOGE(PARAM_INVALID, "Input const_weight_ptr is nullptr.");
  44. return PARAM_INVALID;
  45. }
  46. // src_data == nullptr is supported
  47. if (op_desc_ptr == nullptr) {
  48. GELOGE(PARAM_INVALID, "Input opDescPtr is nullptr.");
  49. return PARAM_INVALID;
  50. }
  51. if (op_desc_ptr->GetInputsSize() != kTransdataInputSize) {
  52. GELOGW("trans_op has more than 1 input_size.");
  53. return NOT_CHANGED;
  54. }
  55. return SUCCESS;
  56. }
  57. Status TransdataKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  58. std::vector<GeTensorPtr> &v_output) {
  59. GE_CHECK_NOTNULL(op_desc_ptr);
  60. GELOGD("TransdataKernel begin.");
  61. Status status = ValidateInput(op_desc_ptr, input);
  62. if (status != SUCCESS) {
  63. return status;
  64. }
  65. ConstGeTensorPtr const_weight_ptr = input[0];
  66. const auto &op_desc = op_desc_ptr->MutableOutputDesc(0);
  67. const auto &op_desc_in = op_desc_ptr->MutableInputDesc(0);
  68. GE_CHECK_NOTNULL(op_desc);
  69. GE_CHECK_NOTNULL(op_desc_in);
  70. const auto &src_format = op_desc_in->GetFormat();
  71. const auto &src_shape = op_desc_in->GetShape().GetDims();
  72. const auto &src_data_type = op_desc_in->GetDataType();
  73. const auto &data_shape = op_desc->GetShape().GetDims();
  74. const auto &data_format = op_desc->GetFormat();
  75. const auto &data_type = op_desc->GetDataType();
  76. const in64_t groups = op_desc_ptr->GetAttr("groups", groups)
  77. GELOGD(
  78. "current node %s, format %s, input shape %s, data type %s, weight format %s, shape %s, data type %s. "
  79. "output format %s, shape %s, data type %s, groups %d",
  80. op_desc_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
  81. formats::ShapeToString(src_shape).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  82. TypeUtils::FormatToSerialString(const_weight_ptr->GetTensorDesc().GetFormat()).c_str(),
  83. formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str(),
  84. TypeUtils::DataTypeToSerialString(const_weight_ptr->GetTensorDesc().GetDataType()).c_str(),
  85. TypeUtils::FormatToSerialString(data_format).c_str(), formats::ShapeToString(data_shape).c_str(),
  86. TypeUtils::DataTypeToSerialString(data_type).c_str(), groups);
  87. const uint8_t *src_data = const_weight_ptr->GetData().data();
  88. const formats::TransArgs trans_args{src_data, src_format, data_format, src_shape, data_shape, src_data_type};
  89. formats::TransResult trans_result;
  90. GELOGD("Trans formats from %s to %s, shape %s to %s, data type %s",
  91. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  92. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  93. TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  94. if (src_data_type != data_type || data_shape.empty() || !formats::IsTransFormatSupport(trans_args)) {
  95. GELOGW("Transfer from format %s to %s, shape %s to %s, data type %s to %s is not supported",
  96. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  97. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  98. TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  99. TypeUtils::DataTypeToSerialString(data_type).c_str());
  100. return NOT_CHANGED;
  101. }
  102. if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
  103. GELOGI("CheckSize failed, input size is not equal to weight size");
  104. return NOT_CHANGED;
  105. }
  106. if((src_format == FOMAT_HWCN) && (data_format == FORMAT_FRACTAL_Z)) {
  107. if (formats::TransFormat(trans_args, trans_result , groups) != SUCCESS) {
  108. GELOGW("Failed to trans formats from %s to %s, shape %s to %s, data type %s",
  109. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  110. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  111. TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  112. return NOT_CHANGED;
  113. }
  114. }
  115. if (formats::TransFormat(trans_args, trans_result) != SUCCESS) {
  116. GELOGW("Failed to trans formats from %s to %s, shape %s to %s, data type %s",
  117. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  118. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  119. TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  120. return NOT_CHANGED;
  121. }
  122. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  123. if (output_ptr == nullptr) {
  124. GELOGE(ge::PARAM_INVALID, "Make shared failed");
  125. return ge::PARAM_INVALID;
  126. }
  127. if (output_ptr->SetData(trans_result.data.get(), trans_result.length) != GRAPH_SUCCESS) {
  128. GELOGW("Compute: SetData failed");
  129. }
  130. v_output.push_back(output_ptr);
  131. return SUCCESS;
  132. }
  133. REGISTER_KERNEL(TRANSDATA, TransdataKernel);
  134. } // namespace ge

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