-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
296 lines (242 loc) · 10.3 KB
/
main.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
import importlib
import numpy as np
import os
import random
import mxnet as mx
import metrics.writer as metrics_writer
from client import Client
from server import TopServer, MiddleServer
from baseline_constants import MODEL_PARAMS
from utils.args import parse_args
from utils.model_utils import read_data
def main():
args = parse_args()
ctx = mx.gpu(args.ctx) if args.ctx >= 0 else mx.cpu()
log_dir = os.path.join(
args.log_dir, args.dataset, str(args.log_rank))
os.makedirs(log_dir, exist_ok=True)
log_fn = "output.%i" % args.log_rank
log_file = os.path.join(log_dir, log_fn)
log_fp = open(log_file, "w+")
# Set the random seed, affects client sampling and batching
random.seed(1 + args.seed)
np.random.seed(12 + args.seed)
mx.random.seed(123 + args.seed)
# Import the client model and server model
client_path = "%s/client_model.py" % args.dataset
server_path = "%s/server_model.py" % args.dataset
if not os.path.exists(client_path) \
or not os.path.exists(server_path):
print("Please specify a valid dataset.",
file=log_fp, flush=True)
return
client_path = "%s.client_model" % args.dataset
server_path = "%s.server_model" % args.dataset
mod = importlib.import_module(client_path)
ClientModel = getattr(mod, "ClientModel")
mod = importlib.import_module(server_path)
ServerModel = getattr(mod, "ServerModel")
# learning rate, num_classes, and so on
param_key = "%s.%s" % (args.dataset, args.model)
model_params = MODEL_PARAMS[param_key]
if args.lr != -1:
model_params_list = list(model_params)
model_params_list[0] = args.lr
model_params = tuple(model_params_list)
num_classes = model_params[1]
# Create the shared client model
client_model = ClientModel(
args.seed, args.dataset, args.model, ctx, *model_params)
# Create the shared middle server model
middle_server_model = ServerModel(
client_model, args.dataset, args.model, num_classes, ctx)
middle_merged_update = ServerModel(
None, args.dataset, args.model, num_classes, ctx)
# Create the top server model
top_server_model = ServerModel(
client_model, args.dataset, args.model, num_classes, ctx)
top_merged_update = ServerModel(
None, args.dataset, args.model, num_classes, ctx)
# Create clients
clients, groups = setup_clients(client_model, args)
_ = get_clients_info(clients)
client_ids, client_groups, client_num_samples = _
print("Total number of clients: %d" % len(clients),
file=log_fp, flush=True)
# Measure the global data distribution
global_dist, _, _ = get_clients_dist(
clients, display=False, max_num_clients=20, metrics_dir=args.metrics_dir)
# Create middle servers
middle_servers = setup_middle_servers(
middle_server_model, middle_merged_update, groups)
# [middle_servers[i].brief(log_fp) for i in range(args.num_groups)]
print("Total number of middle servers: %d" % len(middle_servers),
file=log_fp, flush=True)
# Create the top server
top_server = TopServer(
top_server_model, top_merged_update, middle_servers)
# Display initial status
print("--- Random Initialization ---",
file=log_fp, flush=True)
stat_writer_fn = get_stat_writer_function(
client_ids, client_groups, client_num_samples, args)
print_stats(
0, top_server, client_num_samples, stat_writer_fn,
args.use_val_set, log_fp)
# Training simulation
for r in range(1, args.num_rounds+1):
print("--- Round %d of %d ---" % (r, args.num_rounds),
file=log_fp, flush=True)
# Simulate training on selected clients
top_server.train_model(
r, args.num_syncs, args.clients_per_group,
args.sampler, args.batch_size, global_dist)
# Test model
if r % args.eval_every == 0 or r == args.num_rounds:
print_stats(
r, top_server, client_num_samples, stat_writer_fn,
args.use_val_set, log_fp)
# Save the top server model
top_server.save_model(log_dir)
log_fp.close()
def create_clients(users, groups, train_data, test_data, model, args):
# Randomly assign a group to each client, if groups are not given
random.seed(args.seed)
if len(groups) == 0:
groups = [random.randint(0, args.num_groups - 1)
for _ in users]
# Instantiate clients
clients = [Client(args.seed, u, g, train_data[u],
test_data[u], model, args.batch_size)
for u, g in zip(users, groups)]
return clients
def group_clients(clients, num_groups):
"""Collect clients of each group into a list.
Args:
clients: List of all client objects.
num_groups: Number of groups.
Returns:
groups: List of clients in each group.
"""
groups = [[] for _ in range(num_groups)]
for c in clients:
groups[c.group].append(c)
return groups
def setup_clients(model, args):
"""Load train, test data and instantiate clients.
Args:
model: The shared ClientModel object for all clients.
args: Args entered from the command.
Returns:
clients: List of all client objects.
groups: List of clients in each group.
"""
eval_set = "test" if not args.use_val_set else "val"
train_data_dir = os.path.join("data", args.dataset, "data", "train")
test_data_dir = os.path.join("data", args.dataset, "data", eval_set)
data = read_data(train_data_dir, test_data_dir)
users, groups, train_data, test_data = data
clients = create_clients(
users, groups, train_data, test_data, model, args)
groups = group_clients(clients, args.num_groups)
return clients, groups
def get_clients_info(clients):
"""Returns the ids, groups and num_samples for the given clients.
Args:
clients: List of Client objects.
Returns:
ids: List of client_ids for the given clients.
groups: Map of {client_id: group_id} for the given clients.
num_samples: Map of {client_id: num_samples} for the given
clients.
"""
ids = [c.id for c in clients]
groups = {c.id: c.group for c in clients}
num_samples = {c.id: c.num_samples for c in clients}
return ids, groups, num_samples
def get_clients_dist(
clients, display=False, max_num_clients=20, metrics_dir="metrics"):
"""Return the global data distribution of all clients.
Args:
clients: List of Client objects.
display: Visualize data distribution when set to True.
max_num_clients: Maximum number of clients to plot.
metrics_dir: Directory to save metrics files.
Returns:
global_dist: List of num samples for each class.
global_train_dist: List of num samples for each class in train set.
global_test_dist: List of num samples for each class in test set.
"""
global_train_dist = sum([c.train_sample_dist for c in clients])
global_test_dist = sum([c.test_sample_dist for c in clients])
global_dist = global_train_dist + global_test_dist
if display:
try:
from metrics.visualization_utils import plot_clients_dist
np.random.seed(0)
rand_clients = np.random.choice(clients, max_num_clients)
plot_clients_dist(clients=rand_clients,
global_dist=global_dist,
global_train_dist=global_train_dist,
global_test_dist=global_test_dist,
draw_mean=False,
metrics_dir=metrics_dir)
except ModuleNotFoundError:
pass
return global_dist, global_train_dist, global_test_dist
def setup_middle_servers(server_model, merged_update, groups):
"""Instantiates middle servers based on given ServerModel objects.
Args:
server_model: A shared ServerModel object to store the middle
server model.
merged_update: A shared ServerModel object to merge updates
from clients.
groups: List of clients in each group.
Returns:
middle_servers: List of all middle servers.
"""
num_groups = len(groups)
middle_servers = [
MiddleServer(g, server_model, merged_update, groups[g])
for g in range(num_groups)]
return middle_servers
def get_stat_writer_function(ids, groups, num_samples, args):
def writer_fn(num_round, metrics, partition):
metrics_writer.print_metrics(
num_round, ids, metrics, groups, num_samples,
partition, args.metrics_dir, "{}_{}_{}".format(
args.metrics_name, "stat", args.log_rank))
return writer_fn
def print_stats(num_round, server, num_samples, writer, use_val_set, log_fp=None):
train_stat_metrics = server.test_model(set_to_use="train")
print_metrics(
train_stat_metrics, num_samples, prefix="train_", log_fp=log_fp)
writer(num_round, train_stat_metrics, "train")
eval_set = "test" if not use_val_set else "val"
test_stat_metrics = server.test_model(set_to_use=eval_set)
print_metrics(
test_stat_metrics, num_samples, prefix="{}_".format(eval_set), log_fp=log_fp)
writer(num_round, test_stat_metrics, eval_set)
def print_metrics(metrics, weights, prefix="", log_fp=None):
"""Prints weighted averages of the given metrics.
Args:
metrics: Dict with client ids as keys. Each entry is a dict
with the metrics of that client.
weights: Dict with client ids as keys. Each entry is the weight
for that client.
prefix: String, "train_" or "test_".
log_fp: File pointer for logs.
"""
ordered_weights = [weights[c] for c in sorted(weights)]
metric_names = metrics_writer.get_metrics_names(metrics)
for metric in metric_names:
ordered_metric = [metrics[c][metric] for c in sorted(metrics)]
print("%s: %g, 10th percentile: %g, 50th percentile: %g, 90th percentile %g" \
% (prefix + metric,
np.average(ordered_metric, weights=ordered_weights),
np.percentile(ordered_metric, 10),
np.percentile(ordered_metric, 50),
np.percentile(ordered_metric, 90)),
file=log_fp, flush=True)
if __name__ == "__main__":
main()