PadOp.cpp 7.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */

#include "PadOp.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/math/Vector.h"
D
dangqingqing 已提交
17 18 19 20 21 22 23 24 25 26

namespace paddle {

template <>
void Pad<DEVICE_TYPE_CPU>(real* outputs,
                          const real* inputs,
                          const int num,
                          const int inC,
                          const int inH,
                          const int inW,
D
dangqingqing 已提交
27
                          const PadConf& pad) {
Y
Yu Yang 已提交
28 29 30
  int cstart = pad.channel[0], cend = pad.channel[1];
  int hstart = pad.height[0], hend = pad.height[1];
  int wstart = pad.width[0], wend = pad.width[1];
D
dangqingqing 已提交
31 32 33
  int outC = inC + cstart + cend;
  int outH = inH + hstart + hend;
  int outW = inW + wstart + wend;
D
dangqingqing 已提交
34 35 36 37
  for (int i = 0; i < num; i++) {
    for (int c = 0; c < inC; c++) {
      for (int h = 0; h < inH; h++) {
        int inoff = ((i * inC + c) * inH + h) * inW;
D
dangqingqing 已提交
38 39
        int outoff =
            ((i * outC + c + cstart) * outH + h + hstart) * outW + wstart;
D
dangqingqing 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
        memcpy(outputs + outoff, inputs + inoff, inW * sizeof(real));
      }
    }
  }
}

template <>
void PadGrad<DEVICE_TYPE_CPU>(real* inGrad,
                              const real* outGrad,
                              const int num,
                              const int inC,
                              const int inH,
                              const int inW,
D
dangqingqing 已提交
53
                              const PadConf& pad) {
Y
Yu Yang 已提交
54 55 56
  int cstart = pad.channel[0], cend = pad.channel[1];
  int hstart = pad.height[0], hend = pad.height[1];
  int wstart = pad.width[0], wend = pad.width[1];
D
dangqingqing 已提交
57 58 59
  int outC = inC + cstart + cend;
  int outH = inH + hstart + hend;
  int outW = inW + wstart + wend;
D
dangqingqing 已提交
60 61 62 63
  for (int i = 0; i < num; i++) {
    for (int c = 0; c < inC; c++) {
      for (int h = 0; h < inH; h++) {
        int inoff = ((i * inC + c) * inH + h) * inW;
D
dangqingqing 已提交
64 65
        int outoff =
            ((i * outC + c + cstart) * outH + h + hstart) * outW + wstart;
D
dangqingqing 已提交
66 67 68 69 70 71 72 73
        CpuVector inG = CpuVector(inW, inGrad + inoff);
        CpuVector outG = CpuVector(inW, const_cast<real*>(outGrad + outoff));
        inG += outG;
      }
    }
  }
}

Y
Yu Yang 已提交
74 75 76 77 78 79
static inline PadConf castToPadConf(const FuncConfig& conf) {
  return {conf.get<std::vector<uint32_t>>("channel"),
          conf.get<std::vector<uint32_t>>("height"),
          conf.get<std::vector<uint32_t>>("width")};
}

D
dangqingqing 已提交
80 81 82 83 84 85 86 87
/**
 * \brief Padding zeros to input according to the specify dimension.
 *        The struct pad_ contains the padding size in each dimension.
 *        The input and output is a 4D tensor. In PadFunc, we only
 *        pad zeros to the 2nd to 4th dimension.
 *
 * Argument in this Function:
 * \param pad_    A struct object contains the padding size in each dimension.
D
dangqingqing 已提交
88
 *                It has six integers. The channelStart and channelEnd indicate
D
dangqingqing 已提交
89
 *                how many zeros to add before and after the input in channel
D
dangqingqing 已提交
90 91
 *                dimension. And the heightStart and heightEnd indicate padding
 *                in height dimension. The widthStart and widthEnd indicate the
D
dangqingqing 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 *                padding in width dimension.
 * \param inputs  A 4D tensor, only one input.
 * \param outputs A 4D tensor, the output value after padding.
 *
 * For example,
 * Input(2,2,2,3) = [
 *                    [ [[1,2,3], [3,4,5]],
 *                      [[2,3,5], [1,6,7]] ],
 *                    [ [[4,3,1], [1,8,7]],
 *                      [[3,8,9], [2,3,5]] ]
 *                  ] # the shape is (1,2,2,3)
 *
 * pad_: if channelStart = channelEnd = 1, others are 0.
 * Output(2,4,2,3) = [
 *                    [ [[0,0,0], [0,0,0]],
 *                      [[1,2,3], [3,4,5]],
 *                      [[2,3,5], [1,6,7]],
 *                      [[0,0,0], [0,0,0]] ],
 *                    [ [[0,0,0], [0,0,0]],
 *                      [[4,3,1], [1,8,7]],
 *                      [[3,8,9], [2,3,5]],
 *                      [[0,0,0], [0,0,0]] ]
 *                   ] # the shape is (2,4,2,3)
 *
 * pad_: if widthStart = 1, widthEnd = 2, others are 0.
 * Output(2,2,2,6) = [
 *                     [ [[0,1,2,3,0,0], [0,3,4,5,0,0]],
 *                       [[0,2,3,5,0,0], [0,1,6,7,0,0]] ],
 *                     [ [[0,4,3,1,0,0], [0,1,8,7,0,0]],
 *                       [[0,3,8,9,0,0], [0,2,3,5,0,0]] ],
 *                   ] # the shape is (2,2,2,6)
 *
 * pad_: if heightStart = 1, heightEnd = 1, others are 0.
 * Output(2,2,4,3) = [
 *                     [ [[0,0,0], [1,2,3], [3,4,5], [0,0,0]],
 *                       [[0,0,0], [2,3,5], [1,6,7], [0,0,0]] ],
 *                     [ [[0,0,0], [4,3,1], [1,8,7], [0,0,0]],
 *                       [[0,0,0], [3,8,9], [2,3,5], [0,0,0]] ],
 *                   ] # the shape is (2,2,4,3)
 */

D
dangqingqing 已提交
133 134
template <DeviceType Device>
class PadFunc : public FunctionBase {
W
Wu Yi 已提交
135
 public:
Y
Yu Yang 已提交
136
  void init(const FuncConfig& config) override { pad_ = castToPadConf(config); }
D
dangqingqing 已提交
137

D
dangqingqing 已提交
138
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
D
dangqingqing 已提交
139 140
    CHECK_EQ(1UL, inputs.size());
    CHECK_EQ(1UL, outputs.size());
D
dangqingqing 已提交
141 142 143 144 145 146 147 148 149 150 151 152
    CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO);

