-
Notifications
You must be signed in to change notification settings - Fork 4
/
aggregator.py
996 lines (757 loc) · 33.8 KB
/
aggregator.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
import os
import time
import random
from abc import ABC, abstractmethod
from copy import deepcopy
import numpy as np
import numpy.linalg as LA
from sklearn.metrics import pairwise_distances
from sklearn.cluster import AgglomerativeClustering
from utils.torch_utils import *
class Aggregator(ABC):
r""" Base class for Aggregator. `Aggregator` dictates communications between clients
Attributes
----------
clients: List[Client]
test_clients: List[Client]
global_learners_ensemble: List[Learner]
sampling_rate: proportion of clients used at each round; default is `1.`
sample_with_replacement: is True, client are sampled with replacement; default is False
n_clients:
n_learners:
clients_weights:
model_dim: dimension if the used model
c_round: index of the current communication round
log_freq:
verbose: level of verbosity, `0` to quiet, `1` to show global logs and `2` to show local logs; default is `0`
global_train_logger:
global_test_logger:
rng: random number generator
np_rng: numpy random number generator
Methods
----------
__init__
mix
update_clients
update_test_clients
write_logs
save_state
load_state
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
sampling_rate=1.,
sample_with_replacement=False,
test_clients=None,
verbose=0,
seed=None,
*args,
**kwargs
):
rng_seed = (seed if (seed is not None and seed >= 0) else int(time.time()))
self.rng = random.Random(rng_seed)
self.np_rng = np.random.default_rng(rng_seed)
if test_clients is None:
test_clients = []
self.clients = clients
self.test_clients = test_clients
self.global_learners_ensemble = global_learners_ensemble
self.device = self.global_learners_ensemble.device
self.log_freq = log_freq
self.verbose = verbose
self.global_train_logger = global_train_logger
self.global_test_logger = global_test_logger
self.model_dim = self.global_learners_ensemble.model_dim
self.n_clients = len(clients)
self.n_test_clients = len(test_clients)
self.n_learners = len(self.global_learners_ensemble)
self.clients_weights =\
torch.tensor(
[client.n_train_samples for client in self.clients],
dtype=torch.float32
)
self.clients_weights = self.clients_weights / self.clients_weights.sum()
self.sampling_rate = sampling_rate
self.sample_with_replacement = sample_with_replacement
self.n_clients_per_round = max(1, int(self.sampling_rate * self.n_clients))
self.sampled_clients = list()
self.c_round = 0
self.write_logs()
# Custom -- added for Krum aggregation
self.krum_mode = False
self.exp_adv_nodes = 0
@abstractmethod
def mix(self):
pass
@abstractmethod
def update_clients(self):
pass
def update_test_clients(self):
for client in self.test_clients:
for learner_id, learner in enumerate(client.learners_ensemble):
copy_model(target=learner.model, source=self.global_learners_ensemble[learner_id].model)
for client in self.test_clients:
client.update_sample_weights()
client.update_learners_weights()
def write_logs(self):
self.update_test_clients()
for global_logger, clients in [
(self.global_train_logger, self.clients),
(self.global_test_logger, self.test_clients)
]:
if len(clients) == 0:
continue
global_train_loss = 0.
global_train_acc = 0.
global_test_loss = 0.
global_test_acc = 0.
total_n_samples = 0
total_n_test_samples = 0
for client_id, client in enumerate(clients):
train_loss, train_acc, test_loss, test_acc = client.write_logs()
if self.verbose > 1:
print("*" * 30)
print(f"Client {client_id}..")
with np.printoptions(precision=3, suppress=True):
print("Pi: ", client.learners_weights.numpy())
print(f"Train Loss: {train_loss:.3f} | Train Acc: {train_acc * 100:.3f}%|", end="")
print(f"Test Loss: {test_loss:.3f} | Test Acc: {test_acc * 100:.3f}% |")
global_train_loss += train_loss * client.n_train_samples
global_train_acc += train_acc * client.n_train_samples
global_test_loss += test_loss * client.n_test_samples
global_test_acc += test_acc * client.n_test_samples
total_n_samples += client.n_train_samples
total_n_test_samples += client.n_test_samples
global_train_loss /= total_n_samples
global_test_loss /= total_n_test_samples
global_train_acc /= total_n_samples
global_test_acc /= total_n_test_samples
if self.verbose > 0:
print("+" * 30)
print("Global..")
print(f"Train Loss: {global_train_loss:.3f} | Train Acc: {global_train_acc * 100:.3f}% |", end="")
print(f"Test Loss: {global_test_loss:.3f} | Test Acc: {global_test_acc * 100:.3f}% |")
print("+" * 50)
global_logger.add_scalar("Train/Loss", global_train_loss, self.c_round)
global_logger.add_scalar("Train/Metric", global_train_acc, self.c_round)
global_logger.add_scalar("Test/Loss", global_test_loss, self.c_round)
global_logger.add_scalar("Test/Metric", global_test_acc, self.c_round)
if self.verbose > 0:
print("#" * 80)
def save_state(self, dir_path):
"""
save the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
as `.pt` file, and `learners_weights` for each client in `self.clients` as a single numpy array (`.np` file).
:param dir_path:
"""
for learner_id, learner in enumerate(self.global_learners_ensemble):
save_path = os.path.join(dir_path, f"chkpts_{learner_id}.pt")
torch.save(learner.model.state_dict(), save_path)
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
save_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
for client_id, client in enumerate(clients):
weights[client_id] = client.learners_ensemble.learners_weights
np.save(save_path, weights)
def save_state_intermed(self, dir_path, round_no):
"""
save intermediate state with round number
"""
for learner_id, learner in enumerate(self.global_learners_ensemble):
temp_str = f"chkpts_{learner_id}_r" + str(round_no)+ ".pt"
save_path = os.path.join(dir_path, temp_str)
torch.save(learner.model.state_dict(), save_path)
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
temp_str = f"{mode}_client_weights_r" + str(round_no) + ".npy"
save_path = os.path.join(dir_path, temp_str)
for client_id, client in enumerate(clients):
weights[client_id] = client.learners_ensemble.learners_weights
np.save(save_path, weights)
def load_state(self, dir_path):
"""
load the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
from a `.pt` file, and `learners_weights` for each client in `self.clients` from numpy array (`.np` file).
:param dir_path:
"""
for learner_id, learner in enumerate(self.global_learners_ensemble):
chkpts_path = os.path.join(dir_path, f"chkpts_{learner_id}.pt")
learner.model.load_state_dict(torch.load(chkpts_path))
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
chkpts_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
weights = np.load(chkpts_path)
for client_id, client in enumerate(clients):
client.learners_ensemble.learners_weights = weights[client_id]
def sample_clients(self):
"""
sample a list of clients without repetition
"""
if self.sample_with_replacement:
self.sampled_clients = \
self.rng.choices(
population=self.clients,
weights=self.clients_weights,
k=self.n_clients_per_round,
)
else:
self.sampled_clients = self.rng.sample(self.clients, k=self.n_clients_per_round)
def save_state_local(self, dir_path, extra_name = None):
"""
save the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
as `.pt` file, and `learners_weights` for each client in `self.clients` as a single numpy array (`.np` file).
Save each of the local clients instead and load of the local clients instead
:param dir_path:
"""
client_idx = 0
# Save global weights
for client in self.clients:
# for learner_id, learner in enumerate(client.tuned_learners_ensemble):
for learner_id, learner in enumerate(client.learners_ensemble):
if extra_name is None:
save_path = os.path.join(dir_path, f"chkpts_{client_idx}_{learner_id}.pt")
else:
save_path = os.path.join(dir_path, f"chkpts_r{str(extra_name)}_{client_idx}_{learner_id}.pt")
torch.save(learner.model.state_dict(), save_path)
client_idx += 1
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
# Save local weights
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
if extra_name is None:
save_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
else:
save_path = os.path.join(dir_path, f"r{str(extra_name)}_{mode}_client_weights.npy")
for client_id, client in enumerate(clients):
weights[client_id] = client.tuned_learners_ensemble.learners_weights
np.save(save_path, weights)
def load_state_local(self, dir_path, extra_name = None):
"""
load the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
from a `.pt` file, and `learners_weights` for each client in `self.clients` from numpy array (`.np` file).
:param dir_path:
"""
client_idx = 0
# Load global weights
for client in self.clients:
for learner_id, learner in enumerate(client.learners_ensemble):
if extra_name is None:
chkpts_path = os.path.join(dir_path, f"chkpts_{client_idx}_{learner_id}.pt")
else:
chkpts_path = os.path.join(dir_path, f"chkpts_r{str(extra_name)}_{client_idx}_{learner_id}.pt")
learner.model.load_state_dict(torch.load(chkpts_path))
client_idx += 1
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
if extra_name is None:
chkpts_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
else:
chkpts_path = os.path.join(dir_path, f"r{str(extra_name)}_{mode}_client_weights.npy")
weights = np.load(chkpts_path)
for client_id, client in enumerate(clients):
client.learners_ensemble.learners_weights = weights[client_id]
def assign_new_local_tuning(self, tuning_val):
for client in self.clients:
client.tune_steps = tuning_val
return
class NoCommunicationAggregator(Aggregator):
r"""Clients do not communicate. Each client work locally
"""
def mix(self):
self.sample_clients()
for client in self.sampled_clients:
client.step()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
def update_clients(self):
pass
def save_state(self, dir_path):
"""
save the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
as `.pt` file, and `learners_weights` for each client in `self.clients` as a single numpy array (`.np` file).
Save each of the local clients instead and load of the local clients instead
:param dir_path:
"""
client_idx = 0
# Save global weights
for client in self.clients:
for learner_id, learner in enumerate(client.learners_ensemble):
save_path = os.path.join(dir_path, f"chkpts_{client_idx}.pt")
torch.save(learner.model.state_dict(), save_path)
client_idx += 1
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
# Save local weights
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
save_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
for client_id, client in enumerate(clients):
weights[client_id] = client.learners_ensemble.learners_weights
np.save(save_path, weights)
def load_state(self, dir_path):
"""
load the state of the aggregator, i.e., the state dictionary of each `learner` in `global_learners_ensemble`
from a `.pt` file, and `learners_weights` for each client in `self.clients` from numpy array (`.np` file).
:param dir_path:
"""
client_idx = 0
# Load global weights
for client in self.clients:
for learner_id, learner in enumerate(client.learners_ensemble):
chkpts_path = os.path.join(dir_path, f"chkpts_{client_idx}.pt")
learner.model.load_state_dict(torch.load(chkpts_path))
client_idx += 1
learners_weights = np.zeros((self.n_clients, self.n_learners))
test_learners_weights = np.zeros((self.n_test_clients, self.n_learners))
for mode, weights, clients in [
['train', learners_weights, self.clients],
['test', test_learners_weights, self.test_clients]
]:
chkpts_path = os.path.join(dir_path, f"{mode}_client_weights.npy")
weights = np.load(chkpts_path)
for client_id, client in enumerate(clients):
client.learners_ensemble.learners_weights = weights[client_id]
class CentralizedAggregator(Aggregator):
r""" Standard Centralized Aggregator.
All clients get fully synchronized with the average client.
"""
def mix(self):
self.sample_clients()
for client in self.sampled_clients:
client.step()
if self.krum_mode:
# Krum based aggregation scheme applied
for learner_id, learner in enumerate(self.global_learners_ensemble):
learners = [client.learners_ensemble[learner_id] for client in self.clients]
krum_learners(learners, learner, self.exp_adv_nodes)
else:
for learner_id, learner in enumerate(self.global_learners_ensemble):
learners = [client.learners_ensemble[learner_id] for client in self.clients]
average_learners(learners, learner, weights=self.clients_weights)
# assign the updated model to all clients
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
def update_clients(self):
for client in self.clients:
for learner_id, learner in enumerate(client.learners_ensemble):
copy_model(learner.model, self.global_learners_ensemble[learner_id].model)
if callable(getattr(learner.optimizer, "set_initial_params", None)):
learner.optimizer.set_initial_params(
self.global_learners_ensemble[learner_id].model.parameters()
)
class PersonalizedAggregator(CentralizedAggregator):
r"""
Clients do not synchronize there models, instead they only synchronize optimizers, when needed.
"""
def update_clients(self):
for client in self.clients:
for learner_id, learner in enumerate(client.learners_ensemble):
if callable(getattr(learner.optimizer, "set_initial_params", None)):
learner.optimizer.set_initial_params(self.global_learners_ensemble[learner_id].model.parameters())
class APFLAggregator(Aggregator):
"""
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
alpha,
sampling_rate=1.,
sample_with_replacement=False,
test_clients=None,
verbose=0,
seed=None
):
super(APFLAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
assert self.n_clients == 2, "APFL requires two learners"
self.alpha = alpha
def mix(self):
self.sample_clients()
for client in self.sampled_clients:
for _ in range(client.local_steps):
client.step(single_batch_flag=True)
partial_average(
learners=[client.learners_ensemble[1]],
average_learner=client.learners_ensemble[0],
alpha=self.alpha
)
average_learners(
learners=[client.learners_ensemble[0] for client in self.clients],
target_learner=self.global_learners_ensemble[0],
weights=self.clients_weights
)
# assign the updated model to all clients
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
def update_clients(self):
for client in self.clients:
copy_model(client.learners_ensemble[0].model, self.global_learners_ensemble[0].model)
if callable(getattr(client.learners_ensemble[0].optimizer, "set_initial_params", None)):
client.learners_ensemble[0].optimizer.set_initial_params(
self.global_learners_ensemble[0].model.parameters()
)
class LoopLessLocalSGDAggregator(PersonalizedAggregator):
"""
Implements L2SGD introduced in
'Federated Learning of a Mixture of Global and Local Models'__. (https://arxiv.org/pdf/2002.05516.pdf)
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
communication_probability,
penalty_parameter,
sampling_rate=1.,
sample_with_replacement=False,
test_clients=None,
verbose=0,
seed=None
):
super(LoopLessLocalSGDAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
self.communication_probability = communication_probability
self.penalty_parameter = penalty_parameter
@property
def communication_probability(self):
return self.__communication_probability
@communication_probability.setter
def communication_probability(self, communication_probability):
self.__communication_probability = communication_probability
def mix(self):
communication_flag = self.np_rng.binomial(1, self.communication_probability, 1)
if communication_flag:
for learner_id, learner in enumerate(self.global_learners_ensemble):
learners = [client.learners_ensemble[learner_id] for client in self.clients]
average_learners(learners, learner, weights=self.clients_weights)
partial_average(
learners,
average_learner=learner,
alpha=self.penalty_parameter/self.communication_probability
)
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
else:
self.sample_clients()
for client in self.sampled_clients:
client.step(single_batch_flag=True)
class ClusteredAggregator(Aggregator):
"""
Implements
`Clustered Federated Learning: Model-Agnostic Distributed Multi-Task Optimization under Privacy Constraints`.
Follows implementation from https://github.com/felisat/clustered-federated-learning
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
sampling_rate=1.,
sample_with_replacement=False,
test_clients=None,
verbose=0,
tol_1=0.4,
tol_2=1.6,
seed=None
):
super(ClusteredAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
assert self.n_learners == 1, "ClusteredAggregator only supports single learner clients."
assert self.sampling_rate == 1.0, f"`sampling_rate` is {sampling_rate}, should be {1.0}," \
f" ClusteredAggregator only supports full clients participation."
self.tol_1 = tol_1
self.tol_2 = tol_2
self.global_learners = [self.global_learners_ensemble]
self.clusters_indices = [np.arange(len(clients)).astype("int")]
self.n_clusters = 1
def mix(self):
clients_updates = np.zeros((self.n_clients, self.n_learners, self.model_dim))
for client_id, client in enumerate(self.clients):
clients_updates[client_id] = client.step()
similarities = np.zeros((self.n_learners, self.n_clients, self.n_clients))
for learner_id in range(self.n_learners):
similarities[learner_id] = pairwise_distances(clients_updates[:, learner_id, :], metric="cosine")
similarities = similarities.mean(axis=0)
new_cluster_indices = []
for indices in self.clusters_indices:
max_update_norm = np.zeros(self.n_learners)
mean_update_norm = np.zeros(self.n_learners)
for learner_id in range(self.n_learners):
max_update_norm[learner_id] = LA.norm(clients_updates[indices], axis=1).max()
mean_update_norm[learner_id] = LA.norm(np.mean(clients_updates[indices], axis=0))
max_update_norm = max_update_norm.mean()
mean_update_norm = mean_update_norm.mean()
if mean_update_norm < self.tol_1 and max_update_norm > self.tol_2 and len(indices) > 2:
clustering = AgglomerativeClustering(affinity="precomputed", linkage="complete")
clustering.fit(similarities[indices][:, indices])
cluster_1 = np.argwhere(clustering.labels_ == 0).flatten()
cluster_2 = np.argwhere(clustering.labels_ == 1).flatten()
new_cluster_indices += [cluster_1, cluster_2]
else:
new_cluster_indices += [indices]
self.clusters_indices = new_cluster_indices
self.n_clusters = len(self.clusters_indices)
self.global_learners = [deepcopy(self.clients[0].learners_ensemble) for _ in range(self.n_clusters)]
for cluster_id, indices in enumerate(self.clusters_indices):
cluster_clients = [self.clients[i] for i in indices]
for learner_id in range(self.n_learners):
average_learners(
learners=[client.learners_ensemble[learner_id] for client in cluster_clients],
target_learner=self.global_learners[cluster_id][learner_id],
weights=self.clients_weights[indices] / self.clients_weights[indices].sum()
)
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
def update_clients(self):
for cluster_id, indices in enumerate(self.clusters_indices):
cluster_learners = self.global_learners[cluster_id]
for i in indices:
for learner_id, learner in enumerate(self.clients[i].learners_ensemble):
copy_model(
target=learner.model,
source=cluster_learners[learner_id].model
)
def update_test_clients(self):
pass
class AgnosticAggregator(CentralizedAggregator):
"""
Implements
`Agnostic Federated Learning`__(https://arxiv.org/pdf/1902.00146.pdf).
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
lr_lambda,
sampling_rate=1.,
sample_with_replacement=False,
test_clients=None,
verbose=0,
seed=None
):
super(AgnosticAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
self.lr_lambda = lr_lambda
def mix(self):
self.sample_clients()
clients_losses = []
for client in self.sampled_clients:
client_losses = client.step()
clients_losses.append(client_losses)
clients_losses = torch.tensor(clients_losses)
for learner_id, learner in enumerate(self.global_learners_ensemble):
learners = [client.learners_ensemble[learner_id] for client in self.clients]
average_learners(
learners=learners,
target_learner=learner,
weights=self.clients_weights,
average_gradients=True
)
# update parameters
self.global_learners_ensemble.optimizer_step()
# update clients weights
self.clients_weights += self.lr_lambda * clients_losses.mean(dim=1)
self.clients_weights = simplex_projection(self.clients_weights)
# assign the updated model to all clients
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
class FFLAggregator(CentralizedAggregator):
"""
Implements q-FedAvg from
`FAIR RESOURCE ALLOCATION IN FEDERATED LEARNING`__(https://arxiv.org/pdf/1905.10497.pdf)
"""
def __init__(
self,
clients,
global_learners_ensemble,
log_freq,
global_train_logger,
global_test_logger,
lr,
q=1,
sampling_rate=1.,
sample_with_replacement=True,
test_clients=None,
verbose=0,
seed=None
):
super(FFLAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
self.q = q
self.lr = lr
assert self.sample_with_replacement, 'FFLAggregator only support sample with replacement'
def mix(self):
self.sample_clients()
hs = 0
for client in self.sampled_clients:
hs += client.step(lr=self.lr)
hs /= (self.lr * len(self.sampled_clients)) # take account for the lr used inside optimizer
for learner_id, learner in enumerate(self.global_learners_ensemble):
learners = [client.learners_ensemble[learner_id] for client in self.sampled_clients]
average_learners(
learners=learners,
target_learner=learner,
weights=hs*torch.ones(len(learners)),
average_params=False,
average_gradients=True
)
# update parameters
self.global_learners_ensemble.optimizer_step()
# assign the updated model to all clients
self.update_clients()
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()
class DecentralizedAggregator(Aggregator):
def __init__(
self,
clients,
global_learners_ensemble,
mixing_matrix,
log_freq,
global_train_logger,
global_test_logger,
sampling_rate=1.,
sample_with_replacement=True,
test_clients=None,
verbose=0,
seed=None):
super(DecentralizedAggregator, self).__init__(
clients=clients,
global_learners_ensemble=global_learners_ensemble,
log_freq=log_freq,
global_train_logger=global_train_logger,
global_test_logger=global_test_logger,
sampling_rate=sampling_rate,
sample_with_replacement=sample_with_replacement,
test_clients=test_clients,
verbose=verbose,
seed=seed
)
self.mixing_matrix = mixing_matrix
assert self.sampling_rate >= 1, "partial sampling is not supported with DecentralizedAggregator"
def update_clients(self):
pass
def mix(self):
# update local models
for client in self.clients:
client.step()
# mix models
mixing_matrix = torch.tensor(
self.mixing_matrix.copy(),
dtype=torch.float32,
device=self.device
)
for learner_id, global_learner in enumerate(self.global_learners_ensemble):
state_dicts = [client.learners_ensemble[learner_id].model.state_dict() for client in self.clients]
for key, param in global_learner.model.state_dict().items():
shape_ = param.shape
models_params = torch.zeros(self.n_clients, int(np.prod(shape_)), device=self.device)
for ii, sd in enumerate(state_dicts):
models_params[ii] = sd[key].view(1, -1)
models_params = mixing_matrix @ models_params
for ii, sd in enumerate(state_dicts):
sd[key] = models_params[ii].view(shape_)
for client_id, client in enumerate(self.clients):
client.learners_ensemble[learner_id].model.load_state_dict(state_dicts[client_id])
self.c_round += 1
if self.c_round % self.log_freq == 0:
self.write_logs()