RowConvOpGpu.cu 11.9 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

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

X
Xin Pan 已提交
15 16
#include "paddle/legacy/cuda/include/hl_base.h"
#include "paddle/legacy/function/RowConvOp.h"
D
dangqingqing 已提交
17 18 19

namespace paddle {

L
liaogang 已提交
20 21 22 23 24 25 26 27 28
template <int BLOCK_H, int BLOCK_W>
__global__ void KeRowConv(real* y,
                          const real* x,
                          const real* w,
                          const int* starts,
                          const int height,
                          const int width,
                          const int numSeq,
                          const int context) {
D
dangqingqing 已提交
29 30 31 32 33 34 35 36
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int blky = blockDim.y;
  const int gidx = blockIdx.x * blockDim.x;

  __shared__ real sw[BLOCK_H][BLOCK_W];

  for (int i = tidy; i < context; i += blky) {
L
liaogang 已提交
37
    sw[i][tidx] = gidx + tidx < width ? w[i * width + gidx + tidx] : 0.0;
D
dangqingqing 已提交
38
  }
J
jc 已提交
39

D
dangqingqing 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  __syncthreads();

  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
    for (int j = tidy; j < steps; j += blky) {
      real sum = 0;
      int off = (start + j) * width;
      for (int t = 0; t < context; ++t) {
        if ((start + j + t) < end) {
          int xoff = off + t * width;
          real xVal = gidx + tidx < width ? x[xoff + gidx + tidx] : 0.0;
          sum += sw[t][tidx] * xVal;
        }
      }
      if (gidx + tidx < width) {
        y[off + gidx + tidx] += sum;
      }
    }
  }
}

L
liaogang 已提交
63 64 65 66 67 68 69 70
__global__ void KeRowConv2(real* y,
                           const real* x,
                           const real* w,
                           const int* starts,
                           const int height,
                           const int width,
                           const int numSeq,
                           const int context) {
D
dangqingqing 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int blky = blockDim.y;
  const int gidx = blockIdx.x * blockDim.x;

  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
    for (int j = tidy; j < steps; j += blky) {
      int off = (start + j) * width;
      real sum = 0;
      for (int t = 0; t < context && (start + j + t) < end; ++t) {
        int xoff = off + t * width;
        real xd = gidx + tidx < width ? x[xoff + gidx + tidx] : 0.0;
        real wd = gidx + tidx < width ? w[t * width + gidx + tidx] : 0.0;
        sum += wd * xd;
      }
      if (gidx + tidx < width) {
        y[off + gidx + tidx] += sum;
      }
    }
  }
}

