PadOp.cpp 5.3 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 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* 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,
                          const int padc0,
                          const int padc1,
                          const int padh0,
                          const int padh1,
                          const int padw0,
                          const int padw1) {
  int outC = inC + padc0 + padc1;
  int outH = inH + padh0 + padh1;
  int outW = inW + padw0 + padw1;
  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;
        int outoff = ((i * outC + c + padc0) * outH + h + padh0) * outW + padw0;
        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,
                              const int padc0,
                              const int padc1,
                              const int padh0,
                              const int padh1,
                              const int padw0,
                              const int padw1) {
  int outC = inC + padc0 + padc1;
  int outH = inH + padh0 + padh1;
  int outW = inW + padw0 + padw1;
  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;
        int outoff = ((i * outC + c + padc0) * outH + h + padh0) * outW + padw0;
        CpuVector inG = CpuVector(inW, inGrad + inoff);
        CpuVector outG = CpuVector(inW, const_cast<real*>(outGrad + outoff));
        inG += outG;
      }
    }
  }
}

template <DeviceType Device>
class PadFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
    padc0_ = config.get<int>("padc0");
    padc1_ = config.get<int>("padc1");
    padh0_ = config.get<int>("padh0");
    padh1_ = config.get<int>("padh1");
    padw0_ = config.get<int>("padw0");
    padw1_ = config.get<int>("padw1");
  }

88 89 90 91
  /**
   * \param inputs[0] input value.
   * \param outputs[0] output value.
   */
D
dangqingqing 已提交
92 93 94
  void calc(const Arguments& inputs,
            const Arguments& outputs,
            const Arguments& inouts) override {
D
dangqingqing 已提交
95 96 97
    CHECK_EQ(1UL, inputs.size());
    CHECK_EQ(1UL, outputs.size());
    CHECK_EQ(0UL, inouts.size());
D
dangqingqing 已提交
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 133 134 135 136 137 138

    size_t num = inputs[0].dims_[0];
    size_t inC = inputs[0].dims_[1];
    size_t inH = inputs[0].dims_[2];
    size_t inW = inputs[0].dims_[3];

    Pad<Device>(outputs[0].getData(),
                inputs[0].getData(),
                num,
                inC,
                inH,
                inW,
                padc0_,
                padc1_,
                padh0_,
                padh1_,
                padw0_,
                padw1_);
  }

private:
  int padc0_;
  int padc1_;
  int padh0_;
  int padh1_;
  int padw0_;
  int padw1_;
};

template <DeviceType Device>
class PadGradFunc : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
    padc0_ = config.get<int>("padc0");
    padc1_ = config.get<int>("padc1");
    padh0_ = config.get<int>("padh0");
    padh1_ = config.get<int>("padh1");
    padw0_ = config.get<int>("padw0");
    padw1_ = config.get<int>("padw1");
  }

139 140 141 142
  /**
   * \param inputs[0] output grad.
   * \param inouts[0] input grad.
   */
D
dangqingqing 已提交
143 144 145
  void calc(const Arguments& inputs,
            const Arguments& outputs,
            const Arguments& inouts) override {
D
dangqingqing 已提交
146 147 148
    CHECK_EQ(1UL, inputs.size());
    CHECK_EQ(0UL, outputs.size());
    CHECK_EQ(1UL, inouts.size());
D
dangqingqing 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

    size_t n = inouts[0].dims_[0];
    size_t inC = inouts[0].dims_[1];
    size_t inH = inouts[0].dims_[2];
    size_t inW = inouts[0].dims_[3];

    PadGrad<Device>(inouts[0].getData(),
                    inputs[0].getData(),
                    n,
                    inC,
                    inH,
                    inW,
                    padc0_,
                    padc1_,
                    padh0_,
                    padh1_,
                    padw0_,
                    padw1_);
  }

private:
  int padc0_;
  int padc1_;
  int padh0_;
  int padh1_;
  int padw0_;
  int padw1_;
};

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