DepthwiseConvOpGpu.cu 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 "DepthwiseConvOp.h"
16
#include "GemmFunctor.h"
17
#include "paddle/math/BaseMatrix.h"
18 19

namespace paddle {
20

21
// CUDA kernel to compute the depthwise convolution forward pass
22
template <class T>
L
liaogang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
__global__ void ConvolutionDepthwiseForward(const int nthreads,
                                            const T* const inputData,
                                            const T* const filterData,
                                            const int batchSize,
                                            const int outputChannels,
                                            const int outputHeight,
                                            const int outputWidth,
                                            const int inputChannels,
                                            const int inputHeight,
                                            const int inputWidth,
                                            const int filterMultiplier,
                                            const int filterHeight,
                                            const int filterWidth,
                                            const int strideH,
                                            const int strideW,
                                            const int paddingH,
                                            const int paddingW,
                                            T* const outputData) {
  int index = (blockIdx.x * gridDim.y + blockIdx.y) * blockDim.x + threadIdx.x;
X
xzl 已提交
42 43

  if (index < nthreads) {
44 45 46 47 48
    const int batch = index / outputChannels / outputHeight / outputWidth;
    const int c_out = (index / outputHeight / outputWidth) % outputChannels;
    const int h_out = (index / outputWidth) % outputHeight;
    const int w_out = index % outputWidth;

X
xzl 已提交
49
    const int c_in = c_out / filterMultiplier;
50
    const T* weight = filterData + c_out * filterHeight * filterWidth;
51
    T value = 0;
52 53 54 55
    const int h_in_start = -paddingH + h_out * strideH;
    const int w_in_start = -paddingW + w_out * strideW;
    const int h_in_end = -paddingH + h_out * strideH + filterHeight - 1;
    const int w_in_end = -paddingW + w_out * strideW + filterWidth - 1;
L
liaogang 已提交
56 57 58 59 60 61 62 63 64 65 66 67
    if ((h_in_start >= 0) && (h_in_end < inputHeight) && (w_in_start >= 0) &&
        (w_in_end < inputWidth)) {
      for (int kh = 0; kh < filterHeight; ++kh) {
        for (int kw = 0; kw < filterWidth; ++kw) {
          const int h_in = -paddingH + h_out * strideH + kh;
          const int w_in = -paddingW + w_out * strideW + kw;
          const int offset =
              ((batch * inputChannels + c_in) * inputHeight + h_in) *
                  inputWidth +
              w_in;
          value += (*weight) * inputData[offset];
          ++weight;
X
xzl 已提交
68
        }
L
liaogang 已提交
69
      }
X
xzl 已提交
70
    } else {
L
liaogang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      for (int kh = 0; kh < filterHeight; ++kh) {
        for (int kw = 0; kw < filterWidth; ++kw) {
          const int h_in = -paddingH + h_out * strideH + kh;
          const int w_in = -paddingW + w_out * strideW + kw;
          if ((h_in >= 0) && (h_in < inputHeight) && (w_in >= 0) &&
              (w_in < inputWidth)) {
            const int offset =
                ((batch * inputChannels + c_in) * inputHeight + h_in) *
                    inputWidth +
                w_in;
            value += (*weight) * inputData[offset];
          }
          ++weight;
        }
      }
X
xzl 已提交
86
    }
87
    outputData[index] = value;
88 89 90
  }
}

91
// CUDA kernel to compute the depthwise convolution backprop w.r.t input.
92
template <class T>
L
liaogang 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
__global__ void ConvolutionDepthwiseInputBackward(const int nthreads,
                                                  const T* const top_diff,
                                                  const T* const weight_data,
                                                  const int num,
                                                  const int outputChannels,
                                                  const int outputHeight,
                                                  const int outputWidth,
                                                  const int inputChannels,
                                                  const int inputHeight,
                                                  const int inputWidth,
                                                  const int filterMultiplier,
                                                  const int filterHeight,
                                                  const int filterWidth,
                                                  const int strideH,
                                                  const int strideW,
                                                  const int paddingH,
                                                  const int paddingW,
                                                  T* const bottom_diff) {
  int index = (blockIdx.x * gridDim.y + blockIdx.y) * blockDim.x + threadIdx.x;
X
xzl 已提交
112
  if (index < nthreads) {
113 114 115 116
    const int batch = index / inputChannels / inputHeight / inputWidth;
    const int c_in = (index / inputHeight / inputWidth) % inputChannels;
    const int h_in = (index / inputWidth) % inputHeight;
    const int w_in = index % inputWidth;
117

X
xzl 已提交
118
    const int c_out_start = c_in * filterMultiplier;
119

L
liaogang 已提交
120
    int h_out_start = (h_in - filterHeight + paddingH + strideH) / strideH;
121
    h_out_start = 0 > h_out_start ? 0 : h_out_start;
L
liaogang 已提交
122 123 124
    int h_out_end = (h_in + paddingH) / strideH;
    h_out_end = outputHeight - 1 < h_out_end ? outputHeight - 1 : h_out_end;
    int w_out_start = (w_in - filterWidth + paddingW + strideW) / strideW;
125
    w_out_start = 0 > w_out_start ? 0 : w_out_start;
L
liaogang 已提交
126 127
    int w_out_end = (w_in + paddingW) / strideW;
    w_out_end = outputWidth - 1 < w_out_end ? outputWidth - 1 : w_out_end;
128

129
    T value = 0;
130

L
liaogang 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143
    for (int c_out = c_out_start; c_out < c_out_start + filterMultiplier;
         c_out++) {
      for (int h_out = h_out_start; h_out <= h_out_end; ++h_out) {
        const int filter_h = h_in + paddingH - h_out * strideH;
        for (int w_out = w_out_start; w_out <= w_out_end; ++w_out) {
          const int filter_w = w_in + paddingW - w_out * strideW;
          const int filter_offset = c_out * filterHeight * filterWidth +
                                    filter_h * filterWidth + filter_w;
          const int top_diff_offset =
              ((batch * outputChannels + c_out) * outputHeight + h_out) *
                  outputWidth +
              w_out;
          value += top_diff[top_diff_offset] * weight_data[filter_offset];
144
        }
L
liaogang 已提交
145
      }
146 147
    }
    bottom_diff[index] += value;
L
liaogang 已提交
148
  }
149 150
}

151
// CUDA kernel to compute the depthwise convolution backprop w.r.t filter.
152
template <class T>
L
liaogang 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
__global__ void ConvolutionDepthwiseFilterBackward(const int num_i,
                                                   const int nthreads,
                                                   const T* const top_diff,
                                                   const T* const inputData,
                                                   const int num,
                                                   const int outputChannels,
                                                   const int outputHeight,
                                                   const int outputWidth,
                                                   const int inputChannels,
                                                   const int inputHeight,
                                                   const int inputWidth,
                                                   const int filterMultiplier,
                                                   const int filterHeight,
                                                   const int filterWidth,
                                                   const int strideH,
                                                   const int strideW,
                                                   const int paddingH,
                                                   const int paddingW,
                                                   T* const buffer_data) {
  int index = (blockIdx.x * gridDim.y + blockIdx.y) * blockDim.x + threadIdx.x;
173
  if (index < nthreads) {
174 175
    const int h_out = (index / outputWidth) % outputHeight;
    const int w_out = index % outputWidth;
L
liaogang 已提交
176 177
    const int kh =
        (index / filterWidth / outputHeight / outputWidth) % filterHeight;
178
    const int kw = (index / outputHeight / outputWidth) % filterWidth;
179 180
    const int h_in = -paddingH + h_out * strideH + kh;
    const int w_in = -paddingW + w_out * strideW + kw;
L
liaogang 已提交
181 182 183 184
    if ((h_in >= 0) && (h_in < inputHeight) && (w_in >= 0) &&
        (w_in < inputWidth)) {
      const int c_out =
          index / (filterHeight * filterWidth * outputHeight * outputWidth);
X
xzl 已提交
185
      const int c_in = c_out / filterMultiplier;
186
      const int batch = num_i;
L
liaogang 已提交
187 188 189 190 191 192 193
      const int top_offset =
          ((batch * outputChannels + c_out) * outputHeight + h_out) *
              outputWidth +
          w_out;
      const int bottom_offset =
          ((batch * inputChannels + c_in) * inputHeight + h_in) * inputWidth +
          w_in;
194
      buffer_data[index] = top_diff[top_offset] * inputData[bottom_offset];
195 196 197 198 199 200 201
    } else {
      buffer_data[index] = 0;
    }
  }
}

template <class T>
L
liaogang 已提交
202
class DepthwiseConvFunctor<DEVICE_TYPE_GPU, T> {
203
public:
X
xzl 已提交
204
  void operator()(const T* inputData,
L
liaogang 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                  const T* filterData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int inputChannels,
                  int inputHeight,
                  int inputWidth,
                  int filterMultiplier,
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* outputData) {
221 222
    int outputSize = batchSize * outputChannels * outputHeight * outputWidth;

L
liaogang 已提交
223
    size_t blocks = (outputSize + 1024 - 1) / 1024;
224
    size_t blockX = 512;
L
liaogang 已提交
225
    size_t blockY = (blocks + 512 - 1) / 512;
226 227
    dim3 threads(1024, 1);
    dim3 grid(blockX, blockY);
228

L
liaogang 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    ConvolutionDepthwiseForward<T><<<grid, threads, 0, STREAM_DEFAULT>>>(
        outputSize,
        inputData,
        filterData,
        batchSize,
        outputChannels,
        outputHeight,
        outputWidth,
        inputChannels,
        inputHeight,
        inputWidth,
        filterMultiplier,
        filterHeight,
        filterWidth,
        strideH,
        strideW,
        paddingH,
        paddingW,
        outputData);
  }
249 250 251
};

template <class T>
L
liaogang 已提交
252
class DepthwiseConvGradInputFunctor<DEVICE_TYPE_GPU, T> {
253
public:
254
  void operator()(const T* outputGrad,
L
liaogang 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                  const T* filterData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int inputChannels,
                  int inputHeight,
                  int inputWidth,
                  int filterMultiplier,
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* inputGrad) {
271
    int inputSize = batchSize * inputChannels * inputHeight * inputWidth;
272

L
liaogang 已提交
273
    size_t blocks = (inputSize + 1024 - 1) / 1024;
274
    size_t blockX = 512;
L
liaogang 已提交
275
    size_t blockY = (blocks + 512 - 1) / 512;
276 277 278
    dim3 threads(1024, 1);
    dim3 grid(blockX, blockY);

279
    ConvolutionDepthwiseInputBackward<T>
L
liaogang 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        // NOLINT_NEXT_LINE(whitespace/operators)
        <<<grid, threads, 0, STREAM_DEFAULT>>>(inputSize,
                                               outputGrad,
                                               filterData,
                                               batchSize,
                                               outputChannels,
                                               outputHeight,
                                               outputWidth,
                                               inputChannels,
                                               inputHeight,
                                               inputWidth,
                                               filterMultiplier,
                                               filterHeight,
                                               filterWidth,
                                               strideH,
                                               strideW,
                                               paddingH,
                                               paddingW,
                                               inputGrad);
  }
300 301 302 303 304
};

template <class T>
class DepthwiseConvGradFilterFunctor<DEVICE_TYPE_GPU, T> {
public:
305
  void operator()(const T* outputGrad,
L
liaogang 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
                  const T* inputData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
                  int inputChannels,
                  int inputHeight,
                  int inputWidth,
                  int filterMultiplier,
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* colData,
                  T* filterGrad) {
    int colDataSize = outputChannels * filterHeight * filterWidth *
                      outputHeight * outputWidth;
325

L
liaogang 已提交
326 327 328 329 330 331 332 333 334 335
    size_t blocks = (colDataSize + 1024 - 1) / 1024;
    size_t blockX = 512;
    size_t blockY = (blocks + 512 - 1) / 512;
    dim3 threads(1024, 1);
    dim3 grid(blockX, blockY);
    BaseMatrix filterGradMatrix(outputChannels * filterHeight * filterWidth,
                                1,
                                filterGrad,
                                false,
                                true);
336

L
liaogang 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    for (int i = 0; i < batchSize; i++) {
      ConvolutionDepthwiseFilterBackward<
          T><<<grid, threads, 0, STREAM_DEFAULT>>>(i,
                                                   colDataSize,
                                                   outputGrad,
                                                   inputData,
                                                   batchSize,
                                                   outputChannels,
                                                   outputHeight,
                                                   outputWidth,
                                                   inputChannels,
                                                   inputHeight,
                                                   inputWidth,
                                                   filterMultiplier,
                                                   filterHeight,
                                                   filterWidth,
                                                   strideH,
                                                   strideW,
                                                   paddingH,
                                                   paddingW,
                                                   colData);
      int K = outputHeight * outputWidth;
      int M = colDataSize / K;
360

L
liaogang 已提交
361 362
      BaseMatrix colMatrix(M, K, colData, false, true);
      filterGradMatrix.sumRows(colMatrix, (T)1.0, (T)1.0);
363
    }
L
liaogang 已提交
364
  }
365 366
};

367
#ifdef PADDLE_TYPE_DOUBLE
368 369 370
template class DepthwiseConvGradInputFunctor<DEVICE_TYPE_GPU, double>;
template class DepthwiseConvFunctor<DEVICE_TYPE_GPU, double>;
template class DepthwiseConvGradFilterFunctor<DEVICE_TYPE_GPU, double>;
X
xzl 已提交
371
#else
372 373 374
template class DepthwiseConvGradInputFunctor<DEVICE_TYPE_GPU, float>;
template class DepthwiseConvFunctor<DEVICE_TYPE_GPU, float>;
template class DepthwiseConvGradFilterFunctor<DEVICE_TYPE_GPU, float>;
375
#endif
376 377

}  // namespace paddle