-
Notifications
You must be signed in to change notification settings - Fork 1
/
kilt_impl.h
350 lines (267 loc) · 9.44 KB
/
kilt_impl.h
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
//
// MIT License
//
// Copyright (c) 2021 - 2023 Krai Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.POSSIBILITY OF SUCH DAMAGE.
//
#ifndef KRAI_INFERENCE_LIBRARY_H
#define KRAI_INFERENCE_LIBRARY_H
#include "iconfig.h"
#include "idevice.h"
#include "imodel.h"
#include "config/kilt_config.h"
#include <atomic>
using namespace KRAI;
template <typename Sample> class KraiInferenceLibrary {
public:
KraiInferenceLibrary() {
config = new IConfig();
scheduler_yield_time = config->server_cfg->getSchedulerYieldTime();
dispatch_yield_time = config->server_cfg->getDispatchYieldTime();
terminate = false;
scheduler = std::thread(&KraiInferenceLibrary::Scheduler, this);
model = modelConstruct(config);
for (int ds = 0; ds < config->server_cfg->getDataSourceCount(); ++ds) {
const std::vector<int> datasource_affinity =
config->server_cfg->getDataSourceAffinity(ds);
std::cout << "DataSource: [" << ds << "] affinity: ";
for (int i = 0; i < datasource_affinity.size(); ++i)
std::cout << datasource_affinity[i] << " ";
std::cout << std::endl;
data_sources.push_back(dataSourceConstruct(config, datasource_affinity));
}
n_devices = config->server_cfg->getDeviceCount();
for (int dv = 0; dv < n_devices; ++dv) {
unsigned int device_id = config->server_cfg->getDeviceId(dv);
std::vector<int> device_affinity =
config->server_cfg->getDeviceAffinity(device_id);
unsigned int data_source_id =
config->server_cfg->getDataSourceIdForDevice(device_id);
std::cout << "Device: [" << dv << "] (data source " << data_source_id
<< ") affinity: ";
for (int i = 0; i < device_affinity.size(); ++i)
std::cout << device_affinity[i] << " ";
std::cout << std::endl;
if (data_source_id >= data_sources.size()) {
std::cerr << "Trying to use a dataset with id=" << data_source_id
<< " whereas there are only " << data_sources.size()
<< " datasets."
<< std::endl;
exit(1);
}
IDevice<Sample> *device =
createDevice<Sample>(model, data_sources[data_source_id], config,
device_id, device_affinity);
devices.push_back(device);
}
// Loop until all devices are ready.
for (int dv = 0; dv < n_devices; ++dv) {
switch (devices[dv]->GetState()) {
case IDevice<Sample>::State::READY:
break;
case IDevice<Sample>::State::WAITING:
--dv;
break;
case IDevice<Sample>::State::ERROR:
throw std::runtime_error("Device Error");
}
}
queue_len = std::vector<uint64_t>(n_devices, 0);
// diagnostics
batch_trace = std::vector<uint64_t>(config->server_cfg->getBatchSize(), 0);
distribution = std::vector<uint64_t>(n_devices, 0);
}
~KraiInferenceLibrary() {
terminate = true;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
scheduler.join();
for (int d = 0; d < n_devices; ++d) {
delete devices[d];
}
for (int ds = 0; ds < data_sources.size(); ++ds) {
delete data_sources[ds];
}
std::cout << "Batch sizes dispatched: ";
for (int t = 0; t < batch_trace.size(); ++t)
std::cout << batch_trace[t] << " ";
std::cout << std::endl;
delete model;
}
void ColdRun() {
#if 0
auto vl = config->server_cfg->verbosity_level;
if (vl > 1) {
std::cout << "Triggering a Cold Run..." << std::endl;
} else if (vl) {
std::cout << 'C' << std::flush;
}
// QStatus status = runner->run(totalSetsCompleted, totalInferencesCompleted);
// if (status != QS_SUCCESS)
#endif
}
static void DispatchImpl(void *handle, const void *samples) {
KraiInferenceLibrary<Sample> *ths =
reinterpret_cast<KraiInferenceLibrary<Sample> *>(handle);
const std::vector<Sample> *s =
reinterpret_cast<const std::vector<Sample> *>(samples);
ths->Dispatch(*s);
}
void Inference(const std::vector<Sample> &samples) {
TRACE_BATCH_EVENT_INSTANT(kilt_inference, kilt_utils::GetSampleID(samples[0]));
int num_samples = samples.size();
for (int s = 0; s < num_samples; ++s) {
mtx_samples_queue.lock();
samples_queue.emplace_back(samples[s]);
if (samples_queue.size() == config->server_cfg->getBatchSize()) {
++batch_trace[samples_queue.size() - 1];
model->preprocessSamples(data_sources[0], &samples_queue, this,
DispatchImpl);
// Dispatch(samples_queue);
samples_queue.clear();
prev = std::chrono::steady_clock::now();
}
mtx_samples_queue.unlock();
}
}
void Flush() {
TRACE_EVENT_INSTANT(kilt_flush);
for (auto dev : devices) {
dev->Flush();
}
}
void LoadNextBatch(void *user) {
auto vl = config->server_cfg->getVerbosity();
if (vl) {
std::cout << 'L' << std::flush;
}
for (int d = 0; d < data_sources.size(); ++d) {
data_sources[d]->loadSamples(user);
}
if (vl) {
std::cout << std::endl;
}
}
void UnloadBatch(void *user) {
auto vl = config->server_cfg->getVerbosity();
if (vl) {
std::cout << 'U' << std::flush;
}
for (int d = 0; d < data_sources.size(); ++d) {
data_sources[d]->unloadSamples(user);
}
if (vl) {
std::cout << std::endl;
}
}
const int AvailableSamplesMax() {
return data_sources[0]->getNumAvailableSampleFiles();
}
const int SamplesInMemoryMax() {
return data_sources[0]->getNumMaxSamplesInMemory();
}
const std::string &UniqueServerID() {
return config->server_cfg->getUniqueServerID();
}
int GetCompletedSampleCount() const {
return model->getCompletedSampleCount();
}
private:
int round_robin = 0;
void Dispatch(const std::vector<Sample> &samples) {
int done;
while (1) {
TRACE_BATCH_EVENT_INSTANT(dispatch, kilt_utils::GetSampleID(samples[0]));
done = devices[round_robin]->Inference(samples);
queue_len[round_robin] = done;
round_robin = (round_robin + 1) % n_devices;
if (done >= 0)
break;
if (dispatch_yield_time)
std::this_thread::sleep_for(
std::chrono::microseconds(dispatch_yield_time));
}
++distribution[round_robin];
#if 0
static int counter = 0;
if(++counter == 1000) {
counter = 0;
for( int x=0 ; x<n_devices ; ++x) {
//std::cout << std::setw(2) << queue_len[x] << " ";
std::cout << queue_len[x] << " ";
}
std::cout << "[ ";
for( int x=0 ; x<n_devices ; ++x) {
std::cout << distribution[x] << " ";
}
std::cout << "][ ";
for(int t = 0; t<batch_trace.size() ; ++t)
std::cout << batch_trace[t] << " ";
std::cout << "]" << std::endl;
}
#endif
}
void Scheduler() {
prev = std::chrono::steady_clock::now();
std::chrono::microseconds max_wait =
std::chrono::microseconds(config->server_cfg->getMaxWait());
std::cout << "MaxWait: " << config->server_cfg->getMaxWait() << std::endl;
while (!terminate) {
auto now = std::chrono::steady_clock::now();
mtx_samples_queue.lock();
int qlen = samples_queue.size();
if (qlen) {
if ((now - prev) > max_wait) {
if (config->server_cfg->getVerbosityServer())
std::cout << "(" << qlen << ")";
++batch_trace[samples_queue.size() - 1];
// std::cout << "Timeout triggered." << std::endl;
model->preprocessSamples(data_sources[0], &samples_queue, this,
DispatchImpl);
// Dispatch(samples_queue);
samples_queue.clear();
prev = now;
}
} else {
prev = now;
}
mtx_samples_queue.unlock();
std::this_thread::sleep_for(
std::chrono::microseconds(scheduler_yield_time));
}
std::cout << "KILT Scheduler terminating..." << std::endl;
}
IConfig *config;
int n_devices;
std::vector<uint64_t> batch_trace;
std::vector<uint64_t> queue_len;
std::vector<uint64_t> distribution;
std::vector<IDevice<Sample> *> devices;
std::vector<IDataSource *> data_sources;
IModel *model;
std::vector<Sample> samples_queue;
std::mutex mtx_samples_queue;
std::chrono::time_point<std::chrono::steady_clock> prev;
std::atomic<bool> terminate;
std::thread scheduler;
int scheduler_yield_time;
int dispatch_yield_time;
std::atomic<int32_t> completed_samples;
};
#endif // KRAI_INFERENCE_LIBRARY_H