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
|
/*
* 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 PointwiseNode;
}
} // namespace cudnn_frontend
#include <array>
#include <functional>
#include <memory>
#include <sstream>
#include <utility>
#include <limits>
#include "cudnn_frontend_utils.h"
namespace cudnn_frontend {
///
/// PointWiseDesc Descriptor Class
/// This class tells the properties of the PointWise operation
/// Properties:
/// - compute_type
/// - mode
/// - nan_propagation
/// - upper_clip
/// - lower_clip
/// - lower_clip_slope
/// - elu_alpha
/// - softplus_beta
/// - swish_beta
///
/// Use PointWiseDesc_v8 to build this class.
/// Describe returns a string describing the PointWise operation
///
class PointWiseDesc_v8 : public BackendDescriptor {
public:
friend class PointWiseDescBuilder_v8;
friend class cudnn_frontend::graph::PointwiseNode;
std::string
describe() const override {
std::stringstream ss;
#ifndef CUDNN_FRONTEND_SKIP_JSON_LIB
ss << "CUDNN_BACKEND_POINTWISE_DESCRIPTOR :" << " Mode: " << json{mode} << " Math precision "
<< json{compute_type};
#else
ss << "CUDNN_BACKEND_POINTWISE_DESCRIPTOR :" << " Mode: " << int(mode) << " Math precision "
<< int(compute_type);
#endif
return ss.str();
}
int64_t
getPortCount() const {
return get_pointwise_mode_port_count(mode);
}
PointwiseMode_t
getPointWiseMode() const {
return mode;
}
PointWiseDesc_v8(PointWiseDesc_v8 &&from) = default;
PointWiseDesc_v8 &
operator=(PointWiseDesc_v8 &&from) = default;
~PointWiseDesc_v8() = default;
private:
PointWiseDesc_v8() = default;
PointWiseDesc_v8(PointWiseDesc_v8 const &) = delete;
PointWiseDesc_v8 &
operator=(PointWiseDesc_v8 const &) = delete;
DataType_t compute_type = DataType_t::FLOAT;
PointwiseMode_t mode = PointwiseMode_t::NOT_SET;
cudnnNanPropagation_t nan_propagation = CUDNN_PROPAGATE_NAN;
double upper_clip = std::numeric_limits<double>::max();
double lower_clip = 0.0;
double lower_clip_slope = 0.0;
double elu_alpha = 1.0;
double softplus_beta = 1.0;
double swish_beta = 1.0;
int64_t axis = -1;
};
////
/// PointWiseDescBuilder_v8 Class
/// Helper class used to build PointWiseDesc_v8 class
class PointWiseDescBuilder_v8 {
public:
/** @defgroup PointWiseDescBuilder_v8
* Set individual property of PointWiseDesc_v8 class
* @{
*/
//! Set Math Precision Data Type for the Convolution Operation
auto
setComputeType(DataType_t data_type_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.compute_type = data_type_;
return *this;
}
auto
setComputeType(cudnnDataType_t data_type_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.compute_type = detail::convert_from_cudnn_type(data_type_);
return *this;
}
//! Set upper and lower limits for the RELU activation
auto
setClipping(double l, double u) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.upper_clip = u;
m_pointWiseDesc.lower_clip = l;
return *this;
}
//! Set pointwise mode for the activation
auto
setMode(PointwiseMode_t mode) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.mode = mode;
return *this;
}
//! To be deprecated. Please use setMode(PointwiseMode_t).
auto
setMode(cudnnPointwiseMode_t mode) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.mode = detail::convert_from_cudnn_type(mode);
return *this;
}
//! Set NaN propagation mode
auto
setMode(cudnnNanPropagation_t nan_mode_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.nan_propagation = nan_mode_;
return *this;
}
/** @} */
// TODO Deprecate in v1.0
auto
setMathPrecision(cudnnDataType_t data_type_) -> PointWiseDescBuilder_v8 & {
return setComputeType(data_type_);
}
auto
setReluLowerClip(double lower_clip_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.lower_clip = lower_clip_;
return *this;
}
auto
setReluUpperClip(double upper_clip_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.upper_clip = upper_clip_;
return *this;
}
auto
setReluLowerClipSlope(double lower_clip_slope_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.lower_clip_slope = lower_clip_slope_;
return *this;
}
auto
setEluAlpha(double elu_alpha_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.elu_alpha = elu_alpha_;
return *this;
}
auto
setSoftplusBeta(double softplus_beta_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.softplus_beta = softplus_beta_;
return *this;
}
auto
setSwishBeta(double swish_beta_) -> PointWiseDescBuilder_v8 & {
m_pointWiseDesc.swish_beta = swish_beta_;
return *this;
}
auto
setAxis(int64_t axis_) -> PointWiseDescBuilder_v8 & {
CUDNN_FRONTEND_UNUSED(axis_);
m_pointWiseDesc.axis = axis_;
return *this;
}
//! constructs the PointWiseDesc_v8 by calling the cudnn API
//! Throws the appropriate error message
PointWiseDesc_v8
build() {
// Create a descriptor. Memory allocation happens here.
auto status = m_pointWiseDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_POINTWISE_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc, status, "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_pointWiseDesc);
}
// Once Created lets set the descriptor parameters.
cudnnPointwiseMode_t cudnn_pointwise_mode;
status = detail::convert_to_cudnn_type(m_pointWiseDesc.mode, cudnn_pointwise_mode);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_TYPE_POINTWISE_MODE Failed");
return std::move(m_pointWiseDesc);
}
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_MODE,
CUDNN_TYPE_POINTWISE_MODE,
1,
&cudnn_pointwise_mode);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_TYPE_POINTWISE_MODE Failed");
return std::move(m_pointWiseDesc);
}
cudnnDataType_t cudnn_data_type;
status = detail::convert_to_cudnn_type(m_pointWiseDesc.compute_type, cudnn_data_type);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_MATH_PREC Failed");
return std::move(m_pointWiseDesc);
}
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_MATH_PREC,
CUDNN_TYPE_DATA_TYPE,
1,
&cudnn_data_type);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_MATH_PREC Failed");
return std::move(m_pointWiseDesc);
}
if (m_pointWiseDesc.mode == PointwiseMode_t::RELU_FWD || m_pointWiseDesc.mode == PointwiseMode_t::RELU_BWD) {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_NAN_PROPAGATION,
CUDNN_TYPE_NAN_PROPOGATION,
1,
&m_pointWiseDesc.nan_propagation);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_NAN_PROPAGATION Failed");
return std::move(m_pointWiseDesc);
}
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.lower_clip);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP, Failed");
return std::move(m_pointWiseDesc);
}
if (m_pointWiseDesc.compute_type == DataType_t::FLOAT) {
double clamped_upper_clip =
std::min<double>(m_pointWiseDesc.upper_clip, std::numeric_limits<float>::max());
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_RELU_UPPER_CLIP,
CUDNN_TYPE_DOUBLE,
1,
&clamped_upper_clip);
} else {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_RELU_UPPER_CLIP,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.upper_clip);
}
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_RELU_UPPER_CLIP, Failed");
return std::move(m_pointWiseDesc);
}
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP_SLOPE,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.lower_clip_slope);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute "
"CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP_SLOPE, Failed");
return std::move(m_pointWiseDesc);
}
} else if (m_pointWiseDesc.mode == PointwiseMode_t::ELU_FWD ||
m_pointWiseDesc.mode == PointwiseMode_t::ELU_BWD) {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_ELU_ALPHA,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.elu_alpha);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_ELU_ALPHA, Failed");
return std::move(m_pointWiseDesc);
}
} else if (m_pointWiseDesc.mode == PointwiseMode_t::SOFTPLUS_FWD ||
m_pointWiseDesc.mode == PointwiseMode_t::SOFTPLUS_BWD) {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_SOFTPLUS_BETA,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.softplus_beta);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_SOFTPLUS_BETA, Failed");
return std::move(m_pointWiseDesc);
}
} else if (m_pointWiseDesc.mode == PointwiseMode_t::SWISH_FWD ||
m_pointWiseDesc.mode == PointwiseMode_t::SWISH_BWD) {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_SWISH_BETA,
CUDNN_TYPE_DOUBLE,
1,
&m_pointWiseDesc.swish_beta);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_SWISH_BETA, Failed");
return std::move(m_pointWiseDesc);
}
} else if (m_pointWiseDesc.mode == PointwiseMode_t::GEN_INDEX) {
status = detail::set_attribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_POINTWISE_AXIS,
CUDNN_TYPE_INT64,
1,
&m_pointWiseDesc.axis);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc,
status,
"CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_AXIS, Failed");
return std::move(m_pointWiseDesc);
}
}
// Finalizing the descriptor
status = detail::finalize(m_pointWiseDesc.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_pointWiseDesc, status, "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_pointWiseDesc);
}
CUDNN_FE_LOG_LABEL_ENDL(m_pointWiseDesc);
return std::move(m_pointWiseDesc);
}
explicit PointWiseDescBuilder_v8() = default;
~PointWiseDescBuilder_v8() = default;
PointWiseDescBuilder_v8(PointWiseDescBuilder_v8 &&) = delete;
PointWiseDescBuilder_v8(PointWiseDescBuilder_v8 const &) = delete;
PointWiseDescBuilder_v8 &
operator=(PointWiseDescBuilder_v8 const &) = delete;
private:
PointWiseDesc_v8 m_pointWiseDesc;
};
using PointWiseDescBuilder = PointWiseDescBuilder_v8;
using PointWiseDesc = PointWiseDesc_v8;
using PointwiseDescBuilder = PointWiseDescBuilder_v8;
using PointwiseDesc = PointWiseDesc_v8;
} // namespace cudnn_frontend
|