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_d_kernel.cc 6.6 kB

4 years ago
4 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_d_kernel.h"
  17. #include <memory>
  18. #include "common/fp16_t.h"
  19. #include "common/op/ge_op_utils.h"
  20. #include "common/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "host_kernels/kernel_utils.h"
  23. #include "graph/utils/type_utils.h"
  24. #include "inc/kernel_factory.h"
  25. namespace ge {
  26. namespace {
  27. const int64_t kDimMinusOne = -1;
  28. const int64_t kDimZero = 0;
  29. const int64_t KStrideLengthOne = 1;
  30. const size_t kSliceDInputSize = 1;
  31. const size_t kSliceDOutputSize = 1;
  32. const char *const kSliceDAttrBegin = "offsets";
  33. const char *const kSliceDAttrSize = "size";
  34. } // namespace
  35. Status SliceDKernel::SliceDCheck(const OpDescPtr &op_desc_ptr, const std::vector<ge::ConstGeTensorPtr> &input,
  36. std::vector<int64_t> &begin_list, std::vector<int64_t> &size_list) {
  37. // Check input size and output size
  38. if ((input.size() != kSliceDInputSize) || (op_desc_ptr->GetInputsSize() != kSliceDInputSize) ||
  39. (op_desc_ptr->GetOutputsSize() != kSliceDOutputSize)) {
  40. GELOGW("Unexpected SliceD node, node input size: %zu, node output size: %zu, node name: %s.", input.size(),
  41. op_desc_ptr->GetOutputsSize(), op_desc_ptr->GetName().c_str());
  42. return PARAM_INVALID;
  43. }
  44. ConstGeTensorPtr x_tensor = input.at(0); // index 0 is guaranteed to be valid by input size check.
  45. if (x_tensor == nullptr) {
  46. GELOGW("SliceDKernel input tensor is nullptr.");
  47. return PARAM_INVALID;
  48. }
  49. // Check data
  50. if (x_tensor->GetData().size() == 0) {
  51. GELOGW("SliceDKernel data size of input is 0, node name: %s.", op_desc_ptr->GetName().c_str());
  52. return PARAM_INVALID;
  53. }
  54. // Get attr;
  55. if (!AttrUtils::GetListInt(op_desc_ptr, kSliceDAttrBegin, begin_list)) {
  56. GELOGW("SliceDKernel get attr begin failed, node name: %s.", op_desc_ptr->GetName().c_str());
  57. return PARAM_INVALID;
  58. }
  59. if (!AttrUtils::GetListInt(op_desc_ptr, kSliceDAttrSize, size_list)) {
  60. GELOGW("SliceDKernel get attr size failed, node name: %s.", op_desc_ptr->GetName().c_str());
  61. return PARAM_INVALID;
  62. }
  63. // Check attr;
  64. vector<int64_t> x_dims = x_tensor->GetTensorDesc().GetShape().GetDims();
  65. size_t x_dim_size = x_dims.size();
  66. if (x_dim_size != begin_list.size() || x_dim_size != size_list.size()) {
  67. GELOGW("SliceDKernel rank of all shapes must be the same, input: %zu, begin: %zu, size: %zu, node name: %s.",
  68. x_dim_size, begin_list.size(), size_list.size(), op_desc_ptr->GetName().c_str());
  69. return PARAM_INVALID;
  70. }
  71. for (size_t i = 0; i < x_dim_size; i++) {
  72. int64_t x_dim_i = x_dims[i];
  73. int64_t begin_i = begin_list[i];
  74. int64_t size_i = size_list[i];
  75. if ((begin_i < kDimZero) || (begin_i > x_dim_i) || (size_i < kDimMinusOne) || (size_i > x_dim_i)) {
  76. GELOGW("SliceDKernel dim[%zu] of attr is out of range, node name: %s.", i, op_desc_ptr->GetName().c_str());
  77. return PARAM_INVALID;
  78. }
  79. }
  80. return SUCCESS;
  81. }
  82. Status SliceDKernel::Compute(const OpDescPtr op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  83. std::vector<GeTensorPtr> &v_output) {
  84. GELOGD("SliceDKernel in");
  85. if (op_desc_ptr == nullptr) {
  86. GELOGW("SliceDKernel input opdesc is nullptr.");
  87. return NOT_CHANGED;
  88. }
  89. vector<int64_t> begin_list;
  90. vector<int64_t> size_list;
  91. if (SliceDCheck(op_desc_ptr, input, begin_list, size_list) != SUCCESS) {
  92. GELOGW("SliceDKernel input is invalid, failed to fold node.");
  93. return NOT_CHANGED;
  94. }
  95. ConstGeTensorPtr x_tensor = input.at(0); // index 0 is guaranteed to be valid by input size check.
  96. vector<int64_t> x_dims = x_tensor->GetTensorDesc().GetShape().GetDims();
  97. vector<int64_t> stride_list;
  98. bool has_zero_dim = false;
  99. for (size_t i = 0; i < x_dims.size(); i++) {
  100. int64_t x_dim_i = x_dims[i];
  101. int64_t begin_i = begin_list[i];
  102. int64_t size_i = size_list[i];
  103. if (size_i == kDimMinusOne) {
  104. size_i = x_dim_i - begin_i;
  105. size_list[i] = size_i;
  106. } else if (begin_i + size_i > x_dim_i) {
  107. GELOGW("SliceDKernel dim[%zu] of attr size is out of range, node name: %s.", i, op_desc_ptr->GetName().c_str());
  108. return NOT_CHANGED;
  109. }
  110. stride_list.push_back(KStrideLengthOne);
  111. // 0 appears in dims of input tensor or size tensor
  112. if (size_i == kDimZero || x_dim_i == kDimZero) {
  113. has_zero_dim = true;
  114. }
  115. }
  116. auto x_data_type = x_tensor->GetTensorDesc().GetDataType();
  117. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
  118. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  119. if (output_ptr == nullptr) {
  120. GELOGW("Failed to fold node %s, out of memory", op_desc_ptr->GetName().c_str());
  121. return NOT_CHANGED;
  122. }
  123. output_ptr->MutableTensorDesc().SetShape(GeShape(size_list));
  124. output_ptr->MutableTensorDesc().SetDataType(x_data_type);
  125. if (has_zero_dim) {
  126. v_output.emplace_back(output_ptr);
  127. GELOGI("SliceD folding kernel success, and output tensor has no data.");
  128. return SUCCESS;
  129. }
  130. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(x_tensor->GetData().data()));
  131. int64_t x_data_size = x_tensor->GetTensorDesc().GetShape().GetShapeSize();
  132. Status ret = CheckOutputDims(size_list, op_desc_ptr);
  133. if (ret != SUCCESS) {
  134. return ret;
  135. }
  136. ret = OpUtils::SetOutputSliceData(data, x_data_size, x_data_type, x_dims, begin_list, size_list,
  137. output_ptr.get(), stride_list);
  138. if (ret != SUCCESS) {
  139. GELOGW("Set output data of SliceD failed.");
  140. return NOT_CHANGED;
  141. }
  142. v_output.emplace_back(output_ptr);
  143. GELOGI("SliceD folding kernel success.");
  144. return SUCCESS;
  145. }
  146. Status SliceDKernel::CheckOutputDims(const std::vector<int64_t> &output_dims, const OpDescPtr attr) {
  147. // check dim not all less than 0
  148. for (auto dim : output_dims) {
  149. if (dim > 0) {
  150. return SUCCESS;
  151. }
  152. }
  153. GELOGW("all output dim <=0, can't be processed. op_name : %s", attr->GetName().c_str());
  154. return NOT_CHANGED;
  155. }
  156. REGISTER_KERNEL(SLICED, SliceDKernel);
  157. } // namespace ge

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