-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.py
495 lines (409 loc) · 18.6 KB
/
updater.py
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import zmq, json, sys
from enum import IntEnum
from threading import Thread
from keras.callbacks import Callback
import keras.backend as K
class Updater(type):
RESTRICTIONS = {
'params': {
'batch_size': True
}
}
"""
DEFINITIONS
"""
class OPERATIONS(IntEnum):
UPDATE, ONE_EPOCH = range(2)
class RESPONSES(IntEnum):
OK, WAIT, ERROR = range(3)
class PRIORITIES(IntEnum):
NORMAL, HIGH, VERY_HIGH = range(3)
NEXT = 'next'
"""
Call method
"""
@staticmethod
def __new__(self, *vargs, **kwargs):
callback = Updater.UpdaterInstance(*vargs, **kwargs)
return [callback] + list(callback.callbacks.values())
"""
CREDENTIALS
"""
@staticmethod
def obtain_credentials(credentials):
if credentials is None:
# No user/password
return None
elif isinstance(credentials, (tuple, list)):
# user, password
if len(credentials) == 1:
user = "root"
password = credentials[0]
elif len(credentials) == 2:
user = credentials[0]
password = credentials[1]
elif isinstance(credentials, dict):
# user/password
user = credentials['user']
password = credentials['password']
elif isinstance(credentials, str):
# user:password
if ':' in credentials:
user, password = credentials.split(':')
else:
user = "root"
password = credentials
return {'user': user, 'password': password}
"""
RESTRICTIONS
"""
@staticmethod
def clear_restrictions(config):
restriction_list = []
def __recusirve_restrictions(path, config, restrictions):
for k, v in restrictions.items():
if isinstance(v, dict):
if isinstance(config, dict):
next_config = config[k]
elif k in pointer.__dict__:
next_config = getattr(config, k)
__recusirve_restrictions(path + [k], next_config, v)
else:
if isinstance(config, dict):
config_dict = config
else:
config_dict = config.__dict__
if k in config_dict and restrictions[k]:
restriction_list.append('->'.join(path) + '->' + str(k))
del config[k]
__recusirve_restrictions([], config, Updater.RESTRICTIONS)
return restriction_list
"""
PARSERS
"""
@staticmethod
def parse_epoch(epoch):
if isinstance(epoch, str) and epoch == 'next':
return Updater.NEXT
elif not isinstance(epoch, int):
raise IndexError
return str(epoch)
@staticmethod
def parse_operation(operation):
if isinstance(operation, str):
if operation.lower() == 'update':
operation = Updater.OPERATIONS.UPDATE
elif operation.lower() == 'next_epoch':
operation = Updater.OPERATIONS.ONE_EPOCH
elif not isinstance(operation, Updater.OPERATIONS):
raise IndexError
return operation
@staticmethod
def parse_priority(priority):
if isinstance(priority, str):
if priority.lower() == 'normal':
priority = Updater.PRIORITIES.NORMAL
elif priority.lower() == 'high':
priority = Updater.PRIORITIES.HIGH
elif priority.lower() == 'very high':
priority = Updater.PRIORITIES.VERY_HIGH
elif not (isinstance(priority, Updater.PRIORITIES) or isinstance(priority, int)):
raise IndexError
return priority
@staticmethod
def package_generator(operation, priority, callbacks, model, params, metrics):
return {
'operation': Updater.parse_operation(operation),
'priority': Updater.parse_priority(priority),
'config': {
'callbacks': callbacks,
'model': model,
'params': params,
'metrics': metrics
}
}
@staticmethod
def send_package(socket, msg_obj):
msg_raw = json.dumps(msg_obj)
socket.send_string(msg_raw)
@staticmethod
def read_package(socket):
msg_raw = socket.recv()
return json.loads(msg_raw.decode('utf-8'))
# Client to use in IPython (for instance) or other processes.
class Client(object):
def __init__(self, ip='127.0.0.1:1717', credentials=None):
self.ip = ip
self.credentials = Updater.obtain_credentials(credentials)
context = zmq.Context()
print("Connecting to server...", end="")
self.socket = context.socket(zmq.REQ)
self.socket.connect("tcp://%s" % self.ip)
print(" done")
def __send_operation(self, container):
print("Sending new config to server...", end="")
messages = []
# If restriction
for epoch, config in container.items():
restriction_list = Updater.clear_restrictions(config['config'])
if len(restriction_list) > 0:
messages.append('The following variables in epoch %s are restricted: %s.' % (str(epoch), ', '.join(restriction_list)))
msg_obj = {
'credentials': self.credentials,
'container': container
}
Updater.send_package(self.socket, msg_obj)
#msg_raw = json.dumps(msg_obj)
#self.socket.send_string(msg_raw)
print(" done")
if len(messages) > 0:
print('\n'.join(messages))
# Response
is_first = True
while True:
#msg_raw = self.socket.recv()
#msg_obj = json.loads(msg_raw)
msg_obj = Updater.read_package(self.socket)
if msg_obj['response'] == Updater.RESPONSES.WAIT:
if is_first:
print("")
is_first = False
if 'messages' in msg_obj and len(msg_obj['messages']) > 0:
print('\n'.join(msg_obj['messages']))
else:
break
if 'messages' in msg_obj and len(msg_obj['messages']) > 0:
print('\n'.join(msg_obj['messages']))
return msg_obj['response'] == Updater.RESPONSES.OK
def only_one_epoch_config(self, epoch='next', callbacks={}, model={}, params={}, metrics={}):
return self.__send_operation({
Updater.parse_epoch(epoch): Updater.package_generator(Updater.OPERATIONS.ONE_EPOCH, -1, callbacks, model, params, metrics)
})
def update_config(self, epoch='next', priority='normal', callbacks={}, model={}, params={}, metrics={}):
return self.__send_operation({
Updater.parse_epoch(epoch): Updater.package_generator(Updater.OPERATIONS.UPDATE, priority, callbacks, model, params, metrics)
})
def start_schedule_config(self):
self.schedule_list = {}
def add_schedule_config_step(self, epoch, operation='update', priority='normal', callbacks={}, model={}, params={}, metrics={}):
self.schedule_list[Updater.parse_epoch(epoch)] = Updater.package_generator(operation, priority, callbacks, model, params, metrics)
def end_schedule_config(self):
if len(self.schedule_list) > 0:
aux = self.__send_operation(self.schedule_list)
self.schedule_list = {}
return aux
return False
class UpdaterInstance(Callback):
"""
ADD UPDATES METHODS
"""
def __add_container(self, container, messages):
# Read every epoch
for epoch, next_config in container.items():
# Check restrictions and remove
restriction_list = Updater.clear_restrictions(next_config['config'])
if epoch not in self.temp_epoch_config:
self.temp_epoch_config[epoch] = []
# Add any config in the correspondent epoch
self.temp_epoch_config[epoch].append({
'operation': next_config['operation'],
'priority': next_config['priority'],
'config': next_config['config']
})
# Add messages about restriccions
if len(restriction_list) > 0:
messages.append('The following variables in epoch %s are restricted: %s.' % (str(epoch), ', '.join(restriction_list)))
def __do_operation(self, container):
try:
print("Sending new config to server...", end="")
messages = []
self.__add_container(self, container, messages)
print(" done")
result = True
except Exception as e:
messages.append('Exception: %s' % str(e))
result = False
if len(messages) > 0:
print('\n'.join(messages))
return result
def only_one_epoch_config(self, epoch='next', callbacks={}, model={}, params={}, metrics={}):
return self.__do_operation({
Updater.parse_epoch(epoch): Updater.package_generator(Updater.OPERATIONS.ONE_EPOCH, -1, callbacks, model, params, metrics)
})
def update_config(self, epoch='next', priority='normal', callbacks={}, model={}, params={}, metrics={}):
return self.__do_operation({
Updater.parse_epoch(epoch): Updater.package_generator(Updater.OPERATIONS.UPDATE, priority, callbacks, model, params, metrics)
})
def start_schedule_config(self):
self.schedule_list = {}
def add_schedule_config_step(self, epoch, operation='update', priority='normal', callbacks={}, model={}, params={}, metrics={}):
self.schedule_list[Updater.parse_epoch(epoch)] = Updater.package_generator(operation, priority, callbacks, model, params, metrics)
def end_schedule_config(self):
if len(self.schedule_list) > 0:
aux = self.__do_operation(self.schedule_list)
self.schedule_list = {}
return aux
return False
"""
RESPONSE
"""
def send_response(self, response, messages):
Updater.send_package(self.socket, {'response': response, 'messages': messages})
"""
CONSUMER ZMQ MESSAGES
"""
def __thread_client_consumer(self):
# Infinite loop to read messages
while not self.__stop_loop:
try:
# Init messages
messages = []
# Read message
msg_raw = self.socket.recv()
msg_obj = json.loads(msg_raw.decode('utf-8'))
# Check credentials
if self.credentials is not None:
if 'credentials' not in msg_obj or \
msg_obj['credentials'] is None or \
msg_obj['credentials']['user'] != self.credentials['user'] or \
msg_obj['credentials']['password'] != self.credentials['password']:
messages.append('Wrong credentials.')
continue
# Load container
container = msg_obj['container']
self.__add_container(container, messages)
messages.append("Enqueued config.")
# Send the message to client
self.send_response(Updater.RESPONSES.OK, messages)
except Exception as e:
# Send the error message to client
messages.append('Exception during enqueuing: %s' % str(e))
self.send_response(Updater.RESPONSES.ERROR, messages)
def __init__(self, ip='127.0.0.1:1717', credentials=None, callbacks={}):
super(Updater.UpdaterInstance, self).__init__()
# Configuration about server
self.credentials = Updater.obtain_credentials(credentials)
self.ip = ip
# Queues and change reverses
self.temp_epoch_config = {}
self.temp_changes = None
# Socket config
context = zmq.Context()
self.socket = context.socket(zmq.REP)
self.socket.bind("tcp://%s" % self.ip)
self.__stop_loop = False
self.thread = Thread(target=self.__thread_client_consumer)
self.thread.start()
# Callbacks that Updater has access
self.callbacks = {}
self.add_callbacks(callbacks)
def __call__(self, *vargs, **kwargs):
self.add_callbacks(vargs)
self.add_callbacks(kwargs)
def add_callbacks(self, callbacks):
if isinstance(callbacks, dict):
self.callbacks.update(callbacks)
elif isinstance(callbacks, (list, tuple)):
for c in callbacks:
self.callbacks[c.__class__.__name__] = c
else:
self.callbacks[callbacks.__class__.__name__] = callbacks
def __update_config(self, config):
def __parse_element(element, value):
if isinstance(element, K.tf.Variable):
# Parse to tf.Variable
return K.tf.Variable(value)
else:
# Map variable->variable
return value
def __recusirve_config(pointer, params):
changes = {}
for k, v in params.items():
# If params are more deeper
if isinstance(v, dict):
# If pointer are also an dict
if isinstance(pointer, dict):
next_pointer = pointer[k]
# If pointer are a object
elif k in pointer.__dict__:
next_pointer = getattr(pointer, k)
# Again do recursive config
changes[k] = __recusirve_config(next_pointer, v)
else:
# If pointer are a dict, parse element and set into the pointer
if isinstance(pointer, dict):
changes[k] = pointer[k]
pointer[k] = __parse_element(changes[k], v)
# If pointer are a object, parse element and set into the pointer
elif k in pointer.__dict__:
changes[k] = getattr(pointer, k)
setattr(pointer, k, __parse_element(changes[k], v))
return changes
changes = {}
# Callbacks
if 'callbacks' in config:
changes['callbacks'] = __recusirve_config(self.callbacks, config['callbacks'])
# model
if 'model' in config:
changes['model'] = __recusirve_config(self.model, config['model'])
# params
"""if 'params' in config:
changes['params'] = __recusirve_config(self.params, config['params'])"""
# model
"""if 'model' in config:
changes['model'] = __recusirve_config(self.model, config['model'])"""
return changes
def on_train_begin(self, logs=None):
super(Updater.UpdaterInstance, self).on_train_begin(logs)
def on_epoch_end(self, epoch, logs=None):
def __get_config_current_epoch(epoch):
configs = self.temp_epoch_config.pop(epoch, None)
if configs is not None:
configs.sort(reverse=False, key=lambda x: x['priority'])
return configs
return None
def __recursive_merge_changes(changes_part, changes):
for k, v in changes_part.items():
if isinstance(v, dict):
if k not in changes:
changes[k] = {}
__recursive_merge_changes(v, changes[k])
else:
changes.setdefault(k, v)
epoch_1 = epoch + 1
try:
messages = []
# 1- Revert old changes in last epochs
if self.temp_changes is not None:
self.__update_config(self.temp_changes)
self.temp_changes = None
# 2- Get configs scheduled
has_configs = False
configs = __get_config_current_epoch(str(epoch_1))
if configs is not None:
has_configs = True
for config in configs:
self.__update_config(config['config'])
# 3- Get configs next epoch
configs = self.temp_epoch_config.pop(Updater.NEXT, None)
if configs is not None:
has_configs = True
self.temp_changes = {}
for config in configs:
changes_part = self.__update_config(config['config'])
if config['operation'] == Updater.OPERATIONS.ONE_EPOCH:
__recursive_merge_changes(changes_part, self.temp_changes)
# 4- Run parent
super(Updater.UpdaterInstance, self).on_epoch_end(epoch, logs)
# 5- Response
#if has_configs:
# self.send_response(Updater.RESPONSES.OK, ["The %s epoch is correct processed." % epoch_1])
except Exception as e:
# Avoid crashes during training
messages.append('Exception during execution: %s' % str(e))
self.send_response(Updater.RESPONSES.ERROR, messages)
def on_train_end(self, logs=None):
super(Updater.UpdaterInstance, self).on_train_end(logs)
self.__stop_loop = True
self.socket.close()
del self.socket