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

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 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/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. GELOGD(
  77. "current node %s, format %s, input shape %s, data type %s, weight format %s, shape %s, data type %s. "
  78. "output format %s, shape %s, data type %s",
  79. op_desc_ptr->GetName().c_str(), TypeUtils::FormatToSerialString(src_format).c_str(),
  80. formats::ShapeToString(src_shape).c_str(), TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  81. TypeUtils::FormatToSerialString(const_weight_ptr->GetTensorDesc().GetFormat()).c_str(),
  82. formats::ShapeToString(const_weight_ptr->GetTensorDesc().GetShape()).c_str(),
  83. TypeUtils::DataTypeToSerialString(const_weight_ptr->GetTensorDesc().GetDataType()).c_str(),
  84. TypeUtils::FormatToSerialString(data_format).c_str(), formats::ShapeToString(data_shape).c_str(),
  85. TypeUtils::DataTypeToSerialString(data_type).c_str());
  86. const uint8_t *src_data = const_weight_ptr->GetData().data();
  87. const formats::TransArgs trans_args{src_data, src_format, data_format, src_shape, data_shape, src_data_type};
  88. formats::TransResult trans_result;
  89. GELOGD("Trans formats from %s to %s, shape %s to %s, data type %s",
  90. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  91. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  92. TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  93. if (src_data_type != data_type || data_shape.empty() || !formats::IsTransFormatSupport(trans_args)) {
  94. GELOGW("Transfer from format %s to %s, shape %s to %s, data type %s to %s is not supported",
  95. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  96. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  97. TypeUtils::DataTypeToSerialString(src_data_type).c_str(),
  98. TypeUtils::DataTypeToSerialString(data_type).c_str());
  99. return NOT_CHANGED;
  100. }
  101. if (!KernelUtils::CheckSizeForTransOp(const_weight_ptr, op_desc_ptr)) {
  102. GELOGI("CheckSize failed, input size is not equal to weight size");
  103. return NOT_CHANGED;
  104. }
  105. if (formats::TransFormat(trans_args, trans_result) != SUCCESS) {
  106. GELOGW("Failed to trans formats from %s to %s, shape %s to %s, data type %s",
  107. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(data_format).c_str(),
  108. formats::ShapeToString(src_shape).c_str(), formats::ShapeToString(data_shape).c_str(),
  109. TypeUtils::DataTypeToSerialString(src_data_type).c_str());
  110. return NOT_CHANGED;
  111. }
  112. GeTensorPtr output_ptr = MakeShared<GeTensor>(op_desc_ptr->GetOutputDesc(0));
  113. if (output_ptr == nullptr) {
  114. GELOGE(ge::PARAM_INVALID, "Make shared failed");
  115. return ge::PARAM_INVALID;
  116. }
  117. if (output_ptr->SetData(trans_result.data.get(), trans_result.length) != GRAPH_SUCCESS) {
  118. GELOGW("Compute: SetData failed");
  119. }
  120. v_output.push_back(output_ptr);
  121. return SUCCESS;
  122. }
  123. REGISTER_KERNEL(TRANSDATA, TransdataKernel);
  124. } // namespace ge

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