template <>
C
chengduo 已提交
97
void RowConv<DEVICE_TYPE_GPU>(GpuMatrix& out,  // NOLINT
D
dangqingqing 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                              const GpuMatrix& in,
                              const GpuMatrix& filter,
                              const GpuIVector& seq) {
  const size_t numSeq = seq.getSize() - 1;
  const size_t contextLength = filter.getHeight();
  const size_t height = in.getHeight();
  const size_t width = in.getWidth();

  real* y = out.getData();
  const real* x = in.getData();
  const real* w = filter.getData();
  const int* starts = seq.getData();

  dim3 dimBlock(32, 32);
  dim3 dimGrid(DIVUP(width, dimBlock.x), 1);

  if (contextLength <= 32) {
L
liaogang 已提交
115 116
    KeRowConv<32, 32><<<dimGrid, dimBlock, 0, STREAM_DEFAULT>>>(
        y, x, w, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
117
  } else {
L
liaogang 已提交
118 119
    KeRowConv2<<<dimGrid, dimBlock, 0, STREAM_DEFAULT>>>(
        y, x, w, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
120 121 122 123
  }
  CHECK_SYNC("RowConv");
}

L
liaogang 已提交
124 125 126 127 128 129 130 131 132
template <int BLOCK_H, int BLOCK_W, int CONTEXT>
__global__ void KeRowConvBwWeight(real* dw,
                                  const real* x,
                                  const real* dy,
                                  const int* starts,
                                  const int height,
                                  const int width,
                                  const int numSeq,
                                  const int context) {
D
dangqingqing 已提交
133 134 135 136 137
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int blky = blockDim.y;
  const int gidx = blockIdx.x * blockDim.x;

D
dangqingqing 已提交
138 139
  __shared__ real sh_x[BLOCK_W][BLOCK_H];
  __shared__ real sh_dy[BLOCK_W][BLOCK_H + CONTEXT - 1];
D
dangqingqing 已提交
140 141
  __shared__ real sh_dw[CONTEXT][BLOCK_W];

D
dangqingqing 已提交
142 143
  if (tidy < context) {
    sh_dw[tidy][tidx] = 0.0;
D
dangqingqing 已提交
144 145 146
  }
  __syncthreads();

C
chengduo 已提交
147 148 149 150
  // NOTE(zcd): temporary solution
  unsigned mask = 0u;
  CREATE_SHFL_MASK(mask, true);

D
dangqingqing 已提交
151 152 153 154
  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
L
liaogang 已提交
155
    const int size = ((steps + BLOCK_H - 1) / BLOCK_H) * BLOCK_H;
D
dangqingqing 已提交
156
    for (int j = tidy; j < size; j += BLOCK_H) {
D
dangqingqing 已提交
157 158 159 160
      int xoff = gidx + tidx;
      int yoff = start + j;

      // transpose
L
liaogang 已提交
161 162 163 164
      sh_x[tidx][tidy] =
          (xoff < width && yoff < end) ? x[yoff * width + xoff] : 0.0;
      sh_dy[tidx][tidy + context - 1] =
          (xoff < width && yoff < end) ? dy[yoff * width + xoff] : 0.0;
D
dangqingqing 已提交
165 166 167
      __syncthreads();
      if (tidy < (context - 1)) {
        yoff = yoff - context + 1;
L
liaogang 已提交
168 169
        sh_dy[tidx][tidy] =
            (xoff < width && yoff >= start) ? dy[yoff * width + xoff] : 0.0;
D
dangqingqing 已提交
170
      }
D
dangqingqing 已提交
171 172 173
      __syncthreads();

      for (int t = 0; t < context; t++) {
D
dangqingqing 已提交
174 175
        real val = sh_x[tidy][tidx] * sh_dy[tidy][tidx + context - 1 - t];
        __syncthreads();
D
dangqingqing 已提交
176
        // warp size and blockDim.x is 32.
C
chengduo 已提交
177 178 179 180

        for (int offset = 16; offset > 0; offset /= 2)
          val += __shfl_down_sync(mask, val, offset);

D
dangqingqing 已提交
181
        __syncthreads();
D
dangqingqing 已提交
182 183 184 185 186 187 188 189
        if (tidx == 0) {
          sh_dw[t][tidy] += val;
        }
        __syncthreads();
      }
    }
  }

D
dangqingqing 已提交
190
  for (int t = tidy; (t < context) && ((gidx + tidx) < width); t += blky) {
D
dangqingqing 已提交
191 192 193 194
    dw[t * width + gidx + tidx] += sh_dw[t][tidx];
  }
}

L
liaogang 已提交
195 196 197 198 199 200 201 202 203
template <int BLOCK_H, int BLOCK_W>
__global__ void KeRowConvBwWeight2(real* dw,
                                   const real* x,
                                   const real* dy,
                                   const int* starts,
                                   const int height,
                                   const int width,
                                   const int numSeq,
                                   const int context) {
D
dangqingqing 已提交
204 205 206 207 208 209 210
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int gidx = blockIdx.x * blockDim.x;

  __shared__ real sh_x[BLOCK_H][BLOCK_W];
  __shared__ real sh_dy[BLOCK_H][BLOCK_W];

C
chengduo 已提交
211 212 213 214
  // NOTE(zcd): temporary solution
  unsigned mask = 0u;
  CREATE_SHFL_MASK(mask, true);

D
dangqingqing 已提交
215 216 217 218
  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
D
dangqingqing 已提交
219

L
liaogang 已提交
220
    const int size = ((steps + BLOCK_H - 1) / BLOCK_H) * BLOCK_H;
D
dangqingqing 已提交
221
    for (int j = tidy; j < size; j += BLOCK_H) {
D
dangqingqing 已提交
222 223 224 225
      int xoff = gidx + tidx;
      int yoff = start + j;

      // transpose
L
liaogang 已提交
226 227
      sh_x[tidx][tidy] =
          (xoff < width && yoff < end) ? x[yoff * width + xoff] : 0.0;
D
dangqingqing 已提交
228 229 230
      __syncthreads();

      for (int t = 0; t < context; t++) {
L
liaogang 已提交
231 232 233 234
        sh_dy[tidx][tidy] =
            (xoff < width && (yoff - t) >= start && yoff - t < end)
                ? dy[(yoff - t) * width + xoff]
                : 0.0;
D
dangqingqing 已提交
235 236 237 238
        __syncthreads();

        real val = sh_x[tidy][tidx] * sh_dy[tidy][tidx];
        __syncthreads();
D
dangqingqing 已提交
239
        // warp size and blockDim.x is 32.
C
chengduo 已提交
240 241 242
        for (int offset = 16; offset > 0; offset /= 2)
          val += __shfl_down_sync(mask, val, offset);

D
dangqingqing 已提交
243 244
        __syncthreads();

D
dangqingqing 已提交
245
        if (tidx == 0 && (gidx + tidy) < width) {
L
liaogang 已提交
246
          dw[t * width + gidx + tidy] += val;
D
dangqingqing 已提交
247 248 249 250 251 252
        }
      }
    }
  }
}

