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.

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