transpose_kernel.cc 2.8 KB
Newer Older
H
hong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/transpose_kernel.h"
16

H
hong 已提交
17
#include <vector>
18

H
hong 已提交
19 20 21 22 23 24 25 26 27 28 29 30
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"

namespace phi {

template <typename T, typename Context>
void TransposeKernel(const Context& ctx,
                     const DenseTensor& x,
                     const std::vector<int>& axis,
                     DenseTensor* out) {
31 32 33 34 35 36 37 38
  size_t x_rank = x.dims().size();
  std::vector<int> formated_axis = axis;
  for (size_t i = 0; i < axis.size(); i++) {
    if (axis[i] < 0) {
      formated_axis[i] = axis[i] + x_rank;
    }
  }

H
hong 已提交
39 40 41 42
  ctx.template Alloc<T>(out);
  if (out->numel() == 0) {
    return;
  }
43
  int rank = formated_axis.size();
H
hong 已提交
44
  switch (rank) {
45 46 47
    case 0:
      phi::Copy<Context>(ctx, x, ctx.GetPlace(), false, out);
      break;
H
hong 已提交
48 49
    case 1:
      funcs::Transpose<Context, T, 1> trans1;
50
      trans1(ctx, x, out, formated_axis);
H
hong 已提交
51 52 53
      break;
    case 2:
      funcs::Transpose<Context, T, 2> trans2;
54
      trans2(ctx, x, out, formated_axis);
H
hong 已提交
55 56 57
      break;
    case 3:
      funcs::Transpose<Context, T, 3> trans3;
58
      trans3(ctx, x, out, formated_axis);
H
hong 已提交
59 60 61
      break;
    case 4:
      funcs::Transpose<Context, T, 4> trans4;
62
      trans4(ctx, x, out, formated_axis);
H
hong 已提交
63 64 65
      break;
    case 5:
      funcs::Transpose<Context, T, 5> trans5;
66
      trans5(ctx, x, out, formated_axis);
H
hong 已提交
67 68 69
      break;
    case 6:
      funcs::Transpose<Context, T, 6> trans6;
70
      trans6(ctx, x, out, formated_axis);
H
hong 已提交
71 72 73 74
      break;
    default:
      // for rank >= 7 situation
      funcs::TransposeNormal<Context, T> trans_normal;
75
      trans_normal(ctx, x, out, formated_axis);
H
hong 已提交
76 77
  }
}
78

H
hong 已提交
79 80 81 82 83 84 85 86 87 88 89
}  // namespace phi

PD_REGISTER_KERNEL(transpose,
                   CPU,
                   ALL_LAYOUT,
                   phi::TransposeKernel,
                   bool,
                   float,
                   double,
                   int32_t,
                   int64_t,
90
                   phi::dtype::float16,
H
hong 已提交
91 92 93
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
                   phi::dtype::complex<double>) {}