L
liaogang 已提交
253 254 255 256 257 258 259 260 261
template <int BLOCK_H, int BLOCK_W>
__global__ void KeRowConvBwData(real* dx,
                                const real* w,
                                const real* dy,
                                const int* starts,
                                const int height,
                                const int width,
                                const int numSeq,
                                const int context) {
D
dangqingqing 已提交
262 263 264 265 266 267 268 269
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int blky = blockDim.y;
  const int gidx = blockIdx.x * blockDim.x;

  __shared__ real sw[BLOCK_H][BLOCK_W];

  for (int i = tidy; i < context; i += blky) {
L
liaogang 已提交
270
    sw[i][tidx] = gidx + tidx < width ? w[i * width + gidx + tidx] : 0.0;
D
dangqingqing 已提交
271
  }
J
jc 已提交
272

D
dangqingqing 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  __syncthreads();

  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
    for (int j = tidy; j < steps; j += blky) {
      real sum = 0;
      int off = (start + j) * width;
      for (int t = 0; t < context && (j - t) >= 0; ++t) {
        int dyOff = off - t * width;
        real dyVal = gidx + tidx < width ? dy[dyOff + gidx + tidx] : 0.0;
        sum += sw[t][tidx] * dyVal;
      }
      if (gidx + tidx < width) {
        dx[off + gidx + tidx] += sum;
      }
    }
  }
}

L
liaogang 已提交
294 295 296 297 298 299 300 301
__global__ void KeRowConvBwData2(real* dx,
                                 const real* w,
                                 const real* dy,
                                 const int* starts,
                                 const int height,
                                 const int width,
                                 const int numSeq,
                                 const int context) {
D
dangqingqing 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  const int tidx = threadIdx.x;
  const int tidy = threadIdx.y;
  const int blky = blockDim.y;
  const int gidx = blockIdx.x * blockDim.x;

  for (int i = 0; i < numSeq; ++i) {
    const int start = starts[i];
    const int end = starts[i + 1];
    const int steps = end - start;
    for (int j = tidy; j < steps; j += blky) {
      real sum = 0;
      int off = (start + j) * width;
      for (int t = 0; t < context && (j - t) >= 0; ++t) {
        int dyOff = off - t * width;
        real dyVal = gidx + tidx < width ? dy[dyOff + gidx + tidx] : 0.0;
        real wVal = gidx + tidx < width ? w[t * width + gidx + tidx] : 0.0;
        sum += wVal * dyVal;
      }
      if (gidx + tidx < width) {
        dx[off + gidx + tidx] += sum;
      }
    }
  }
}

template <>
void RowConvGrad<DEVICE_TYPE_GPU>(const GpuMatrix& outG,
L
liaogang 已提交
329 330
                                  const GpuMatrix& in,
                                  const GpuMatrix& filter,
C
chengduo 已提交
331 332
                                  GpuMatrix& inG,      // NOLINT
                                  GpuMatrix& filterG,  // NOLINT
L
liaogang 已提交
333
                                  const GpuIVector& seq) {
D
dangqingqing 已提交
334 335 336 337 338 339 340 341 342 343
  const size_t numSeq = seq.getSize() - 1;
  const size_t contextLength = filter.getHeight();
  const size_t height = in.getHeight();
  const size_t width = in.getWidth();

  const real* dy = outG.getData();
  const real* x = in.getData();
  const real* w = filter.getData();
  const int* starts = seq.getData();

D
dangqingqing 已提交
344 345 346 347
  if (filterG) {
    dim3 dimBlock(32, 32);
    dim3 dimGrid(DIVUP(width, dimBlock.x), 1);
    real* dw = filterG.getData();
J
jc 已提交
348
    if (contextLength <= 32) {
L
liaogang 已提交
349 350
      KeRowConvBwWeight<32, 32, 32><<<dimGrid, dimBlock, 0, STREAM_DEFAULT>>>(
          dw, x, dy, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
351
    } else {
L
liaogang 已提交
352 353
      KeRowConvBwWeight2<32, 32><<<dimGrid, dimBlock, 0, STREAM_DEFAULT>>>(
          dw, x, dy, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
354
    }
D
dangqingqing 已提交
355 356
  }

D
dangqingqing 已提交
357 358 359 360 361
  if (inG) {
    real* dx = inG.getData();
    dim3 dimBlock2(32, 32);
    dim3 dimGrid2(DIVUP(width, dimBlock2.x), 1);
    if (contextLength <= 64) {
L
liaogang 已提交
362 363
      KeRowConvBwData<32, 64><<<dimGrid2, dimBlock2, 0, STREAM_DEFAULT>>>(
          dx, w, dy, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
364
    } else {
L
liaogang 已提交
365 366
      KeRowConvBwData2<<<dimGrid2, dimBlock2, 0, STREAM_DEFAULT>>>(
          dx, w, dy, starts, height, width, numSeq, contextLength);
D
dangqingqing 已提交
367
    }
D
dangqingqing 已提交
368 369 370 371 372 373
  }

  CHECK_SYNC("RowConvGrad");
}

}  // namespace paddle