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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <algorithm>
namespace cudnn_frontend {
namespace graph {
class ConvolutionNode;
}
} // namespace cudnn_frontend
namespace cudnn_frontend {
namespace graph {
class DgradNode;
}
} // namespace cudnn_frontend
namespace cudnn_frontend {
namespace graph {
class WgradNode;
}
} // namespace cudnn_frontend
#include <array>
#include <functional>
#include <memory>
#include <sstream>
#include <utility>
#include "cudnn_frontend_utils.h"
namespace cudnn_frontend {
///
/// Convolution Descriptor Class
/// This class tells the properties of the Convolution operation
/// Properties:
/// - padLower
/// - padUpper
/// - Dilation
/// - Stride
/// - Math Operation Data Type
/// - Convolution Mode
/// - Convolution spatial dimensions
///
/// Use ConvDescBuilder_v8 to build this class.
/// Describe returns a string describing the convolution operation
///
class ConvDesc_v8 : public BackendDescriptor {
public:
friend class ConvDescBuilder_v8;
friend class cudnn_frontend::graph::ConvolutionNode;
friend class cudnn_frontend::graph::DgradNode;
friend class cudnn_frontend::graph::WgradNode;
std::string
describe() const override {
std::stringstream ss;
char sep = ' ';
#ifndef CUDNN_FRONTEND_SKIP_JSON_LIB
ss << "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR :" << " Datatype: " << json{compute_type} << " Mode: " << json{mode}
#else
ss << "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR :" << " Datatype: " << int(compute_type) << " Mode: " << int(mode)
#endif
<< " Num Dimensions: " << nDims;
ss << " PadLower [";
for (auto i = 0; i < nDims; i++) {
ss << sep << padLower[i];
sep = ',';
}
ss << " ] PadUpper [";
for (auto i = 0; i < nDims; i++) {
ss << sep << padUpper[i];
sep = ',';
}
ss << " ] Dilation [";
for (auto i = 0; i < nDims; i++) {
ss << sep << dilation[i];
sep = ',';
}
ss << " ] Stride [";
for (auto i = 0; i < nDims; i++) {
ss << sep << stride[i];
sep = ',';
}
ss << "]";
return ss.str();
}
ConvDesc_v8(ConvDesc_v8 &&from) = default;
ConvDesc_v8 &
operator=(ConvDesc_v8 &&) = default;
~ConvDesc_v8() = default;
// TODO: Deprecate in v1.0
DataType_t
getComputePrecision() const {
return compute_type;
}
DataType_t
getComputeType() const {
return compute_type;
}
int64_t
getSpatialDimCount() const {
return nDims;
}
int64_t const *
getPadding() const {
return padLower;
}
int64_t const *
getSpatialStride() const {
return stride;
}
int64_t const *
getDilation() const {
return dilation;
}
cudnnConvolutionMode_t
getMathMode() const {
return detail::convert_to_cudnn_type(mode);
}
// TODO: Deprecate in v1.0
int64_t
getDimensionCount() const {
return getSpatialDimCount();
}
// TODO: Deprecate in v1.0
int64_t const *
getStride() const {
return getSpatialStride();
}
private:
ConvDesc_v8() = default;
ConvDesc_v8(ConvDesc_v8 const &) = delete;
ConvDesc_v8 &
operator=(ConvDesc_v8 const &) = delete;
DataType_t compute_type = DataType_t::FLOAT; //! Convolution operation data type
ConvolutionMode_t mode = ConvolutionMode_t::CONVOLUTION; //! Convolution vs cross correlation
int64_t nDims = -1; //! number of dimensions
int64_t padLower[CUDNN_DIM_MAX + 1] = {0}; //! d, h, w
int64_t padUpper[CUDNN_DIM_MAX + 1] = {0}; //! d, h, w
int64_t dilation[CUDNN_DIM_MAX + 1] = {0}; //! d, h, w
int64_t stride[CUDNN_DIM_MAX + 1] = {-1}; //! d, h, w
};
///
/// ConvDescBuilder_v8 Class
/// Helper class used to build ConvDesc_v8 class
class ConvDescBuilder_v8 {
public:
/** @defgroup ConvDescBuilder_v8
* Set individual property of ConvDesc_v8 class
* @{
*/
//! Set Datatype for the Convolution Operation
auto
setComputeType(DataType_t data_type) -> ConvDescBuilder_v8 & {
m_convDesc.compute_type = data_type;
return *this;
}
// To be deprecated in v1.0.
auto
setComputeType(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 & {
m_convDesc.compute_type = detail::convert_from_cudnn_type(data_type_);
return *this;
}
//! Set Padding Lower of the convDesc
auto
setPrePadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 & {
std::copy(padding, padding + ndims, m_convDesc.padLower);
return *this;
}
//! Set Padding Upper of the convDesc
auto
setPostPadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 & {
std::copy(padding, padding + ndims, m_convDesc.padUpper);
return *this;
}
//! Set Dilation of the convDesc
auto
setDilation(int64_t ndims, int64_t const *dilation) -> ConvDescBuilder_v8 & {
std::copy(dilation, dilation + ndims, m_convDesc.dilation);
return *this;
}
//! Set Strides of the convDesc
auto
setSpatialStride(int64_t ndims, int64_t const *strides) -> ConvDescBuilder_v8 & {
std::copy(strides, strides + ndims, m_convDesc.stride);
return *this;
}
//! Set Num Spatial Dimensions of the convolution Operation
auto
setSpatialDimCount(int64_t nDims_) -> ConvDescBuilder_v8 & {
m_convDesc.nDims = nDims_;
return *this;
}
//! Set Convolution Mode of the convolution Operation
auto
setMathMode(ConvolutionMode_t mode_) -> ConvDescBuilder_v8 & {
m_convDesc.mode = mode_;
return *this;
}
auto
setMathMode(cudnnConvolutionMode_t mode_) -> ConvDescBuilder_v8 & {
m_convDesc.mode = detail::convert_from_cudnn_type(mode_);
return *this;
}
/** @} */
// TODO: Deprecate in v1.0
auto
setNDims(int64_t nDims_) -> ConvDescBuilder_v8 & {
return setSpatialDimCount(nDims_);
}
// TODO: Deprecate in v1.0
auto
setDataType(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 & {
return setComputeType(data_type_);
}
// TODO: Deprecate in v1.0
auto
setComputePrecision(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 & {
return setComputeType(data_type_);
}
// TODO: Deprecate in v1.0
auto
setStrides(int64_t ndims, int64_t const *strides) -> ConvDescBuilder_v8 & {
return setSpatialStride(ndims, strides);
}
//! constructs the ConvDesc_v8 by calling the cudnn API
//! Throws the appropriate error message
ConvDesc_v8 &&
build() {
// Sanity check if non-default fields have been set correctly.
if (m_convDesc.nDims <= 0) {
set_error_and_throw_exception(
&m_convDesc,
CUDNN_STATUS_BAD_PARAM,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Check and Set the CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS field");
return std::move(m_convDesc);
};
if (m_convDesc.stride[0] <= 0) {
set_error_and_throw_exception(
&m_convDesc,
CUDNN_STATUS_BAD_PARAM,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Check and Set the CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES field");
return std::move(m_convDesc);
}
// Create a descriptor. Memory allocation happens here.
auto status = m_convDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Bad descriptor created");
return std::move(m_convDesc);
}
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_convDesc);
}
// Once Created lets set the descriptor parameters.
cudnnDataType_t cudnn_data_type;
status = detail::convert_to_cudnn_type(m_convDesc.compute_type, cudnn_data_type);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_COMP_TYPE Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_COMP_TYPE,
CUDNN_TYPE_DATA_TYPE,
1,
&cudnn_data_type);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_COMP_TYPE Failed");
return std::move(m_convDesc);
}
cudnnConvolutionMode_t mode_ = detail::convert_to_cudnn_type(m_convDesc.mode);
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_CONV_MODE,
CUDNN_TYPE_CONVOLUTION_MODE,
1,
&mode_);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_CONV_MODE Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS,
CUDNN_TYPE_INT64,
1,
&m_convDesc.nDims);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_PRE_PADDINGS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.padLower);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_PRE_PADDINGS Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_POST_PADDINGS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.padUpper);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_POST_PADDINGS Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_DILATIONS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.dilation);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_DILATIONS Failed");
return std::move(m_convDesc);
}
status = detail::set_attribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.stride);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES Failed");
return std::move(m_convDesc);
}
// Finalizing the descriptor
status = detail::finalize(m_convDesc.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_convDesc);
}
CUDNN_FE_LOG_LABEL_ENDL(m_convDesc)
return std::move(m_convDesc);
}
explicit ConvDescBuilder_v8() = default;
~ConvDescBuilder_v8() = default;
ConvDescBuilder_v8(ConvDescBuilder_v8 &&) = delete;
ConvDescBuilder_v8(ConvDescBuilder_v8 const &) = delete;
ConvDescBuilder_v8 &
operator=(ConvDescBuilder_v8 const &) = delete;
private:
ConvDesc_v8 m_convDesc;
};
using ConvDesc = ConvDesc_v8;
using ConvDescBuilder = ConvDescBuilder_v8;
} // namespace cudnn_frontend
|