concat.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/function/concat.h"
  15. #include "ultra_infer/utils/utils.h"
  16. #include <cstring>
  17. #include <limits>
  18. #include <set>
  19. #include <sstream>
  20. namespace ultra_infer {
  21. namespace function {
  22. std::vector<int64_t>
  23. ComputeAndCheckConcatOutputShape(const std::vector<FDTensor> &input, int axis) {
  24. const size_t n = input.size();
  25. auto out_dims = input[0].shape;
  26. size_t in_zero_dims_size = out_dims.size();
  27. for (size_t i = 1; i < n; ++i) {
  28. FDASSERT(input[i].shape.size() == out_dims.size(),
  29. "The shape of input[0] and input[%d] is expected to be equal. But "
  30. "received input[0]'s shape = %s, input[%d]'s shape = %s.",
  31. i, Str(out_dims).c_str(), i, Str(input[i].shape).c_str());
  32. for (size_t j = 0; j < in_zero_dims_size; j++) {
  33. if (j == axis) {
  34. out_dims[axis] += input[i].shape[axis];
  35. } else {
  36. FDASSERT(
  37. input[0].shape[j] == input[i].shape[j],
  38. "The %d-th dimension of input[0] and input[%d] is expected to be "
  39. "equal."
  40. "But received input[0]'s shape = %s, input[%d]'s shape = %s.",
  41. j, i, Str(input[0].shape).c_str(), i, Str(input[i].shape).c_str());
  42. }
  43. }
  44. }
  45. return out_dims;
  46. }
  47. template <typename T> struct ConcatFunctor {
  48. void operator()(const std::vector<FDTensor> &input, int axis,
  49. FDTensor *output) {
  50. size_t num = input.size();
  51. int64_t rows = 1;
  52. auto dim_0 = input[0].shape;
  53. for (int i = 0; i < axis; ++i) {
  54. rows *= dim_0[i];
  55. }
  56. int64_t out_rows = rows, out_cols = 0;
  57. std::vector<int64_t> input_cols(num);
  58. for (size_t i = 0; i < num; ++i) {
  59. int64_t t_cols = input[i].Numel() / rows;
  60. out_cols += t_cols;
  61. input_cols[i] = t_cols;
  62. }
  63. // computation
  64. T *output_data = reinterpret_cast<T *>(output->Data());
  65. int64_t col_idx = 0;
  66. for (size_t j = 0; j < num; ++j) {
  67. int64_t col_len = input_cols[j];
  68. const T *input_data = reinterpret_cast<const T *>(input[j].Data());
  69. for (int64_t k = 0; k < out_rows; ++k) {
  70. FDTensor::CopyBuffer(output_data + k * out_cols + col_idx,
  71. input_data + k * col_len, sizeof(T) * col_len,
  72. input[j].device, input[j].is_pinned_memory);
  73. }
  74. col_idx += col_len;
  75. }
  76. }
  77. };
  78. template <typename T>
  79. void ConcatKernel(const std::vector<FDTensor> &input, FDTensor *output,
  80. int axis) {
  81. auto output_shape = ComputeAndCheckConcatOutputShape(input, axis);
  82. FDTensor output_tmp;
  83. output_tmp.Resize(output_shape, TypeToDataType<T>::dtype, output->name,
  84. input[0].device);
  85. ConcatFunctor<T> functor;
  86. functor(input, axis, &output_tmp);
  87. *output = std::move(output_tmp);
  88. }
  89. void Concat(const std::vector<FDTensor> &x, FDTensor *out, int axis) {
  90. FDASSERT(x.size() > 0,
  91. "The number of FDTensor array should be larger than 0, but the size "
  92. "of input is %d",
  93. x.size());
  94. int64_t rank = x[0].shape.size();
  95. FDASSERT(axis >= -rank && axis < rank,
  96. "The axis is expected to be in range of [%d, %d), but got %d", -rank,
  97. rank, axis);
  98. if (axis < 0) {
  99. axis += rank;
  100. }
  101. FD_VISIT_ALL_TYPES(x[0].dtype, "Concat",
  102. ([&] { ConcatKernel<data_t>(x, out, axis); }));
  103. }
  104. } // namespace function
  105. } // namespace ultra_infer