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 3.4 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. begin_ = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, BEGIN);
  24. size_ = AnfAlgo::GetNodeAttr<std::vector<int>>(kernel_node, SIZE);
  25. input_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  26. if (input_shape_.size() < 4) {
  27. for (size_t i = 0; i < 4 - input_shape_.size(); ++i) {
  28. input_shape_.insert(input_shape_.begin(), 1);
  29. begin_.insert(begin_.begin(), 0);
  30. size_.insert(size_.begin(), 1);
  31. }
  32. }
  33. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  34. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  35. for (size_t i = 0; i < begin_.size(); i++) {
  36. if (begin_[i] < 0) {
  37. begin_[i] = begin_[i] + input_shape_[i];
  38. }
  39. }
  40. for (size_t i = 0; i < size_.size(); i++) {
  41. if (size_[i] < 0) {
  42. size_[i] = (size_[i] + input_shape_[i]) > 0 ? (size_[i] + input_shape_[i]) : 0;
  43. }
  44. }
  45. }
  46. bool SliceCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  47. const std::vector<kernel::AddressPtr> & /*workspace*/,
  48. const std::vector<kernel::AddressPtr> &outputs) {
  49. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  50. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  51. for (int i = begin_[0]; i < begin_[0] + size_[0]; ++i) {
  52. for (int j = begin_[1]; j < begin_[1] + size_[1]; ++j) {
  53. for (int k = begin_[2]; k < begin_[2] + size_[2]; ++k) {
  54. for (int m = begin_[3]; m < begin_[3] + size_[3]; ++m) {
  55. auto offset = CPUKernelUtils::CalcOffset(input_shape_, i, j, k, m);
  56. *output_addr++ = input_addr[offset];
  57. }
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. void SliceCPUKernel::CheckParam(const CNodePtr &kernel_node) {
  64. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  65. if (input_num != 1) {
  66. MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but SliceCPUKernel needs 1 inputs.";
  67. }
  68. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  69. if (output_num != 1) {
  70. MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but SliceCPUKernel needs 1 output.";
  71. }
  72. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  73. if (input_shape.size() > 4) {
  74. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but SliceCPUKernel olny support 4d or lower.";
  75. }
  76. if (input_shape.size() == 0) {
  77. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", scalar is not supported.";
  78. }
  79. }
  80. } // namespace kernel
  81. } // namespace mindspore