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

5 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
5 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright 2019-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/ge_inner_error_codes.h"
  22. #include "common/op/ge_op_utils.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "host_kernels/kernel_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.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, DT_INT64};
  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. std::vector<int64_t> y_data_int64_t;
  82. // Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  83. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
  84. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  85. if (output_ptr == nullptr) {
  86. GELOGE(MEMALLOC_FAILED, "MakeShared failed.");
  87. return MEMALLOC_FAILED;
  88. }
  89. GeShape data0_shape = tensor->GetTensorDesc().GetShape();
  90. int64_t loop = 1;
  91. for (int i = 0; i < tidx; i++) {
  92. loop *= data0_shape.GetDim(i);
  93. }
  94. switch (data_type) {
  95. SET_OUTPUT(DT_INT32, int32_t)
  96. SET_OUTPUT(DT_FLOAT, float)
  97. SET_OUTPUT(DT_INT64, int64_t)
  98. default:
  99. break;
  100. }
  101. output_ptr->MutableTensorDesc().SetDataType(data_type);
  102. output_ptr->MutableTensorDesc().SetShape(GeShape({op_desc_ptr->GetOutputDesc(0).GetShape()}));
  103. v_output.push_back(output_ptr);
  104. GELOGI("ConcatV2Kernel success.");
  105. return SUCCESS;
  106. }
  107. Status ConcatV2Kernel::ConcatV2PreCompute(const std::vector<ConstGeTensorPtr> &input, int &tidx,
  108. ConstGeTensorPtr &tensor) {
  109. size_t input_size = input.size();
  110. // N >= 2 and N + 1 >= 3
  111. if (input_size < kConcatV2InputNum) {
  112. GELOGI("The number of input for ConcatV2 must not be less than %zu.", kConcatV2InputNum);
  113. return NOT_CHANGED;
  114. }
  115. bool has_empty_tensor = false;
  116. input_size--;
  117. for (size_t i = 0; i < input_size; i++) {
  118. if (input[i] == nullptr) {
  119. GELOGI("Input%zu must not be null.", i);
  120. return NOT_CHANGED;
  121. }
  122. if (input.at(i)->GetData().size() == 0) {
  123. GELOGW("input[%zu] is with no data.", i);
  124. has_empty_tensor = true;
  125. continue;
  126. }
  127. if (tensor == nullptr) {
  128. tensor = input.at(i); // get first valid tensor with data
  129. }
  130. }
  131. GE_CHECK_NOTNULL(tensor);
  132. DataType data_type = tensor->GetTensorDesc().GetDataType();
  133. for (size_t i = 1; i < input_size; i++) {
  134. if (data_type != input.at(i)->GetTensorDesc().GetDataType()) {
  135. GELOGI("Data type of N inputs for ConcatV2 not the same, check input %zu failed.", i);
  136. return NOT_CHANGED;
  137. }
  138. }
  139. // check if input data type is supported
  140. if (concatv2_supported_type.find(data_type) == concatv2_supported_type.end()) {
  141. GELOGI("ConcatV2 does not support this Data type: %s.", TypeUtils::DataTypeToSerialString(data_type).c_str());
  142. return NOT_CHANGED;
  143. }
  144. ConstGeTensorPtr tensor_axis = input.at(input_size);
  145. GE_CHECK_NOTNULL(tensor_axis);
  146. const int *axis = reinterpret_cast<const int *>(tensor_axis->GetData().data());
  147. GE_CHECK_NOTNULL(axis);
  148. tidx = axis[0]; // [-rank(values), rank(values))
  149. int rank = static_cast<int>(tensor->GetTensorDesc().GetShape().GetDimNum()); // rank
  150. if (tidx < 0) {
  151. tidx += rank;
  152. }
  153. // 1. tidx should in range [0,rank)
  154. // 2. empty tensor only support case: [n],[m],[]
  155. // case: [[],[]] ,[[],[]] ,[] or other case when rank >=2 is not supported
  156. if (tidx < 0 || tidx >= rank || (has_empty_tensor && rank > kSupportEmptyTensorRank)) {
  157. GELOGW("ConcatV2 info: tidx[%d]_rank[%d]_has_empty_tensor[bool:%d] cannot be supported, skip fold.", tidx, rank,
  158. has_empty_tensor);
  159. return NOT_CHANGED;
  160. }
  161. return SUCCESS;
  162. }
  163. REGISTER_KERNEL(CONCATV2, ConcatV2Kernel);
  164. } // namespace ge

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