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
|
#include "caffe2/core/context_gpu.h"
#include "caffe2/core/cudnn_wrappers.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/types.h"
namespace caffe2 {
class CuDNNLRNOp final : public Operator<CUDAContext> {
public:
USE_OPERATOR_FUNCTIONS(CUDAContext);
template <class... Args>
explicit CuDNNLRNOp(Args&&... args)
: Operator<CUDAContext>(std::forward<Args>(args)...),
cudnn_wrapper_(&context_),
size_(OperatorBase::GetSingleArgument<int>("size", 0)),
alpha_(OperatorBase::GetSingleArgument<float>("alpha", 0)),
beta_(OperatorBase::GetSingleArgument<float>("beta", 0)),
bias_(OperatorBase::GetSingleArgument<float>("bias", 1)) {
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&data_desc_));
CUDNN_ENFORCE(cudnnCreateLRNDescriptor(&norm_desc_));
CUDNN_ENFORCE(
cudnnSetLRNDescriptor(norm_desc_, size_, alpha_, beta_, bias_));
}
~CuDNNLRNOp() override {
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(data_desc_));
CUDNN_ENFORCE(cudnnDestroyLRNDescriptor(norm_desc_));
}
template <typename T, typename M>
bool DoRunWithType();
bool RunOnDevice() override;
protected:
CuDNNWrapper cudnn_wrapper_;
cudnnTensorDescriptor_t data_desc_;
cudnnLRNDescriptor_t norm_desc_;
vector<int64_t> cudnn_input_dims_;
const int size_;
const float alpha_;
const float beta_;
const float bias_;
// Input: X, Output: Y
};
class CuDNNLRNGradientOp final : public Operator<CUDAContext> {
public:
USE_OPERATOR_FUNCTIONS(CUDAContext);
template <class... Args>
explicit CuDNNLRNGradientOp(Args&&... args)
: Operator<CUDAContext>(std::forward<Args>(args)...),
cudnn_wrapper_(&context_),
size_(OperatorBase::GetSingleArgument<int>("size", 0)),
alpha_(OperatorBase::GetSingleArgument<float>("alpha", 0)),
beta_(OperatorBase::GetSingleArgument<float>("beta", 0)),
bias_(OperatorBase::GetSingleArgument<float>("bias", 1)) {
CUDNN_ENFORCE(cudnnCreateTensorDescriptor(&data_desc_));
CUDNN_ENFORCE(cudnnCreateLRNDescriptor(&norm_desc_));
CUDNN_ENFORCE(
cudnnSetLRNDescriptor(norm_desc_, size_, alpha_, beta_, bias_));
}
~CuDNNLRNGradientOp() override {
CUDNN_ENFORCE(cudnnDestroyTensorDescriptor(data_desc_));
CUDNN_ENFORCE(cudnnDestroyLRNDescriptor(norm_desc_));
}
template <typename T, typename M>
bool DoRunWithType();
bool RunOnDevice() override;
protected:
CuDNNWrapper cudnn_wrapper_;
cudnnTensorDescriptor_t data_desc_;
cudnnLRNDescriptor_t norm_desc_;
vector<int64_t> cudnn_input_dims_;
const int size_;
const float alpha_;
const float beta_;
const float bias_;
// Input: X, Y, dY
// Output: dX
};
template <typename T, typename M>
bool CuDNNLRNOp::DoRunWithType() {
const auto& X = Input(0);
auto* Y = Output(0);
// Reshape tensor descriptors if necessary
if (X.sizes() != cudnn_input_dims_) {
VLOG(1) << "Setting descriptors";
cudnn_input_dims_ = X.sizes().vec();
int C = 1, H = 1, W = 1;
// Normal 4-dimensional tensors for images.
C = X.dim32(1);
H = X.dim32(2);
W = X.dim32(3);
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
data_desc_,
GetCudnnTensorFormat(StorageOrder::NCHW),
cudnnTypeWrapper<T>::type,
X.dim32(0),
C,
H,
W));
}
// now actually run the computation
CUDNN_ENFORCE(cudnnLRNCrossChannelForward(
cudnn_wrapper_.inline_cudnn_handle(),
norm_desc_,
CUDNN_LRN_CROSS_CHANNEL_DIM1,
cudnnTypeWrapper<T>::kOne(),
data_desc_,
X.template data<T>(),
cudnnTypeWrapper<T>::kZero(),
data_desc_,
Y->template mutable_data<T>()));
return true;
}
bool CuDNNLRNOp::RunOnDevice() {
// dispatch based on contents of tensor(s)
const auto& X = Input(0);
auto* Y = Output(0);
Y->ResizeLike(X);
if (X.IsType<float>()) {
return DoRunWithType<float, float>();
} else if (X.IsType<at::Half>()) {
return DoRunWithType<at::Half, float>();
} else {
CAFFE_THROW("Unsupported input type");
}
return false;
}
template <typename T, typename M>
bool CuDNNLRNGradientOp::DoRunWithType() {
const auto& X = Input(0);
const auto& Y = Input(1);
const auto& dY = Input(2);
auto* dX = Output(0);
if (dY.sizes() != cudnn_input_dims_) {
VLOG(1) << "Setting descriptors";
cudnn_input_dims_ = dY.sizes().vec();
int C = 1, H = 1, W = 1;
// Normal 4-dimensional tensors for images.
C = dY.dim32(1);
H = dY.dim32(2);
W = dY.dim32(3);
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
data_desc_,
GetCudnnTensorFormat(StorageOrder::NCHW),
cudnnTypeWrapper<T>::type,
dY.dim32(0),
C,
H,
W));
}
// run the computation
CUDNN_ENFORCE(cudnnLRNCrossChannelBackward(
cudnn_wrapper_.inline_cudnn_handle(),
norm_desc_,
CUDNN_LRN_CROSS_CHANNEL_DIM1,
cudnnTypeWrapper<T>::kOne(),
data_desc_,
Y.template data<T>(),
data_desc_,
dY.template data<T>(),
data_desc_,
X.template data<T>(),
cudnnTypeWrapper<T>::kZero(),
data_desc_,
dX->template mutable_data<T>()));
return true;
}
bool CuDNNLRNGradientOp::RunOnDevice() {
// dispatch based on contents of tensor(s)
const auto& dY = Input(2);
auto* dX = Output(0);
dX->ResizeLike(dY);
if (dY.IsType<float>()) {
return DoRunWithType<float, float>();
} else if (dY.IsType<at::Half>()) {
return DoRunWithType<at::Half, float>();
} else {
CAFFE_THROW("Unsupported input type");
}
return false;
}
namespace {
REGISTER_CUDNN_OPERATOR(LRN, CuDNNLRNOp);
REGISTER_CUDNN_OPERATOR(LRNGradient, CuDNNLRNGradientOp);
}
}; // namespace caffe2
|