    size_t num = inputs[0].shape()[0];
    size_t inC = inputs[0].shape()[1];
    size_t inH = inputs[0].shape()[2];
    size_t inW = inputs[0].shape()[3];
    typename Tensor<real, Device>::Vector vec(outputs[0].shape().getElements(),
                                              outputs[0].data<real>());
    vec.zero();

    Pad<Device>(outputs[0].data<real>(),
                inputs[0].data<real>(),
D
dangqingqing 已提交
153 154 155 156
                num,
                inC,
                inH,
                inW,
D
dangqingqing 已提交
157
                pad_);
D
dangqingqing 已提交
158 159
  }

W
Wu Yi 已提交
160
 private:
D
dangqingqing 已提交
161
  PadConf pad_;
D
dangqingqing 已提交
162 163
};

D
dangqingqing 已提交
164 165 166 167 168 169 170 171 172 173
/**
 * \brief The backward propagation of padding Function. Remove the elements
 *        in the padding positions of forward.
 *
 * Argument in this Function:
 * \param pad_    The same meaning as it in PadFunc.
 * \param inputs  The gradient with respect to the output value of PadFunc.
 * \param outputs The gradient with respect to the input value of PadFunc.
 */

D
dangqingqing 已提交
174 175
template <DeviceType Device>
class PadGradFunc : public FunctionBase {
W
Wu Yi 已提交
176
 public:
Y
Yu Yang 已提交
177
  void init(const FuncConfig& config) override { pad_ = castToPadConf(config); }
D
dangqingqing 已提交
178

D
dangqingqing 已提交
179
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
D
dangqingqing 已提交
180
    CHECK_EQ(1UL, inputs.size());
D
dangqingqing 已提交
181
    CHECK_EQ(1UL, outputs.size());
D
dangqingqing 已提交
182

D
dangqingqing 已提交
183 184 185 186 187 188 189 190 191 192 193
    size_t num = outputs[0].shape()[0];
    size_t inC = outputs[0].shape()[1];
    size_t inH = outputs[0].shape()[2];
    size_t inW = outputs[0].shape()[3];

    if (outputs[0].getArgType() != ADD_TO) {
      // for unit test
      typename Tensor<real, Device>::Vector tmp(
          outputs[0].shape().getElements(), outputs[0].data<real>());
      tmp.zero();
    }
D
dangqingqing 已提交
194

D
dangqingqing 已提交
195 196 197
    PadGrad<Device>(outputs[0].data<real>(),
                    inputs[0].data<real>(),
                    num,
D
dangqingqing 已提交
198 199 200
                    inC,
                    inH,
                    inW,
D
dangqingqing 已提交
201
                    pad_);
D
dangqingqing 已提交
202 203
  }

W
Wu Yi 已提交
204
 private:
D
dangqingqing 已提交
205
  PadConf pad_;
D
dangqingqing 已提交
206 207 208 209
};

REGISTER_TYPED_FUNC(Pad, CPU, PadFunc);
REGISTER_TYPED_FUNC(PadGrad, CPU, PadGradFunc);
210
#ifdef PADDLE_WITH_CUDA
D
dangqingqing 已提交
211 212 213 214 215
REGISTER_TYPED_FUNC(Pad, GPU, PadFunc);
REGISTER_TYPED_FUNC(PadGrad, GPU, PadGradFunc);
#endif

}  // namespace paddle