transpose_op.h 3.9 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 {

24 25
enum { kTransposeMKLDNNFP32 = 1, kTransposeMKLDNNINT8 = 2 };

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

Q
QI JUN 已提交
63
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
64
class TransposeKernel : public framework::OpKernel<T> {
X
xzl 已提交
65 66
 public:
  void Compute(const framework::ExecutionContext& context) const override {
67 68 69 70 71 72 73 74 75 76
    auto* x = context.InputVar("X");
    auto* out = context.OutputVar("Out");

    const framework::Tensor* x_tensor =
        GetLoDTensorOrSelectedRowsValueFromVar(*x);
    framework::Tensor* out_tensor =
        GetMutableLoDTensorOrSelectedRowsValueFromVar(out);

    out_tensor->mutable_data<T>(context.GetPlace());
    if (out_tensor->numel() == 0) {
77 78
      return;
    }
79

80
    std::vector<int> axis = context.Attr<std::vector<int>>("axis");
X
xzl 已提交
81
    int ndims = axis.size();
Q
QI JUN 已提交
82
    auto& dev_ctx = context.template device_context<DeviceContext>();
83
    TransCompute<DeviceContext, T>(ndims, dev_ctx, *x_tensor, out_tensor, axis);
X
xzl 已提交
84 85 86
  }
};

Q
QI JUN 已提交
87
template <typename DeviceContext, typename T>
Y
Yu Yang 已提交
88
class TransposeGradKernel : public framework::OpKernel<T> {
X
xzl 已提交
89 90
 public:
  void Compute(const framework::ExecutionContext& context) const override {
91 92 93 94 95 96 97 98 99 100 101 102 103
    auto* out_grad = context.InputVar(framework::GradVarName("Out"));
    auto* x_grad = context.OutputVar(framework::GradVarName("X"));

    if (!x_grad) {
      return;
    }
    const framework::Tensor* out_grad_tensor =
        GetLoDTensorOrSelectedRowsValueFromVar(*out_grad);
    framework::Tensor* x_grad_tensor =
        GetMutableLoDTensorOrSelectedRowsValueFromVar(x_grad);

    x_grad_tensor->mutable_data<T>(context.GetPlace());
    if (x_grad_tensor->numel() == 0) {
104 105 106
      return;
    }

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

110 111
    for (size_t i = 0; i < axis.size(); i++) {
      reversed_axis[axis[i]] = i;
X
xzl 已提交
112
    }
113 114

    int ndims = axis.size();
Q
QI JUN 已提交
115
    auto& dev_ctx = context.template device_context<DeviceContext>();
116 117
    TransCompute<DeviceContext, T>(ndims, dev_ctx, *out_grad_tensor,
                                   x_grad_tensor, reversed_axis);
X
xzl 已提交
118 119 120 121 122
  }
};

}  // namespace operators
}  // namespace paddle