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 88 89 90 91 92 93 94 95 96 97 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 139 140 141 142 143 144 145 146 147 148 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
#include <cub/block/block_reduce.cuh>
#include "caffe2/core/context_gpu.h"
#include "caffe2/operators/reduce_front_back_sum_mean_ops.h"
#include "caffe2/utils/cub_namespace.cuh"
namespace caffe2 {
namespace {
template <typename T, bool NORMALIZE>
__global__ void columnwise_fill_kernel(
const int rows,
const int cols,
const T* dY,
const int* lengths,
T* dX) {
CUDA_1D_KERNEL_LOOP(i, rows * cols) {
int row = i / cols;
int col = i % cols;
if (lengths == nullptr) {
dX[i] = NORMALIZE ? dY[col] / rows : dY[col];
} else if (row < lengths[col]) {
dX[i] = NORMALIZE ? dY[col] / lengths[col] : dY[col];
} else {
dX[i] = 0;
}
}
}
template <typename T, bool NORMALIZE>
__global__ void rowwise_fill_kernel(
const int rows,
const int cols,
const T* dY,
const int* lengths,
T* dX) {
CUDA_1D_KERNEL_LOOP(i, rows * cols) {
int row = i / cols;
int col = i % cols;
if (lengths == nullptr) {
dX[i] = NORMALIZE ? dY[row] / cols : dY[row];
} else if (col < lengths[row]) {
dX[i] = NORMALIZE ? dY[row] / lengths[row] : dY[row];
} else {
dX[i] = 0;
}
}
}
template <typename T, bool NORMALIZE>
__global__ void rowwise_sum_kernel(
const int rows,
const int cols,
const T* data,
const int* lengths,
T* out) {
typedef cub::BlockReduce<float, CAFFE_CUDA_NUM_THREADS> BlockReduce;
__shared__ typename BlockReduce::TempStorage temp_storage;
for (int rowIndex = blockIdx.x; rowIndex < rows; rowIndex += gridDim.x) {
T sum = 0;
const int rowOffset = rowIndex * cols;
const int length = lengths == nullptr ? cols : lengths[rowIndex];
for (int colIndex = threadIdx.x; colIndex < length;
colIndex += blockDim.x) {
sum += data[rowOffset + colIndex];
}
sum = BlockReduce(temp_storage).Reduce(sum, cub::Sum());
if (threadIdx.x == 0) {
out[rowIndex] = NORMALIZE ? sum / length : sum;
}
__syncthreads();
}
}
template <typename T, bool NORMALIZE>
__global__ void columnwise_sum_kernel(
const int rows,
const int cols,
const T* data,
const int* lengths,
T* out) {
typedef cub::BlockReduce<float, CAFFE_CUDA_NUM_THREADS> BlockReduce;
__shared__ typename BlockReduce::TempStorage temp_storage;
for (int colIndex = blockIdx.x; colIndex < cols; colIndex += gridDim.x) {
T sum = 0;
const int length = lengths == nullptr ? rows : lengths[colIndex];
for (int rowIndex = threadIdx.x; rowIndex < length;
rowIndex += blockDim.x) {
sum += data[rowIndex * cols + colIndex];
}
sum = BlockReduce(temp_storage).Reduce(sum, cub::Sum());
if (threadIdx.x == 0) {
out[colIndex] = NORMALIZE ? sum / length : sum;
}
__syncthreads();
}
}
} // anonymous namespace
/***
Sum Ops
***/
// ReduceFrontSum: columnwise sum
template <>
template <typename T>
void SumReduceDimsOp<CUDAContext, true, false>::Compute(
int rows,
int cols,
const T* in_data,
const int* lengths_data,
T* out_data) {
columnwise_sum_kernel<T, false>
<<<std::min(cols, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, in_data, lengths_data, out_data);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceBackSum: rowwise sum
template <>
template <typename T>
void SumReduceDimsOp<CUDAContext, false, false>::Compute(
int rows,
int cols,
const T* in_data,
const int* lengths_data,
T* out_data) {
rowwise_sum_kernel<T, false>
<<<std::min(rows, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, in_data, lengths_data, out_data);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceFrontSumGradient
template <>
template <typename T>
void SumReduceDimsGradientOp<CUDAContext, true, false>::Compute(
int rows,
int cols,
const T* dYdata,
const int* lengths_data,
T* dXdata) {
columnwise_fill_kernel<T, false>
<<<CAFFE_GET_BLOCKS(rows * cols),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, dYdata, lengths_data, dXdata);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceBackSumGradient
template <>
template <typename T>
void SumReduceDimsGradientOp<CUDAContext, false, false>::Compute(
int rows,
int cols,
const T* dYdata,
const int* lengths_data,
T* dXdata) {
rowwise_fill_kernel<T, false>
<<<CAFFE_GET_BLOCKS(rows * cols),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, dYdata, lengths_data, dXdata);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
REGISTER_CUDA_OPERATOR(
ReduceFrontSum,
SumReduceDimsOp<CUDAContext, true, false>);
REGISTER_CUDA_OPERATOR(
ReduceFrontSumGradient,
SumReduceDimsGradientOp<CUDAContext, true, false>);
REGISTER_CUDA_OPERATOR(
ReduceBackSum,
SumReduceDimsOp<CUDAContext, false, false>);
REGISTER_CUDA_OPERATOR(
ReduceBackSumGradient,
SumReduceDimsGradientOp<CUDAContext, false, false>);
/***
Mean Ops
***/
// ReduceFrontMean: columnwise mean
template <>
template <typename T>
void SumReduceDimsOp<CUDAContext, true, true>::Compute(
int rows,
int cols,
const T* in_data,
const int* lengths_data,
T* out_data) {
columnwise_sum_kernel<T, true>
<<<std::min(cols, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, in_data, lengths_data, out_data);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceBackMean: rowwise mean
template <>
template <typename T>
void SumReduceDimsOp<CUDAContext, false, true>::Compute(
int rows,
int cols,
const T* in_data,
const int* lengths_data,
T* out_data) {
rowwise_sum_kernel<T, true>
<<<std::min(rows, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, in_data, lengths_data, out_data);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceFrontMeanGradient
template <>
template <typename T>
void SumReduceDimsGradientOp<CUDAContext, true, true>::Compute(
int rows,
int cols,
const T* dYdata,
const int* lengths_data,
T* dXdata) {
columnwise_fill_kernel<T, true>
<<<CAFFE_GET_BLOCKS(rows * cols),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, dYdata, lengths_data, dXdata);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
// ReduceBackMeanGradient
template <>
template <typename T>
void SumReduceDimsGradientOp<CUDAContext, false, true>::Compute(
int rows,
int cols,
const T* dYdata,
const int* lengths_data,
T* dXdata) {
rowwise_fill_kernel<T, true>
<<<CAFFE_GET_BLOCKS(rows * cols),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(rows, cols, dYdata, lengths_data, dXdata);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
REGISTER_CUDA_OPERATOR(
ReduceFrontMean,
SumReduceDimsOp<CUDAContext, true, true>);
REGISTER_CUDA_OPERATOR(
ReduceFrontMeanGradient,
SumReduceDimsGradientOp<CUDAContext, true, true>);
REGISTER_CUDA_OPERATOR(
ReduceBackMean,
SumReduceDimsOp<CUDAContext, false, true>);
REGISTER_CUDA_OPERATOR(
ReduceBackMeanGradient,
SumReduceDimsGradientOp<CUDAContext, false, true>);
} // namespace caffe2
|