forked from NVIDIA/cudnn-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorm_zero_centered_gamma.cpp
More file actions
307 lines (251 loc) · 14 KB
/
norm_zero_centered_gamma.cpp
File metadata and controls
307 lines (251 loc) · 14 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2024, 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.
*/
#include <catch2/catch_test_macros.hpp>
#include "../utils/helpers.h"
#include <cudnn_frontend.h>
// This sample file uses zero centered gamma with layernorm but you can also use it with adalayernorm or rmsnorm
TEST_CASE("LayerNorm Zero Centered Gamma Training", "[layernorm][graph][zero_centered_gamma]") {
namespace fe = cudnn_frontend;
#if (CUDNN_VERSION < 90700)
SKIP("Zero centered gamma is not supported in cudnn versions prior to 9.7.0");
#endif
fe::graph::Graph graph;
graph.set_io_data_type(fe::DataType_t::HALF)
.set_intermediate_data_type(fe::DataType_t::FLOAT)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto batch_size = 4;
auto seq_length = 1024;
auto hidden_size = 128;
auto X = graph.tensor(fe::graph::Tensor_attributes()
.set_name("X")
.set_dim({batch_size * seq_length, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size}));
auto scale_zero_centered = graph.tensor(fe::graph::Tensor_attributes()
.set_name("scale_zero_centered")
.set_dim({1, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size})
.set_data_type(fe::DataType_t::FLOAT));
auto bias = graph.tensor(fe::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size})
.set_data_type(fe::DataType_t::FLOAT));
float scalar_epsilon = 1e-05f;
fe::graph::Tensor_attributes s_epsilon(scalar_epsilon);
auto epsilon = graph.tensor(s_epsilon.set_name("epsilon"));
float scalar_one = 1.0f;
fe::graph::Tensor_attributes s_one(scalar_one);
auto one = graph.tensor(s_one.set_name("one"));
// Pointwise add operation for scale_zero_centered
auto pw_add_attributes = fe::graph::Pointwise_attributes()
.set_mode(fe::PointwiseMode_t::ADD)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto scale_add_one = graph.pointwise(scale_zero_centered, one, pw_add_attributes);
scale_add_one->set_data_type(fe::DataType_t::FLOAT).set_dim({1, hidden_size, 1, 1});
auto layernorm_options =
fe::graph::Layernorm_attributes().set_forward_phase(fe::NormFwdPhase_t::TRAINING).set_epsilon(epsilon);
auto [Y, mean, inv_variance] = graph.layernorm(X, scale_add_one, bias, layernorm_options);
mean->set_output(true).set_data_type(fe::DataType_t::FLOAT);
inv_variance->set_output(true).set_data_type(fe::DataType_t::FLOAT);
Y->set_output(true);
if (check_device_arch_newer_than("ampere") == false) {
SKIP("LayerNorm requires Ampere and up");
}
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
std::cout << graph << std::endl;
REQUIRE(graph.validate().is_good());
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A, fe::HeurMode_t::FALLBACK}).is_good());
REQUIRE(graph.check_support().is_good());
REQUIRE(graph.build_plans().is_good());
Surface<half> X_tensor(batch_size * seq_length * hidden_size);
Surface<float> Mean_tensor(batch_size * seq_length);
Surface<float> Var_tensor(batch_size * seq_length);
Surface<float> Scale_tensor(hidden_size);
Surface<float> Bias_tensor(hidden_size);
Surface<half> Y_tensor(batch_size * seq_length * hidden_size);
int64_t workspace_size;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {
{X, X_tensor.devPtr},
{mean, Mean_tensor.devPtr},
{inv_variance, Var_tensor.devPtr},
{scale_zero_centered, Scale_tensor.devPtr},
{bias, Bias_tensor.devPtr},
{Y, Y_tensor.devPtr}};
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}
TEST_CASE("LayerNorm Zero Centered Gamma Inference", "[layernorm][graph][zero_centered_gamma]") {
namespace fe = cudnn_frontend;
#if (CUDNN_VERSION < 90700)
SKIP("Zero centered gamma is not supported in cudnn versions prior to 9.7.0");
#endif
fe::graph::Graph graph;
graph.set_io_data_type(fe::DataType_t::HALF)
.set_intermediate_data_type(fe::DataType_t::FLOAT)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto batch_size = 4;
auto seq_length = 1024;
auto hidden_size = 128;
auto X = graph.tensor(fe::graph::Tensor_attributes()
.set_name("X")
.set_dim({batch_size * seq_length, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size}));
auto scale_zero_centered = graph.tensor(fe::graph::Tensor_attributes()
.set_name("scale_zero_centered")
.set_dim({1, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size})
.set_data_type(fe::DataType_t::FLOAT));
auto bias = graph.tensor(fe::graph::Tensor_attributes()
.set_name("bias")
.set_dim({1, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size})
.set_data_type(fe::DataType_t::FLOAT));
float scalar_epsilon = 1e-05f;
fe::graph::Tensor_attributes s_epsilon(scalar_epsilon);
auto epsilon = graph.tensor(s_epsilon.set_name("epsilon"));
float scalar_one = 1.0f;
fe::graph::Tensor_attributes s_one(scalar_one);
auto one = graph.tensor(s_one.set_name("one"));
// Pointwise add operation for scale_zero_centered
auto pw_add_attributes = fe::graph::Pointwise_attributes()
.set_mode(fe::PointwiseMode_t::ADD)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto scale_add_one = graph.pointwise(scale_zero_centered, one, pw_add_attributes);
scale_add_one->set_data_type(fe::DataType_t::FLOAT).set_dim({1, hidden_size, 1, 1});
auto layernorm_options =
fe::graph::Layernorm_attributes().set_forward_phase(fe::NormFwdPhase_t::INFERENCE).set_epsilon(epsilon);
auto [Y, mean, inv_variance] = graph.layernorm(X, scale_add_one, bias, layernorm_options);
Y->set_output(true);
REQUIRE(mean == nullptr);
REQUIRE(inv_variance == nullptr);
if (check_device_arch_newer_than("ampere") == false) {
SKIP("LayerNorm requires Ampere and up");
}
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
std::cout << graph << std::endl;
REQUIRE(graph.validate().is_good());
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A, fe::HeurMode_t::FALLBACK}).is_good());
REQUIRE(graph.check_support().is_good());
REQUIRE(graph.build_plans().is_good());
Surface<half> X_tensor(batch_size * seq_length * hidden_size);
Surface<float> Scale_tensor(hidden_size);
Surface<float> Bias_tensor(hidden_size);
Surface<half> Y_tensor(batch_size * seq_length * hidden_size);
int64_t workspace_size;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {
{X, X_tensor.devPtr},
{scale_zero_centered, Scale_tensor.devPtr},
{bias, Bias_tensor.devPtr},
{Y, Y_tensor.devPtr}};
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}
TEST_CASE("LayerNorm Zero Centered Gamma Backward", "[layernorm][graph][zero_centered_gamma]") {
namespace fe = cudnn_frontend;
#if (CUDNN_VERSION < 90700)
SKIP("Zero centered gamma is not supported in cudnn versions prior to 9.7.0");
#endif
fe::graph::Graph graph;
graph.set_io_data_type(fe::DataType_t::HALF)
.set_intermediate_data_type(fe::DataType_t::FLOAT)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto batch_size = 4;
auto seq_length = 1024;
auto hidden_size = 128;
auto X = graph.tensor(fe::graph::Tensor_attributes()
.set_name("X")
.set_dim({batch_size * seq_length, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size}));
auto DY = graph.tensor(fe::graph::Tensor_attributes()
.set_name("DY")
.set_dim({batch_size * seq_length, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size}));
auto scale_zero_centered = graph.tensor(fe::graph::Tensor_attributes()
.set_name("scale_zero_centered")
.set_dim({1, hidden_size, 1, 1})
.set_stride({hidden_size, 1, hidden_size, hidden_size})
.set_data_type(fe::DataType_t::FLOAT));
auto mean = graph.tensor(fe::graph::Tensor_attributes()
.set_name("mean")
.set_dim({batch_size * seq_length, 1, 1, 1})
.set_stride({1, 1, 1, 1})
.set_data_type(fe::DataType_t::FLOAT));
auto inv_variance = graph.tensor(fe::graph::Tensor_attributes()
.set_name("inv_variance")
.set_dim({batch_size * seq_length, 1, 1, 1})
.set_stride({1, 1, 1, 1})
.set_data_type(fe::DataType_t::FLOAT));
float scalar_one = 1.0f;
fe::graph::Tensor_attributes s_one(scalar_one);
auto one = graph.tensor(s_one.set_name("one"));
// Pointwise add operation for scale_zero_centered
auto pw_add_attributes = fe::graph::Pointwise_attributes()
.set_mode(fe::PointwiseMode_t::ADD)
.set_compute_data_type(fe::DataType_t::FLOAT);
auto scale_add_one = graph.pointwise(scale_zero_centered, one, pw_add_attributes);
scale_add_one->set_data_type(fe::DataType_t::FLOAT).set_dim({1, hidden_size, 1, 1});
auto DLN_options = fe::graph::Layernorm_backward_attributes().set_saved_mean_and_inv_variance(mean, inv_variance);
auto [DX, dscale, dbias] = graph.layernorm_backward(DY, X, scale_add_one, DLN_options);
DX->set_output(true);
dscale->set_output(true).set_data_type(fe::DataType_t::FLOAT);
dbias->set_output(true).set_data_type(fe::DataType_t::FLOAT);
if (check_device_arch_newer_than("ampere") == false) {
SKIP("LayerNorm Backward requires Ampere and up");
}
// Create a unique_ptr for the cuDNN handle
auto handle_ptr = create_cudnn_handle();
auto handle = *handle_ptr;
REQUIRE(graph.validate().is_good());
REQUIRE(graph.build_operation_graph(handle).is_good());
REQUIRE(graph.create_execution_plans({fe::HeurMode_t::A, fe::HeurMode_t::FALLBACK}).is_good());
REQUIRE(graph.check_support().is_good());
REQUIRE(graph.build_plans().is_good());
Surface<half> X_tensor(batch_size * seq_length * hidden_size);
Surface<half> DY_tensor(batch_size * seq_length * hidden_size);
Surface<float> Mean_tensor(batch_size * seq_length);
Surface<float> Inv_variance_tensor(batch_size * seq_length);
Surface<float> Scale_tensor(hidden_size);
Surface<float> Dscale_tensor(hidden_size);
Surface<float> Dbias_tensor(hidden_size);
Surface<half> DX_tensor(batch_size * seq_length * hidden_size);
int64_t workspace_size;
REQUIRE(graph.get_workspace_size(workspace_size).is_good());
Surface<int8_t> workspace(workspace_size);
std::unordered_map<std::shared_ptr<fe::graph::Tensor_attributes>, void*> variant_pack = {
{X, X_tensor.devPtr},
{DY, DY_tensor.devPtr},
{mean, Mean_tensor.devPtr},
{inv_variance, Inv_variance_tensor.devPtr},
{scale_zero_centered, Scale_tensor.devPtr},
{dscale, Dscale_tensor.devPtr},
{dbias, Dbias_tensor.devPtr},
{DX, DX_tensor.devPtr}};
REQUIRE(graph.execute(handle, variant_pack, workspace.devPtr).is_good());
}