transpose_op.h 3.7 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 23
/* 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 {

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

  auto eigen_in = framework::EigenTensor<T, Dims>::From(in);
  auto eigen_out = framework::EigenTensor<T, Dims>::From(out);
  auto& dev = context.GetEigenDevice<Place>();
  eigen_out.device(dev) = eigen_in.shuffle(permute);
}

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

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

template <typename Place, typename T>
class TransposeGradKernel : public framework::OpKernel {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
78 79 80 81 82
    auto* output_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Output"));
    auto* input_grad =
        context.Output<framework::Tensor>(framework::GradVarName("Input"));
    input_grad->mutable_data<T>(context.GetPlace());
X
xzl 已提交
83

X
xzl 已提交
84
    auto axis_temp = context.Attr<std::vector<int>>("axis");
X
xzl 已提交
85 86 87 88 89 90 91 92 93
    std::vector<int> axis(axis_temp);

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

    int ndims = axis.size();

    switch (ndims) {
94 95
      case 1:
        break;
X
xzl 已提交
96
      case 2:
97
        EigenTranspose<Place, T, 2>(context, *output_grad, *input_grad, axis);
X
xzl 已提交
98 99
        break;
      case 3:
100
        EigenTranspose<Place, T, 3>(context, *output_grad, *input_grad, axis);
X
xzl 已提交
101 102
        break;
      case 4:
103
        EigenTranspose<Place, T, 4>(context, *output_grad, *input_grad, axis);
X
xzl 已提交
104 105
        break;
      case 5:
106
        EigenTranspose<Place, T, 5>(context, *output_grad, *input_grad, axis);
X
xzl 已提交
107
        break;
108 109
      case 6:
        EigenTranspose<Place, T, 6>(context, *output_grad, *input_grad, axis);
X
xzl 已提交
110
        break;
111 112
      default:
        PADDLE_THROW("Tensors with rank at most 6 are supported");
X
xzl 已提交
113 114 115 116 117 118
    }
  }
};

}  // namespace operators
}  // namespace paddle