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
|
#include "caffe2/operators/arg_ops.h"
#include <limits>
#include "caffe2/utils/cub_namespace.cuh"
#include <cub/block/block_reduce.cuh>
#include "caffe2/core/common_gpu.h"
#include "caffe2/core/context_gpu.h"
#include "caffe2/utils/fixed_divisor.h"
namespace caffe2 {
namespace {
template <typename K, typename V>
using KeyValuePair = cub::KeyValuePair<K, V>;
template <typename K, typename V>
using BlockReduce =
cub::BlockReduce<KeyValuePair<K, V>, CAFFE_CUDA_NUM_THREADS>;
template <typename T, class Reducer>
__global__ void ComputeArgCUDAKernel(
const int outer_size,
const int inner_size,
const FixedDivisor<int> stride,
const Reducer reducer,
const T init,
const T* X,
int64_t* Y) {
__shared__ typename BlockReduce<int, T>::TempStorage temp_storage;
const int d = stride.d();
for (int idx = blockIdx.x; idx < outer_size; idx += gridDim.x) {
int i;
int j;
stride.DivMod(idx, &i, &j);
KeyValuePair<int, T> kv = {-1, init};
for (int k = threadIdx.x; k < inner_size; k += blockDim.x) {
kv = reducer({k, X[i * inner_size * d + k * d + j]}, kv);
}
kv = BlockReduce<int, T>(temp_storage).Reduce(kv, reducer);
if (threadIdx.x == 0) {
Y[idx] = static_cast<int64_t>(kv.key);
}
__syncthreads();
}
}
} // namespace
template <>
template <typename T>
bool ArgMaxReducer<CUDAContext>::operator()(
const int prev_size,
const int next_size,
const int n,
const T* X,
int64_t* Y,
CUDAContext* context) const {
const int outer_size = prev_size * next_size;
const FixedDivisor<int> stride(next_size);
ComputeArgCUDAKernel<<<
std::min(outer_size, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context->cuda_stream()>>>(
outer_size,
n,
stride,
cub::ArgMax(),
std::numeric_limits<T>::lowest(),
X,
Y);
C10_CUDA_KERNEL_LAUNCH_CHECK();
return true;
}
template <>
template <typename T>
bool ArgMinReducer<CUDAContext>::operator()(
const int prev_size,
const int next_size,
const int n,
const T* X,
int64_t* Y,
CUDAContext* context) const {
const int outer_size = prev_size * next_size;
const FixedDivisor<int> stride(next_size);
ComputeArgCUDAKernel<<<
std::min(outer_size, CAFFE_MAXIMUM_NUM_BLOCKS),
CAFFE_CUDA_NUM_THREADS,
0,
context->cuda_stream()>>>(
outer_size,
n,
stride,
cub::ArgMin(),
std::numeric_limits<T>::max(),
X,
Y);
C10_CUDA_KERNEL_LAUNCH_CHECK();
return true;
}
REGISTER_CUDA_OPERATOR(ArgMax, ArgOp<CUDAContext, ArgMaxReducer<CUDAContext>>);
REGISTER_CUDA_OPERATOR(ArgMin, ArgOp<CUDAContext, ArgMinReducer<CUDAContext>>);
} // namespace caffe2
|