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.

gather_cpu_kernel.cc 4.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/gather_cpu_kernel.h"
  17. #include "device/cpu/cpu_device_address.h"
  18. #include "ir/primitive.h"
  19. namespace mindspore {
  20. namespace kernel {
  21. void GatherV2CPUKernel::InitKernel(const CNodePtr &kernel_node) {
  22. CheckParam(kernel_node);
  23. input_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  24. indices_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 1);
  25. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  26. axis_ = AnfAlgo::GetNodeAttr<int>(kernel_node, AXIS);
  27. if (axis_ < 0) {
  28. axis_ = axis_ + SizeToInt(input_shape_.size());
  29. }
  30. axis_ += 4 - input_shape_.size();
  31. CPUKernelUtils::ExpandDimsTo4(&input_shape_);
  32. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  33. }
  34. bool GatherV2CPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  35. const std::vector<kernel::AddressPtr> & /*workspace*/,
  36. const std::vector<kernel::AddressPtr> &outputs) {
  37. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  38. size_t dim0 = input_shape_[0];
  39. size_t dim1 = input_shape_[1];
  40. size_t dim2 = input_shape_[2];
  41. if (axis_ == 3) {
  42. for (size_t i = 0; i < dim0; ++i) {
  43. for (size_t j = 0; j < dim1; ++j) {
  44. for (size_t k = 0; k < dim2; ++k) {
  45. CopyDataToOutput(inputs, i, j, k, &output_addr);
  46. }
  47. }
  48. }
  49. } else if (axis_ == 2) {
  50. for (size_t i = 0; i < dim0; ++i) {
  51. for (size_t j = 0; j < dim1; ++j) {
  52. CopyDataToOutput(inputs, i, j, 0, &output_addr);
  53. }
  54. }
  55. } else if (axis_ == 1) {
  56. for (size_t i = 0; i < dim0; ++i) {
  57. CopyDataToOutput(inputs, i, 0, 0, &output_addr);
  58. }
  59. } else if (axis_ == 0) {
  60. CopyDataToOutput(inputs, 0, 0, 0, &output_addr);
  61. }
  62. return true;
  63. }
  64. void GatherV2CPUKernel::CopyDataToOutput(const std::vector<kernel::AddressPtr> &inputs, size_t dim0, size_t dim1,
  65. size_t dim2, float **output_addr) {
  66. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  67. auto indices_addr = reinterpret_cast<int *>(inputs[1]->addr);
  68. for (size_t i = 0; i < output_shape_[axis_]; ++i) {
  69. size_t index = IntToSize(indices_addr[i]);
  70. size_t pos = 0;
  71. if (axis_ == 3) {
  72. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, dim2, index);
  73. } else if (axis_ == 2) {
  74. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, index, 0);
  75. } else if (axis_ == 1) {
  76. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, index, 0, 0);
  77. } else if (axis_ == 0) {
  78. pos = CPUKernelUtils::CalcOffset(input_shape_, index, 0, 0, 0);
  79. }
  80. size_t num = CPUKernelUtils::GetElementNumOnAxis(input_shape_, axis_);
  81. auto ret = memcpy_s(*output_addr, num * sizeof(float), input_addr + pos, num * sizeof(float));
  82. if (ret != EOK) {
  83. MS_LOG(EXCEPTION) << "memcpy failed.";
  84. }
  85. *output_addr += num;
  86. }
  87. }
  88. void GatherV2CPUKernel::CheckParam(const CNodePtr &kernel_node) {
  89. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  90. if (input_shape.size() > 4) {
  91. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but GatherV2CPUKernel olny support 4d or lower.";
  92. }
  93. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  94. if (input_num != 2) {
  95. MS_LOG(EXCEPTION) << "Argument number is " << input_num << ", but GatherV2CPUKernel needs 2.";
  96. }
  97. }
  98. } // namespace kernel
  99. } // namespace mindspore