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.

concat_v2_kernel.cc 6.6 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
5 years ago
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
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/concat_v2_kernel.h"
  17. #include <memory>
  18. #include <set>
  19. #include "common/debug/log.h"
  20. #include "common/fp16_t.h"
  21. #include "common/op/ge_op_utils.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "host_kernels/kernel_utils.h"
  24. #include "graph/utils/type_utils.h"
  25. #include "inc/kernel_factory.h"
  26. #include "framework/common/types.h"
  27. namespace ge {
  28. namespace {
  29. const size_t kConcatV2InputNum = 3;
  30. const int kSupportEmptyTensorRank = 1;
  31. const std::set<DataType> concatv2_supported_type = {DT_INT32, DT_FLOAT};
  32. template <typename T>
  33. void GetOutputData(std::vector<T> &y_data, int64_t loop, size_t &input_size,
  34. const std::vector<ConstGeTensorPtr> &input) {
  35. for (int64_t i = 0; i < loop; i++) {
  36. for (size_t k = 0; k < input_size; k++) {
  37. GeShape datak_shape = input.at(k)->GetTensorDesc().GetShape();
  38. auto buffer = input.at(k)->GetData();
  39. const T *datak = reinterpret_cast<const T *>(buffer.data());
  40. if (datak == nullptr || buffer.size() == 0) {
  41. GELOGW("input[%zu] is with no data", k);
  42. continue;
  43. }
  44. int64_t gapk = datak_shape.GetShapeSize() / loop; // [2,3] is 6/loop
  45. for (int64_t j = 0; j < gapk; j++) {
  46. y_data.push_back(datak[j + gapk * i]);
  47. }
  48. }
  49. }
  50. }
  51. #define SET_OUTPUT(DTYPE, TYPE) \
  52. case DTYPE: \
  53. GetOutputData(y_data_##TYPE, loop, input_size, input); \
  54. (void)output_ptr->SetData(reinterpret_cast<uint8_t *>(y_data_##TYPE.data()), y_data_##TYPE.size() * length); \
  55. break;
  56. } // namespace
  57. Status ConcatV2Kernel::Compute(const ge::OpDescPtr op_desc_ptr, const vector<ge::ConstGeTensorPtr> &input,
  58. vector<ge::GeTensorPtr> &v_output) {
  59. GELOGI("ConcatV2Kernel in.");
  60. if (op_desc_ptr == nullptr) {
  61. GELOGE(PARAM_INVALID, "input opdesc is nullptr.");
  62. return PARAM_INVALID;
  63. }
  64. int tidx = -1;
  65. ConstGeTensorPtr tensor = nullptr;
  66. Status ret = ConcatV2PreCompute(input, tidx, tensor);
  67. if (ret != SUCCESS) {
  68. return ret;
  69. }
  70. size_t input_size = input.size(); // N + 1
  71. input_size--; // N
  72. GE_CHECK_NOTNULL(tensor);
  73. DataType data_type = tensor->GetTensorDesc().GetDataType();
  74. uint32_t length = 0;
  75. if (!TypeUtils::GetDataTypeLength(data_type, length)) {
  76. GELOGW("Can't GetDataTypeLength of data_type: %s", TypeUtils::DataTypeToSerialString(data_type).c_str());
  77. return NOT_CHANGED;
  78. }
  79. std::vector<int32_t> y_data_int32_t;
  80. std::vector<float> y_data_float;
  81. // Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  82. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
  83. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  84. if (output_ptr == nullptr) {
  85. GELOGE(MEMALLOC_FAILED, "MakeShared failed.");
  86. return MEMALLOC_FAILED;
  87. }
  88. GeShape data0_shape = tensor->GetTensorDesc().GetShape();
  89. int64_t loop = 1;
  90. for (int i = 0; i < tidx; i++) {
  91. loop *= data0_shape.GetDim(i);
  92. }
  93. switch (data_type) {
  94. SET_OUTPUT(DT_INT32, int32_t)
  95. SET_OUTPUT(DT_FLOAT, float)
  96. default:
  97. break;
  98. }
  99. output_ptr->MutableTensorDesc().SetDataType(data_type);
  100. output_ptr->MutableTensorDesc().SetShape(GeShape({op_desc_ptr->GetOutputDesc(0).GetShape()}));
  101. v_output.push_back(output_ptr);
  102. GELOGI("ConcatV2Kernel success.");
  103. return SUCCESS;
  104. }
  105. Status ConcatV2Kernel::ConcatV2PreCompute(const std::vector<ConstGeTensorPtr> &input,
  106. int &tidx,
  107. ConstGeTensorPtr &tensor) {
  108. size_t input_size = input.size();
  109. // N >= 2 and N + 1 >= 3
  110. if (input_size < kConcatV2InputNum) {
  111. GELOGI("The number of input for ConcatV2 must not be less than %zu.", kConcatV2InputNum);
  112. return NOT_CHANGED;
  113. }
  114. bool has_empty_tensor = false;
  115. input_size--;
  116. for (size_t i = 0; i < input_size; i++) {
  117. if (input[i] == nullptr) {
  118. GELOGI("Input%zu must not be null.", i);
  119. return NOT_CHANGED;
  120. }
  121. if (input.at(i)->GetData().size() == 0) {
  122. GELOGW("input[%zu] is with no data.", i);
  123. has_empty_tensor = true;
  124. continue;
  125. }
  126. if (tensor == nullptr) {
  127. tensor = input.at(i); // get first valid tensor with data
  128. }
  129. }
  130. GE_CHECK_NOTNULL(tensor);
  131. DataType data_type = tensor->GetTensorDesc().GetDataType();
  132. for (size_t i = 1; i < input_size; i++) {
  133. if (data_type != input.at(i)->GetTensorDesc().GetDataType()) {
  134. GELOGI("Data type of N inputs for ConcatV2 not the same, check input %zu failed.", i);
  135. return NOT_CHANGED;
  136. }
  137. }
  138. // check if input data type is supported
  139. if (concatv2_supported_type.find(data_type) == concatv2_supported_type.end()) {
  140. GELOGI("ConcatV2 does not support this Data type: %s.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  141. return NOT_CHANGED;
  142. }
  143. ConstGeTensorPtr tensor_axis = input.at(input_size);
  144. GE_CHECK_NOTNULL(tensor_axis);
  145. const int *axis = reinterpret_cast<const int *>(tensor_axis->GetData().data());
  146. GE_CHECK_NOTNULL(axis);
  147. tidx = axis[0]; // [-rank(values), rank(values))
  148. int rank = static_cast<int>(tensor->GetTensorDesc().GetShape().GetDimNum()); // rank
  149. if (tidx < 0) {
  150. tidx += rank;
  151. }
  152. // 1. tidx should in range [0,rank)
  153. // 2. empty tensor only support case: [n],[m],[]
  154. // case: [[],[]] ,[[],[]] ,[] or other case when rank >=2 is not supported
  155. if (tidx < 0 || tidx >= rank || (has_empty_tensor && rank > kSupportEmptyTensorRank)) {
  156. GELOGW("ConcatV2 info: tidx[%d]_rank[%d]_has_empty_tensor[bool:%d] cannot be supported, skip fold.",
  157. tidx, rank, has_empty_tensor);
  158. return NOT_CHANGED;
  159. }
  160. return SUCCESS;
  161. }
  162. REGISTER_KERNEL(CONCATV2, ConcatV2Kernel);
  163. } // namespace ge

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