-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
179 lines (142 loc) · 5.44 KB
/
manager.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
"""Manager"""
import json
import threading
import time
import pygame
import tensorflow as tf
import keras
from keras.datasets import mnist
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dense, Dropout, Flatten
from keras.models import Sequential
import numpy as np
from messages import Init, Init_Response, Pull, Pull_Response, Push
from messages import Message, Terminate, Empty, Heartbeat
import comms.server as srvr
from visualization import visualization as vis
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
class Worker():
def __init__(self, name):
self.name = name
self.active = True
self.last_heard = time.time()
workerlist = [] # I'm sure this won't cause any problems /s
def init_model():
"""Create the model.
Taken from <https://keras.io/examples/mnist_cnn/>
TODO: Warm start
"""
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# (x_train, y_train), (x_test, y_test) = mnist.load_data()
# x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
# x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
model = Sequential()
model.add(keras.layers.Dense(512, activation="relu", input_shape=(784,)) )
model.add(keras.layers.Dense(512, activation="relu"))
# We need the activation function in this last layer to be "softmax" because we want the model to choose 1 out of the 10 classes
model.add(keras.layers.Dense(10, activation="softmax"))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
def worker_watchdog():
CHANGE = pygame.USEREVENT+1
REMOVE = pygame.USEREVENT+3
while 1:
for worker in list(workerlist):
now = time.time()
if worker.active:
if now - worker.last_heard > 8:
worker.active = False
my_event = pygame.event.Event(CHANGE, name=worker.name)
pygame.event.post(my_event)
else:
if now - worker.last_heard < 8:
worker.active = True
my_event = pygame.event.Event(CHANGE, name=worker.name)
pygame.event.post(my_event)
# elif now - worker.last_heard > 12:
# workerlist.remove(worker)
# my_event = pygame.event.Event(REMOVE, name=worker.name)
# pygame.event.post(my_event)
time.sleep(1)
def response_policy(model, msg_json: str):
"""The manager's response policy to different messages.
Returns: json formatted string
"""
# Create User Events (Fingers crossed that these stay in sync)
CHANGE = pygame.USEREVENT+1
ADD = pygame.USEREVENT+2
REMOVE = pygame.USEREVENT+3
PING = pygame.USEREVENT+4
msg_dict = json.loads(msg_json)
if msg_dict["type"] == "init":
msg = Init(msg_dict)
my_event = pygame.event.Event(ADD, name=msg.hostname)
pygame.event.post(my_event)
workerlist.append(Worker(msg.hostname))
resp_obj = Init_Response()
serialized = model.to_json()
resp_obj.model = serialized
elif msg_dict["type"] == "pull":
msg = Pull(msg_dict)
my_event = pygame.event.Event(PING, push=False, name=msg.hostname)
pygame.event.post(my_event)
for worker in workerlist:
if worker.name == msg.hostname:
worker.last_heard = time.time()
resp_obj = Pull_Response()
weights_np = model.get_weights()
weights = []
for weight in weights_np:
weights.append(weight.tolist())
resp_obj.weights = weights
elif msg_dict["type"] == "push":
msg = Push(msg_dict)
my_event = pygame.event.Event(PING, push=True, name=msg.hostname)
pygame.event.post(my_event)
for worker in workerlist:
if worker.name == msg.hostname:
worker.last_heard = time.time()
remote_weights_np = []
for remote_weight in msg.weights:
remote_weights_np.append(np.array(remote_weight))
weights = []
weights.append(remote_weights_np)
weights.append(model.get_weights())
# <https://stackoverflow.com/questions/48212110/average-weights-in-keras-models>
# <https://arxiv.org/abs/1803.05407>
new_weights = []
for weights_list_tuple in zip(*weights):
new_weights.append(
[np.array(weights_).mean(axis=0)\
for weights_ in zip(*weights_list_tuple)])
model.set_weights(new_weights)
resp_obj = Empty()
elif msg_dict["type"] == "heartbeat":
msg = Heartbeat(msg_dict)
for worker in workerlist:
if worker.name == msg.hostname:
worker.last_heard = time.time()
resp_obj = Empty()
else:
raise TypeError("Didn't receive a known message type.")
resp = json.dumps(resp_obj.__dict__)
return resp
def main():
# Initialize Visualization
vis_thread = threading.Thread(target=vis.main)
vis_thread.start()
watch_thread = threading.Thread(target=worker_watchdog)
watch_thread.start()
# GO
model = init_model()
server = srvr.Server(model, response_policy)
server.run()
if __name__ == "__main__":
main()