transpose_op.h 4.1 KB
Newer Older
X
xzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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. */

#pragma once

#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

23
template <typename Place, typename T, int Rank>
24 25 26
void EigenTranspose(const framework::ExecutionContext& context,
                    const framework::Tensor& in, framework::Tensor& out,
                    std::vector<int> axis) {
27 28
  Eigen::array<int, Rank> permute;
  for (int i = 0; i < Rank; i++) {
X
xzl 已提交
29 30 31 32 33
    permute[i] = axis[i];
  }
  auto in_dim = in.dims();
  auto out_dim = out.dims();

34 35
  auto eigen_in = framework::EigenTensor<T, Rank>::From(in);
  auto eigen_out = framework::EigenTensor<T, Rank>::From(out);
X
xzl 已提交
36 37 38 39 40
  auto& dev = context.GetEigenDevice<Place>();
  eigen_out.device(dev) = eigen_in.shuffle(permute);
}

template <typename Place, typename T>
Y
Yu Yang 已提交
41
class TransposeKernel : public framework::OpKernel<T> {
X
xzl 已提交
42 43
 public:
  void Compute(const framework::ExecutionContext& context) const override {
X
xzl 已提交
44 45 46
    auto* x = context.Input<framework::Tensor>("X");
    auto* out = context.Output<framework::Tensor>("Out");
    out->mutable_data<T>(context.GetPlace());
X
xzl 已提交
47

48
    std::vector<int> axis = context.Attr<std::vector<int>>("axis");
X
xzl 已提交
49 50
    int ndims = axis.size();
    switch (ndims) {
51
      case 1:
X
xzl 已提交
52
        EigenTranspose<Place, T, 1>(context, *x, *out, axis);
53
        break;
X
xzl 已提交
54
      case 2:
X
xzl 已提交
55
        EigenTranspose<Place, T, 2>(context, *x, *out, axis);
X
xzl 已提交
56 57
        break;
      case 3:
X
xzl 已提交
58
        EigenTranspose<Place, T, 3>(context, *x, *out, axis);
X
xzl 已提交
59 60
        break;
      case 4:
X
xzl 已提交
61
        EigenTranspose<Place, T, 4>(context, *x, *out, axis);
X
xzl 已提交
62 63
        break;
      case 5:
X
xzl 已提交
64
        EigenTranspose<Place, T, 5>(context, *x, *out, axis);
X
xzl 已提交
65
        break;
66
      case 6:
X
xzl 已提交
67
        EigenTranspose<Place, T, 6>(context, *x, *out, axis);
X
xzl 已提交
68
        break;
69 70
      default:
        PADDLE_THROW("Tensors with rank at most 6 are supported");
X
xzl 已提交
71 72 73 74 75
    }
  }
};

template <typename Place, typename T>
Y
Yu Yang 已提交
76
class TransposeGradKernel : public framework::OpKernel<T> {
X
xzl 已提交
77 78
 public:
  void Compute(const framework::ExecutionContext& context) const override {
X
xzl 已提交
79 80 81 82 83 84
    auto* out_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* x_grad =
        context.Output<framework::Tensor>(framework::GradVarName("X"));
    if (x_grad) {
      x_grad->mutable_data<T>(context.GetPlace());
85 86 87 88 89 90 91 92 93 94 95 96

      std::vector<int> axis = context.Attr<std::vector<int>>("axis");
      std::vector<int> reversed_axis(axis);

      for (size_t i = 0; i < axis.size(); i++) {
        reversed_axis[axis[i]] = i;
      }

      int ndims = axis.size();

      switch (ndims) {
        case 1:
X
xzl 已提交
97
          EigenTranspose<Place, T, 1>(context, *out_grad, *x_grad,
98 99 100
                                      reversed_axis);
          break;
        case 2:
X
xzl 已提交
101
          EigenTranspose<Place, T, 2>(context, *out_grad, *x_grad,
102 103 104
                                      reversed_axis);
          break;
        case 3:
X
xzl 已提交
105
          EigenTranspose<Place, T, 3>(context, *out_grad, *x_grad,
106 107 108
                                      reversed_axis);
          break;
        case 4:
X
xzl 已提交
109
          EigenTranspose<Place, T, 4>(context, *out_grad, *x_grad,
110 111 112
                                      reversed_axis);
          break;
        case 5:
X
xzl 已提交
113
          EigenTranspose<Place, T, 5>(context, *out_grad, *x_grad,
114 115 116
                                      reversed_axis);
          break;
        case 6:
X
xzl 已提交
117
          EigenTranspose<Place, T, 6>(context, *out_grad, *x_grad,
118 119 120 121 122
                                      reversed_axis);
          break;
        default:
          PADDLE_THROW("Tensors with rank at most 6 are supported");
      }
X
xzl 已提交
123 124 125 126 127 128
    }
  }
};

}  // namespace operators
}  // namespace paddle