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
|
/*
* 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>
#include <array>
#include <functional>
#include <memory>
#include <sstream>
#include <utility>
#include "cudnn_frontend_utils.h"
namespace cudnn_frontend {
namespace graph {
class RngNode;
}
} // namespace cudnn_frontend
namespace cudnn_frontend {
///
/// Rng Descriptor Class
/// This class tells the properties of the Rng operation
/// Properties:
///
/// Use RngDescBuilder_v8 to build this class.
/// Describe returns a string describing the Rng operation
///
class RngDesc_v8 : public BackendDescriptor {
public:
friend class RngDescBuilder_v8;
friend class graph::RngNode;
std::string
describe() const override {
std::stringstream ss;
#if (CUDNN_VERSION >= 8700)
#ifndef CUDNN_FRONTEND_SKIP_JSON_LIB
ss << "CUDNN_BACKEND_RNG_DESCRIPTOR: " << "Distribution Type: " << json{distribution}
#else
ss << "CUDNN_BACKEND_RNG_DESCRIPTOR: " << "Distribution Type: " << int(distribution)
#endif
<< ", Normal Distribution Mean: " << normal_dist_mean
<< ", Normal Distribution Standard Deviation: " << normal_dist_std_dev
<< ", Uniform Distribution Maximum: " << uniform_dist_max
<< ", Uniform Distribution Minimum: " << uniform_dist_min
<< ", Bernoulli Distribution Probability: " << bernoulli_dist_probability;
#endif
return ss.str();
}
RngDesc_v8(RngDesc_v8 &&from) = default;
RngDesc_v8 &
operator=(RngDesc_v8 &&) = default;
~RngDesc_v8() = default;
/** @defgroup RngDescBuilder_v8
* Get individual property of RngDesc_v8 class
* @{
*/
double
getNormalDistMean() const {
return normal_dist_mean;
}
double
getNormalDistStdDev() const {
return normal_dist_std_dev;
}
double
getUniformDistMax() const {
return uniform_dist_max;
}
double
getUniformDistMin() const {
return normal_dist_std_dev;
}
double
getBernoulliDistProbability() const {
return bernoulli_dist_probability;
}
RngDistribution_t
getDistribution() const {
return distribution;
}
/** @} */
private:
RngDesc_v8() = default;
RngDesc_v8(RngDesc_v8 const &) = delete;
RngDesc_v8 &
operator=(RngDesc_v8 const &) = delete;
// default values for attributes
double normal_dist_mean = -1;
double normal_dist_std_dev = -1;
double uniform_dist_max = -1;
double uniform_dist_min = -1;
double bernoulli_dist_probability = -1;
RngDistribution_t distribution = RngDistribution_t::NOT_SET;
};
///
/// RngDescBuilder_v8 Class
/// Helper class used to build RngDesc_v8 class
class RngDescBuilder_v8 {
public:
/** @defgroup RngDescBuilder_v8
* Set individual property of RngDesc_v8 class
* @{
*/
//! Set Rng distribution for the Rng Operation
auto
setRngDistribution(RngDistribution_t distribution_) -> RngDescBuilder_v8 & {
m_RngDesc.distribution = distribution_;
return *this;
}
#if (CUDNN_VERSION >= 8700)
//! Set Rng distribution for the Rng Operation
auto
setRngDistribution(cudnnRngDistribution_t distribution_) -> RngDescBuilder_v8 & {
m_RngDesc.distribution = detail::convert_from_cudnn_type(distribution_);
return *this;
}
#endif
//! Set normal distribution params (mean and std dev) for the Rng Operation
auto
setNormalDistParams(double normal_dist_mean_, double normal_dist_std_dev_) -> RngDescBuilder_v8 & {
m_RngDesc.normal_dist_mean = normal_dist_mean_;
m_RngDesc.normal_dist_std_dev = normal_dist_std_dev_;
return *this;
}
//! Set normal distribution mean for the Rng Operation
auto
setNormalDistMean(double normal_dist_mean_) -> RngDescBuilder_v8 & {
m_RngDesc.normal_dist_mean = normal_dist_mean_;
return *this;
}
//! Set normal distribution std dev for the Rng Operation
auto
setNormalDistStdDev(double normal_dist_std_dev_) -> RngDescBuilder_v8 & {
m_RngDesc.normal_dist_std_dev = normal_dist_std_dev_;
return *this;
}
//! Set uniform distribution params (min and max) for the Rng Operation
auto
setUniformDistParams(double uniform_dist_max_, double uniform_dist_min_) -> RngDescBuilder_v8 & {
m_RngDesc.uniform_dist_max = uniform_dist_max_;
m_RngDesc.uniform_dist_min = uniform_dist_min_;
return *this;
}
//! Set uniform distribution max for the Rng Operation
auto
setUniformDistMax(double uniform_dist_max_) -> RngDescBuilder_v8 & {
m_RngDesc.uniform_dist_max = uniform_dist_max_;
return *this;
}
//! Set uniform distribution min for the Rng Operation
auto
setUniformDistMin(double uniform_dist_min_) -> RngDescBuilder_v8 & {
m_RngDesc.uniform_dist_min = uniform_dist_min_;
return *this;
}
//! Set bernoulli distribution probability for the Rng Operation
auto
setBernoulliDistProbability(double bernoulli_dist_probability_) -> RngDescBuilder_v8 & {
m_RngDesc.bernoulli_dist_probability = bernoulli_dist_probability_;
return *this;
}
/** @} */
//! constructs the RngDesc_v8 by calling the cudnn API
//! Throws the appropriate error message
RngDesc_v8 &&
build() {
#if (CUDNN_VERSION >= 8700)
// Create a descriptor. Memory allocation happens here.
NV_CUDNN_FE_DYNAMIC_CHECK_BACKEND_DESCRIPTOR(
8700, m_RngDesc, "CUDNN_BACKEND_RNG_DESCRIPTOR: Requires cudnn 8.7.0");
auto status = m_RngDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_RNG_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_RngDesc, status, "CUDNN_BACKEND_RNG_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_RngDesc);
}
// Once Created lets set the descriptor parameters.
cudnnRngDistribution_t cudnn_rng_distribution;
status = detail::convert_to_cudnn_type(m_RngDesc.distribution, cudnn_rng_distribution);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc, status, "CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_DISTRIBUTION Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_DISTRIBUTION,
CUDNN_TYPE_RNG_DISTRIBUTION,
1,
&cudnn_rng_distribution);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc, status, "CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_DISTRIBUTION Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_NORMAL_DIST_MEAN,
CUDNN_TYPE_DOUBLE,
1,
&(m_RngDesc.normal_dist_mean));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc,
status,
"CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_NORMAL_DIST_MEAN Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_NORMAL_DIST_STANDARD_DEVIATION,
CUDNN_TYPE_DOUBLE,
1,
&(m_RngDesc.normal_dist_std_dev));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc,
status,
"CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_NORMAL_DIST_STANDARD_DEVIATION Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_UNIFORM_DIST_MAXIMUM,
CUDNN_TYPE_DOUBLE,
1,
&(m_RngDesc.uniform_dist_max));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc,
status,
"CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_UNIFORM_DIST_MAXIMUM Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_UNIFORM_DIST_MINIMUM,
CUDNN_TYPE_DOUBLE,
1,
&(m_RngDesc.uniform_dist_min));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc,
status,
"CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_UNIFORM_DIST_MINIMUM Failed");
return std::move(m_RngDesc);
}
status = detail::set_attribute(m_RngDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_RNG_BERNOULLI_DIST_PROBABILITY,
CUDNN_TYPE_DOUBLE,
1,
&(m_RngDesc.bernoulli_dist_probability));
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_RngDesc,
status,
"CUDNN_BACKEND_RNG_DESCRIPTOR: SetAttribute CUDNN_ATTR_RNG_BERNOULLI_DIST_PROBABILITY Failed");
return std::move(m_RngDesc);
}
// Finalizing the descriptor
status = detail::finalize(m_RngDesc.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(&m_RngDesc, status, "CUDNN_BACKEND_RNG_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_RngDesc);
}
CUDNN_FE_LOG_LABEL_ENDL(m_RngDesc);
return std::move(m_RngDesc);
#else
set_error_and_throw_exception(
&m_RngDesc, CUDNN_STATUS_NOT_SUPPORTED, "CUDNN_RNG_DESCRIPTOR: Rng only supported in cuDNN v8.7 or later");
return std::move(m_RngDesc);
#endif
}
explicit RngDescBuilder_v8() = default;
~RngDescBuilder_v8() = default;
RngDescBuilder_v8(RngDescBuilder_v8 &&) = delete;
RngDescBuilder_v8(RngDescBuilder_v8 const &) = delete;
RngDescBuilder_v8 &
operator=(RngDescBuilder_v8 const &) = delete;
private:
RngDesc_v8 m_RngDesc;
};
using RngDesc = RngDesc_v8;
using RngDescBuilder = RngDescBuilder_v8;
} // namespace cudnn_frontend
|