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.

slice_kernel.cc 5.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/slice_kernel.h"
  17. #include <set>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "common/op/ge_op_utils.h"
  20. #include "common/types.h"
  21. #include "common/util.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "graph/utils/type_utils.h"
  24. #include "host_kernels/kernel_utils.h"
  25. #include "inc/kernel_factory.h"
  26. namespace ge {
  27. namespace {
  28. const size_t kSliceInputSize = 3;
  29. const size_t kSliceInputIndexX = 0;
  30. const size_t kSliceInputIndexBegin = 1;
  31. const size_t kSliceInputIndexSize = 2;
  32. const std::set<ge::DataType> kSupportedDataTypeToLength = {
  33. DT_BOOL,
  34. DT_INT64,
  35. DT_UINT64,
  36. DT_FLOAT,
  37. DT_INT32,
  38. DT_UINT32,
  39. DT_INT8,
  40. DT_UINT8,
  41. DT_INT16,
  42. DT_UINT16,
  43. DT_FLOAT16,
  44. DT_DOUBLE,
  45. DT_DUAL,
  46. DT_DUAL_SUB_INT8,
  47. DT_DUAL_SUB_UINT8,
  48. DT_COMPLEX64,
  49. DT_COMPLEX128,
  50. DT_QINT8,
  51. DT_QINT16,
  52. DT_QINT32,
  53. DT_QUINT8,
  54. DT_QUINT16,
  55. };
  56. } // namespace
  57. Status SliceKernel::Compute(const OpDescPtr attr, const std::vector<ConstGeTensorPtr> &input,
  58. vector<GeTensorPtr> &v_output) {
  59. GELOGI("SliceKernel in.");
  60. if (attr == nullptr) {
  61. GELOGW("Input opdescptr is nullptr.");
  62. return NOT_CHANGED;
  63. }
  64. // check input size
  65. if (input.size() != kSliceInputSize) {
  66. GELOGW("The number of input for slice must be %zu.", kSliceInputSize);
  67. return NOT_CHANGED;
  68. }
  69. ConstGeTensorPtr x_ = input[kSliceInputIndexX];
  70. ConstGeTensorPtr begin = input[kSliceInputIndexBegin];
  71. ConstGeTensorPtr size = input[kSliceInputIndexSize];
  72. if (x_ == nullptr || begin == nullptr || size == nullptr) {
  73. GELOGW("input tensor is nullptr.");
  74. return NOT_CHANGED;
  75. }
  76. // data type in input_x
  77. auto data_type = x_->GetTensorDesc().GetDataType();
  78. // check supported
  79. if (kSupportedDataTypeToLength.count(data_type) == 0) {
  80. GELOGW("input_x data_type is [%s], does not supported!", TypeUtils::DataTypeToSerialString(data_type).c_str());
  81. return NOT_CHANGED;
  82. }
  83. uint32_t type_size = 0;
  84. bool is_success = TypeUtils::GetDataTypeLength(data_type, type_size);
  85. if (!is_success) {
  86. return NOT_CHANGED;
  87. }
  88. // check data type of begin and size
  89. if (begin->GetTensorDesc().GetDataType() != DT_INT32 || size->GetTensorDesc().GetDataType() != DT_INT32) {
  90. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  91. return NOT_CHANGED;
  92. }
  93. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(x_->GetData().data()));
  94. int32_t *begin_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(begin->GetData().GetData()));
  95. int32_t *size_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(size->GetData().GetData()));
  96. GE_CHECK_NOTNULL(data);
  97. GE_CHECK_NOTNULL(begin_data);
  98. GE_CHECK_NOTNULL(size_data);
  99. size_t data_size = x_->GetData().size() / type_size;
  100. size_t begin_size = begin->GetData().size() / sizeof(int32_t);
  101. size_t size_size = size->GetData().size() / sizeof(int32_t);
  102. const ge::GeShape &x_shape = x_->GetTensorDesc().GetShape();
  103. size_t dim_size = x_shape.GetDimNum();
  104. if (dim_size != begin_size || dim_size != size_size) {
  105. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  106. return NOT_CHANGED;
  107. }
  108. std::vector<int64_t> input_dims;
  109. std::vector<int64_t> begin_vec;
  110. std::vector<int64_t> output_dims;
  111. std::vector<int64_t> stride_vec;
  112. for (size_t i = 0; i < dim_size; i++) {
  113. int32_t begin_i = begin_data[i];
  114. int32_t size_i = size_data[i];
  115. int64_t dim_i = x_shape.GetDim(i);
  116. if (size_i < 0) {
  117. GE_IF_BOOL_EXEC(((dim_i - begin_i) > INT32_MAX) || ((dim_i - begin_i) < INT32_MIN),
  118. GELOGE(PARAM_INVALID, " %ld and %d sub can result in overflow!.", dim_i, begin_i);
  119. return INTERNAL_ERROR);
  120. size_i = dim_i - begin_i;
  121. }
  122. input_dims.push_back(dim_i);
  123. begin_vec.push_back(begin_i);
  124. output_dims.push_back(size_i);
  125. stride_vec.push_back(1);
  126. }
  127. // construct tensorDesc
  128. ge::GeShape output_shape(output_dims);
  129. auto attr_output_tensor_desc = attr->GetOutputDesc(0);
  130. GeTensorDesc output_tensor_desc(attr_output_tensor_desc);
  131. output_tensor_desc.SetShape(output_shape);
  132. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  133. if (output_ptr == nullptr) {
  134. GELOGW("make_shared ge::GeTensor failed, node name %s.", attr->GetName().c_str());
  135. return NOT_CHANGED;
  136. }
  137. Status ret = CheckOutputDims(output_dims, attr);
  138. if (ret != SUCCESS) {
  139. return ret;
  140. }
  141. ret = OpUtils::SetOutputSliceData(data, static_cast<int64_t>(data_size), data_type, input_dims, begin_vec,
  142. output_dims, output_ptr.get(), stride_vec);
  143. if (ret != SUCCESS) {
  144. GELOGW("SetOutputSliceData failed.");
  145. return NOT_CHANGED;
  146. }
  147. v_output.push_back(output_ptr);
  148. GELOGI("SliceKernel success.");
  149. return SUCCESS;
  150. }
  151. Status SliceKernel::CheckOutputDims(const std::vector<int64_t> &output_dims, const OpDescPtr attr) {
  152. // check dim not all less than 0
  153. for (auto dim : output_dims) {
  154. if (dim > 0) {
  155. return SUCCESS;
  156. }
  157. }
  158. GELOGW("all output dim <=0, can't be processed. op_name : %s", attr->GetName().c_str());
  159. return NOT_CHANGED;
  160. }
  161. REGISTER_KERNEL(SLICE, SliceKernel);
  162. } // namespace ge

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