-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.py
510 lines (446 loc) · 19 KB
/
algorithm.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
"""
"""
from copy import deepcopy
from typing import Any, Dict, List, Optional, Sequence
import numpy as np
import torch
from sklearn.cluster import DBSCAN
from sklearn_extra.cluster import KMedoids
from torch_ecg.utils.misc import add_docstring
from tqdm.auto import tqdm
try:
from fl_sim.algorithms import register_algorithm
from fl_sim.algorithms.fedopt import FedAvgClient as BaseClient
from fl_sim.algorithms.fedopt import FedAvgClientConfig as BaseClientConfig
from fl_sim.algorithms.fedopt import FedAvgServer as BaseServer
from fl_sim.algorithms.fedopt import FedAvgServerConfig as BaseServerConfig
from fl_sim.nodes import ClientMessage
_base_algorithm = "FedAvg"
except ModuleNotFoundError:
# not installed,
# import from the submodule instead
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent / "fl-sim"))
from fl_sim.algorithms import register_algorithm
from fl_sim.algorithms.fedopt import FedAvgClient as BaseClient
from fl_sim.algorithms.fedopt import FedAvgClientConfig as BaseClientConfig
from fl_sim.algorithms.fedopt import FedAvgServer as BaseServer
from fl_sim.algorithms.fedopt import FedAvgServerConfig as BaseServerConfig
from fl_sim.nodes import ClientMessage
_base_algorithm = "FedAvg"
__all__ = [
"LCFLServer",
"LCFLClient",
"LCFLServerConfig",
"LCFLClientConfig",
]
@register_algorithm()
class LCFLServerConfig(BaseServerConfig):
"""Server config for the LCFL algorithm.
Parameters
----------
num_clusters : int
The number of clusters.
num_iters : int
The number of (outer) iterations.
num_clients : int
The number of clients.
clients_sample_ratio : float, default 1
The ratio of clients to participate in each round.
cluster_method : str, default "kmedoids"
The clustering method to use based on the distance matrix.
Currently only support "kmedoids" and "dbscan".
num_warmup_iters : int, default 10
The number of warmup iterations.
local_warmup : bool, default False
Whether to use local warmup or federated warmup.
warmup_clients_sample_ratio : float, optional
The ratio of clients to participate in each warmup round.
If not specified, will use the same ratio as ``clients_sample_ratio``.
**kwargs : dict, optional
Additional keyword arguments:
- ``log_dir`` : str or Path, optional
The log directory.
If not specified, will use the default log directory.
If not absolute, will be relative to the default log directory.
- ``txt_logger`` : bool, default True
Whether to use txt logger.
- ``json_logger`` : bool, default True
Whether to use json logger.
- ``eval_every`` : int, default 1
The number of iterations to evaluate the model.
- ``seed`` : int, default 0
The random seed.
- ``tag`` : str, optional
The tag of the experiment.
- ``verbose`` : int, default 1
The verbosity level.
- ``gpu_proportion`` : float, default 0.2
The proportion of clients to use GPU.
Used to similate the system heterogeneity of the clients.
Not used in the current version, reserved for future use.
"""
__name__ = "LCFLServerConfig"
def __init__(
self,
num_clusters: int,
num_iters: int,
num_clients: int,
clients_sample_ratio: float = 1,
cluster_method: str = "kmedoids",
num_warmup_iters: int = 10,
local_warmup: bool = False,
warmup_clients_sample_ratio: Optional[float] = None,
**kwargs: Any,
) -> None:
super().__init__(
num_iters,
num_clients,
clients_sample_ratio=clients_sample_ratio,
**kwargs,
)
self.algorithm = "LCFL"
self.num_clusters = num_clusters
self.cluster_method = cluster_method
self.num_warmup_iters = num_warmup_iters
self.local_warmup = local_warmup
self.warmup_clients_sample_ratio = warmup_clients_sample_ratio or clients_sample_ratio
@register_algorithm()
class LCFLClientConfig(BaseClientConfig):
"""Client config for the LCFL algorithm.
Parameters
----------
batch_size : int
The batch size.
num_epochs : int
The number of epochs.
lr : float, default 1e-2
The learning rate.
**kwargs : dict, optional
Additional keyword arguments:
- ``scheduler`` : dict, optional
The scheduler config.
None for no scheduler, using constant learning rate.
- ``verbose`` : int, default 1
The verbosity level.
- ``latency`` : float, default 0.0
The latency of the client.
Not used in the current version, reserved for future use.
"""
__name__ = "LCFLClientConfig"
def __init__(
self,
batch_size: int,
num_epochs: int,
lr: float = 1e-2,
**kwargs: Any,
) -> None:
super().__init__(
batch_size=batch_size,
num_epochs=num_epochs,
lr=lr,
**kwargs,
)
self.algorithm = "LCFL"
@register_algorithm()
@add_docstring(BaseServer.__doc__.replace(_base_algorithm, "LCFL"))
class LCFLServer(BaseServer):
__name__ = "LCFLServer"
def _post_init(self) -> None:
"""
check if all required field in the config are set,
check compatibility of server and client configs,
and set cluster centers
"""
super()._post_init()
assert self.config.num_clusters > 0
if self.config.cluster_method.lower() == "kmedoids":
self._cluster_method = KMedoids(n_clusters=self.config.num_clusters, metric="precomputed")
elif self.config.cluster_method.lower() == "dbscan":
self._cluster_method = DBSCAN(metric="precomputed")
else:
raise ValueError(
"Currenly only support 'dbscan' and 'kmedoids' as cluster method, " f"got {self.config.cluster_method}"
)
# self._cluster_centers = {
# i: {"center_model": deepcopy(self.model), "client_ids": []}
# for i in range(self.config.num_clusters)
# }
self._cluster_centers = None
@property
def client_cls(self) -> type:
return LCFLClient
@property
def config_cls(self) -> Dict[str, type]:
return {
"server": LCFLServerConfig,
"client": LCFLClientConfig,
}
@property
def required_config_fields(self) -> List[str]:
return ["num_clusters", "num_warmup_iters", "local_warmup"]
def communicate(self, target: "LCFLClient") -> None:
"""Send cluster centers to client"""
if self._cluster_centers is None:
# the warm up stage
if self.config.local_warmup:
# local warmup, do nothing on the server side
return
super().communicate(target)
else:
# NOTE: for simplicity,
# the clustering is done on the server side,
# which indeed should be done on the clients
# send cluster centers to client
target._received_messages = {
"parameters": [
p.detach().clone() for p in self._cluster_centers[target._cluster_id]["center_model"].parameters()
]
}
@torch.no_grad()
def update(self) -> None:
"""Update cluster centers"""
if self._cluster_centers is None:
# the warm up stage
if self.config.local_warmup:
# local warmup, do nothing on the server side
return
super().update()
else:
# federated training on each cluster
for cluster_id in self._cluster_centers:
cluster_center = self._cluster_centers[cluster_id]
cluster_size = len(cluster_center["client_ids"])
alpha = 1 / (cluster_size * self.config.clients_sample_ratio)
# average the cluster center model using received parameters
for idx, p in enumerate(cluster_center["center_model"].parameters()):
# add received delta_parameters to the cluster center model
for m in self._received_messages:
if m["cluster_id"] != cluster_id:
continue
p.add_(m["delta_parameters"][idx].to(self.device), alpha=alpha)
def train_federated(self, extra_configs: Optional[dict] = None) -> None:
"""Federated (distributed) training,
conducted on the clients and the server.
Parameters
----------
extra_configs : dict, optional
The extra configs for federated training.
Returns
-------
None
TODO
----
Run clients training in parallel.
"""
if self._complete_experiment:
# reset before training if a previous experiment is completed
self._reset()
self._logger_manager.log_message("Training federated...")
# warm up stage
self._warmup()
# cluster clients
self._perform_clustering()
# perform federated training on each cluster
self._train_cluster_federated()
self._logger_manager.log_message("Federated training finished...")
self._logger_manager.flush()
# self._logger_manager.reset()
self._complete_experiment = True
def _warmup(self) -> None:
"""Warm up stage."""
self._logger_manager.log_message("Warming up...")
self.n_iter = 0
with tqdm(
range(self.config.num_warmup_iters),
total=self.config.num_warmup_iters,
desc=f"{self.config.algorithm} Warmup",
unit="iter",
mininterval=1.0,
) as outer_pbar:
for self.n_iter in outer_pbar:
# selected_clients = list(range(self.config.num_clients))
selected_clients = self._sample_clients(clients_sample_ratio=self.config.warmup_clients_sample_ratio)
with tqdm(
total=len(selected_clients),
desc=f"Warm Up Iter {self.n_iter+1}/{self.config.num_warmup_iters}",
unit="client",
mininterval=max(1, len(selected_clients) // 20),
disable=self.config.verbose < 1,
) as pbar:
for client_id in selected_clients:
client = self._clients[client_id]
if not self.config.local_warmup:
self._communicate(client)
if (self.n_iter > 0) and ((self.n_iter + 1) % self.config.eval_every == 0):
for part in self.dataset.data_parts:
metrics = client.evaluate(part)
metrics["cluster_id"] = -1
# print(f"metrics: {metrics}")
self._logger_manager.log_metrics(
client_id,
metrics,
step=self.n_iter,
epoch=self.n_iter,
part=part,
)
client._update()
if not self.config.local_warmup:
client._communicate(self)
pbar.update(1)
if (
(self.n_iter > 0)
and ((self.n_iter + 1) % self.config.eval_every == 0)
and (not self.config.local_warmup)
):
self.aggregate_client_metrics()
if not self.config.local_warmup:
self._update()
@torch.no_grad()
def _perform_clustering(self) -> None:
"""Perform clustering.
For simplicity, we omit the transimission of client models
and directly use the client models for clustering
the transmission of distance vectors is also simplified
dist stored in server.
"""
self._logger_manager.log_message("Perform clustering...")
dist = {client_id: np.zeros((self.config.num_clients,)) for client_id in range(self.config.num_clients)}
with tqdm(
range(self.config.num_clients),
total=self.config.num_clients,
desc="Compute distance vectors",
unit="client",
mininterval=5.0,
) as pbar:
for client_id in pbar:
client = self._clients[client_id]
client.model.eval()
client_data, client_label = client.get_all_data()
half_dist_vec = np.zeros(len(self._clients))
for another_client_id in range(self.config.num_clients):
# server broadcast model parameters of another_client_id
# to client_id
if client_id == another_client_id:
continue
another_client = self._clients[another_client_id]
another_client.model.eval()
half_dist_vec[another_client_id] = (
client.criterion(
client.model(client_data.to(client.model.device)),
client_label.to(client.model.device),
).cpu()
- another_client.criterion(
another_client.model(client_data.to(another_client.model.device)),
client_label.to(another_client.model.device),
).cpu()
).abs()
# transmit half_dist_vec to server
dist[client_id] = half_dist_vec
dist_mat = np.zeros((self.config.num_clients, self.config.num_clients))
for i in range(self.config.num_clients - 1):
for j in range(i + 1, self.config.num_clients):
dist_mat[i][j] = dist[i][j] + dist[j][i]
# the diagonal of dist_mat is 0, so we can simply add dist_mat and its transpose
dist_mat = dist_mat + dist_mat.T
if isinstance(self._cluster_method, DBSCAN):
self._cluster_method.eps = np.percentile(dist_mat, 100 / (self.config.num_clusters - 1))
cluster_ids = self._cluster_method.fit_predict(dist_mat)
# form cluster centers
self._cluster_centers = {
i: {
"center_model": deepcopy(self.model),
"client_ids": np.where(cluster_ids == i)[0].tolist(),
}
for i in np.unique(cluster_ids)
}
# assign cluster id to clients
for client_id in range(self.config.num_clients):
self._clients[client_id]._cluster_id = cluster_ids[client_id]
def _train_cluster_federated(self) -> None:
"""Perform federated training on each cluster."""
self._logger_manager.log_message("Perform federated training on each cluster...")
total_iters = self.config.num_warmup_iters + self.config.num_iters
with tqdm(
range(self.config.num_warmup_iters, total_iters),
total=self.config.num_iters,
desc=f"{self.config.algorithm} Clustered Federated Training",
unit="iter",
mininterval=1.0,
) as outer_pbar:
for self.n_iter in outer_pbar:
for cluster_id in self._cluster_centers:
# selected_clients = self._cluster_centers[cluster_id]["client_ids"]
selected_clients = self._sample_clients(
subset=self._cluster_centers[cluster_id]["client_ids"],
clients_sample_ratio=self.config.clients_sample_ratio,
)
with tqdm(
total=len(selected_clients),
desc=f"Iter {self.n_iter+1}/{total_iters} | Cluster {cluster_id}",
unit="client",
mininterval=max(1, len(selected_clients) // 20),
disable=self.config.verbose < 1,
) as pbar:
for client_id in selected_clients:
client = self._clients[client_id]
self._communicate(client)
if (self.n_iter + 1) % self.config.eval_every == 0:
for part in self.dataset.data_parts:
metrics = client.evaluate(part)
metrics["cluster_id"] = cluster_id
# print(f"metrics: {metrics}")
self._logger_manager.log_metrics(
client_id,
metrics,
step=self.n_iter,
epoch=self.n_iter,
part=part,
)
client._update()
client._communicate(self)
pbar.update(1)
if (self.n_iter + 1) % self.config.eval_every == 0:
self.aggregate_client_metrics()
self._update()
def aggregate_client_metrics(self, ignore: Sequence[str] = ["cluster_id"]) -> None:
"""Aggregate the metrics transmitted from the clients.
Parameters
----------
ignore : Sequence[str], default ["cluster_id"]
The metrics to ignore.
Returns
-------
None
"""
super().aggregate_client_metrics(ignore=ignore)
@property
def doi(self) -> List[str]:
return ["10.48550/arXiv.2407.09360"]
@register_algorithm()
@add_docstring(BaseClient.__doc__.replace(_base_algorithm, "LCFL"))
class LCFLClient(BaseClient):
__name__ = "LCFLClient"
def _post_init(self) -> None:
"""
check if all required field in the config are set,
and set attributes for maintaining itermidiate states
"""
super()._post_init()
self._cluster_id = -1
@property
def required_config_fields(self) -> List[str]:
return []
def communicate(self, target: "LCFLServer") -> None:
delta_parameters = self.get_detached_model_parameters()
for dp, rp in zip(delta_parameters, self._cached_parameters):
dp.data.add_(rp.data, alpha=-1)
message = {
"client_id": self.client_id,
"cluster_id": self._cluster_id,
"delta_parameters": delta_parameters,
"train_samples": len(self.train_loader.dataset),
"metrics": self._metrics,
}
target._received_messages.append(ClientMessage(**message))