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.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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "common/ge_inner_error_codes.h"
  18. #include "common/op/ge_op_utils.h"
  19. #include "common/types.h"
  20. #include "common/util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/utils/type_utils.h"
  23. #include "host_kernels/kernel_utils.h"
  24. #include "inc/kernel_factory.h"
  25. namespace ge {
  26. namespace {
  27. const size_t kSliceInputSize = 3;
  28. const size_t kSliceInputIndexX = 0;
  29. const size_t kSliceInputIndexBegin = 1;
  30. const size_t kSliceInputIndexSize = 2;
  31. } // namespace
  32. Status SliceKernel::Compute(const OpDescPtr attr, const std::vector<ConstGeTensorPtr> &input,
  33. vector<GeTensorPtr> &v_output) {
  34. GELOGI("SliceKernel in.");
  35. if (attr == nullptr) {
  36. GELOGW("Input opdescptr is nullptr.");
  37. return NOT_CHANGED;
  38. }
  39. // check input size
  40. if (input.size() != kSliceInputSize) {
  41. GELOGW("The number of input for slice must be %zu.", kSliceInputSize);
  42. return NOT_CHANGED;
  43. }
  44. ConstGeTensorPtr x_ = input[kSliceInputIndexX];
  45. ConstGeTensorPtr begin = input[kSliceInputIndexBegin];
  46. ConstGeTensorPtr size = input[kSliceInputIndexSize];
  47. if (x_ == nullptr || begin == nullptr || size == nullptr) {
  48. GELOGW("input tensor is nullptr.");
  49. return NOT_CHANGED;
  50. }
  51. // data type in input_x
  52. auto data_type = x_->GetTensorDesc().GetDataType();
  53. // check data type of begin and size
  54. if (begin->GetTensorDesc().GetDataType() != DT_INT32 || size->GetTensorDesc().GetDataType() != DT_INT32) {
  55. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  56. return NOT_CHANGED;
  57. }
  58. void *data = reinterpret_cast<void *>(const_cast<uint8_t *>(x_->GetData().data()));
  59. int32_t *begin_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(begin->GetData().GetData()));
  60. int32_t *size_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(size->GetData().GetData()));
  61. GE_CHECK_NOTNULL(data);
  62. GE_CHECK_NOTNULL(begin_data);
  63. GE_CHECK_NOTNULL(size_data);
  64. size_t data_size = x_->GetData().size() / sizeof(int32_t);
  65. size_t begin_size = begin->GetData().size() / sizeof(int32_t);
  66. size_t size_size = size->GetData().size() / sizeof(int32_t);
  67. const ge::GeShape &x_shape = x_->GetTensorDesc().GetShape();
  68. size_t dim_size = x_shape.GetDimNum();
  69. if (dim_size != begin_size || dim_size != size_size) {
  70. GELOGW("Data type of begin and size for slice are not DT_INT32.");
  71. return NOT_CHANGED;
  72. }
  73. std::vector<int64_t> input_dims;
  74. std::vector<int64_t> begin_vec;
  75. std::vector<int64_t> output_dims;
  76. std::vector<int64_t> stride_vec;
  77. for (size_t i = 0; i < dim_size; i++) {
  78. int32_t begin_i = begin_data[i];
  79. int32_t size_i = size_data[i];
  80. int64_t dim_i = x_shape.GetDim(i);
  81. if (size_i < 0) {
  82. GE_IF_BOOL_EXEC(((dim_i - begin_i) > INT32_MAX) || ((dim_i - begin_i) < INT32_MIN),
  83. GELOGE(PARAM_INVALID, " %ld and %d sub can result in overflow!.", dim_i, begin_i);
  84. return INTERNAL_ERROR);
  85. size_i = dim_i - begin_i;
  86. }
  87. input_dims.push_back(dim_i);
  88. begin_vec.push_back(begin_i);
  89. output_dims.push_back(size_i);
  90. stride_vec.push_back(1);
  91. }
  92. // construct tensorDesc
  93. ge::GeShape output_shape(output_dims);
  94. auto attr_output_tensor_desc = attr->GetOutputDesc(0);
  95. GeTensorDesc output_tensor_desc(attr_output_tensor_desc);
  96. output_tensor_desc.SetShape(output_shape);
  97. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  98. if (output_ptr == nullptr) {
  99. GELOGW("make_shared ge::GeTensor failed, node name %s.", attr->GetName().c_str());
  100. return NOT_CHANGED;
  101. }
  102. Status ret = CheckOutputDims(output_dims, attr);
  103. if (ret != SUCCESS) {
  104. return ret;
  105. }
  106. ret = OpUtils::SetOutputSliceData(data, static_cast<int64_t>(data_size), data_type, input_dims, begin_vec,
  107. output_dims, output_ptr.get(), stride_vec);
  108. if (ret != SUCCESS) {
  109. GELOGW("SetOutputSliceData failed.");
  110. return NOT_CHANGED;
  111. }
  112. v_output.push_back(output_ptr);
  113. GELOGI("SliceKernel success.");
  114. return SUCCESS;
  115. }
  116. Status SliceKernel::CheckOutputDims(const std::vector<int64_t> &output_dims, const OpDescPtr attr) {
  117. // check dim not all less than 0
  118. for (auto dim : output_dims) {
  119. if (dim > 0) {
  120. return SUCCESS;
  121. }
  122. }
  123. GELOGW("all output dim <=0, can't be processed. op_name : %s", attr->GetName().c_str());
  124. return NOT_CHANGED;
  125. }
  126. REGISTER_KERNEL(SLICE, SliceKernel);
  127. } // namespace ge

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