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