-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbindings.cc
374 lines (310 loc) · 13.9 KB
/
bindings.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
#include <torch/extension.h>
#include <ps/ps.h>
#include <iostream>
#include <stdexcept>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
typedef float ValT;
typedef ps::DefaultColoServerHandle<ValT> HandleT;
typedef ps::ColoKVServer<ValT, HandleT> ServerT;
typedef ps::ColoKVWorker<ValT, HandleT> WorkerT;
using namespace std;
namespace py = pybind11;
void setup(int num_keys, int num_threads, const std::string& use_techniques="", const int num_channels = -1) {
// set specifically which techniques AdaPM should use to manage parameters
if (use_techniques != "") {
ps::Postoffice::Get()->set_management_techniques(
ps::tokenToMgmtTechniques(use_techniques));
}
// set specifically how many communication channels AdaPM should use
if (num_channels != -1) {
ps::Postoffice::Get()->set_num_channels(num_channels);
}
ps::Postoffice::Get()->setup(num_keys, num_threads);
}
void scheduler(int num_keys, int num_threads) {
setup(num_keys, num_threads);
ps::Scheduler();
}
void assert_correct_value_length(WorkerT& worker, long int* provided_keys, int provided_keys_length,
int provided_value_length) {
int needed_value_length = 0;
for(int i=0; i!=provided_keys_length; ++i) {
needed_value_length += worker.GetLen(provided_keys[i]);
}
if (needed_value_length != provided_value_length) {
throw length_error("The provided value array does not match the size specified in the parameter server: " +
to_string(provided_value_length) +
" != " +
to_string(needed_value_length));
}
}
void assert_keys_in_range(long int max_key, long int* provided_keys, int provided_keys_length) {
for (int i = 0; i < provided_keys_length; i++) {
if (provided_keys[i] >= max_key) {
throw length_error("At least one of the provided keys (" +
to_string(provided_keys[i]) +
") is outside the key range [0, " +
to_string(max_key) + ")");
}
}
}
// sampling
std::mt19937 generator;
std::uniform_int_distribution<ps::Key> uniform;
inline ps::Key UniformSampling() {
return uniform(generator);
}
std::uniform_real_distribution<long double> uniform_real;
ps::Key min_key, max_key;
inline ps::Key LogUniformSampling() {
return static_cast<ps::Key>(exp(uniform_real(generator)*log(max_key-min_key+1))+min_key-1);
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("setup", &setup, "set up AdaPM",
py::arg("num_keys"), py::arg("num_threads"),
py::arg("use_techniques") = "", py::arg("num_channels") = -1);
m.def("scheduler", &scheduler, "run scheduler");
// Server
py::class_<ServerT>(m, "Server")
.def(py::init<int>())
.def(py::init([](torch::Tensor& value_lengths) {
auto t = value_lengths.to(torch::kInt64).contiguous(); // torch kInt64 should be a c++ long
std::vector<size_t> lengths(t.data_ptr<long>(), t.data_ptr<long>() + t.numel());
return new ServerT(lengths);
}), py::arg("value_lengths").noconvert())
// set up sampling
.def("enable_sampling_support", [](ServerT& server,
const std::string scheme,
const bool with_replacement,
const std::string distribution,
const ps::Key min, // incl.
const ps::Key max // excl.
){
// This enables to use the AdaPM sampling support from the PyTorch bindings.
// Out of the box, only some pre-provided sampling distributions are supported:
// - uniform: uniform sampling from a continuous range [min, max) (incl. min, excl. max)
// - log-uniform: log-uniform sampling from a continuous range [min, max) (incl. min, excl. max)
generator = std::mt19937(ps::Postoffice::Get()->my_rank());
ps::Sampling<ValT, WorkerT, ServerT>::with_replacement_ = with_replacement;
// sampling scheme
if (scheme == "naive") ps::Sampling<ValT, WorkerT, ServerT>::scheme = ps::SamplingScheme::Naive;
else if (scheme == "preloc") ps::Sampling<ValT, WorkerT, ServerT>::scheme = ps::SamplingScheme::Preloc;
else if (scheme == "pool") ps::Sampling<ValT, WorkerT, ServerT>::scheme = ps::SamplingScheme::Pool;
else if (scheme == "local") ps::Sampling<ValT, WorkerT, ServerT>::scheme = ps::SamplingScheme::Local;
else {
ALOG("Unknown sampling scheme '" << scheme << "'. Aborting.");
abort();
}
// sampling distribution
if (distribution == "uniform") {
uniform = std::uniform_int_distribution<ps::Key>{min, static_cast<int>(max-1)};
server.enable_sampling_support(&UniformSampling, min, max);
} else if (distribution == "log-uniform") {
min_key = min;
max_key = max;
uniform_real = std::uniform_real_distribution<long double>{0, 1};
server.enable_sampling_support(&LogUniformSampling, min, max);
} else {
ALOG("Unknown sampling distribution '" << distribution << "'. Aborting.");
abort();
}
}, py::arg("scheme"), py::arg("with_replacement"), py::arg("distribution"), py::arg("min"), py::arg("max"))
.def("barrier", [](ServerT& server){
server.Barrier();
})
.def("shutdown", [](ServerT& server){
server.shutdown();
})
.def("my_rank", [](ServerT& server){ return ps::MyRank(); })
;
// Worker
py::class_<WorkerT>(m, "Worker")
.def(py::init([](const int customer_id, ServerT& server) {
return new WorkerT(customer_id, server);
}))
.def("wait", [](WorkerT& worker, int timestamp){ worker.Wait(timestamp); })
.def("waitall", [](WorkerT& worker){ worker.WaitAll(); })
.def("finalize", [](WorkerT& worker){
py::gil_scoped_release release;
worker.Finalize();
})
// pull
.def("pull", [](WorkerT& worker, torch::Tensor& keys, torch::Tensor& vals, bool async){
ValT* val_ptr = static_cast<ValT*> (vals.data_ptr());
size_t num_keys = keys.size(0);
size_t num_vals = 1;
for (int i = 0; i < vals.dim(); i++){
num_vals *= vals.size(i);
}
long int* key_ptr = static_cast<long int*> (keys.data_ptr());
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Pull(key_ptr, num_keys, val_ptr);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
.def("pull", [](WorkerT& worker, py::array_t<long int>& keys, py::array_t<ValT>& vals, bool async){
auto val_buffer = vals.request();
ValT* val_ptr = static_cast<ValT*> (val_buffer.ptr);
auto key_buffer = keys.request();
size_t num_keys = key_buffer.shape[0];
size_t num_vals = 1;
for (int i = 0; i < val_buffer.ndim; i++){
num_vals *= val_buffer.shape[i];
}
long int* key_ptr = static_cast<long int*> (key_buffer.ptr);
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Pull(key_ptr, num_keys, val_ptr);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
// push
.def("push", [](WorkerT& worker, torch::Tensor& keys, torch::Tensor& vals, bool async){
ValT* val_ptr = static_cast<ValT*> (vals.data_ptr());
size_t num_keys = keys.size(0);
size_t num_vals = 1;
for (int i = 0; i < vals.dim(); i++){
num_vals *= vals.size(i);
}
long int* key_ptr = static_cast<long int*> (keys.data_ptr());
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Push(key_ptr, num_keys, val_ptr, false);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
.def("push", [](WorkerT& worker, py::array_t<long int>& keys, py::array_t<ValT>& vals, bool async){
auto val_buffer = vals.request();
ValT* val_ptr = static_cast<ValT*> (val_buffer.ptr);
auto key_buffer = keys.request();
size_t num_keys = key_buffer.shape[0];
size_t num_vals = 1;
for (int i = 0; i < val_buffer.ndim; i++){
num_vals *= val_buffer.shape[i];
}
long int* key_ptr = static_cast<long int*> (key_buffer.ptr);
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Push(key_ptr, num_keys, val_ptr, false);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
// set (a variant of push, use carefully with replication)
.def("set", [](WorkerT& worker, torch::Tensor& keys, torch::Tensor& vals, bool async){
ValT* val_ptr = static_cast<ValT*> (vals.data_ptr());
size_t num_keys = keys.size(0);
size_t num_vals = 1;
for (int i = 0; i < vals.dim(); i++){
num_vals *= vals.size(i);
}
long int* key_ptr = static_cast<long int*> (keys.data_ptr());
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Push(key_ptr, num_keys, val_ptr, true);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
.def("set", [](WorkerT& worker, py::array_t<long int>& keys, py::array_t<ValT>& vals, bool async){
auto val_buffer = vals.request();
ValT* val_ptr = static_cast<ValT*> (val_buffer.ptr);
auto key_buffer = keys.request();
size_t num_keys = key_buffer.shape[0];
size_t num_vals = 1;
for (int i = 0; i < val_buffer.ndim; i++){
num_vals *= val_buffer.shape[i];
}
long int* key_ptr = static_cast<long int*> (key_buffer.ptr);
assert_correct_value_length(worker, key_ptr, num_keys, num_vals);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
auto ts = worker.Push(key_ptr, num_keys, val_ptr, true);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
// intent
.def("intent", [](WorkerT& worker, torch::Tensor& keys, ps::Clock start, ps::Clock end){
size_t num_keys = keys.size(0);
long int* key_ptr = static_cast<long int*> (keys.data_ptr());
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
return worker.Intent(key_ptr, num_keys, start, end);
}, py::arg("keys").noconvert(), py::arg("start"), py::arg("end")=0)
.def("intent", [](WorkerT& worker, py::array_t<long int>& keys, ps::Clock start, ps::Clock end){
auto key_buffer = keys.request();
size_t num_keys = key_buffer.shape[0];
long int* key_ptr = static_cast<long int*> (key_buffer.ptr);
assert_keys_in_range(worker.GetNumKeys(), key_ptr, num_keys);
return worker.Intent(key_ptr, num_keys, start, end);
}, py::arg("keys").noconvert(), py::arg("start"), py::arg("end")=0)
.def("advance_clock", [](WorkerT& worker){
worker.advanceClock();
})
.def("current_clock", [](WorkerT& worker){
return worker.currentClock();
})
// sampling
.def("prepare_sample", [](WorkerT& worker, size_t K, ps::Clock start, ps::Clock end){
return worker.PrepareSample(K, start, end);
}, py::arg("K"), py::arg("start"), py::arg("end")=0)
.def("pull_sample", [](WorkerT& worker, ps::SampleID id, torch::Tensor& keys, torch::Tensor& vals, bool async){
ValT* val_ptr = static_cast<ValT*> (vals.data_ptr());
size_t num_keys = keys.size(0);
long int* key_ptr = static_cast<long int*> (keys.data_ptr());
auto ts = worker.PullSample(id, key_ptr, num_keys, val_ptr);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("id"), py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
.def("pull_sample", [](WorkerT& worker, ps::SampleID id, py::array_t<long int>& keys, py::array_t<ValT>& vals, bool async){
auto val_buffer = vals.request();
ValT* val_ptr = static_cast<ValT*> (val_buffer.ptr);
auto key_buffer = keys.request();
size_t num_keys = key_buffer.shape[0];
long int* key_ptr = static_cast<long int*> (key_buffer.ptr);
auto ts = worker.PullSample(id, key_ptr, num_keys, val_ptr);
if (!async) {
worker.Wait(ts);
}
return ts;
}, py::arg("id"), py::arg("keys").noconvert(), py::arg("vals").noconvert(), py::arg("async") = false)
// misc
.def("begin_setup", [](WorkerT& worker){
py::gil_scoped_release release;
worker.BeginSetup();
})
.def("end_setup", [](WorkerT& worker){
py::gil_scoped_release release;
worker.EndSetup();
})
.def("wait_sync", [](WorkerT& worker){
py::gil_scoped_release release;
worker.WaitSync();
})
.def("wait_replica_sync", [](WorkerT& worker){ // DEPRECATED
ALOG("[DEPRECATED] You are using worker.wait_replica_sync(). The method has been renamed to worker_wait_sync(). Please switch.");
py::gil_scoped_release release;
worker.WaitSync();
})
.def("barrier", [](WorkerT& worker){
py::gil_scoped_release release; // allow for multiple workers to reach barrier()
worker.Barrier();
})
.def("get_key_size", [](WorkerT& worker, long int key_id){
return worker.GetLen(key_id);
}, py::arg("key_id") = 0)
.def_property_readonly("num_keys", &WorkerT::GetNumKeys)
;
}