forked from jolibrain/deepdetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlservice.h
386 lines (365 loc) · 12.3 KB
/
mlservice.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
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
/**
* DeepDetect
* Copyright (c) 2014 Emmanuel Benazera
* Author: Emmanuel Benazera <[email protected]>
*
* This file is part of deepdetect.
*
* deepdetect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* deepdetect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MLSERVICE_H
#define MLSERVICE_H
#include "mllibstrategy.h"
#include "mlmodel.h"
#include <string>
#include <future>
#include <mutex>
//#include <shared_mutex>
#include <boost/thread/shared_mutex.hpp>
#include <unordered_map>
#include <chrono>
#include <iostream>
namespace dd
{
/**
* \brief lock exception
*/
class MLServiceLockException : public std::exception
{
public:
MLServiceLockException(const std::string &s)
:_s(s) {}
~MLServiceLockException() {}
const char* what() const noexcept { return _s.c_str(); }
private:
std::string _s;
};
/**
* \brief training job
*/
class tjob
{
public:
tjob(std::future<int> &&ft,
const std::chrono::time_point<std::chrono::system_clock> &tstart)
:_ft(std::move(ft)),_tstart(tstart),_status(1) {}
tjob(tjob &&tj)
:_ft(std::move(tj._ft)),_tstart(std::move(tj._tstart)),_status(std::move(tj._status)) {}
~tjob() {}
std::future<int> _ft; /**< training job output status upon termination. */
std::chrono::time_point<std::chrono::system_clock> _tstart; /**< date at which the training job has started*/
int _status = 0; /**< 0: not started, 1: running, 2: finished or terminated */
};
/**
* \brief main machine learning service encapsulation
*/
template<template <class U,class V,class W> class TMLLib, class TInputConnectorStrategy, class TOutputConnectorStrategy, class TMLModel>
class MLService : public TMLLib<TInputConnectorStrategy,TOutputConnectorStrategy,TMLModel>
{
public:
/**
* \brief machine learning service creation
* @param sname service name
* @param mlmodel model object
* @param description optional string
*/
MLService(const std::string &sname,
const TMLModel &mlmodel,
const std::string &description="")
:TMLLib<TInputConnectorStrategy,TOutputConnectorStrategy,TMLModel>(mlmodel),_sname(sname),_description(description),_tjobs_counter(0)
{}
/**
* \brief copy-constructor
* @param mls ML service
*/
MLService(MLService &&mls) noexcept
:TMLLib<TInputConnectorStrategy,TOutputConnectorStrategy,TMLModel>(std::move(mls)),_sname(std::move(mls._sname)),_description(std::move(mls._description)),_tjobs_counter(mls._tjobs_counter.load()),_training_jobs(std::move(mls._training_jobs))
{}
/**
* \brief destructor
*/
~MLService()
{
kill_jobs();
}
/**
* \brief machine learning service initialization:
* - init of input connector
* - init of output conector
* - init of ML library
* @param ad root data object
*/
void init(const APIData &ad)
{
this->_inputc._model_repo = ad.getobj("model").get("repository").get<std::string>();
if (this->_inputc._model_repo.empty())
throw MLLibBadParamException("empty repository");
this->_inputc.init(ad.getobj("parameters").getobj("input"));
this->_outputc.init(ad.getobj("parameters").getobj("output"));
this->init_mllib(ad.getobj("parameters").getobj("mllib"));
}
/**
* \brief terminates all service's jobs
*/
void kill_jobs()
{
std::lock_guard<std::mutex> lock(_tjobs_mutex);
auto hit = _training_jobs.begin();
while(hit!=_training_jobs.end())
{
std::future_status status = (*hit).second._ft.wait_for(std::chrono::seconds(0));
if (status == std::future_status::timeout
&& (*hit).second._status == 1) // process is running, terminate it
{
this->_tjob_running.store(false);
(*hit).second._ft.wait();
auto ohit = _training_out.find((*hit).first);
if (ohit!=_training_out.end())
_training_out.erase(ohit);
}
++hit;
}
}
/**
* \brief get info about the service
* @return info data object
*/
APIData info() const
{
APIData ad;
ad.add("name",_sname);
ad.add("description",_description);
ad.add("mllib",this->_libname);
return ad;
}
//
/**
* \brief get status of the service
* To be surcharged in related classes
* @return status data object
*/
APIData status()
{
APIData ad;
ad.add("name",_sname);
ad.add("description",_description);
ad.add("mllib",this->_libname);
std::vector<APIData> vad;
std::lock_guard<std::mutex> lock(_tjobs_mutex);
auto hit = _training_jobs.begin();
while(hit!=_training_jobs.end())
{
APIData jad;
jad.add("job",(*hit).first);
int jstatus = (*hit).second._status;
if (jstatus == 0)
jad.add("status","not started");
else if (jstatus == 1)
jad.add("status","running");
else if (jstatus == 2)
jad.add("status","finished");
vad.push_back(jad);
++hit;
}
ad.add("jobs",vad);
return ad;
}
/**
* \brief starts a possibly asynchronous trainin job and returns status or job number (async job).
* @param ad root data object
* @param out output data object
* @return training job number if async, otherwise status upon termination
*/
int train_job(const APIData &ad, APIData &out)
{
APIData jmrepo;
jmrepo.add("repository",this->_mlmodel._repo);
out.add("model",jmrepo);
if (!ad.has("async") || (ad.has("async") && ad.get("async").get<bool>()))
{
std::lock_guard<std::mutex> lock(_tjobs_mutex);
std::chrono::time_point<std::chrono::system_clock> tstart = std::chrono::system_clock::now();
++_tjobs_counter;
int local_tcounter = _tjobs_counter;
_training_jobs.emplace(local_tcounter,
std::move(tjob(std::async(std::launch::async,
[this,ad,local_tcounter]
{
// XXX: due to lock below, queued jobs may not start in requested order
boost::unique_lock< boost::shared_mutex > lock(_train_mutex);
APIData out;
int run_code = this->train(ad,out);
std::pair<int,APIData> p(local_tcounter,std::move(out));
_training_out.insert(std::move(p));
return run_code;
}),
tstart)));
return _tjobs_counter;
}
else
{
boost::unique_lock< boost::shared_mutex > lock(_train_mutex);
int status = this->train(ad,out);
//this->collect_measures(out);
APIData ad_params_out = ad.getobj("parameters").getobj("output");
if (ad_params_out.has("measure_hist") && ad_params_out.get("measure_hist").get<bool>())
this->collect_measures_history(out);
return status;
}
}
/**
* \brief get status of an asynchronous training job
* @param ad root data object
* @param out output data object
* @return 0 if OK, 1 if job not found
*/
int training_job_status(const APIData &ad, APIData &out)
{
int j = ad.get("job").get<int>();
int secs = 0;
if (ad.has("timeout"))
secs = ad.get("timeout").get<int>();
APIData ad_params_out = ad.getobj("parameters").getobj("output");
std::lock_guard<std::mutex> lock(_tjobs_mutex);
std::unordered_map<int,tjob>::iterator hit;
if ((hit=_training_jobs.find(j))!=_training_jobs.end())
{
std::future_status status = (*hit).second._ft.wait_for(std::chrono::seconds(secs));
if (status == std::future_status::timeout)
{
out.add("status","running");
this->collect_measures(out);
std::chrono::time_point<std::chrono::system_clock> trun = std::chrono::system_clock::now();
out.add("time",std::chrono::duration_cast<std::chrono::seconds>(trun-(*hit).second._tstart).count());
if (ad_params_out.has("measure_hist") && ad_params_out.get("measure_hist").get<bool>())
this->collect_measures_history(out);
}
else if (status == std::future_status::ready)
{
int st;
try
{
st = (*hit).second._ft.get();
}
catch (std::exception &e)
{
auto ohit = _training_out.find((*hit).first);
if (ohit!=_training_out.end())
_training_out.erase(ohit);
_training_jobs.erase(hit);
throw;
}
auto ohit = _training_out.find((*hit).first);
if (ohit!=_training_out.end())
{
out = std::move((*ohit).second); // get async process output object
_training_out.erase(ohit);
}
if (st == 0)
out.add("status","finished");
else out.add("status","unknown error");
//this->collect_measures(out); // XXX: beware if there was a queue, since the job has finished, there might be a new one running.
APIData jmrepo;
jmrepo.add("repository",this->_mlmodel._repo);
out.add("model",jmrepo);
std::chrono::time_point<std::chrono::system_clock> trun = std::chrono::system_clock::now();
out.add("time",std::chrono::duration_cast<std::chrono::seconds>(trun-(*hit).second._tstart).count());
if (ad_params_out.has("measure_hist") && ad_params_out.get("measure_hist").get<bool>())
this->collect_measures_history(out);
_training_jobs.erase(hit);
}
return 0;
}
else
{
return 1; // job not found
}
}
/**
* \brief terminate a training job
* @param ad root data object
* @param out output data object
* @return 0 if OK, 1 if job not found
*/
int training_job_delete(const APIData &ad, APIData &out)
{
int j = ad.get("job").get<int>();
std::lock_guard<std::mutex> lock(_tjobs_mutex);
std::unordered_map<int,tjob>::iterator hit;
if ((hit=_training_jobs.find(j))!=_training_jobs.end())
{
std::future_status status = (*hit).second._ft.wait_for(std::chrono::seconds(0));
if (status == std::future_status::timeout
&& (*hit).second._status == 1) // process is running, terminate it
{
this->_tjob_running.store(false); // signals the process
(*hit).second._ft.wait(); // XXX: default timeout in case the process does not return ?
out.add("status","terminated");
std::chrono::time_point<std::chrono::system_clock> trun = std::chrono::system_clock::now();
out.add("time",std::chrono::duration_cast<std::chrono::seconds>(trun-(*hit).second._tstart).count());
_training_jobs.erase(hit);
auto ohit = _training_out.find((*hit).first);
if (ohit!=_training_out.end())
_training_out.erase(ohit);
}
else if ((*hit).second._status == 0)
{
out.add("status","not started");
}
return 0;
}
else return 1; // job not found
}
/**
* \brief starts a predict job, makes sure no training call is running.
* @param ad root data object
* @param out output data object
* @return predict job status
*/
int predict_job(const APIData &ad, APIData &out)
{
if (!this->_online)
{
if (!_train_mutex.try_lock_shared())
throw MLServiceLockException("Predict call while training with an offline learning algorithm");
int err = 0;
try
{
err = this->predict(ad,out);
}
catch(std::exception &e)
{
_train_mutex.unlock_shared();
throw;
}
_train_mutex.unlock_shared();
return err;
}
else // wait til a lock can be acquired
{
boost::shared_lock< boost::shared_mutex > lock(_train_mutex);
return this->predict(ad,out);
}
return 0;
}
std::string _sname; /**< service name. */
std::string _description; /**< optional description of the service. */
std::mutex _tjobs_mutex; /**< mutex around training jobs. */
std::atomic<int> _tjobs_counter = {0}; /**< training jobs counter. */
std::unordered_map<int,tjob> _training_jobs; // XXX: the futures' dtor blocks if the object is being terminated
std::unordered_map<int,APIData> _training_out;
boost::shared_mutex _train_mutex;
};
}
#endif