-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathoptimizer.cc
214 lines (189 loc) · 7.1 KB
/
optimizer.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
#include "local-execution/optimizer.h"
#include "kernels/optimizer_kernels.h"
#include "local-execution/profiling.h"
#include "utils/overload.h"
namespace FlexFlow {
enum Slots { ATTRS, WEIGHT, SGD_V, PROFILING, ADAM_M, ADAM_V, HANDLE };
TaskSignature get_sgd_update_signature() {
TaskSignature sig = make_empty_task_signature();
add_slot(sig, WEIGHT, IsGrad::YES);
add_slot(sig, WEIGHT, IsGrad::NO);
add_slot(sig, SGD_V, IsGrad::YES);
add_arg_slot<SGDOptimizerAttrs>(sig, ATTRS);
add_arg_slot<ProfilingSettings>(sig, PROFILING);
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
add_unchecked_arg_slot<PerDeviceFFHandle>(sig, HANDLE);
}
return sig;
}
TaskInvocation sgd_update(SGDOptimizerAttrs const &attrs,
tensor_guid_t const &weight,
tensor_guid_t const &sgd_v) {
TaskBinding b;
b.bind(WEIGHT, TensorGuidSpec{weight, IsGrad::YES});
b.bind(WEIGHT, TensorGuidSpec{weight, IsGrad::NO});
if (attrs.momentum > 0.0f) {
b.bind(SGD_V, TensorGuidSpec{sgd_v, IsGrad::YES});
}
b.bind_arg(ATTRS, attrs);
b.bind_arg(PROFILING, profiling_settings());
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
b.bind_arg(HANDLE, ff_handle());
return {task_id_t::SGD_UPD_NCCL_TASK_ID, b};
}
return {task_id_t::SGD_UPD_PS_TASK_ID, b};
}
static void sgd_update_task_impl(TaskArgumentAccessor const &acc) {
auto attrs = acc.get_argument<SGDOptimizerAttrs>(ATTRS);
auto weight_grad = acc.get_tensor_grad<Permissions::RO>(WEIGHT);
auto weight = acc.get_tensor<Permissions::RW>(WEIGHT);
auto profiling = acc.get_argument<ProfilingSettings>(PROFILING);
assert(weight.shape == weight_grad.shape);
size_t size = weight_grad.shape.get_volume();
assert(weight_grad.shape.get_volume() & weight.shape.get_volume() == 0);
size_t num_replicas =
weight_grad.shape.get_volume() / weight.shape.get_volume();
float *sgd_v_ptr;
if (attrs.momentum > 0.0f) {
auto sgd_v = acc.get_tensor<Permissions::RW>(SGD_V);
assert(sgd_v.shape == weight.shape);
sgd_v_ptr = sgd_v.get_float_ptr();
}
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
auto handle = acc.get_argument<PerDeviceFFHandle>(HANDLE);
profile(sgd_nccl_update_task_gpu,
profiling,
"[SGD NCCL] update_time = %.2lfms\n",
attrs.lr,
attrs.momentum,
attrs.nesterov,
attrs.weight_decay,
handle,
weight_grad.get_float_ptr(),
size,
weight.get_float_ptr(),
sgd_v_ptr);
} else {
profile(sgd_ps_update_task_gpu,
profiling,
"[SGD PS] update_time = %.2lfms\n",
attrs.lr,
attrs.momentum,
attrs.nesterov,
attrs.weight_decay,
weight_grad.get_float_ptr(),
size,
num_replicas,
weight.get_float_ptr(),
sgd_v_ptr);
}
}
TaskImplFunction get_sgd_update_task_impl() {
return TaskImplFunction{GenericTaskImplFunction{sgd_update_task_impl}};
}
TaskSignature get_adam_update_signature() {
TaskSignature sig = make_empty_task_signature();
add_slot(sig, WEIGHT, IsGrad::YES);
add_slot(sig, WEIGHT, IsGrad::NO);
add_slot(sig, ADAM_V, IsGrad::YES);
add_slot(sig, ADAM_M, IsGrad::YES);
add_arg_slot<AdamOptimizerAttrs>(sig, ATTRS);
add_arg_slot<ProfilingSettings>(sig, PROFILING);
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
add_unchecked_arg_slot<PerDeviceFFHandle>(sig, HANDLE);
}
return sig;
}
TaskInvocation adam_update(AdamOptimizerAttrs const &attrs,
tensor_guid_t const &weight,
tensor_guid_t const &adam_v,
tensor_guid_t const &adam_m) {
TaskBinding b;
b.bind(WEIGHT, TensorGuidSpec{weight, IsGrad::YES});
b.bind(WEIGHT, TensorGuidSpec{weight, IsGrad::NO});
b.bind(ADAM_M, TensorGuidSpec{adam_m, IsGrad::YES});
b.bind(ADAM_V, TensorGuidSpec{adam_v, IsGrad::YES});
b.bind_arg(ATTRS, attrs);
b.bind_arg(PROFILING, profiling_settings());
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
b.bind_arg(HANDLE, ff_handle());
return {task_id_t::ADAM_UPD_NCCL_TASK_ID, b};
}
return {task_id_t::ADAM_UPD_PS_TASK_ID, b};
}
static void adam_update_task_impl(TaskArgumentAccessor const &acc) {
auto attrs = acc.get_argument<AdamOptimizerAttrs>(ATTRS);
auto weight_grad = acc.get_tensor_grad<Permissions::RO>(WEIGHT);
auto weight = acc.get_tensor<Permissions::RW>(WEIGHT);
auto v_tensor = acc.get_tensor<Permissions::RW>(ADAM_V);
auto m_tensor = acc.get_tensor<Permissions::RW>(ADAM_M);
auto profiling = acc.get_argument<ProfilingSettings>(PROFILING);
assert(weight.shape == weight_grad.shape);
size_t size = weight_grad.shape.get_volume();
assert(weight_grad.shape.get_volume() % weight.shape.get_volume() == 0);
size_t num_replicas =
weight_grad.shape.get_volume() / weight.shape.get_volume();
if (CHOSEN_SYNC_TYPE == ParamSync::NCCL) {
auto handle = acc.get_argument<PerDeviceFFHandle>(HANDLE);
profile(adam_nccl_update_task_gpu,
profiling,
"[Adam NCCL] update_time = %.2lfms\n",
attrs.alpha_t,
attrs.beta1,
attrs.beta2,
attrs.weight_decay,
attrs.epsilon,
size,
handle,
weight_grad.get_float_ptr(),
m_tensor.get_float_ptr(),
v_tensor.get_float_ptr(),
weight.get_float_ptr());
} else {
profile(adam_ps_update_task_gpu,
profiling,
"[Adam NCCL] update_time = %.2lfms\n",
attrs.alpha_t,
attrs.beta1,
attrs.beta2,
attrs.weight_decay,
attrs.epsilon,
size,
num_replicas,
weight_grad.get_float_ptr(),
m_tensor.get_float_ptr(),
v_tensor.get_float_ptr(),
weight.get_float_ptr());
}
}
TaskImplFunction get_adam_update_task_impl() {
return TaskImplFunction{GenericTaskImplFunction{adam_update_task_impl}};
}
TaskSignature get_update_signature(OptimizerAttrs const &attrs) {
return attrs.visit<TaskSignature>(overload{
[&](SGDOptimizerAttrs const &s) { return get_sgd_update_signature(); },
[&](AdamOptimizerAttrs const &s) {
return get_adam_update_signature();
}});
}
TaskInvocation
get_update_invocation(OptimizerAttrs const &attrs,
tensor_guid_t const &weight,
std::vector<tensor_guid_t> const &buffer_tensors) {
return attrs.visit<TaskInvocation>(
overload{[&](SGDOptimizerAttrs const &s) {
return sgd_update(s, weight, buffer_tensors.at(0));
},
[&](AdamOptimizerAttrs const &s) {
return adam_update(
s, weight, buffer_tensors.at(0), buffer_tensors.at(1));
}});
}
TaskImplFunction get_update_task_impl(OptimizerAttrs const &attrs) {
return attrs.visit<TaskImplFunction>(overload{
[&](SGDOptimizerAttrs const &s) { return get_sgd_update_task_impl(); },
[&](AdamOptimizerAttrs const &s) {
return get_adam_update_task_impl();
}});
}
} // namespace FlexFlow