pad.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/pad.h"
  15. #include <cstdlib>
  16. #include "ultra_infer/function/eigen.h"
  17. #include "ultra_infer/utils/utils.h"
  18. namespace ultra_infer {
  19. namespace function {
  20. template <typename T, int Rank> struct PadEigen {
  21. using Array = std::array<std::pair<int64_t, int64_t>, Rank>;
  22. using Array32Bit = std::array<std::pair<int, int>, Rank>;
  23. using InType = Eigen::TensorMap<
  24. Eigen::Tensor<const T, Rank, Eigen::RowMajor, Eigen::DenseIndex>>;
  25. using InType32BitIndex =
  26. Eigen::TensorMap<Eigen::Tensor<const T, Rank, Eigen::RowMajor, int>,
  27. Eigen::Aligned>;
  28. using OutType = Eigen::TensorMap<
  29. Eigen::Tensor<T, Rank, Eigen::RowMajor, Eigen::DenseIndex>>;
  30. using OutType32BitIndex =
  31. Eigen::TensorMap<Eigen::Tensor<T, Rank, Eigen::RowMajor, int>,
  32. Eigen::Aligned>;
  33. static void Eval(const Eigen::DefaultDevice &dev, OutType out,
  34. const InType &in, const Array &padding, const T value) {
  35. out.device(dev) = in.pad(padding, value);
  36. }
  37. static void Eval32(const Eigen::DefaultDevice &dev, OutType32BitIndex out,
  38. const InType32BitIndex &in, const Array32Bit &padding,
  39. const T value) {
  40. out.device(dev) = in.pad(padding, value);
  41. }
  42. };
  43. template <typename T, size_t D>
  44. void PadFunction(const std::vector<int> &pads, const FDTensor &src, T pad_value,
  45. FDTensor *out) {
  46. std::array<std::pair<int64_t, int64_t>, D> paddings;
  47. for (size_t i = 0; i < paddings.size(); ++i) {
  48. paddings[i].first = pads[i * 2];
  49. paddings[i].second = pads[i * 2 + 1];
  50. }
  51. auto src_tensor = EigenTensor<T, D>::From(src);
  52. auto out_tensor = EigenTensor<T, D>::From(*out);
  53. const auto &dev = *EigenDeviceWrapper::GetInstance()->GetDevice();
  54. PadEigen<T, D>::Eval(dev, out_tensor, src_tensor, paddings, pad_value);
  55. }
  56. template <typename T>
  57. void PaddingFunctor(int rank, const std::vector<int> &pads, T pad_value,
  58. const FDTensor &src, FDTensor *out) {
  59. switch (rank) {
  60. case 1:
  61. PadFunction<T, 1>(pads, src, pad_value, out);
  62. break;
  63. case 2:
  64. PadFunction<T, 2>(pads, src, pad_value, out);
  65. break;
  66. case 3:
  67. PadFunction<T, 3>(pads, src, pad_value, out);
  68. break;
  69. case 4:
  70. PadFunction<T, 4>(pads, src, pad_value, out);
  71. break;
  72. case 5:
  73. PadFunction<T, 5>(pads, src, pad_value, out);
  74. break;
  75. case 6:
  76. PadFunction<T, 6>(pads, src, pad_value, out);
  77. break;
  78. default:
  79. FDASSERT(
  80. false,
  81. "Pad only support tensors with no more than 6 dimensions currently.");
  82. }
  83. }
  84. template <typename T>
  85. void PadKernel(const FDTensor &x, const std::vector<int> &paddings,
  86. const T &pad_value, FDTensor *out) {
  87. std::vector<int64_t> new_shape(x.shape.size());
  88. for (size_t i = 0; i < x.shape.size(); ++i) {
  89. new_shape[i] = x.shape[i] + paddings[2 * i] + paddings[2 * i + 1];
  90. }
  91. out->Allocate(new_shape, x.dtype);
  92. PaddingFunctor<T>(x.shape.size(), paddings, pad_value, x, out);
  93. }
  94. void Pad(const FDTensor &x, FDTensor *out, const std::vector<int> &pads,
  95. float value) {
  96. FDASSERT(pads.size() == x.shape.size() * 2,
  97. "Size of pads:%zu must be 2 times of rank:%zu.", pads.size(),
  98. x.shape.size());
  99. FDTensor out_tmp;
  100. FD_VISIT_ALL_TYPES(x.dtype, "PadKernel",
  101. ([&] { PadKernel<data_t>(x, pads, value, &out_tmp); }));
  102. *out = std::move(out_tmp);
  103. }
  104. } // namespace function
  105. } // namespace ultra_infer