forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FusedAdam.cpp
174 lines (164 loc) · 4.9 KB
/
FusedAdam.cpp
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
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/core/Tensor.h>
#include <ATen/native/DispatchStub.h>
#include <ATen/native/FusedAdam.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_fused_adam.h>
#include <ATen/ops/_fused_adam_native.h>
#include <ATen/ops/_fused_adamw.h>
#include <ATen/ops/_fused_adamw_native.h>
#endif
namespace at::native {
void _fused_adam_kernel_cpu_(
at::TensorList params,
at::TensorList grads,
at::TensorList exp_avgs,
at::TensorList exp_avg_sqs,
at::TensorList max_exp_avg_sqs,
at::TensorList state_steps,
const double lr,
const double beta1,
const double beta2,
const double weight_decay,
const double eps,
const bool amsgrad,
const bool maximize,
const std::optional<at::Tensor>& grad_scale,
const std::optional<at::Tensor>& found_inf) {
const float* grad_scale_ptr =
grad_scale.has_value() ? grad_scale->data_ptr<float>() : nullptr;
const float* found_inf_ptr =
found_inf.has_value() ? found_inf->data_ptr<float>() : nullptr;
if (found_inf_ptr && *found_inf_ptr == 1.0) {
return;
}
size_t n_tensors = params.size();
TORCH_CHECK(grads.size() == n_tensors);
TORCH_CHECK(exp_avgs.size() == n_tensors);
TORCH_CHECK(exp_avg_sqs.size() == n_tensors);
if (amsgrad) {
TORCH_CHECK(max_exp_avg_sqs.size() == n_tensors);
} else {
TORCH_CHECK(max_exp_avg_sqs.empty());
}
TORCH_CHECK(state_steps.size() == n_tensors);
at::Tensor max_exp_avg_sq = at::Tensor();
for (size_t i = 0; i < n_tensors; i++){
if (amsgrad) max_exp_avg_sq = max_exp_avg_sqs[i];
fused_adam_stub(
kCPU,
params[i],
grads[i],
exp_avgs[i],
exp_avg_sqs[i],
max_exp_avg_sq,
state_steps[i],
lr,
beta1,
beta2,
weight_decay,
eps,
amsgrad,
maximize,
grad_scale_ptr,
ADAM_MODE::ORIGINAL);
}
}
// The following overload simply has a Tensor lr
void _fused_adam_kernel_cpu_(
at::TensorList params,
at::TensorList grads,
at::TensorList exp_avgs,
at::TensorList exp_avg_sqs,
at::TensorList max_exp_avg_sqs,
at::TensorList state_steps,
const at::Tensor& lr,
const double beta1,
const double beta2,
const double weight_decay,
const double eps,
const bool amsgrad,
const bool maximize,
const std::optional<at::Tensor>& grad_scale,
const std::optional<at::Tensor>& found_inf) {
_fused_adam_kernel_cpu_(params, grads, exp_avgs, exp_avg_sqs, max_exp_avg_sqs, state_steps, lr.item<double>(), beta1, beta2, weight_decay, eps, amsgrad, maximize, grad_scale, found_inf);
}
void _fused_adamw_kernel_cpu_(
at::TensorList params,
at::TensorList grads,
at::TensorList exp_avgs,
at::TensorList exp_avg_sqs,
at::TensorList max_exp_avg_sqs,
at::TensorList state_steps,
const double lr,
const double beta1,
const double beta2,
const double weight_decay,
const double eps,
const bool amsgrad,
const bool maximize,
const std::optional<at::Tensor>& grad_scale,
const std::optional<at::Tensor>& found_inf) {
const float* grad_scale_ptr =
grad_scale.has_value() ? grad_scale->data_ptr<float>() : nullptr;
const float* found_inf_ptr =
found_inf.has_value() ? found_inf->data_ptr<float>() : nullptr;
if (found_inf_ptr && *found_inf_ptr == 1.0) {
return;
}
size_t n_tensors = params.size();
TORCH_CHECK(grads.size() == n_tensors);
TORCH_CHECK(exp_avgs.size() == n_tensors);
TORCH_CHECK(exp_avg_sqs.size() == n_tensors);
if (amsgrad) {
TORCH_CHECK(max_exp_avg_sqs.size() == n_tensors);
} else {
TORCH_CHECK(max_exp_avg_sqs.empty());
}
TORCH_CHECK(state_steps.size() == n_tensors);
at::Tensor max_exp_avg_sq = at::Tensor();
for (size_t i = 0; i < n_tensors; i++){
if (amsgrad) max_exp_avg_sq = max_exp_avg_sqs[i];
fused_adam_stub(
kCPU,
params[i],
grads[i],
exp_avgs[i],
exp_avg_sqs[i],
max_exp_avg_sq,
state_steps[i],
lr,
beta1,
beta2,
weight_decay,
eps,
amsgrad,
maximize,
grad_scale_ptr,
ADAM_MODE::ADAMW);
}
}
// The following overload simply has a Tensor lr
void _fused_adamw_kernel_cpu_(
at::TensorList params,
at::TensorList grads,
at::TensorList exp_avgs,
at::TensorList exp_avg_sqs,
at::TensorList max_exp_avg_sqs,
at::TensorList state_steps,
const at::Tensor& lr,
const double beta1,
const double beta2,
const double weight_decay,
const double eps,
const bool amsgrad,
const bool maximize,
const std::optional<at::Tensor>& grad_scale,
const std::optional<at::Tensor>& found_inf) {
_fused_adamw_kernel_cpu_(params, grads, exp_avgs, exp_avg_sqs, max_exp_avg_sqs, state_steps, lr.item<double>(), beta1, beta2, weight_decay, eps, amsgrad, maximize, grad_scale, found_inf);
}
DEFINE_DISPATCH(fused_adam_stub);
}