PadOpGpu.cu 3.5 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
/* 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 "hl_base.h"
#include "PadOp.h"

namespace paddle {

__global__ void KePad(real* outputs, const real* inputs,
                      int inC, int inH, int inW,
                      int padc, int padh, int padw,
                      int outC, int outH, int outW, int nthreads) {
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < nthreads) {
    const int w = idx % inW;
    const int h = (idx / inW) % inH;
    const int c = (idx / inW / inH) % inC;
    const int n = idx / inW / inH / inC;

    const int off = ((n * outC + c + padc) * outH + h + padh) * outW + padw + w;
    outputs[off] = inputs[idx];
  }
}

template <>
void Pad<DEVICE_TYPE_GPU>(real* outputs,
                          const real* inputs,
                          const int num,
                          const int inC,
                          const int inH,
                          const int inW,
D
dangqingqing 已提交
43
                          const PadConf& pad) {
D
dangqingqing 已提交
44 45 46
  size_t nth = num * inC * inH * inW;
  int blockSize = 1024;
  int gridSize = (nth + 1024 - 1) / 1024;
D
dangqingqing 已提交
47 48 49 50 51 52
  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 已提交
53
  KePad<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>
D
dangqingqing 已提交
54
    (outputs, inputs, inC, inH, inW, cstart, hstart, wstart,
D
dangqingqing 已提交
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
     outC, outH, outW, nth);
  CHECK_SYNC("Pad");
}

__global__ void KePadDiff(real* inGrad, const real* outGrad,
                          int inC, int inH, int inW,
                          int padc, int padh, int padw,
                          int outC, int outH, int outW, int nthreads) {
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < nthreads) {
    const int w = idx % inW;
    const int h = (idx / inW) % inH;
    const int c = (idx / inW / inH) % inC;
    const int n = idx / inW / inH / inC;

    const int off = ((n * outC + c + padc) * outH + h + padh) * outW + padw + w;
    inGrad[idx] += outGrad[off];
  }
}

template <>
void PadGrad<DEVICE_TYPE_GPU>(real* inGrad,
                              const real* outGrad,
                              const int num,
                              const int inC,
                              const int inH,
                              const int inW,
D
dangqingqing 已提交
82
                              const PadConf& pad) {
D
dangqingqing 已提交
83 84 85
  int nth = num * inC * inH * inW;
  int blockSize = 1024;
  int gridSize = (nth + 1024 - 1) / 1024;
D
dangqingqing 已提交
86 87 88 89 90 91
  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 已提交
92
  KePadDiff <<<gridSize, blockSize, 0, STREAM_DEFAULT>>>
D
dangqingqing 已提交
93
    (inGrad, outGrad, inC, inH, inW, cstart, hstart, wstart,
D
dangqingqing 已提交
94 95 96 97 98
     outC, outH, outW, nth);
  CHECK_SYNC("PadGrad");
}

}  // namespace paddle