cudnn.hpp
10.7 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_DNN_CUDA4DNN_CSL_CUDNN_CUDNN_HPP
#define OPENCV_DNN_CUDA4DNN_CSL_CUDNN_CUDNN_HPP
#include "../pointer.hpp"
#include <cudnn.h>
#include <cstddef>
#include <array>
#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>
#include <type_traits>
#include <iterator>
#define CUDA4DNN_CHECK_CUDNN(call) \
::cv::dnn::cuda4dnn::csl::cudnn::detail::check((call), CV_Func, __FILE__, __LINE__)
namespace cv { namespace dnn { namespace cuda4dnn { namespace csl { namespace cudnn {
/** @brief exception class for errors thrown by the cuDNN API */
class cuDNNException : public CUDAException {
public:
cuDNNException(cudnnStatus_t code, const std::string& msg, const std::string& func, const std::string& file, int line)
: CUDAException(Error::GpuApiCallError, msg, func, file, line), cudnnError{code}
{
}
cudnnStatus_t getCUDNNStatus() const noexcept { return cudnnError; }
private:
cudnnStatus_t cudnnError;
};
namespace detail {
inline void check(cudnnStatus_t status, const char* func, const char* file, int line) {
if (status != CUDNN_STATUS_SUCCESS)
throw cuDNNException(status, cudnnGetErrorString(status), func, file, line);
}
/** get_data_type<T> returns the equivalent cudnn enumeration constant for type T */
using cudnn_data_enum_type = decltype(CUDNN_DATA_FLOAT);
template <class> cudnn_data_enum_type get_data_type();
template <> inline cudnn_data_enum_type get_data_type<half>() { return CUDNN_DATA_HALF; }
template <> inline cudnn_data_enum_type get_data_type<float>() { return CUDNN_DATA_FLOAT; }
}
/** @brief noncopyable cuDNN smart handle
*
* UniqueHandle is a smart non-sharable wrapper for cuDNN handle which ensures that the handle
* is destroyed after use.
*/
class UniqueHandle {
public:
UniqueHandle() noexcept : handle{ nullptr } { }
UniqueHandle(UniqueHandle&) = delete;
UniqueHandle(UniqueHandle&& other) noexcept {
stream = std::move(other.stream);
handle = other.handle;
other.handle = nullptr;
}
/** creates a cuDNN handle and associates it with the stream specified
*
* Exception Guarantee: Basic
*/
UniqueHandle(Stream strm) : stream(std::move(strm)) {
CV_Assert(stream);
CUDA4DNN_CHECK_CUDNN(cudnnCreate(&handle));
try {
CUDA4DNN_CHECK_CUDNN(cudnnSetStream(handle, stream.get()));
} catch (...) {
/* cudnnDestroy won't throw if a valid handle is passed */
CUDA4DNN_CHECK_CUDNN(cudnnDestroy(handle));
throw;
}
}
~UniqueHandle() noexcept {
if (handle != nullptr) {
/* cudnnDestroy won't throw if a valid handle is passed */
CUDA4DNN_CHECK_CUDNN(cudnnDestroy(handle));
}
}
UniqueHandle& operator=(const UniqueHandle&) = delete;
UniqueHandle& operator=(UniqueHandle&& other) noexcept {
CV_Assert(other);
if (&other != this) {
UniqueHandle(std::move(*this)); /* destroy current handle */
stream = std::move(other.stream);
handle = other.handle;
other.handle = nullptr;
}
return *this;
}
/** returns the raw cuDNN handle */
cudnnHandle_t get() const noexcept {
CV_Assert(handle);
return handle;
}
/** returns true if the handle is valid */
explicit operator bool() const noexcept { return static_cast<bool>(handle); }
private:
Stream stream;
cudnnHandle_t handle;
};
/** @brief sharable cuDNN smart handle
*
* Handle is a smart sharable wrapper for cuDNN handle which ensures that the handle
* is destroyed after all references to the handle are destroyed. The handle must always
* be associated with a non-default stream. The stream must be specified during construction.
*
* @note Moving a Handle object to another invalidates the former
*/
class Handle {
public:
Handle() = default;
Handle(const Handle&) = default;
Handle(Handle&&) = default;
/** creates a cuDNN handle and associates it with the stream specified
*
* Exception Guarantee: Basic
*/
Handle(Stream strm) : handle(std::make_shared<UniqueHandle>(std::move(strm))) { }
Handle& operator=(const Handle&) = default;
Handle& operator=(Handle&&) = default;
/** returns true if the handle is valid */
explicit operator bool() const noexcept { return static_cast<bool>(handle); }
/** returns the raw cuDNN handle */
cudnnHandle_t get() const noexcept {
CV_Assert(handle);
return handle->get();
}
private:
std::shared_ptr<UniqueHandle> handle;
};
/** describe a tensor
*
* @tparam T type of elements in the tensor
*/
template <class T>
class TensorDescriptor {
public:
TensorDescriptor() noexcept : descriptor{ nullptr } { }
TensorDescriptor(const TensorDescriptor&) = delete;
TensorDescriptor(TensorDescriptor&& other) noexcept
: descriptor{ other.descriptor } {
other.descriptor = nullptr;
}
/** constructs a tensor descriptor from the axis lengths provided in \p shape
*
* Exception Guarantee: Basic
*/
template <class SequenceContainer, typename = decltype(std::begin(std::declval<SequenceContainer>()))>
TensorDescriptor(const SequenceContainer& shape) {
constructor(shape.begin(), shape.end());
}
/** constructs a tensor descriptor from the axis lengths provided in [begin, end)
*
* Exception Guarantee: Basic
*/
template <class ForwardItr, typename = typename std::enable_if<!std::is_integral<ForwardItr>::value, void>::type> // TODO is_iterator
TensorDescriptor(ForwardItr begin, ForwardItr end) {
constructor(begin, end);
}
/** constructs a tensor descriptor from the axis lengths provided as arguments
*
* Exception Guarantee: Basic
*/
template <class ...Sizes>
TensorDescriptor(Sizes ...sizes) {
static_assert(sizeof...(Sizes) <= CUDNN_DIM_MAX, "required rank exceeds maximum supported rank");
std::array<int, sizeof...(Sizes)> dims = { static_cast<int>(sizes)... };
constructor(std::begin(dims), std::end(dims));
}
~TensorDescriptor() noexcept {
if (descriptor != nullptr) {
/* cudnnDestroyTensorDescriptor will not fail */
CUDA4DNN_CHECK_CUDNN(cudnnDestroyTensorDescriptor(descriptor));
}
}
TensorDescriptor& operator=(const TensorDescriptor&) = delete;
TensorDescriptor& operator=(TensorDescriptor&& other) noexcept {
descriptor = other.descriptor;
other.descriptor = nullptr;
return *this;
};
cudnnTensorDescriptor_t get() const noexcept { return descriptor; }
private:
template <class ForwardItr>
void constructor(ForwardItr start, ForwardItr end) {
CV_Assert(start != end);
CV_Assert(std::distance(start, end) <= CUDNN_DIM_MAX);
CUDA4DNN_CHECK_CUDNN(cudnnCreateTensorDescriptor(&descriptor));
try {
/* cuDNN documentation recommends using the 4d tensor API whenever possible
* hence, we create a 4d tensor descriptors for 3d tensor
*/
const auto rank = std::distance(start, end);
if (rank <= 4) {
std::array<int, 4> dims;
std::fill(std::begin(dims), std::end(dims), 1);
/* suppose we have a 3d tensor, the first axis is the batch axis and
* the second axis is the channel axis (generally)
*
* cuDNN frequently assumes that the first axis is the batch axis and the
* second axis is the channel axis; hence, we copy the shape of a lower rank
* tensor to the beginning of `dims`
*/
std::copy(start, end, std::begin(dims));
CUDA4DNN_CHECK_CUDNN(
cudnnSetTensor4dDescriptor(descriptor,
CUDNN_TENSOR_NCHW, detail::get_data_type<T>(),
dims[0], dims[1], dims[2], dims[3]
)
);
} else {
std::vector<int> stride(rank);
stride.back() = 1;
/* WHAT WE HAVE NOW:
* stride[-1] = 1
* stride[-2] = garbage
* stride[-3] = garbage
* stride[-4] = garbage
* ...
*/
std::copy(start + 1, end, stride.begin());
/* WHAT WE HAVE NOW:
* stride[-1] = 1
* stride[-2] = dim[-1]
* stride[-3] = dim[-2]
* stride[-4] = dim[-3]
* ...
*/
std::partial_sum(stride.rbegin(), stride.rend(), stride.rbegin(), std::multiplies<int>());
/* WHAT WE HAVE NOW:
* stride[-1] = 1
* stride[-2] = stride[-1] * dim[-1]
* stride[-3] = stride[-2] * dim[-2]
* stride[-4] = stride[-3] * dim[-3]
* ...
*/
std::vector<int> dims(start, end);
CUDA4DNN_CHECK_CUDNN(
cudnnSetTensorNdDescriptor(descriptor,
detail::get_data_type<T>(), rank,
dims.data(), stride.data()
)
);
}
} catch (...) {
/* cudnnDestroyTensorDescriptor will not fail */
CUDA4DNN_CHECK_CUDNN(cudnnDestroyTensorDescriptor(descriptor));
throw;
}
}
cudnnTensorDescriptor_t descriptor;
};
}}}}} /* namespace cv::dnn::cuda4dnn::csl::cudnn */
#endif /* OPENCV_DNN_CUDA4DNN_CSL_CUDNN_HPP */