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 6.1 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
4 years ago
5 years ago
4 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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "framework/common/ge_inner_error_codes.h"
  19. #include "framework/common/op/ge_op_utils.h"
  20. #include "framework/common/types.h"
  21. #include "framework/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. Status ret = CheckInput(x_, begin, size);
  73. if (ret != SUCCESS) {
  74. return ret;
  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. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(x_->GetData().data()));
  89. int32_t *begin_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(begin->GetData().GetData()));
  90. int32_t *size_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(size->GetData().GetData()));
  91. GE_CHECK_NOTNULL(data);
  92. GE_CHECK_NOTNULL(begin_data);
  93. GE_CHECK_NOTNULL(size_data);
  94. size_t data_size = x_->GetData().size() / type_size;
  95. size_t begin_size = begin->GetData().size() / sizeof(int32_t);
  96. size_t size_size = size->GetData().size() / sizeof(int32_t);
  97. const ge::GeShape &x_shape = x_->GetTensorDesc().GetShape();
  98. size_t dim_size = x_shape.GetDimNum();
  99. if (dim_size != begin_size || dim_size != size_size) {
  100. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  101. return NOT_CHANGED;
  102. }
  103. std::vector<int64_t> input_dims;
  104. std::vector<int64_t> begin_vec;
  105. std::vector<int64_t> output_dims;
  106. std::vector<int64_t> stride_vec;
  107. for (size_t i = 0; i < dim_size; i++) {
  108. int32_t begin_i = begin_data[i];
  109. int32_t size_i = size_data[i];
  110. int64_t dim_i = x_shape.GetDim(i);
  111. if (size_i < 0) {
  112. GE_IF_BOOL_EXEC(((dim_i - begin_i) > INT32_MAX) || ((dim_i - begin_i) < INT32_MIN),
  113. GELOGE(PARAM_INVALID, " %ld and %d sub can result in overflow!.", dim_i, begin_i);
  114. return INTERNAL_ERROR);
  115. size_i = dim_i - begin_i;
  116. }
  117. input_dims.push_back(dim_i);
  118. begin_vec.push_back(begin_i);
  119. output_dims.push_back(size_i);
  120. stride_vec.push_back(1);
  121. }
  122. // construct tensorDesc
  123. ge::GeShape output_shape(output_dims);
  124. auto attr_output_tensor_desc = attr->GetOutputDesc(0);
  125. GeTensorDesc output_tensor_desc(attr_output_tensor_desc);
  126. output_tensor_desc.SetShape(output_shape);
  127. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  128. if (output_ptr == nullptr) {
  129. GELOGW("make_shared ge::GeTensor failed, node name %s.", attr->GetName().c_str());
  130. return NOT_CHANGED;
  131. }
  132. ret = CheckOutputDims(output_dims, attr);
  133. if (ret != SUCCESS) {
  134. return ret;
  135. }
  136. ret = OpUtils::SetOutputSliceData(data, static_cast<int64_t>(data_size), data_type, input_dims, begin_vec,
  137. output_dims, output_ptr.get(), stride_vec);
  138. if (ret != SUCCESS) {
  139. GELOGW("SetOutputSliceData failed.");
  140. return NOT_CHANGED;
  141. }
  142. v_output.push_back(output_ptr);
  143. GELOGI("SliceKernel success.");
  144. return SUCCESS;
  145. }
  146. Status SliceKernel::CheckInput(const ConstGeTensorPtr &x_, const ConstGeTensorPtr &begin,
  147. const ConstGeTensorPtr &size) {
  148. if (x_ == nullptr || begin == nullptr || size == nullptr) {
  149. GELOGW("input tensor is nullptr.");
  150. return NOT_CHANGED;
  151. }
  152. // check data type of begin and size
  153. if (begin->GetTensorDesc().GetDataType() != DT_INT32 || size->GetTensorDesc().GetDataType() != DT_INT32) {
  154. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  155. return NOT_CHANGED;
  156. }
  157. return SUCCESS;
  158. }
  159. Status SliceKernel::CheckOutputDims(const std::vector<int64_t> &output_dims, const OpDescPtr attr) {
  160. // check dim not all less than 0
  161. for (auto dim : output_dims) {
  162. if (dim > 0) {
  163. return SUCCESS;
  164. }
  165. }
  166. GELOGW("all output dim <=0, can't be processed. op_name : %s", attr->GetName().c_str());
  167. return NOT_CHANGED;
  168. }
  169. REGISTER_KERNEL(SLICE, SliceKernel);
  170. } // namespace ge

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