-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathegru.cc
438 lines (399 loc) · 14.3 KB
/
egru.cc
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
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) 2023 Khaleelulla Khan Nazeer
// This file incorporates work covered by the following copyright:
// Copyright 2020 LMNT, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==============================================================================
#include <fstream>
#include <iostream>
#ifdef WITH_CUDA
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#endif
#include <torch/extension.h>
#include <vector>
#include "evnn.h"
#include "support.h"
namespace
{
#ifdef WITH_CUDA
using evnn::v0::egru::BackwardPass;
using evnn::v0::egru::ForwardPass;
#endif
using evnn::v0::egru::BackwardPassCPU;
using evnn::v0::egru::ForwardPassCPU;
using torch::Tensor;
#ifdef WITH_CUDA
std::vector<Tensor> egru_forward(
bool training,
float zoneout_prob,
Tensor x,
Tensor y0,
Tensor kernel,
Tensor recurrent_kernel,
Tensor bias,
Tensor recurrent_bias,
Tensor thr,
Tensor zoneout_mask)
{
const auto time_steps = x.size(0);
const auto batch_size = x.size(1);
const auto input_size = x.size(2);
const auto hidden_size = recurrent_kernel.size(0);
const bool has_zoneout = zoneout_prob && zoneout_mask.size(0);
CHECK_INPUT(x);
CHECK_INPUT(y0);
CHECK_INPUT(kernel);
CHECK_INPUT(recurrent_kernel);
CHECK_INPUT(bias);
CHECK_INPUT(recurrent_bias);
CHECK_INPUT(thr);
CHECK_INPUT(zoneout_mask);
const auto options = x.options();
const at::cuda::CUDAGuard guard(options.device_index());
Tensor h = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor output = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor output_gate = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor cache = torch::zeros({time_steps, batch_size, hidden_size * 5}, options);
Tensor tmp_Wx = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
Tensor tmp_Rh = torch::zeros({batch_size, hidden_size * 3}, options);
Tensor trace = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
output[0] = y0;
AT_DISPATCH_FLOATING_TYPES_AND_HALF(x.scalar_type(), "egru_forward", ([&]
{
ForwardPass<typename native_type<scalar_t>::T> forward(
training,
batch_size,
input_size,
hidden_size,
at::cuda::getCurrentCUDABlasHandle(),
at::cuda::getCurrentCUDAStream());
forward.Run(
time_steps,
ptr<scalar_t>(kernel),
ptr<scalar_t>(recurrent_kernel),
ptr<scalar_t>(bias),
ptr<scalar_t>(recurrent_bias),
ptr<scalar_t>(x),
ptr<scalar_t>(h),
ptr<scalar_t>(output),
ptr<scalar_t>(cache),
ptr<scalar_t>(output_gate),
ptr<scalar_t>(thr),
ptr<scalar_t>(tmp_Wx),
ptr<scalar_t>(tmp_Rh),
ptr<scalar_t>(trace),
has_zoneout ? zoneout_prob : 0.0f,
has_zoneout ? ptr<scalar_t>(zoneout_mask) : nullptr); }));
return {output, cache, h, output_gate, trace};
}
std::vector<Tensor> egru_backward(
Tensor x_t,
Tensor kernel_t,
Tensor recurrent_kernel_t,
Tensor bias,
Tensor recurrent_bias,
Tensor thr,
Tensor zoneout_mask,
Tensor dampening_factor,
Tensor pseudo_derivative_support,
Tensor max_grad_norm,
Tensor y,
Tensor h,
Tensor cache,
Tensor dy_new,
Tensor dh_new,
Tensor dout_gate,
Tensor dtrs)
{
const auto input_size = x_t.size(0);
const auto time_steps = x_t.size(1);
const auto batch_size = x_t.size(2);
const auto hidden_size = recurrent_kernel_t.size(1);
const bool has_zoneout = !!zoneout_mask.size(0);
CHECK_INPUT(x_t);
CHECK_INPUT(kernel_t);
CHECK_INPUT(recurrent_kernel_t);
CHECK_INPUT(bias);
CHECK_INPUT(recurrent_bias);
CHECK_INPUT(thr);
CHECK_INPUT(dampening_factor);
CHECK_INPUT(pseudo_derivative_support);
CHECK_INPUT(h);
CHECK_INPUT(y);
CHECK_INPUT(cache);
CHECK_INPUT(dy_new);
CHECK_INPUT(dh_new);
CHECK_INPUT(dout_gate);
CHECK_INPUT(dtrs);
CHECK_INPUT(zoneout_mask);
const auto options = x_t.options();
const at::cuda::CUDAGuard guard(options.device_index());
Tensor dx = torch::zeros({time_steps, batch_size, input_size}, options);
Tensor dW = torch::zeros({input_size, hidden_size * 3}, options);
Tensor dR = torch::zeros({hidden_size, hidden_size * 3}, options);
Tensor dbx = torch::zeros({hidden_size * 3}, options);
Tensor dbr = torch::zeros({hidden_size * 3}, options);
Tensor dthr = torch::zeros({hidden_size}, options);
Tensor dy = torch::zeros({batch_size, hidden_size}, options);
Tensor dh = torch::zeros({batch_size, hidden_size}, options);
Tensor dp = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
Tensor dq = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(x_t.scalar_type(), "egru_backward", ([&]
{
BackwardPass<typename native_type<scalar_t>::T> backward(
batch_size,
input_size,
hidden_size,
at::cuda::getCurrentCUDABlasHandle(),
at::cuda::getCurrentCUDAStream());
backward.Run(
time_steps,
ptr<scalar_t>(dampening_factor),
ptr<scalar_t>(pseudo_derivative_support),
ptr<scalar_t>(kernel_t),
ptr<scalar_t>(recurrent_kernel_t),
ptr<scalar_t>(bias),
ptr<scalar_t>(recurrent_bias),
ptr<scalar_t>(thr),
ptr<scalar_t>(x_t),
ptr<scalar_t>(y),
ptr<scalar_t>(h),
ptr<scalar_t>(cache),
ptr<scalar_t>(dy_new),
ptr<scalar_t>(dh_new),
ptr<scalar_t>(dout_gate),
ptr<scalar_t>(dx),
ptr<scalar_t>(dW),
ptr<scalar_t>(dR),
ptr<scalar_t>(dbx),
ptr<scalar_t>(dbr),
ptr<scalar_t>(dthr),
ptr<scalar_t>(dy),
ptr<scalar_t>(dh),
ptr<scalar_t>(dtrs),
ptr<scalar_t>(dp),
ptr<scalar_t>(dq),
has_zoneout ? ptr<scalar_t>(zoneout_mask) : nullptr); }));
auto clip_coef_clamped = torch::ones({1}, options);
if (max_grad_norm.item<double>() > 0.0)
{
Tensor norms = torch::empty({7}, options);
auto index = 0;
for (auto &grad : {&dy, &dW, &dR, &dbx, &dbr, &dthr})
{
norms[index] = torch::norm(*grad);
index++;
}
auto total_norm = torch::norm(norms);
const auto clip_coef = max_grad_norm / (total_norm + 1e-6);
clip_coef_clamped = torch::clamp(clip_coef, 0.0, 1.0);
// std::cout << total_norm << std::endl;
// if(clip_coef_clamped.item<double>() < 1.0){
// std::cout << clip_coef_clamped << std::endl;
// }
}
return {dx,
dy * clip_coef_clamped,
dW * clip_coef_clamped,
dR * clip_coef_clamped,
dbx * clip_coef_clamped,
dbr * clip_coef_clamped,
dthr * clip_coef_clamped,
dp, dq,
clip_coef_clamped};
}
#endif
std::vector<Tensor> egru_forward_cpu(
bool training,
float zoneout_prob,
Tensor x,
Tensor y0,
Tensor kernel,
Tensor recurrent_kernel,
Tensor bias,
Tensor recurrent_bias,
Tensor thr,
Tensor zoneout_mask)
{
const auto time_steps = x.size(0);
const auto batch_size = x.size(1);
const auto input_size = x.size(2);
const auto hidden_size = recurrent_kernel.size(0);
const bool has_zoneout = zoneout_prob && zoneout_mask.size(0);
CHECK_CPU_INPUT(x);
CHECK_CPU_INPUT(y0);
CHECK_CPU_INPUT(kernel);
CHECK_CPU_INPUT(recurrent_kernel);
CHECK_CPU_INPUT(bias);
CHECK_CPU_INPUT(recurrent_bias);
CHECK_CPU_INPUT(thr);
CHECK_CPU_INPUT(zoneout_mask);
const auto options = x.options();
Tensor h = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor output = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor output_gate = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
Tensor cache = torch::zeros({time_steps, batch_size, hidden_size * 5}, options);
Tensor tmp_Wx = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
Tensor tmp_Rh = torch::zeros({batch_size, hidden_size * 3}, options);
Tensor trace = torch::zeros({time_steps + 1, batch_size, hidden_size}, options);
output[0] = y0;
AT_DISPATCH_FLOATING_TYPES(x.scalar_type(), "egru_forward_cpu", ([&]
{
ForwardPassCPU<typename native_type<scalar_t>::T> forward(
training,
batch_size,
input_size,
hidden_size);
forward.Run(
time_steps,
ptr<scalar_t>(kernel),
ptr<scalar_t>(recurrent_kernel),
ptr<scalar_t>(bias),
ptr<scalar_t>(recurrent_bias),
ptr<scalar_t>(x),
ptr<scalar_t>(h),
ptr<scalar_t>(output),
ptr<scalar_t>(cache),
ptr<scalar_t>(output_gate),
ptr<scalar_t>(thr),
ptr<scalar_t>(tmp_Wx),
ptr<scalar_t>(tmp_Rh),
ptr<scalar_t>(trace),
has_zoneout ? zoneout_prob : 0.0f,
has_zoneout ? ptr<scalar_t>(zoneout_mask) : nullptr); }));
return {output, cache, h, output_gate, trace};
}
std::vector<Tensor> egru_backward_cpu(
Tensor x_t,
Tensor kernel_t,
Tensor recurrent_kernel_t,
Tensor bias,
Tensor recurrent_bias,
Tensor thr,
Tensor zoneout_mask,
Tensor dampening_factor,
Tensor pseudo_derivative_support,
Tensor max_grad_norm,
Tensor y,
Tensor h,
Tensor cache,
Tensor dy_new,
Tensor dh_new,
Tensor dout_gate,
Tensor dtrs)
{
const auto input_size = x_t.size(0);
const auto time_steps = x_t.size(1);
const auto batch_size = x_t.size(2);
const auto hidden_size = recurrent_kernel_t.size(1);
const bool has_zoneout = !!zoneout_mask.size(0);
CHECK_CPU_INPUT(x_t);
CHECK_CPU_INPUT(kernel_t);
CHECK_CPU_INPUT(recurrent_kernel_t);
CHECK_CPU_INPUT(bias);
CHECK_CPU_INPUT(recurrent_bias);
CHECK_CPU_INPUT(thr);
CHECK_CPU_INPUT(dampening_factor);
CHECK_CPU_INPUT(pseudo_derivative_support);
CHECK_CPU_INPUT(h);
CHECK_CPU_INPUT(y);
CHECK_CPU_INPUT(cache);
CHECK_CPU_INPUT(dy_new);
CHECK_CPU_INPUT(dh_new);
CHECK_CPU_INPUT(dout_gate);
CHECK_CPU_INPUT(dtrs);
CHECK_CPU_INPUT(zoneout_mask);
const auto options = x_t.options();
Tensor dx = torch::zeros({time_steps, batch_size, input_size}, options);
Tensor dW = torch::zeros({input_size, hidden_size * 3}, options);
Tensor dR = torch::zeros({hidden_size, hidden_size * 3}, options);
Tensor dbx = torch::zeros({hidden_size * 3}, options);
Tensor dbr = torch::zeros({hidden_size * 3}, options);
Tensor dthr = torch::zeros({hidden_size}, options);
Tensor dy = torch::zeros({batch_size, hidden_size}, options);
Tensor dh = torch::zeros({batch_size, hidden_size}, options);
Tensor dp = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
Tensor dq = torch::zeros({time_steps, batch_size, hidden_size * 3}, options);
AT_DISPATCH_FLOATING_TYPES(x_t.scalar_type(), "egru_backward_cpu", ([&]
{
BackwardPassCPU<typename native_type<scalar_t>::T> backward(
batch_size,
input_size,
hidden_size);
backward.Run(
time_steps,
ptr<scalar_t>(dampening_factor),
ptr<scalar_t>(pseudo_derivative_support),
ptr<scalar_t>(kernel_t),
ptr<scalar_t>(recurrent_kernel_t),
ptr<scalar_t>(bias),
ptr<scalar_t>(recurrent_bias),
ptr<scalar_t>(thr),
ptr<scalar_t>(x_t),
ptr<scalar_t>(y),
ptr<scalar_t>(h),
ptr<scalar_t>(cache),
ptr<scalar_t>(dy_new),
ptr<scalar_t>(dh_new),
ptr<scalar_t>(dout_gate),
ptr<scalar_t>(dx),
ptr<scalar_t>(dW),
ptr<scalar_t>(dR),
ptr<scalar_t>(dbx),
ptr<scalar_t>(dbr),
ptr<scalar_t>(dthr),
ptr<scalar_t>(dy),
ptr<scalar_t>(dh),
ptr<scalar_t>(dtrs),
ptr<scalar_t>(dp),
ptr<scalar_t>(dq),
has_zoneout ? ptr<scalar_t>(zoneout_mask) : nullptr); }));
auto clip_coef_clamped = torch::ones({1}, options);
if (max_grad_norm.item<double>() > 0.0)
{
Tensor norms = torch::empty({7}, options);
auto index = 0;
for (auto &grad : {&dy, &dW, &dR, &dbx, &dbr, &dthr})
{
norms[index] = torch::norm(*grad);
index++;
}
auto total_norm = torch::norm(norms);
const auto clip_coef = max_grad_norm / (total_norm + 1e-6);
clip_coef_clamped = torch::clamp(clip_coef, 0.0, 1.0);
// std::cout << total_norm << std::endl;
// if(clip_coef_clamped.item<double>() < 1.0){
// std::cout << clip_coef_clamped << std::endl;
// }
}
return {dx,
dy * clip_coef_clamped,
dW * clip_coef_clamped,
dR * clip_coef_clamped,
dbx * clip_coef_clamped,
dbr * clip_coef_clamped,
dthr * clip_coef_clamped,
dp, dq,
clip_coef_clamped};
}
} // anonymous namespace
void egru_init(py::module &m)
{
#ifdef WITH_CUDA
m.def("egru_forward", &egru_forward, "EGRU forward", py::call_guard<py::gil_scoped_release>());
m.def("egru_backward", &egru_backward, "EGRU backward", py::call_guard<py::gil_scoped_release>());
#endif
m.def("egru_forward_cpu", &egru_forward_cpu, "EGRU forward (CPU)", py::call_guard<py::gil_scoped_release>());
m.def("egru_backward_cpu", &egru_backward_cpu, "EGRU backward (CPU)", py::call_guard<py::gil_scoped_release>());
}