transpose_op.h 3.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
X
xzl 已提交
2

L
Luo Tao 已提交
3 4 5
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
X
xzl 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
X
xzl 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
X
xzl 已提交
14 15 16

#pragma once

17
#include <vector>
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
X
xzl 已提交
20 21 22 23

namespace paddle {
namespace operators {

Q
QI JUN 已提交
24 25
template <typename DeviceContext, typename T>
inline void TransCompute(const int dim, const DeviceContext& dev_ctx,
26 27 28 29
                         const framework::Tensor& in, framework::Tensor* out,
                         const std::vector<int>& axis) {
  switch (dim) {
    case 1:
Q
QI JUN 已提交
30
      math::Transpose<DeviceContext, T, 1> trans1;
31 32 33
      trans1(dev_ctx, in, out, axis);
      break;
    case 2:
Q
QI JUN 已提交
34
      math::Transpose<DeviceContext, T, 2> trans2;
35 36 37
      trans2(dev_ctx, in, out, axis);
      break;
    case 3:
Q
QI JUN 已提交
38
      math::Transpose<DeviceContext, T, 3> trans3;
39 40 41
      trans3(dev_ctx, in, out, axis);
      break;
    case 4:
Q
QI JUN 已提交
42
      math::Transpose<DeviceContext, T, 4> trans4;
43 44 45
      trans4(dev_ctx, in, out, axis);
      break;
    case 5:
Q
QI JUN 已提交
46
      math::Transpose<DeviceContext, T, 5> trans5;
47 48 49
      trans5(dev_ctx, in, out, axis);
      break;
    case 6:
Q
QI JUN 已提交
50
      math::Transpose<DeviceContext, T, 6> trans6;
51 52 53 54
      trans6(dev_ctx, in, out, axis);
      break;
    default:
      PADDLE_THROW("Tensors with rank at most 6 are supported");
X
xzl 已提交
55 56 57
  }
}

Q
QI JUN 已提交
58
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
59
class TransposeKernel : public framework::OpKernel<T> {
X
xzl 已提交
60 61
 public:
  void Compute(const framework::ExecutionContext& context) const override {
X
xzl 已提交
62 63 64
    auto* x = context.Input<framework::Tensor>("X");
    auto* out = context.Output<framework::Tensor>("Out");
    out->mutable_data<T>(context.GetPlace());
X
xzl 已提交
65

66
    std::vector<int> axis = context.Attr<std::vector<int>>("axis");
X
xzl 已提交
67
    int ndims = axis.size();
Q
QI JUN 已提交
68 69
    auto& dev_ctx = context.template device_context<DeviceContext>();
    TransCompute<DeviceContext, T>(ndims, dev_ctx, *x, out, axis);
X
xzl 已提交
70 71 72
  }
};

Q
QI JUN 已提交
73
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
74
class TransposeGradKernel : public framework::OpKernel<T> {
X
xzl 已提交
75 76
 public:
  void Compute(const framework::ExecutionContext& context) const override {
X
xzl 已提交
77 78 79 80
    auto* out_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* x_grad =
        context.Output<framework::Tensor>(framework::GradVarName("X"));
81
    if (!x_grad) return;
82

83 84 85
    x_grad->mutable_data<T>(context.GetPlace());
    std::vector<int> axis = context.Attr<std::vector<int>>("axis");
    std::vector<int> reversed_axis(axis);
86

87 88
    for (size_t i = 0; i < axis.size(); i++) {
      reversed_axis[axis[i]] = i;
X
xzl 已提交
89
    }
90 91

    int ndims = axis.size();
Q
QI JUN 已提交
92 93 94
    auto& dev_ctx = context.template device_context<DeviceContext>();
    TransCompute<DeviceContext, T>(ndims, dev_ctx, *out_grad, x_grad,
                                   reversed_axis);
X
xzl 已提交
95 96 97 98 99
  }
};

}  // namespace operators
}  // namespace paddle