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