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.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. auto buff_size = outputs[0]->size;
  39. size_t dim0 = input_shape_[0];
  40. size_t dim1 = input_shape_[1];
  41. size_t dim2 = input_shape_[2];
  42. if (axis_ == 3) {
  43. for (size_t i = 0; i < dim0; ++i) {
  44. for (size_t j = 0; j < dim1; ++j) {
  45. for (size_t k = 0; k < dim2; ++k) {
  46. CopyDataToOutput(inputs, i, j, k, &output_addr, &buff_size);
  47. }
  48. }
  49. }
  50. } else if (axis_ == 2) {
  51. for (size_t i = 0; i < dim0; ++i) {
  52. for (size_t j = 0; j < dim1; ++j) {
  53. CopyDataToOutput(inputs, i, j, 0, &output_addr, &buff_size);
  54. }
  55. }
  56. } else if (axis_ == 1) {
  57. for (size_t i = 0; i < dim0; ++i) {
  58. CopyDataToOutput(inputs, i, 0, 0, &output_addr, &buff_size);
  59. }
  60. } else if (axis_ == 0) {
  61. CopyDataToOutput(inputs, 0, 0, 0, &output_addr, &buff_size);
  62. }
  63. return true;
  64. }
  65. void GatherV2CPUKernel::CopyDataToOutput(const std::vector<kernel::AddressPtr> &inputs, size_t dim0, size_t dim1,
  66. size_t dim2, float **output_addr, size_t *buff_size) {
  67. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  68. auto indices_addr = reinterpret_cast<int *>(inputs[1]->addr);
  69. for (size_t i = 0; i < output_shape_[axis_]; ++i) {
  70. size_t index = IntToSize(indices_addr[i]);
  71. size_t pos = 0;
  72. if (axis_ == 3) {
  73. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, dim2, index);
  74. } else if (axis_ == 2) {
  75. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, index, 0);
  76. } else if (axis_ == 1) {
  77. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, index, 0, 0);
  78. } else if (axis_ == 0) {
  79. pos = CPUKernelUtils::CalcOffset(input_shape_, index, 0, 0, 0);
  80. }
  81. size_t num = CPUKernelUtils::GetElementNumOnAxis(input_shape_, axis_);
  82. auto ret = memcpy_s(*output_addr, *buff_size, input_addr + pos, num * sizeof(float));
  83. if (ret != EOK) {
  84. MS_LOG(EXCEPTION) << "memcpy failed.";
  85. }
  86. *output_addr += num;
  87. *buff_size -= num * sizeof(float);
  88. }
  89. }
  90. void GatherV2CPUKernel::CheckParam(const CNodePtr &kernel_node) {
  91. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  92. if (input_shape.size() > 4) {
  93. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but GatherV2CPUKernel olny support 4d or lower.";
  94. }
  95. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  96. if (input_num != 2) {
  97. MS_LOG(EXCEPTION) << "Argument number is " << input_num << ", but GatherV2CPUKernel needs 2.";
  98. }
  99. }
  100. } // namespace kernel
  101. } // namespace mindspore