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