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.6 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. namespace mindspore {
  19. namespace kernel {
  20. void GatherV2CPUKernel::InitKernel(const CNodePtr &kernel_node) {
  21. CheckParam(kernel_node);
  22. input_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  23. indices_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 1);
  24. output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0);
  25. axis_ = AnfAlgo::GetNodeAttr<int>(kernel_node, AXIS);
  26. if (axis_ < 0) {
  27. axis_ = axis_ + SizeToInt(input_shape_.size());
  28. }
  29. axis_ += 4 - input_shape_.size();
  30. CPUKernelUtils::ExpandDimsTo4(&input_shape_);
  31. CPUKernelUtils::ExpandDimsTo4(&output_shape_);
  32. }
  33. bool GatherV2CPUKernel::Launch(const std::vector<kernel::AddressPtr> &inputs,
  34. const std::vector<kernel::AddressPtr> & /*workspace*/,
  35. const std::vector<kernel::AddressPtr> &outputs) {
  36. auto output_addr = reinterpret_cast<float *>(outputs[0]->addr);
  37. auto buff_size = outputs[0]->size;
  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, &buff_size);
  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, &buff_size);
  53. }
  54. }
  55. } else if (axis_ == 1) {
  56. for (size_t i = 0; i < dim0; ++i) {
  57. CopyDataToOutput(inputs, i, 0, 0, &output_addr, &buff_size);
  58. }
  59. } else if (axis_ == 0) {
  60. CopyDataToOutput(inputs, 0, 0, 0, &output_addr, &buff_size);
  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, size_t *buff_size) {
  66. auto input_addr = reinterpret_cast<float *>(inputs[0]->addr);
  67. auto indices_addr = reinterpret_cast<int *>(inputs[1]->addr);
  68. size_t elem_num = inputs[1]->size / 4;
  69. size_t num = CPUKernelUtils::GetElementNumOnAxis(input_shape_, axis_);
  70. for (size_t i = 0; i < elem_num; ++i) {
  71. if (indices_addr[i] < 0) {
  72. MS_LOG(EXCEPTION) << "The indices value is less than 0.";
  73. }
  74. size_t index = IntToSize(indices_addr[i]);
  75. if (index >= input_shape_[IntToSize(axis_)]) {
  76. auto ret = memset_s(*output_addr, *buff_size, 0., num * sizeof(float));
  77. if (ret != EOK) {
  78. MS_LOG(EXCEPTION) << "memset failed.";
  79. }
  80. } else {
  81. size_t pos = 0;
  82. if (axis_ == 3) {
  83. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, dim2, index);
  84. } else if (axis_ == 2) {
  85. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, dim1, index, 0);
  86. } else if (axis_ == 1) {
  87. pos = CPUKernelUtils::CalcOffset(input_shape_, dim0, index, 0, 0);
  88. } else if (axis_ == 0) {
  89. pos = CPUKernelUtils::CalcOffset(input_shape_, index, 0, 0, 0);
  90. }
  91. auto ret = memcpy_s(*output_addr, *buff_size, input_addr + pos, num * sizeof(float));
  92. if (ret != EOK) {
  93. MS_LOG(EXCEPTION) << "memcpy failed.";
  94. }
  95. }
  96. *output_addr += num;
  97. *buff_size -= num * sizeof(float);
  98. }
  99. } // namespace kernel
  100. void GatherV2CPUKernel::CheckParam(const CNodePtr &kernel_node) {
  101. auto input_shape = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
  102. if (input_shape.size() > 4) {
  103. MS_LOG(EXCEPTION) << "Input dims is " << input_shape.size() << ", but GatherV2CPUKernel olny support 4d or lower.";
  104. }
  105. size_t input_num = AnfAlgo::GetInputTensorNum(kernel_node);
  106. if (input_num != 2) {
  107. MS_LOG(EXCEPTION) << "Argument number is " << input_num << ", but GatherV2CPUKernel needs 2.";
  108. }
  109. }
  110. } // namespace kernel
  111. } // namespace mindspore