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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  72. bool SliceCPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  73. const std::vector<kernel::AddressPtr> & /*workspace*/,
  74. const std::vector<kernel::AddressPtr> &outputs) {
  75. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  76. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  77. for (int i = begin_[0]; i < end_[0]; i += strides_[0]) {
  78. for (int j = begin_[1]; j < end_[1]; j += strides_[1]) {
  79. for (int k = begin_[2]; k < end_[2]; k += strides_[2]) {
  80. for (int m = begin_[3]; m < end_[3]; m += strides_[3]) {
  81. auto offset = CPUKernelUtils::CalcOffset(input_shape_, i, j, k, m);
  82. *output_addr++ = input_addr[offset];
  83. }
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. void SliceCPUKernel::CheckParam(const CNodePtr &kernel_node) {
  90. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  91. if (input_num != 1) {
  92. MS_LOG(EXCEPTION) << "Input number is " << input_num << ", but SliceCPUKernel needs 1 inputs.";
  93. }
  94. size_t output_num = AnfAlgo::GetOutputTensorNum(kernel_node);
  95. if (output_num != 1) {
  96. MS_LOG(EXCEPTION) << "Output number is " << output_num << ", but SliceCPUKernel needs 1 output.";
  97. }
  98. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  99. if (input_shape.size() > 4) {
  100. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but SliceCPUKernel olny support 4d or lower.";
  101. }
  102. if (input_shape.size() == 0) {
  103. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", scalar is not supported.";
  104. }
  105. }
  106. } // namespace kernel
  107. } // namespace mindspore