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_cpu_kernel.cc 7.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "kernel/cpu/slice_cpu_kernel.h"
  17. #include "device/cpu/cpu_device_address.h"
  18. #include "ir/primitive.h"
  19. namespace mindspore {
  20. namespace kernel {
  21. void SliceCPUKernel::InitKernel(const CNodePtr &kernel_node) {
  22. CheckParam(kernel_node);
  23. input_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  24. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  25. begin_ = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, BEGIN);
  26. for (size_t i = 0; i < begin_.size(); i++) {
  27. if (begin_[i] < 0) {
  28. begin_[i] = begin_[i] + input_shape_[i];
  29. }
  30. }
  31. auto prim = AnfAlgo::GetCNodePrimitive(kernel_node);
  32. MS_EXCEPTION_IF_NULL(prim);
  33. auto strides = prim->GetAttr(STRIDES);
  34. if (strides != nullptr) {
  35. strides_ = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, STRIDES);
  36. end_ = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, END);
  37. if (strides_.size() != end_.size() || strides_.size() != input_shape_.size()) {
  38. MS_LOG(EXCEPTION) << "stride|end|input size must be equal";
  39. }
  40. for (size_t i = 0; i < strides_.size(); ++i) {
  41. if (strides_[i] < 0) {
  42. strides_[i] = (strides_[i] + input_shape_[i]) > 0 ? (strides_[i] + input_shape_[i]) : 0;
  43. }
  44. if (end_[i] < 0) {
  45. end_[i] = (end_[i] + input_shape_[i]) > 0 ? (end_[i] + input_shape_[i]) : 0;
  46. }
  47. }
  48. } else {
  49. auto sizes = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, SIZE);
  50. if (sizes.size() != input_shape_.size() || begin_.size() != input_shape_.size()) {
  51. MS_LOG(EXCEPTION) << "begin|size|input size must be equal";
  52. }
  53. for (size_t i = 0; i < sizes.size(); ++i) {
  54. if (sizes[i] < 0) {
  55. sizes[i] = (sizes[i] + input_shape_[i]) > 0 ? (sizes[i] + input_shape_[i]) : 0;
  56. }
  57. strides_.emplace_back(1);
  58. end_.emplace_back(begin_[i] + sizes[i]);
  59. }
  60. }
  61. ExpandAllMemberDims();
  62. CPUKernelUtils::GetElementNumEveryDim(input_shape_, &input_element_num_);
  63. CPUKernelUtils::GetElementNumEveryDim(output_shape_, &output_element_num_);
  64. }
  65. void SliceCPUKernel::ExpandAllMemberDims() {
  66. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  67. auto input_len = input_shape_.size();
  68. if (input_len < 4) {
  69. for (size_t i = 0; i < 4 - input_len; ++i) {
  70. input_shape_.insert(input_shape_.begin(), 1);
  71. begin_.insert(begin_.begin(), 0);
  72. strides_.insert(strides_.begin(), 1);
  73. end_.insert(end_.begin(), 1);
  74. }
  75. }
  76. }
  77. bool SliceCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  78. const std::vector<kernel::AddressPtr> & /*workspace*/,
  79. const std::vector<kernel::AddressPtr> &outputs) {
  80. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  81. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  82. bool can_copy_memory[3] = {CanCopyMemoryOnAxis(0), CanCopyMemoryOnAxis(1), CanCopyMemoryOnAxis(2)};
  83. size_t in_start_offset[3] = {begin_[0] * input_element_num_[0], begin_[1] * input_element_num_[1],
  84. begin_[2] * input_element_num_[2]};
  85. size_t in_step_size[3] = {strides_[0] * input_element_num_[0], strides_[1] * input_element_num_[1],
  86. strides_[2] * input_element_num_[2]};
  87. auto in_n_offset = in_start_offset[0];
  88. auto out_n_offset = 0;
  89. for (int i = begin_[0]; i < end_[0];
  90. i += strides_[0], in_n_offset += in_step_size[0], out_n_offset += output_element_num_[0]) {
  91. if (can_copy_memory[0]) {
  92. CopyDataToOutput(inputs, in_n_offset, outputs, out_n_offset, input_element_num_[0]);
  93. continue;
  94. }
  95. auto in_c_offset = in_start_offset[1];
  96. auto out_c_offset = 0;
  97. for (int j = begin_[1]; j < end_[1];
  98. j += strides_[1], in_c_offset += in_step_size[1], out_c_offset += output_element_num_[1]) {
  99. if (can_copy_memory[1]) {
  100. CopyDataToOutput(inputs, in_n_offset + in_c_offset, outputs, out_n_offset + out_c_offset,
  101. input_element_num_[1]);
  102. continue;
  103. }
  104. auto in_h_offset = in_start_offset[2];
  105. auto out_h_offset = 0;
  106. for (int k = begin_[2]; k < end_[2];
  107. k += strides_[2], in_h_offset += in_step_size[2], out_h_offset += output_element_num_[2]) {
  108. if (can_copy_memory[2]) {
  109. CopyDataToOutput(inputs, in_n_offset + in_c_offset + in_h_offset, outputs,
  110. out_n_offset + out_c_offset + out_h_offset, input_element_num_[2]);
  111. continue;
  112. }
  113. for (int m = begin_[3]; m < end_[3]; m += strides_[3]) {
  114. *output_addr++ = input_addr[in_n_offset + in_c_offset + in_h_offset + m];
  115. }
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. bool SliceCPUKernel::CanCopyMemoryOnAxis(size_t dim) const {
  122. for (size_t i = dim + 1; i < 4; ++i) {
  123. if (begin_[i] != 0 || end_[i] != SizeToInt(input_shape_[i]) || strides_[i] != 1) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. void SliceCPUKernel::CopyDataToOutput(const std::vector<kernel::AddressPtr> &inputs, size_t in_offset,
  130. const std::vector<kernel::AddressPtr> &outputs, size_t out_offset,
  131. size_t copy_num) const {
  132. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  133. auto in_buff_size = inputs[0]->size;
  134. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  135. auto out_buff_size = outputs[0]->size;
  136. if ((in_offset + copy_num) * sizeof(float) > in_buff_size) {
  137. MS_LOG(EXCEPTION) << "input memory out of bounds.";
  138. }
  139. if ((out_offset + copy_num) * sizeof(float) > out_buff_size) {
  140. MS_LOG(EXCEPTION) << "output memory out of bounds.";
  141. }
  142. auto ret = memcpy_s(output_addr + out_offset, out_buff_size - out_offset * sizeof(float), input_addr + in_offset,
  143. copy_num * sizeof(float));
  144. if (ret != EOK) {
  145. MS_LOG(EXCEPTION) << "memcpy failed. ret:" << ret;
  146. }
  147. }
  148. void SliceCPUKernel::CheckParam(const CNodePtr &kernel_node) const {
  149. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  150. if (input_num != 1) {
  151. MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but SliceCPUKernel needs 1 inputs.";
  152. }
  153. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  154. if (output_num != 1) {
  155. MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but SliceCPUKernel needs 1 output.";
  156. }
  157. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  158. if (input_shape.size() > 4) {
  159. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but SliceCPUKernel olny support 4d or lower.";
  160. }
  161. if (input_shape.size() == 0) {
  162. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", scalar is not supported.";
  163. }
  164. }
  165. } // namespace kernel
  166. } // namespace mindspore