-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclients_sampler.py
699 lines (497 loc) · 20.4 KB
/
clients_sampler.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
import time
import json
from abc import ABC, abstractmethod
from utils.constants import *
from utils.divergence import *
class ClientsSampler(ABC):
r"""Base class for clients_dict sampler
Attributes
----------
activity_simulator: ActivitySimulator
clients_ids:
n_clients: int
clients_weights_dict: Dict[int: float]
maps clients ids to their corresponding weight/importance in the true objective function
_availability_dict: Dict[int: float]
maps clients ids to their stationary participation probability
_stability_dict: Dict[int: float]
maps clients ids to the spectral gap of their corresponding markov chains
history: Dict[int: Dict[str: List]]
stores the active and sampled clients and their weights at every time step
_time_step: int
tracks the number of steps
rng:
Methods
----------
__init__
_update_estimates
sample_clients
step
save_history
"""
def __init__(
self,
activity_simulator,
activity_estimator,
clients_weights_dict,
rng=None,
*args,
**kwargs
):
"""
Parameters
----------
activity_simulator: ActivitySimulator
activity_estimator: ActivityEstimator
clients_weights_dict: Dict[int: float]
rng:
"""
self.activity_simulator = activity_simulator
self.activity_estimator = activity_estimator
self.clients_ids = list(clients_weights_dict.keys())
self.n_clients = len(self.clients_ids)
self.clients_weights_dict = clients_weights_dict
self._availability_types_dict = dict()
self._availability_dict = dict()
self._stability_types_dict = dict()
self._stability_dict = dict()
self.history = dict()
self._time_step = -1
if rng is None:
seed = int(time.time())
self.rng = np.random.default_rng(seed)
else:
self.rng = rng
def get_active_clients(self):
"""receive the list of active clients
Returns
-------
* List[int]
"""
return self.activity_simulator.get_active_clients()
def _update_estimates(self, active_clients, loss_dict=None):
"""update the estimate of clients' activities
Returns
-------
* Tuple[dict, dict, dict, dict]:
availability_types_dict, availability_dict, stability_types_dict, stability_dict
"""
self._availability_types_dict, self._availability_dict, _, _ = \
self.activity_estimator.gather_activity_estimates(active_clients)
def step(self, active_clients, sampled_clients_ids, sampled_clients_weights):
"""update the internal state of the clients sampler
Parameters
----------
active_clients: List[int]
sampled_clients_ids: List[int]
sampled_clients_weights: Dict[int: float]
Returns
-------
None
"""
self.activity_simulator.step()
self.activity_estimator.step()
self._time_step += 1
current_state = {
"active_clients": active_clients,
"sampled_clients_ids": sampled_clients_ids,
"sampled_clients_weights": sampled_clients_weights
}
self.history[self._time_step] = current_state
def save_history(self, json_path):
"""save history and clients metadata
save a dictionary with:
* history: stores the active and sampled clients and their weights at every time step
* clients_ids: list of clients ids stored as integers
* clients_true_weights: dictionary mapping clients ids to their true weights
* clients_availability_types: dictionary mapping clients ids to their availability types
* clients_true_availability: dictionary mapping clients ids to their true availabilities
* clients_stability_types: dictionary mapping clients ids to their stability types
* clients_true_stability: dictionary mapping clients ids to their true stabilities
Parameters
----------
json_path: path of a .json file
Returns
-------
None
"""
metadata = {
"history": self.history,
"clients_ids": self.clients_ids,
"clients_true_weights": self.clients_weights_dict,
"clients_availability_types": self._availability_types_dict,
"clients_true_availability": self._availability_dict,
"clients_stability_types": self._stability_types_dict,
"clients_true_stability": self._stability_dict
}
with open(json_path, "w") as f:
json.dump(metadata, f)
@abstractmethod
def sample(self, active_clients, loss_dict):
"""sample clients_dict
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float] or None
Returns
-------
* List[int]: indices of the sampled clients_dict
* List[float]: weights to be associated to the sampled clients_dict
"""
pass
class UnbiasedClientsSampler(ClientsSampler):
"""
Samples all active clients with aggregation weight inversely proportional to their participation
"""
def sample(self, active_clients, loss_dict=None):
"""implementation of the abstract method ClientSampler.sample for the UnbiasedClientSampler
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float] or None
Returns
-------
* List[int]: indices of the sampled clients_dict
* List[float]: weights to be associated to the sampled clients_dict
"""
sampled_clients_ids, sampled_clients_weights = [], []
self._update_estimates(active_clients)
for client_id in active_clients:
sampled_clients_ids.append(client_id)
sampled_clients_weights.append(
self.clients_weights_dict[client_id] / self._availability_dict[client_id]
)
self.step(active_clients, sampled_clients_ids, sampled_clients_weights)
return sampled_clients_ids, sampled_clients_weights
class MarkovianClientsSampler(ClientsSampler):
"""Markovian clients sampler, also known as CA-Fed (Correlation-Aware Federated Learning)
Considers the availability and stability of clients
Attributes
----------
_smoothness_param: float
parameter used for the estimation of the loss vector
_tolerance: float
tolerance used for the stopping criteria
_bias_const: float
coefficient of the bias error, controls the number of excluded clients
_availability_vec: 1-D numpy.array
entry at position client_id, gives the availability value of client_id
_clients_weights_vec: 1-D numpy.array
entry at position client_id, gives the weight of client_id in the true objective function
_clients_optimums_vec: 1-D numpy.array
entry at position client_id, gives the optimum loss value of client_id
_clients_ids_by_stability: List[int]
clients ids ordered by the absolute value of their stability
_clients_ids_by_availability: List[int]
clients ids ordered by the value of their availability
_loss_estimates_vec: 1-D numpy.array
entry at position client_id, gives the estimated loss value of client_id
_allocation_vec: 1-D numpy.array
entry at position client_id, gives the current estimate of the allocation value of client_id
__counter: int
initialized with -1
Methods
----------
_update_estimates
_estimate_optimization_objective
_estimate_dissimilarity
__init_allocation_vec
"""
def __init__(
self,
activity_simulator,
activity_estimator,
clients_weights_dict,
clients_optimums_dict,
smoothness_param=0.0,
tolerance=0.0,
bias_const=1.0,
rng=None
):
super(MarkovianClientsSampler, self).__init__(
activity_simulator=activity_simulator,
activity_estimator=activity_estimator,
clients_weights_dict=clients_weights_dict,
rng=rng
)
self._smoothness_param = smoothness_param
self._tolerance = tolerance
self._bias_const = bias_const
self._clients_weights_vec = np.array(
[self.clients_weights_dict[idx] for idx in range(self.n_clients)]
)
assert_array_in_simplex(self._clients_weights_vec)
self._clients_optimums_vec = np.array(
[clients_optimums_dict[idx] for idx in range(self.n_clients)]
)
self._loss_estimates_vec = np.zeros(self.n_clients, dtype=np.float32)
self._allocation_vec = np.zeros(self.n_clients, dtype=np.float32)
self._clients_ids_by_stability = list()
self._clients_ids_by_availability = list()
self.__counter = -1
def __init_allocation_vec(self):
return np.copy(self._clients_weights_vec) / np.copy(self._availability_vec)
def _update_estimates(self, active_clients, loss_dict=None):
"""update the estimates of clients losses
Initialize and updates the _loss_estimates_vec attribute
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float]
Returns
-------
None
"""
active_clients = set(active_clients)
# update loss estimates
if self.__counter == -1:
mean_active_clients_loss = 0
for client_id in active_clients:
self._loss_estimates_vec[client_id] = loss_dict[client_id]
mean_active_clients_loss += loss_dict[client_id]
mean_active_clients_loss /= len(active_clients)
for client_id in self.clients_ids:
if client_id not in active_clients:
self._loss_estimates_vec[client_id] = mean_active_clients_loss
else:
for client_id in active_clients:
self._loss_estimates_vec[client_id] = \
self._smoothness_param * self._loss_estimates_vec[client_id] + \
(1 - self._smoothness_param) * loss_dict[client_id]
# update clients' activity estimates
self._availability_types_dict, self._availability_dict, self._stability_types_dict, self._stability_dict = \
self.activity_estimator.gather_activity_estimates(list(active_clients))
self._availability_vec = np.array(
[self._availability_dict[idx] for idx in range(self.n_clients)]
)
self.__counter += 1
def _estimate_dissimilarity(self):
"""estimates clients dissimilarity
Returns
-------
float
"""
return np.max(self._loss_estimates_vec - self._clients_optimums_vec)
def _estimate_optimization_objective(self, allocation_vector):
"""computes the Markovian sampler objective
Estimate the total error of the true global objective as sum of:
* an optimization term: the optimization error of the biased global objective
* a bias term, expressed in terms of product between:
** the total variation distance between the true and the current weights
** the client dissimilarity
CA-Fed identifies the number of clients to remove by attempting
to minimize this total error (see _truncate_clients).
Parameters
----------
allocation_vector: 1-D numpy.array
entry at position client_id, gives the allocation value of client_id
Returns
-------
* float: total error
"""
weights = self._availability_vec * allocation_vector
weights /= weights.sum()
optimization_term = (self._loss_estimates_vec - self._clients_optimums_vec) @ weights
dissimilarity = self._estimate_dissimilarity()
tv_value = np.square(tv_distance(self._clients_weights_vec, weights))
bias_term = dissimilarity * tv_value * self._bias_const
total_error = optimization_term + bias_term
return total_error
def _truncate_clients(self, ordered_ids):
"""
progressively truncates some clients by attempting to minimize
the estimate of the optimization objective
Parameters
----------
ordered_ids: List[int]
ordered list of clients_ids, the clients will be explored according to the order
Returns
-------
1-D numpy.array: allocation vector
"""
running_allocation_vec = np.copy(self._allocation_vec)
current_allocation_vec = np.copy(self._allocation_vec)
running_objective = \
self._estimate_optimization_objective(running_allocation_vec)
for client_id in ordered_ids:
current_allocation_vec[client_id] = 0
if current_allocation_vec.sum() == 0:
break
current_objective = \
self._estimate_optimization_objective(current_allocation_vec)
if running_objective - current_objective >= self._tolerance:
running_allocation_vec = np.copy(current_allocation_vec)
running_objective = current_objective
else:
current_allocation_vec = np.copy(running_allocation_vec)
return running_allocation_vec
def sample(self, active_clients, loss_dict):
"""implementation of the abstract method ClientSampler.sample for CA-Fed
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float] or None
Returns
-------
* List[int]: indices of the sampled clients_dict
* List[float]: weights to be associated to the sampled clients_dict
"""
self._update_estimates(
active_clients=active_clients,
loss_dict=loss_dict
)
self._allocation_vec = self.__init_allocation_vec()
self._clients_ids_by_stability = \
sorted(self.clients_ids, key=lambda idx: abs(self._stability_dict[idx]), reverse=True)
self._allocation_vec = self._truncate_clients(
ordered_ids=self._clients_ids_by_stability
)
self._clients_ids_by_availability = \
sorted(self.clients_ids, key=lambda idx: self._availability_dict[idx], reverse=False)
self._allocation_vec = self._truncate_clients(
ordered_ids=self._clients_ids_by_availability
)
sampled_clients_ids, sampled_clients_weights = [], []
for client_id in active_clients:
if self._allocation_vec[client_id] > ERROR:
sampled_clients_ids.append(client_id)
sampled_clients_weights.append(
self._allocation_vec[client_id]
)
self.step(active_clients, sampled_clients_ids, sampled_clients_weights)
return sampled_clients_ids, sampled_clients_weights
class AdaFedClientsSampler(ClientsSampler):
r"""Participation-Aware Federated Learning
Implements AdaFed proposed in
"AdaFed: Optimizing Participation-Aware FederatedLearning with
Adaptive Aggregation Weights"__(https://ieeexplore.ieee.org/abstract/document/9762058)
Attributes
----------
_full_participation: bool
if True, all clients are taken at every round
Methods
----------
_update_estimates
_build_optimization_problem
"""
def __init__(
self,
activity_simulator,
activity_estimator,
clients_weights_dict,
full_participation=True,
rng=None
):
super(AdaFedClientsSampler, self).__init__(
activity_simulator=activity_simulator,
activity_estimator=activity_estimator,
clients_weights_dict=clients_weights_dict,
rng=rng
)
self._full_participation = full_participation
def _build_optimization_problem(self):
"""
Returns
-------
"""
raise NotImplementedError()
def _normalize_weights(self, clients_weights):
""" normalize weights before aggregation
Parameters
----------
clients_weights: List[float]
Returns
-------
* List[float]
"""
if isinstance(clients_weights, list):
clients_weights = np.array(clients_weights)
clients_weights /= clients_weights.sum()
return clients_weights.tolist()
def sample(self, active_clients, loss_dict):
"""implementation of the abstract method ClientSampler.sample for AdaFed
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float] or None
Returns
-------
* List[int]: indices of the sampled clients_dict
* List[float]: weights to be associated to the sampled clients_dict
"""
self._update_estimates(active_clients=active_clients)
sampled_clients_ids, sampled_clients_weights = [], []
if self._full_participation:
for client_id in active_clients:
sampled_clients_ids.append(client_id)
sampled_clients_weights.append(
self.clients_weights_dict[client_id] / self._availability_dict[client_id]
)
else:
self._build_optimization_problem()
sampled_clients_weights = self._normalize_weights(sampled_clients_weights)
self.step(active_clients, sampled_clients_ids, sampled_clients_weights)
return sampled_clients_ids, sampled_clients_weights
class F3AST(ClientsSampler):
r"""Federated Averaging aided by an Adaptive Sampling Technique
Implements F3AST proposed in
"Federated Learning Under Intermittent Client Availability andTime-Varying
Communication Constraints"__(https://arxiv.org/abs/2205.06730)
Attributes
----------
_smoothness_param: float
_n_clients_per_round: int
number of clients to sample at every round
"""
def __init__(
self,
activity_simulator,
activity_estimator,
clients_weights_dict,
n_clients_per_round,
smoothness_param=0.0,
rng=None
):
super(F3AST, self).__init__(
activity_simulator=activity_simulator,
activity_estimator=activity_estimator,
clients_weights_dict=clients_weights_dict,
rng=rng
)
self._smoothness_param = smoothness_param
self._n_clients_per_round = n_clients_per_round
def _update_estimates(self, active_clients, loss_dict=None):
# update clients' activity estimates
self._availability_types_dict, self._availability_dict, _, _ = \
self.activity_estimator.gather_activity_estimates(active_clients)
# smoothing average of past participation rates
active_clients = set(active_clients)
for client_id in self.clients_ids:
self._availability_dict[client_id] *= (1 - self._smoothness_param)
if client_id in active_clients:
self._availability_dict[client_id] += self._smoothness_param
def sample(self, active_clients, loss_dict=None):
"""implementation of the abstract method ClientSampler.sample for F3AST
Parameters
----------
active_clients: List[int]
loss_dict: Dict[int: float] or None
Returns
-------
* List[int]: indices of the sampled clients_dict
* List[float]: weights to be associated to the sampled clients_dict
"""
self._update_estimates(active_clients)
clients_ids_by_rate = sorted(
active_clients,
key=lambda idx: self.clients_weights_dict[idx] ** 2 / self._availability_dict[idx] ** 2,
reverse=True
)
sampled_clients_ids = clients_ids_by_rate[:self._n_clients_per_round]
sampled_clients_weights = list()
for client_id in sampled_clients_ids:
sampled_clients_weights.append(
self.clients_weights_dict[client_id] / self._availability_dict[client_id]
)
self.step(active_clients, sampled_clients_ids, sampled_clients_weights)
return sampled_clients_ids, sampled_clients_weights