-
Notifications
You must be signed in to change notification settings - Fork 74k
/
training_ops_gpu.cu.cc
657 lines (592 loc) · 28.1 KB
/
training_ops_gpu.cu.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* Copyright 2015 The TensorFlow Authors. 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.
==============================================================================*/
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#define EIGEN_USE_GPU
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/kernels/training_ops.h"
#include "tensorflow/core/util/gpu_kernel_helper.h"
namespace tensorflow {
typedef Eigen::GpuDevice GPUDevice;
namespace functor {
template <typename T>
__global__ void ApplyAdamKernel(int32 data_dim, T* var, T* m, T* v,
const T* const beta1_power_,
const T* const beta2_power_, const T* const lr_,
const T* const beta1_, const T* const beta2_,
const T* const epsilon_, const T* grad,
bool use_nesterov) {
eigen_assert(blockDim.y == 1);
eigen_assert(blockDim.z == 1);
eigen_assert(gridDim.y == 1);
eigen_assert(gridDim.z == 1);
const T mul_factor = (*lr_) * sqrt(static_cast<T>(1.0) - (*beta2_power_)) /
(static_cast<T>(1.0) - (*beta1_power_));
const T epsilon = (*epsilon_);
const T beta1 = (*beta1_);
const T one_minus_beta1 = static_cast<T>(1.0) - (beta1);
const T one_minus_beta2 = static_cast<T>(1.0) - (*beta2_);
const int32 stripe = gridDim.x * blockDim.x;
for (int32 i = blockIdx.x * blockDim.x + threadIdx.x; i < data_dim;
i += stripe) {
auto m_i = m[i];
auto g_i = grad[i];
auto v_i = v[i];
m_i += one_minus_beta1 * (g_i - m_i);
v_i += one_minus_beta2 * (g_i * g_i - v_i);
if (use_nesterov) {
var[i] -= mul_factor * (m_i * beta1 + one_minus_beta1 * g_i) /
(epsilon + sqrt(v_i));
} else {
var[i] -= mul_factor * m_i / (epsilon + sqrt(v_i));
}
m[i] = m_i;
v[i] = v_i;
}
}
template <typename T, typename Tindex>
__global__ void SparseApplyKerasMomentumKernel(
T* var, T* accum, const T* lr, const T* grad, const Tindex* indices,
const T* momentum, bool use_nesterov, Tindex param_rows,
Tindex updates_size, Tindex indices_size) {
Tindex col_size = updates_size / indices_size;
GPU_1D_KERNEL_LOOP(grad_index, updates_size) {
Tindex indices_row = grad_index / col_size;
Tindex param_row = indices[indices_row];
if (param_row < 0 || param_row >= param_rows) {
// Ignore indices that are out of range.
continue;
}
// Compute the index of var and accum.
Tindex param_index = param_row * col_size + (grad_index % col_size);
// Read variables.
T var_i = var[param_index];
T accum_i = accum[param_index];
T grad_i = grad[grad_index];
const T momentum_t = *momentum;
const T lr_t = *lr;
// Variable update computation.
accum_i = momentum_t * accum_i - lr_t * grad_i;
// static branching in cuda does not impact performance.
if (use_nesterov) {
var_i += (momentum_t * accum_i - lr_t * grad_i);
} else {
var_i += accum_i;
}
// Write update back to variables.
var[param_index] = var_i;
accum[param_index] = accum_i;
}
}
template <typename T>
struct ApplyGradientDescent<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
var.device(d) -= lr.reshape(single).broadcast(bcast) * grad;
}
};
template <typename T>
struct ApplyAdagrad<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstFlat grad, bool update_slots) {
if (update_slots) {
accum.device(d) += grad.square();
}
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
var.device(d) -= lr.reshape(single).broadcast(bcast) * grad * accum.rsqrt();
}
};
template <typename T>
struct ApplyAdagradV2<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad, bool update_slots) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
if (update_slots) {
accum.device(d) += grad.square();
}
const auto update =
grad / (accum.sqrt() + epsilon.reshape(single).broadcast(bcast));
var.device(d) -= lr.reshape(single).broadcast(bcast) * update;
}
};
template <typename T>
struct ApplyAdadelta<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::Flat accum_update,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar rho,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
accum.device(d) = accum * rho.reshape(single).broadcast(bcast) +
grad.square() * (grad.constant(T(1)) -
rho.reshape(single).broadcast(bcast));
const auto update =
(accum_update + epsilon.reshape(single).broadcast(bcast)).sqrt() *
(accum + epsilon.reshape(single).broadcast(bcast)).rsqrt() * grad;
var.device(d) -= update * lr.reshape(single).broadcast(bcast);
accum_update.device(d) =
accum_update * rho.reshape(single).broadcast(bcast) +
update.square() *
(grad.constant(T(1)) - rho.reshape(single).broadcast(bcast));
}
};
template <typename T>
struct ApplyFtrl<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::Flat linear,
typename TTypes<T>::ConstFlat grad,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar l1,
typename TTypes<T>::ConstScalar l2,
typename TTypes<T>::ConstScalar lr_power) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
auto l1_bcast = l1.reshape(single).broadcast(bcast);
auto l2_bcast = l2.reshape(single).broadcast(bcast);
auto lr_bcast = lr.reshape(single).broadcast(bcast);
auto lr_power_bcast = -lr_power.reshape(single).broadcast(bcast);
const auto two = static_cast<T>(2.0);
auto new_accum = accum + grad.square();
auto accum_power = accum.binaryExpr(lr_power_bcast,
Eigen::internal::scalar_pow_op<T, T>());
auto new_accum_power = new_accum.binaryExpr(
lr_power_bcast, Eigen::internal::scalar_pow_op<T, T>());
linear.device(d) += grad - (new_accum_power - accum_power) * var / lr_bcast;
auto x = (l1_bcast * linear.sign() - linear);
auto y = (new_accum_power / lr_bcast) + linear.constant(two) * l2_bcast;
auto pre_shrink = x / y;
var.device(d) = (linear.abs() > l1_bcast)
.select(pre_shrink, var.constant(static_cast<T>(0)));
accum.device(d) += grad.square();
}
};
template <typename T>
struct ApplyFtrlV2<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::Flat linear,
typename TTypes<T>::ConstFlat grad,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar l1,
typename TTypes<T>::ConstScalar l2,
typename TTypes<T>::ConstScalar l2_shrinkage,
typename TTypes<T>::ConstScalar lr_power) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
auto l1_bcast = l1.reshape(single).broadcast(bcast);
auto l2_bcast = l2.reshape(single).broadcast(bcast);
auto l2_shrinkage_bcast = l2_shrinkage.reshape(single).broadcast(bcast);
auto lr_bcast = lr.reshape(single).broadcast(bcast);
auto lr_power_bcast = -lr_power.reshape(single).broadcast(bcast);
const auto two = static_cast<T>(2.0);
auto new_accum = accum + grad.square();
auto accum_power = accum.binaryExpr(lr_power_bcast,
Eigen::internal::scalar_pow_op<T, T>());
auto new_accum_power = new_accum.binaryExpr(
lr_power_bcast, Eigen::internal::scalar_pow_op<T, T>());
auto grad_with_shrinkage =
grad + (var.constant(two) * l2_shrinkage_bcast * var);
linear.device(d) +=
grad_with_shrinkage - (new_accum_power - accum_power) * var / lr_bcast;
auto x = (l1_bcast * linear.sign() - linear);
auto y = (new_accum_power / lr_bcast) + linear.constant(two) * l2_bcast;
auto pre_shrink = x / y;
var.device(d) = (linear.abs() > l1_bcast)
.select(pre_shrink, var.constant(static_cast<T>(0)));
accum.device(d) += grad.square();
}
};
template <typename T>
struct ApplyMomentum<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstFlat grad,
typename TTypes<T>::ConstScalar momentum, bool use_nesterov) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
accum.device(d) = accum * momentum.reshape(single).broadcast(bcast) + grad;
if (use_nesterov) {
var.device(d) -= grad * lr.reshape(single).broadcast(bcast) +
accum * momentum.reshape(single).broadcast(bcast) *
lr.reshape(single).broadcast(bcast);
} else {
var.device(d) -= lr.reshape(single).broadcast(bcast) * accum;
}
}
};
template <typename T>
struct ApplyKerasMomentum<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat accum,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstFlat grad,
typename TTypes<T>::ConstScalar momentum, bool use_nesterov) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
accum.device(d) = (accum * momentum.reshape(single).broadcast(bcast) -
grad * lr.reshape(single).broadcast(bcast));
if (use_nesterov) {
var.device(d) += (accum * momentum.reshape(single).broadcast(bcast) -
grad * lr.reshape(single).broadcast(bcast));
} else {
var.device(d) += accum;
}
}
};
template <typename T, typename Tindex>
struct SparseApplyKerasMomentum<GPUDevice, T, Tindex> {
Tindex operator()(const GPUDevice& d, typename TTypes<T>::Matrix var,
typename TTypes<T>::Matrix accum,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstMatrix grad,
typename TTypes<Tindex>::ConstVec indices,
typename TTypes<T>::ConstScalar momentum,
bool use_nesterov) {
const Tindex first_dim_size = var.dimension(0);
const Tindex grad_size = grad.size();
const Tindex indices_size = indices.size();
GpuLaunchConfig config = GetGpuLaunchConfig(grad_size, d);
TF_CHECK_OK(GpuLaunchKernel(
SparseApplyKerasMomentumKernel<T, Tindex>, config.block_count,
config.thread_per_block, 0, d.stream(), var.data(), accum.data(),
lr.data(), grad.data(), indices.data(), momentum.data(), use_nesterov,
first_dim_size, grad_size, indices_size));
return static_cast<Tindex>(-1);
}
};
template <typename T>
struct ApplyAdam<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat m, typename TTypes<T>::Flat v,
typename TTypes<T>::ConstScalar beta1_power,
typename TTypes<T>::ConstScalar beta2_power,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar beta1,
typename TTypes<T>::ConstScalar beta2,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad, bool use_nesterov) {
int32 data_dim = grad.dimension(0);
GpuLaunchConfig config = GetGpuLaunchConfig(data_dim, d);
eigen_assert(static_cast<int64>(grad.dimension(0)) +
static_cast<int64>(config.block_count) *
static_cast<int64>(config.thread_per_block) <
std::numeric_limits<int32>::max());
TF_CHECK_OK(GpuLaunchKernel(
ApplyAdamKernel<T>, config.block_count, config.thread_per_block, 0,
d.stream(), data_dim, var.data(), m.data(), v.data(),
beta1_power.data(), beta2_power.data(), lr.data(), beta1.data(),
beta2.data(), epsilon.data(), grad.data(), use_nesterov));
}
};
template <typename T>
struct ApplyAdamWithAmsgrad<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat m, typename TTypes<T>::Flat v,
typename TTypes<T>::Flat vhat,
typename TTypes<T>::ConstScalar beta1_power,
typename TTypes<T>::ConstScalar beta2_power,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar beta1,
typename TTypes<T>::ConstScalar beta2,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
const auto one = static_cast<T>(1.0);
m.device(d) =
m + (beta1.constant(one) - beta1).reshape(single).broadcast(bcast) *
(grad - m);
v.device(d) =
v + (beta2.constant(one) - beta2).reshape(single).broadcast(bcast) *
(grad.square() - v);
vhat.device(d) = vhat.cwiseMax(v);
var.device(d) -= (lr * (beta2_power.constant(one) - beta2_power).sqrt() /
(beta1_power.constant(one) - beta1_power))
.reshape(single)
.broadcast(bcast) *
m /
(epsilon.reshape(single).broadcast(bcast) + vhat.sqrt());
}
};
template <typename T>
struct ApplyAdaMax<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat m, typename TTypes<T>::Flat v,
typename TTypes<T>::ConstScalar beta1_power,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar beta1,
typename TTypes<T>::ConstScalar beta2,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
const auto one = static_cast<T>(1.0);
m.device(d) +=
(beta1.constant(one) - beta1).reshape(single).broadcast(bcast) *
(grad - m);
v.device(d) =
(beta2.reshape(single).broadcast(bcast) * v).cwiseMax(grad.abs());
var.device(d) -= lr.reshape(single).broadcast(bcast) /
(beta1_power.constant(one) - beta1_power)
.reshape(single)
.broadcast(bcast) *
(m / (v + epsilon.reshape(single).broadcast(bcast)));
}
};
template <typename T>
struct ApplyRMSProp<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat ms, typename TTypes<T>::Flat mom,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar rho,
typename TTypes<T>::ConstScalar momentum,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
const auto one = static_cast<T>(1.0);
ms.device(d) =
ms + (rho.constant(one) - rho).reshape(single).broadcast(bcast) *
(grad.square() - ms);
mom.device(d) =
mom * momentum.reshape(single).broadcast(bcast) +
lr.reshape(single).broadcast(bcast) * grad /
((epsilon.reshape(single).broadcast(bcast) + ms).sqrt());
var.device(d) -= mom;
}
};
template <typename T>
struct ApplyCenteredRMSProp<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat mg, typename TTypes<T>::Flat ms,
typename TTypes<T>::Flat mom,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar rho,
typename TTypes<T>::ConstScalar momentum,
typename TTypes<T>::ConstScalar epsilon,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
const auto one = static_cast<T>(1.0);
const auto one_minus_rho =
(rho.constant(one) - rho).reshape(single).broadcast(bcast);
ms.device(d) = ms + one_minus_rho * (grad.square() - ms);
mg.device(d) = mg + one_minus_rho * (grad - mg);
auto denom = (ms - mg.square()) + epsilon.reshape(single).broadcast(bcast);
mom.device(d) = mom * momentum.reshape(single).broadcast(bcast) +
lr.reshape(single).broadcast(bcast) * grad / denom.sqrt();
var.device(d) -= mom;
}
};
template <typename T>
struct ApplyAddSign<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat m,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar alpha,
typename TTypes<T>::ConstScalar sign_decay,
typename TTypes<T>::ConstScalar beta,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
// The following is the GPU equivalent of the CPU version:
// m.device(d) = m * beta() + grad * (static_cast<T>(1) - beta());
const auto one = static_cast<T>(1.0);
auto beta_bcast = beta.reshape(single).broadcast(bcast);
auto one_minus_beta =
(beta.constant(one) - beta).reshape(single).broadcast(bcast);
m.device(d) = m * beta_bcast + grad * one_minus_beta;
// The following is the GPU equivalent of the CPU version:
// var.device(d) -= lr() * (alpha() + sign_decay() * sign_gm) * grad;
auto sign_gm = grad.sign() * m.sign();
auto lr_bcast = lr.reshape(single).broadcast(bcast);
auto alpha_bcast = alpha.reshape(single).broadcast(bcast);
auto sign_decay_bcast = sign_decay.reshape(single).broadcast(bcast);
var.device(d) -=
lr_bcast * (alpha_bcast + sign_decay_bcast * sign_gm) * grad;
}
};
template <typename T>
struct ApplyPowerSign<GPUDevice, T> {
void operator()(const GPUDevice& d, typename TTypes<T>::Flat var,
typename TTypes<T>::Flat m,
typename TTypes<T>::ConstScalar lr,
typename TTypes<T>::ConstScalar logbase,
typename TTypes<T>::ConstScalar sign_decay,
typename TTypes<T>::ConstScalar beta,
typename TTypes<T>::ConstFlat grad) {
Eigen::array<typename TTypes<T>::Tensor::Index, 1> bcast;
bcast[0] = grad.dimension(0);
Eigen::Sizes<1> single;
// The following is the GPU equivalent of the CPU version:
// m.device(d) = m * beta() + grad * (static_cast<T>(1) - beta());
const auto one = static_cast<T>(1.0);
auto beta_bcast = beta.reshape(single).broadcast(bcast);
auto one_minus_beta =
(beta.constant(one) - beta).reshape(single).broadcast(bcast);
m.device(d) = m * beta_bcast + grad * one_minus_beta;
// The following is the GPU equivalent of the CPU version:
// auto grad_scale = (logbase() * sign_decay() * sign_gm).exp();
// var.device(d) -= lr() * grad_scale * grad;
auto sign_gm = grad.sign() * m.sign();
auto lr_bcast = lr.reshape(single).broadcast(bcast);
auto logbase_bcast = logbase.reshape(single).broadcast(bcast);
auto sign_decay_bcast = sign_decay.reshape(single).broadcast(bcast);
auto grad_scale = (logbase_bcast * sign_decay_bcast * sign_gm).exp();
var.device(d) -= lr_bcast * grad_scale * grad;
}
};
} // namespace functor
template struct functor::ApplyGradientDescent<GPUDevice, Eigen::half>;
template struct functor::ApplyGradientDescent<GPUDevice, float>;
template struct functor::ApplyGradientDescent<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyGradientDescent<GPUDevice, complex64>;
template struct functor::ApplyGradientDescent<GPUDevice, complex128>;
#endif
template struct functor::ApplyAdagrad<GPUDevice, Eigen::half>;
template struct functor::ApplyAdagrad<GPUDevice, float>;
template struct functor::ApplyAdagrad<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyAdagrad<GPUDevice, complex64>;
template struct functor::ApplyAdagrad<GPUDevice, complex128>;
#endif
template struct functor::ApplyAdagradV2<GPUDevice, Eigen::half>;
template struct functor::ApplyAdagradV2<GPUDevice, float>;
template struct functor::ApplyAdagradV2<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyAdagradV2<GPUDevice, complex64>;
template struct functor::ApplyAdagradV2<GPUDevice, complex128>;
#endif
template struct functor::ApplyAdadelta<GPUDevice, Eigen::half>;
template struct functor::ApplyAdadelta<GPUDevice, float>;
template struct functor::ApplyAdadelta<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyAdadelta<GPUDevice, complex64>;
template struct functor::ApplyAdadelta<GPUDevice, complex128>;
#endif
template struct functor::ApplyFtrl<GPUDevice, Eigen::half>;
template struct functor::ApplyFtrl<GPUDevice, float>;
template struct functor::ApplyFtrl<GPUDevice, double>;
template struct functor::ApplyFtrlV2<GPUDevice, Eigen::half>;
template struct functor::ApplyFtrlV2<GPUDevice, float>;
template struct functor::ApplyFtrlV2<GPUDevice, double>;
template struct functor::ApplyMomentum<GPUDevice, Eigen::half>;
template struct functor::ApplyMomentum<GPUDevice, float>;
template struct functor::ApplyMomentum<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyMomentum<GPUDevice, complex64>;
template struct functor::ApplyMomentum<GPUDevice, complex128>;
#endif
template struct functor::ApplyKerasMomentum<GPUDevice, Eigen::half>;
template struct functor::ApplyKerasMomentum<GPUDevice, float>;
template struct functor::ApplyKerasMomentum<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyKerasMomentum<GPUDevice, complex64>;
template struct functor::ApplyKerasMomentum<GPUDevice, complex128>;
#endif
template struct functor::SparseApplyKerasMomentum<GPUDevice, Eigen::half,
int32>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, Eigen::half,
int64>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, float, int32>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, float, int64>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, double, int32>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, double, int64>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::SparseApplyKerasMomentum<GPUDevice, complex64, int32>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, complex64, int64>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, complex128, int32>;
template struct functor::SparseApplyKerasMomentum<GPUDevice, complex128, int64>;
#endif
template struct functor::ApplyAdam<GPUDevice, Eigen::half>;
template struct functor::ApplyAdam<GPUDevice, float>;
template struct functor::ApplyAdam<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyAdam<GPUDevice, complex64>;
template struct functor::ApplyAdam<GPUDevice, complex128>;
#endif
template struct functor::ApplyAdamWithAmsgrad<GPUDevice, Eigen::half>;
template struct functor::ApplyAdamWithAmsgrad<GPUDevice, float>;
template struct functor::ApplyAdamWithAmsgrad<GPUDevice, double>;
template struct functor::ApplyAdaMax<GPUDevice, Eigen::half>;
template struct functor::ApplyAdaMax<GPUDevice, float>;
template struct functor::ApplyAdaMax<GPUDevice, double>;
template struct functor::ApplyRMSProp<GPUDevice, Eigen::half>;
template struct functor::ApplyRMSProp<GPUDevice, float>;
template struct functor::ApplyRMSProp<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyRMSProp<GPUDevice, complex64>;
template struct functor::ApplyRMSProp<GPUDevice, complex128>;
#endif
template struct functor::ApplyCenteredRMSProp<GPUDevice, Eigen::half>;
template struct functor::ApplyCenteredRMSProp<GPUDevice, float>;
template struct functor::ApplyCenteredRMSProp<GPUDevice, double>;
#if !defined(TENSORFLOW_USE_NVCC) && \
!defined(TENSORFLOW_USE_ROCM) // TODO(b/143684500): Eigen to support
// complex sqrt
template struct functor::ApplyCenteredRMSProp<GPUDevice, complex64>;
template struct functor::ApplyCenteredRMSProp<GPUDevice, complex128>;
#endif
template struct functor::ApplyAddSign<GPUDevice, Eigen::half>;
template struct functor::ApplyAddSign<GPUDevice, float>;
template struct functor::ApplyAddSign<GPUDevice, double>;
template struct functor::ApplyPowerSign<GPUDevice, Eigen::half>;
template struct functor::ApplyPowerSign<GPUDevice, float>;
template struct functor::ApplyPowerSign<GPUDevice, double>;
} // end namespace tensorflow
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM