transpose_op.h 3.1 KB
Newer Older
X
xzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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/op_registry.h"
18
#include "paddle/operators/math/math_function.h"
X
xzl 已提交
19 20 21 22

namespace paddle {
namespace operators {

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

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

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

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

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

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

    int ndims = axis.size();
    auto& dev_ctx = context.device_context();
    TransCompute<Place, T>(ndims, dev_ctx, *out_grad, x_grad, reversed_axis);
X
xzl 已提交
93 94 95 96 97
  }
};

}  // namespace operators
}  // namespace paddle