-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfl_server_unsupervised.py
223 lines (151 loc) · 6.78 KB
/
fl_server_unsupervised.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
import socket
import pickle
import select
import time
import numpy as np
import pandas as pd
import sys
import logging
from timeit import default_timer as timer
import gc
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s : [%(levelname)s] %(message)s',
handlers=[
logging.FileHandler('server.log'),
logging.StreamHandler(sys.stdout)
]
)
class Server:
def __init__(self, MODEL, ROUNDS , weights_path, graph_id, MAX_CONN = 2, IP= socket.gethostname(), PORT = 5000, HEADER_LENGTH = 10 ):
# Parameters
self.HEADER_LENGTH = HEADER_LENGTH
self.IP = IP
self.PORT = PORT
self.MAX_CONN = MAX_CONN
self.ROUNDS = ROUNDS
self.weights_path = weights_path
self.graph_id = graph_id
# Global model
self.GLOBAL_WEIGHTS = MODEL.get_weights()
self.global_modlel_ready = False
self.weights = []
self.training_cycles = 0
self.stop_flag = False
# List of sockets for select.select()
self.sockets_list = []
self.clients = {}
self.client_ids = {}
# Craete server socket
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.IP, self.PORT))
self.server_socket.listen(self.MAX_CONN)
self.sockets_list.append(self.server_socket)
def update_model(self,new_weights):
self.weights.append(new_weights)
if len(self.weights) == self.MAX_CONN:
new_weights = np.mean(self.weights, axis=0)
self.weights = []
#self.GLOBAL_MODEL.set_weights(new_weights)
self.GLOBAL_WEIGHTS = new_weights
self.training_cycles += 1
# weights file name : global_weights_graphid.npy
weights_path = self.weights_path + 'global_weights_' + self.graph_id + ".npy"
np.save(weights_path,new_weights)
logging.info("Training cycle %s done!", self.training_cycles)
for soc in self.sockets_list[1:]:
self.send_model(soc)
def send_model(self, client_socket):
if self.ROUNDS == self.training_cycles:
self.stop_flag = True
weights = np.array(self.GLOBAL_WEIGHTS)
data = {"STOP_FLAG":self.stop_flag,"WEIGHTS":weights}
data = pickle.dumps(data)
data = bytes(f"{len(data):<{self.HEADER_LENGTH}}", 'utf-8') + data
client_socket.sendall(data)
logging.info('Sent global model to client-%s at %s:%s',self.client_ids[client_socket],*self.clients[client_socket])
def receive(self, client_socket):
try:
message_header = client_socket.recv(self.HEADER_LENGTH)
if not len(message_header):
logging.error('Client-%s closed connection at %s:%s',self.client_ids[client_socket], *self.clients[client_socket])
return False
message_length = int(message_header.decode('utf-8').strip())
#full_msg = client_socket.recv(message_length)
full_msg = b''
while True:
msg = client_socket.recv(message_length)
full_msg += msg
if len(full_msg) == message_length:
break
return pickle.loads(full_msg)
except Exception as e:
logging.error('Client-%s closed connection at %s:%s',self.client_ids[client_socket], *self.clients[client_socket])
return False
def run(self):
while not self.stop_flag:
read_sockets, write_sockets, exception_sockets = select.select(self.sockets_list, [], self.sockets_list)
for notified_socket in read_sockets:
if notified_socket == self.server_socket:
client_socket, client_address = self.server_socket.accept()
self.sockets_list.append(client_socket)
self.clients[client_socket] = client_address
self.client_ids[client_socket] = "new"
logging.info('Accepted new connection at %s:%s',*client_address)
self.send_model(client_socket)
else:
message = self.receive(notified_socket)
if message is False:
self.sockets_list.remove(notified_socket)
del self.clients[notified_socket]
continue
else:
client_id = message['CLIENT_ID']
weights = message['WEIGHTS']
self.client_ids[notified_socket] = client_id
logging.info('Recieved model from client-%s at %s:%s',client_id, *self.clients[notified_socket])
self.update_model(weights)
for notified_socket in exception_sockets:
self.sockets_list.remove(notified_socket)
del self.clients[notified_socket]
if __name__ == "__main__":
from models.unsupervised import Model
arg_names = [
'path_weights',
'path_nodes',
'path_edges',
'graph_id',
'partition_id',
'num_clients',
'num_rounds',
'IP',
'PORT'
]
args = dict(zip(arg_names, sys.argv[1:]))
logging.warning('Server started , graph ID %s, number of clients %s, number of rounds %s',args['graph_id'],args['num_clients'],args['num_rounds'])
if 'IP' not in args.keys() or args['IP'] == 'localhost':
args['IP'] = socket.gethostname()
if 'PORT' not in args.keys():
args['PORT'] = 5000
path_nodes = args['path_nodes'] + args['graph_id'] + '_nodes_' + args['partition_id'] + ".csv"
nodes = pd.read_csv(path_nodes,index_col=0)
nodes = nodes.astype("float32")
path_edges = args['path_edges'] + args['graph_id'] + '_edges_' + args['partition_id'] + ".csv"
edges = pd.read_csv(path_edges)
edges = edges.astype({"source":"uint32","target":"uint32"})
model = Model(nodes,edges)
model.initialize()
logging.info('Model initialized')
server = Server(model,ROUNDS=int(args['num_rounds']),weights_path=args['path_weights'],graph_id=args['graph_id'],MAX_CONN=int(args['num_clients']),IP=args['IP'],PORT=int(args['PORT']))
del nodes
del edges
del model
gc.collect()
logging.info('Federated training started!')
start = timer()
server.run()
end = timer()
elapsed_time = end -start
logging.info('Federated training done!')
logging.warning('Training report : Elapsed time %s seconds, graph ID %s, number of clients %s, number of rounds %s',elapsed_time,args['graph_id'],args['num_clients'],args['num_rounds'])