CrossMapNormalOpGpu.cu 6.8 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

H
hedaoyuan 已提交
15
#include "CrossMapNormalOp.h"
L
liaogang 已提交
16
#include "hl_base.h"
H
hedaoyuan 已提交
17 18 19

namespace paddle {

L
liaogang 已提交
20 21 22 23 24 25 26
__global__ void KeCMRNormFillScale(size_t imageSize,
                                   const real* in,
                                   real* scale,
                                   size_t channels,
                                   size_t height,
                                   size_t width,
                                   size_t size,
H
hedaoyuan 已提交
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
                                   real alpha) {
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < imageSize) {
    const int w = idx % width;
    const int h = (idx / width) % height;
    const int n = idx / width / height;
    const int offset = (n * channels * height + h) * width + w;

    in += offset;
    scale += offset;
    const int step = height * width;
    const int pre_pad = (size - 1) / 2;
    const int post_pad = size - pre_pad - 1;

    real accum = 0;
    int index = 0;
    while (index < channels + post_pad) {
      if (index < channels) {
        accum += in[index * step] * in[index * step];
      }
      if (index >= size) {
        accum -= in[(index - size) * step] * in[(index - size) * step];
      }
      if (index >= post_pad) {
        scale[(index - post_pad) * step] = 1. + accum * alpha;
      }
      ++index;
    }
  }
}

L
liaogang 已提交
58 59 60 61
__global__ void KeCMRNormOutput(size_t inputSize,
                                const real* in,
                                const real* scale,
                                real negative_beta,
H
hedaoyuan 已提交
62 63 64 65 66 67 68 69
                                real* out) {
  const int index = threadIdx.x + blockIdx.x * blockDim.x;
  if (index < inputSize) {
    out[index] = in[index] * pow(scale[index], negative_beta);
  }
}

template <>
H
hedaoyuan 已提交
70 71
void CrossMapNormal<DEVICE_TYPE_GPU>(real* outputs,
                                     real* denoms,
H
hedaoyuan 已提交
72
                                     const real* inputs,
H
hedaoyuan 已提交
73 74 75 76 77 78 79 80
                                     size_t numSamples,
                                     size_t channels,
                                     size_t height,
                                     size_t width,
                                     size_t size,
                                     real scale,
                                     real pow) {
  size_t imageSize = numSamples * height * width;
H
hedaoyuan 已提交
81 82
  int blockSize = 1024;
  int gridSize = (imageSize + 1024 - 1) / 1024;
L
liaogang 已提交
83 84
  KeCMRNormFillScale<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(
      imageSize, inputs, denoms, channels, height, width, size, scale);
H
hedaoyuan 已提交
85

L
liaogang 已提交
86
  size_t inputSize = numSamples * height * width * channels;
H
hedaoyuan 已提交
87 88
  blockSize = 1024;
  gridSize = (inputSize + 1024 - 1) / 1024;
L
liaogang 已提交
89 90
  KeCMRNormOutput<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(
      inputSize, inputs, denoms, -pow, outputs);
H
hedaoyuan 已提交
91

H
hedaoyuan 已提交
92
  CHECK_SYNC("CrossMapNormal");
H
hedaoyuan 已提交
93 94
}

L
liaogang 已提交
95 96 97 98 99 100 101 102 103 104 105 106
__global__ void KeCMRNormDiff(size_t imageSize,
                              const real* bottom_data,
                              const real* top_data,
                              const real* scale,
                              const real* top_diff,
                              size_t channels,
                              size_t height,
                              size_t width,
                              size_t size,
                              real negative_beta,
                              real cache_ratio,
                              real* bottom_diff) {
H
hedaoyuan 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < imageSize) {
    const int w = idx % width;
    const int h = (idx / width) % height;
    const int n = idx / width / height;
    const int offset = (n * channels * height + h) * width + w;
    bottom_data += offset;
    top_data += offset;
    scale += offset;
    top_diff += offset;
    bottom_diff += offset;

    const int step = height * width;
    const int pre_pad = size - (size + 1) / 2;
    const int post_pad = size - pre_pad - 1;

    int index = 0;
    real accum = 0;
    while (index < channels + post_pad) {
      if (index < channels) {
        accum += top_diff[index * step] * top_data[index * step] /
L
liaogang 已提交
128
                 scale[index * step];
H
hedaoyuan 已提交
129 130 131
      }
      if (index >= size) {
        accum -= top_diff[(index - size) * step] *
L
liaogang 已提交
132
                 top_data[(index - size) * step] / scale[(index - size) * step];
H
hedaoyuan 已提交
133 134 135
      }
      if (index >= post_pad) {
        bottom_diff[(index - post_pad) * step] +=
L
liaogang 已提交
136 137 138
            top_diff[(index - post_pad) * step] *
                pow(scale[(index - post_pad) * step], negative_beta) -
            cache_ratio * bottom_data[(index - post_pad) * step] * accum;
H
hedaoyuan 已提交
139 140 141 142 143 144 145
      }
      ++index;
    }
  }
}

template <>
H
hedaoyuan 已提交
146
void CrossMapNormalGrad<DEVICE_TYPE_GPU>(real* inputsGrad,
H
hedaoyuan 已提交
147 148 149 150
                                         const real* inputsValue,
                                         const real* outputsValue,
                                         const real* outputsGrad,
                                         const real* denoms,
H
hedaoyuan 已提交
151 152 153 154 155 156 157 158
                                         size_t numSamples,
                                         size_t channels,
                                         size_t height,
                                         size_t width,
                                         size_t size,
                                         real scale,
                                         real pow) {
  size_t imageSize = numSamples * height * width;
H
hedaoyuan 已提交
159 160 161

  int blockSize = 1024;
  int gridSize = (imageSize + 1024 - 1) / 1024;
L
liaogang 已提交
162 163 164 165 166 167 168 169 170 171 172 173
  KeCMRNormDiff<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(imageSize,
                                                            inputsValue,
                                                            outputsValue,
                                                            denoms,
                                                            outputsGrad,
                                                            channels,
                                                            height,
                                                            width,
                                                            size,
                                                            -pow,
                                                            2.0f * pow * scale,
                                                            inputsGrad);
H
hedaoyuan 已提交
174
  CHECK_SYNC("CrossMapNormalGrad");
H
hedaoyuan 已提交
175 176 177
}

}  // namespace paddle