pad_op.h 4.1 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* 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 {

using Tensor = framework::Tensor;

template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;

W
wanghaoshuang 已提交
29 30
template <typename Place, typename T, size_t D>
void PadFunction(const framework::ExecutionContext& context) {
W
wanghaoshuang 已提交
31
  auto pads = context.Attr<std::vector<int>>("paddings");
W
wanghaoshuang 已提交
32
  Eigen::array<std::pair<int, int>, D> paddings;
W
wanghaoshuang 已提交
33
  for (size_t i = 0; i < paddings.size(); ++i) {
W
wanghaoshuang 已提交
34 35
    paddings[i].first = pads[i * 2];
    paddings[i].second = pads[i * 2 + 1];
W
wanghaoshuang 已提交
36
  }
W
wanghaoshuang 已提交
37
  T pad_value = context.Attr<T>("pad_value");
W
wanghaoshuang 已提交
38

W
wanghaoshuang 已提交
39 40 41
  auto* x = context.Input<Tensor>("X");
  auto* out = context.Output<Tensor>("Out");
  out->mutable_data<T>(context.GetPlace());
W
wanghaoshuang 已提交
42

W
wanghaoshuang 已提交
43 44
  auto x_tensor = EigenTensor<T, D>::From(*x);
  auto out_tensor = EigenTensor<T, D>::From(*out);
W
wanghaoshuang 已提交
45
  auto place = context.GetEigenDevice<Place>();
W
wanghaoshuang 已提交
46
  out_tensor.device(place) = x_tensor.pad(paddings, pad_value);
W
wanghaoshuang 已提交
47 48
}

W
wanghaoshuang 已提交
49
template <typename Place, typename T>
Y
Yu Yang 已提交
50
class PadKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
51 52
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
53 54
    int rank = context.Input<Tensor>("X")->dims().size();
    switch (rank) {
W
wanghaoshuang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      case 1:
        PadFunction<Place, T, 1>(context);
        break;
      case 2:
        PadFunction<Place, T, 2>(context);
        break;
      case 3:
        PadFunction<Place, T, 3>(context);
        break;
      case 4:
        PadFunction<Place, T, 4>(context);
        break;
      case 5:
        PadFunction<Place, T, 5>(context);
        break;
      case 6:
        PadFunction<Place, T, 6>(context);
        break;
      default:
W
wanghaoshuang 已提交
74 75
        PADDLE_THROW(
            "PadOp only support tensors with no more than 6 dimensions.");
W
wanghaoshuang 已提交
76
    }
W
wanghaoshuang 已提交
77 78 79
  }
};

W
wanghaoshuang 已提交
80 81
template <typename Place, typename T, size_t D>
void PadGradFunction(const framework::ExecutionContext& context) {
W
wanghaoshuang 已提交
82
  auto pads = context.Attr<std::vector<int>>("paddings");
W
wanghaoshuang 已提交
83
  Eigen::array<std::pair<int, int>, D> paddings;
W
wanghaoshuang 已提交
84
  for (size_t i = 0; i < paddings.size(); ++i) {
W
wanghaoshuang 已提交
85 86
    paddings[i].first = -pads[i * 2];
    paddings[i].second = -pads[i * 2 + 1];
W
wanghaoshuang 已提交
87
  }
W
wanghaoshuang 已提交
88 89
  auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
  auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
W
wanghaoshuang 已提交
90 91 92 93 94 95 96
  if (d_x != nullptr) {
    d_x->mutable_data<T>(context.GetPlace());
    auto d_x_tensor = EigenTensor<T, D>::From(*d_x);
    auto d_out_tensor = EigenTensor<T, D>::From(*d_out);
    auto place = context.GetEigenDevice<Place>();
    d_x_tensor.device(place) = d_out_tensor.pad(paddings, 0);
  }
W
wanghaoshuang 已提交
97 98
}

W
wanghaoshuang 已提交
99
template <typename Place, typename T>
Y
Yu Yang 已提交
100
class PadGradKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
101
 public:
W
wanghaoshuang 已提交
102
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
103
    size_t rank =
W
wanghaoshuang 已提交
104
        context.Input<Tensor>(framework::GradVarName("Out"))->dims().size();
W
wanghaoshuang 已提交
105
    switch (rank) {
W
wanghaoshuang 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      case 1:
        PadGradFunction<Place, T, 1>(context);
        break;
      case 2:
        PadGradFunction<Place, T, 2>(context);
        break;
      case 3:
        PadGradFunction<Place, T, 3>(context);
        break;
      case 4:
        PadGradFunction<Place, T, 4>(context);
        break;
      case 5:
        PadGradFunction<Place, T, 5>(context);
        break;
      case 6:
        PadGradFunction<Place, T, 6>(context);
        break;
      default:
W
wanghaoshuang 已提交
125 126
        PADDLE_THROW(
            "PadOp only support tensors with no more than 6 dimensions.");
W
wanghaoshuang 已提交
127 128 129 130 131 132
    }
  }
};

}  // namespace operators
}  // namespace paddle