pad_op.h 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
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
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
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. */
W
wanghaoshuang 已提交
14 15 16

#pragma once

17 18
#include <utility>
#include <vector>
Y
Yi Wang 已提交
19 20
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
W
wanghaoshuang 已提交
21 22 23 24 25 26 27 28 29 30

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>;

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

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

W
wanghaoshuang 已提交
45 46
  auto x_tensor = EigenTensor<T, D>::From(*x);
  auto out_tensor = EigenTensor<T, D>::From(*out);
Q
QI JUN 已提交
47 48
  auto& place =
      *context.template device_context<DeviceContext>().eigen_device();
W
wanghaoshuang 已提交
49
  out_tensor.device(place) = x_tensor.pad(paddings, pad_value);
W
wanghaoshuang 已提交
50 51
}

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

Q
QI JUN 已提交
83
template <typename DeviceContext, typename T, size_t D>
W
wanghaoshuang 已提交
84
void PadGradFunction(const framework::ExecutionContext& context) {
W
wanghaoshuang 已提交
85
  auto pads = context.Attr<std::vector<int>>("paddings");
W
wanghaoshuang 已提交
86
  Eigen::array<std::pair<int, int>, D> paddings;
W
wanghaoshuang 已提交
87
  for (size_t i = 0; i < paddings.size(); ++i) {
W
wanghaoshuang 已提交
88 89
    paddings[i].first = -pads[i * 2];
    paddings[i].second = -pads[i * 2 + 1];
W
wanghaoshuang 已提交
90
  }
W
wanghaoshuang 已提交
91 92
  auto* d_out = context.Input<Tensor>(framework::GradVarName("Out"));
  auto* d_x = context.Output<Tensor>(framework::GradVarName("X"));
W
wanghaoshuang 已提交
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);
Q
QI JUN 已提交
97 98
    auto& place =
        *context.template device_context<DeviceContext>().eigen_device();
W
wanghaoshuang 已提交
99 100
    d_x_tensor.device(place) = d_out_tensor.pad(paddings, 0);
  }
W
wanghaoshuang 已提交
101 102
}

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

}  // namespace operators
}  // namespace paddle