PadOp.cpp 7.7 KB
Newer Older
D
dangqingqing 已提交
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
/* 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. */

#include "PadOp.h"
#include "paddle/math/Vector.h"

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 28 29 30 31 32 33
                          const PadConf& pad) {
  int cstart = pad.channelStart, cend = pad.channelEnd;
  int hstart = pad.heightStart, hend = pad.heightEnd;
  int wstart = pad.widthStart, wend = pad.widthEnd;
  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 54 55 56 57 58 59
                              const PadConf& pad) {
  int cstart = pad.channelStart, cend = pad.channelEnd;
  int hstart = pad.heightStart, hend = pad.heightEnd;
  int wstart = pad.widthStart, wend = pad.widthEnd;
  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;
      }
    }
  }
}

D
dangqingqing 已提交
74 75 76 77 78 79 80 81
/**
 * \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 已提交
82
 *                It has six integers. The channelStart and channelEnd indicate
D
dangqingqing 已提交
83
 *                how many zeros to add before and after the input in channel
D
dangqingqing 已提交
84 85
 *                dimension. And the heightStart and heightEnd indicate padding
 *                in height dimension. The widthStart and widthEnd indicate the
D
dangqingqing 已提交
86 87 88 89 90 91 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
 *                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 已提交
127 128 129 130
template <DeviceType Device>
class PadFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
D
dangqingqing 已提交
131 132 133 134 135 136
    pad_.channelStart = config.get<int>("cstart");
    pad_.channelEnd = config.get<int>("cend");
    pad_.heightStart = config.get<int>("hstart");
    pad_.heightEnd = config.get<int>("hend");
    pad_.widthStart = config.get<int>("wstart");
    pad_.widthEnd = config.get<int>("wend");
D
dangqingqing 已提交
137 138
  }

D
dangqingqing 已提交
139
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
D
dangqingqing 已提交
140 141
    CHECK_EQ(1UL, inputs.size());
    CHECK_EQ(1UL, outputs.size());
D
dangqingqing 已提交
142 143 144 145 146 147 148 149 150 151 152 153
    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 已提交
154 155 156 157
                num,
                inC,
                inH,
                inW,
D
dangqingqing 已提交
158
                pad_);
D
dangqingqing 已提交
159 160 161
  }

private:
D
dangqingqing 已提交
162
  PadConf pad_;
D
dangqingqing 已提交
163 164
};

D
dangqingqing 已提交
165 166 167 168 169 170 171 172 173 174
/**
 * \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 已提交
175 176 177 178
template <DeviceType Device>
class PadGradFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
D
dangqingqing 已提交
179 180 181 182 183 184
    pad_.channelStart = config.get<int>("cstart");
    pad_.channelEnd = config.get<int>("cend");
    pad_.heightStart = config.get<int>("hstart");
    pad_.heightEnd = config.get<int>("hend");
    pad_.widthStart = config.get<int>("wstart");
    pad_.widthEnd = config.get<int>("wend");
D
dangqingqing 已提交
185 186
  }

D
dangqingqing 已提交
187
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
D
dangqingqing 已提交
188
    CHECK_EQ(1UL, inputs.size());
D
dangqingqing 已提交
189
    CHECK_EQ(1UL, outputs.size());
D
dangqingqing 已提交
190

D
dangqingqing 已提交
191 192 193 194 195 196 197 198 199 200 201
    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 已提交
202

D
dangqingqing 已提交
203 204 205
    PadGrad<Device>(outputs[0].data<real>(),
                    inputs[0].data<real>(),
                    num,
D
dangqingqing 已提交
206 207 208
                    inC,
                    inH,
                    inW,
D
dangqingqing 已提交
209
                    pad_);
D
dangqingqing 已提交
210 211 212
  }

private:
D
dangqingqing 已提交
213
  PadConf pad_;
D
dangqingqing 已提交
214 215 216 217 218 219 220 221 222 223
};

REGISTER_TYPED_FUNC(Pad, CPU, PadFunc);
REGISTER_TYPED_FUNC(PadGrad, CPU, PadGradFunc);
#ifndef PADDLE_ONLY_CPU
REGISTER_TYPED_FUNC(Pad, GPU, PadFunc);
REGISTER_TYPED_FUNC(PadGrad, GPU, PadGradFunc);
#endif

}  // namespace paddle