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 | class FederatedServer:
"""Server-side Federated Learning orchestrating class."""
# one-too-many attribute; pylint: disable=too-many-instance-attributes
def __init__(
self,
model: Union[Model, str, Dict[str, Any]],
netwk: Union[NetworkServer, NetworkServerConfig, Dict[str, Any], str],
optim: Union[FLOptimConfig, str, Dict[str, Any]],
metrics: Union[MetricSet, List[MetricInputType], None] = None,
checkpoint: Union[Checkpointer, Dict[str, Any], str, None] = None,
logger: Union[logging.Logger, str, None] = None,
) -> None:
"""Instantiate the orchestrating server for a federated learning task.
Parameters
----------
model: Model or dict or str
Model instance, that may be serialized as an ObjectConfig,
a config dict or a JSON file the path to which is provided.
netwk: NetworkServer or NetworkServerConfig or dict or str
NetworkServer communication endpoint instance, or configuration
dict, dataclass or path to a TOML file enabling its instantiation.
In the latter three cases, the object's default logger will
be set to that of this `FederatedServer`.
optim: FLOptimConfig or dict or str
FLOptimConfig instance or instantiation dict (using
the `from_params` method) or TOML configuration file path.
This object specifies the optimizers to use by the clients
and the server, as well as the client-updates aggregator.
metrics: MetricSet or list[MetricInputType] or None, default=None
MetricSet instance or list of Metric instances and/or specs
to wrap into one, defining evaluation metrics to compute in
addition to the model's loss.
If None, only compute and report the model's loss.
checkpoint: Checkpointer or dict or str or None, default=None
Optional Checkpointer instance or instantiation dict to be
used so as to save round-wise model, optimizer and metrics.
If a single string is provided, treat it as the checkpoint
folder path and use default values for other parameters.
logger: logging.Logger or str or None, default=None,
Logger to use, or name of a logger to set up with
`declearn.utils.get_logger`. If None, use `type(self)`.
"""
# arguments serve modularity; pylint: disable=too-many-arguments
# Assign the logger.
if not isinstance(logger, logging.Logger):
logger = get_logger(logger or type(self).__name__)
self.logger = logger
# Assign the wrapped Model.
if not isinstance(model, Model):
model = deserialize_object(model) # type: ignore
if not isinstance(model, Model):
raise TypeError(
"'model' should be a declearn Model, opt. in serialized form."
)
self.model = model
# Assign the wrapped NetworkServer.
if isinstance(netwk, str):
netwk = NetworkServerConfig.from_toml(netwk)
elif isinstance(netwk, dict):
netwk = NetworkServerConfig(**netwk)
if isinstance(netwk, NetworkServerConfig):
if netwk.logger is None:
netwk.logger = self.logger
netwk = netwk.build_server()
if not isinstance(netwk, NetworkServer):
raise TypeError(
"'netwk' should be a declearn.communication.api.NetworkServer,"
" or the valid configuration of one."
)
self.netwk = netwk
# Assign the wrapped FLOptimConfig.
if isinstance(optim, str):
optim = FLOptimConfig.from_toml(optim)
elif isinstance(optim, dict):
optim = FLOptimConfig.from_params(**optim)
if not isinstance(optim, FLOptimConfig):
raise TypeError(
"'optim' should be a declearn.main.config.FLOptimConfig "
"or a dict of parameters or the path to a TOML file from "
"which to instantiate one."
)
self.aggrg = optim.aggregator
self.optim = optim.server_opt
self.c_opt = optim.client_opt
# Assign the wrapped MetricSet.
self.metrics = MetricSet.from_specs(metrics)
# Assign an optional checkpointer.
if checkpoint is not None:
checkpoint = Checkpointer.from_specs(checkpoint)
self.ckptr = checkpoint
# Set up private attributes to record the loss values and best weights.
self._loss = {} # type: Dict[int, float]
self._best = None # type: Optional[Vector]
def run(
self,
config: Union[FLRunConfig, str, Dict[str, Any]],
) -> None:
"""Orchestrate the federated learning routine.
Parameters
----------
config: FLRunConfig or str or dict
Container instance wrapping grouped hyper-parameters that
specify the federated learning process, including clients
registration, training and validation rounds' setup, plus
an optional early-stopping criterion.
May be a str pointing to a TOML configuration file.
May be as a dict of keyword arguments to be parsed.
"""
if isinstance(config, dict):
config = FLRunConfig.from_params(**config)
if isinstance(config, str):
config = FLRunConfig.from_toml(config)
if not isinstance(config, FLRunConfig):
raise TypeError("'config' should be a FLRunConfig object or str.")
asyncio.run(self.async_run(config))
async def async_run(
self,
config: FLRunConfig,
) -> None:
"""Orchestrate the federated learning routine.
Note: this method is the async backend of `self.run`.
Parameters
----------
config: FLRunConfig
Container instance wrapping grouped hyper-parameters that
specify the federated learning process, including clients
registration, training and validation rounds' setup, plus
optional elements: local differential-privacy parameters,
and/or an early-stopping criterion.
"""
# Instantiate the early-stopping criterion, if any.
early_stop = None # type: Optional[EarlyStopping]
if config.early_stop is not None:
early_stop = config.early_stop.instantiate()
# Start the communications server and run the FL process.
async with self.netwk:
# Conduct the initialization phase.
await self.initialization(config)
if self.ckptr:
self.ckptr.checkpoint(self.model, self.optim, first_call=True)
# Iteratively run training and evaluation rounds.
round_i = 0
while True:
round_i += 1
await self.training_round(round_i, config.training)
await self.evaluation_round(round_i, config.evaluate)
if not self._keep_training(round_i, config.rounds, early_stop):
break
# Interrupt training when time comes.
self.logger.info("Stopping training.")
await self.stop_training(round_i)
async def initialization(
self,
config: FLRunConfig,
) -> None:
"""Orchestrate the initialization steps to set up training.
Wait for clients to register and process their data information.
Send instructions to clients to set up their model and optimizer.
Await clients to have finalized their initialization step; raise
and cancel training if issues are reported back.
Parameters
----------
config: FLRunConfig
Container instance wrapping hyper-parameters that specify
the planned federated learning process, including clients
registration ones as a RegisterConfig dataclass instance.
Raises
------
RuntimeError
In case any of the clients returned an Error message rather
than an Empty ping-back message. Send CancelTraining to all
clients before raising.
"""
# Gather the RegisterConfig instance from the main FLRunConfig.
regst_cfg = config.register
# Wait for clients to register and process their data information.
self.logger.info("Starting clients registration process.")
data_info = await self.netwk.wait_for_clients(
regst_cfg.min_clients, regst_cfg.max_clients, regst_cfg.timeout
)
self.logger.info("Clients' registration is now complete.")
await self._process_data_info(data_info)
# Serialize intialization information and send it to clients.
message = messaging.InitRequest(
model=self.model,
optim=self.c_opt,
metrics=self.metrics.get_config()["metrics"],
dpsgd=config.privacy is not None,
)
self.logger.info("Sending initialization requests to clients.")
await self.netwk.broadcast_message(message)
# Await a confirmation from clients that initialization went well.
# If any client has failed to initialize, raise.
self.logger.info("Waiting for clients' responses.")
await self._collect_results(
clients=self.netwk.client_names,
msgtype=messaging.GenericMessage,
context="initialization",
)
# If local differential privacy is configured, set it up.
if config.privacy is not None:
await self._initialize_dpsgd(config)
self.logger.info("Initialization was successful.")
async def _process_data_info(
self,
clients_data_info: Dict[str, Dict[str, Any]],
) -> None:
"""Validate, aggregate and make use of clients' data-info.
Parameters
----------
clients_data_info: dict[str, dict[str, any]]
Client-wise data-info dict, that are to be aggregated
and passed to the global model for initialization.
Raises
------
AggregationError
In case (some of) the clients' data info is invalid, or
incompatible. Send CancelTraining to all clients before
raising.
"""
fields = self.model.required_data_info # revise: add optimizer, etc.
# Try aggregating the input data_info.
try:
info = aggregate_clients_data_info(clients_data_info, fields)
# In case of failure, cancel training, notify clients, log and raise.
except AggregationError as exc:
messages = {
client: messaging.CancelTraining(reason)
for client, reason in exc.messages.items()
} # type: Dict[str, messaging.Message]
await self.netwk.send_messages(messages)
self.logger.error(exc.error)
raise exc
# Otherwise, initialize the model based on the aggregated information.
self.model.initialize(info)
async def _collect_results(
self,
clients: Set[str],
msgtype: Type[MessageT],
context: str,
) -> Dict[str, MessageT]:
"""Collect some results sent by clients and ensure they are okay.
Parameters
----------
clients: set[str]
Names of the clients that are expected to send messages.
msgtype: type[messaging.Message]
Type of message that clients are expected to send.
context: str
Context of the results collection (e.g. "training" or
"evaluation"), used in logging or error messages.
Raises
------
RuntimeError
If any client sent an incorrect message or reported
failure to conduct the evaluation step properly.
Send CancelTraining to all clients before raising.
Returns
-------
results: dict[str, `msgtype`]
Client-wise collected messages.
"""
# Await clients' responses and type-check them.
replies = await self.netwk.wait_for_messages(clients)
results = {} # type: Dict[str, MessageT]
errors = {} # type: Dict[str, str]
for client, message in replies.items():
if isinstance(message, msgtype):
results[client] = message
elif isinstance(message, messaging.Error):
errors[client] = f"{context} failed: {message.message}"
else:
errors[client] = f"Unexpected message: {message}"
# If any client has failed to send proper results, raise.
# future: modularize errors-handling behaviour
if errors:
err_msg = f"{context} failed for another client."
messages = {
client: messaging.CancelTraining(errors.get(client, err_msg))
for client in self.netwk.client_names
} # type: Dict[str, messaging.Message]
await self.netwk.send_messages(messages)
err_msg = f"{context} failed for {len(errors)} clients:" + "".join(
f"\n {client}: {error}" for client, error in errors.items()
)
self.logger.error(err_msg)
raise RuntimeError(err_msg)
# Otherwise, return collected results.
return results
async def _initialize_dpsgd(
self,
config: FLRunConfig,
) -> None:
"""Send a differential privacy setup request to all registered clients.
Parameters
----------
config: FLRunConfig
FLRunConfig wrapping information on the overall FL process
and on the local DP parameters. Its `privacy` section must
be defined.
"""
self.logger.info("Sending privacy requests to all clients.")
assert config.privacy is not None # else this method is not called
params = {
"rounds": config.rounds,
"batches": config.training.batch_cfg,
"n_epoch": config.training.n_epoch,
"n_steps": config.training.n_steps,
**dataclasses.asdict(config.privacy),
} # type: Dict[str, Any]
message = messaging.PrivacyRequest(**params)
await self.netwk.broadcast_message(message)
self.logger.info("Waiting for clients' responses.")
await self._collect_results(
clients=self.netwk.client_names,
msgtype=messaging.GenericMessage,
context="Privacy initialization",
)
self.logger.info("Privacy requests were processed by clients.")
async def training_round(
self,
round_i: int,
train_cfg: TrainingConfig,
) -> None:
"""Orchestrate a training round.
Parameters
----------
round_i: int
Index of the training round.
train_cfg: TrainingConfig
TrainingConfig dataclass instance wrapping data-batching
and computational effort constraints hyper-parameters.
"""
self.logger.info("Initiating training round %s", round_i)
clients = self._select_training_round_participants()
await self._send_training_instructions(clients, round_i, train_cfg)
self.logger.info("Awaiting clients' training results.")
results = await self._collect_results(
clients, messaging.TrainReply, "training"
)
self.logger.info("Conducting server-side optimization.")
self._conduct_global_update(results)
def _select_training_round_participants(
self,
) -> Set[str]:
"""Return the names of clients that should participate in the round."""
return self.netwk.client_names
async def _send_training_instructions(
self,
clients: Set[str],
round_i: int,
train_cfg: TrainingConfig,
) -> None:
"""Send training instructions to selected clients.
Parameters
----------
clients: set[str]
Names of the clients participating in the training round.
round_i: int
Index of the training round.
train_cfg: TrainingConfig
TrainingConfig dataclass instance wrapping data-batching
and computational effort constraints hyper-parameters.
"""
# Set up shared training parameters.
params = {
"round_i": round_i,
"weights": self.model.get_weights(trainable=True),
**train_cfg.message_params,
} # type: Dict[str, Any]
messages = {} # type: Dict[str, messaging.Message]
# Dispatch auxiliary variables (which may be client-specific).
aux_var = self.optim.collect_aux_var()
for client in clients:
params["aux_var"] = {
key: val.get(client, val) for key, val in aux_var.items()
}
messages[client] = messaging.TrainRequest(**params)
# Send client-wise messages.
await self.netwk.send_messages(messages)
def _conduct_global_update(
self,
results: Dict[str, messaging.TrainReply],
) -> None:
"""Use training results from clients to update the global model.
Parameters
----------
results: dict[str, TrainReply]
Client-wise TrainReply message sent after a training round.
"""
# Reformat received auxiliary variables and pass them to the Optimizer.
aux_var = {} # type: Dict[str, Dict[str, Dict[str, Any]]]
for client, result in results.items():
for module, params in result.aux_var.items():
aux_var.setdefault(module, {})[client] = params
self.optim.process_aux_var(aux_var)
# Compute aggregated "gradients" (updates) and apply them to the model.
# revise: pass n_epoch / t_spent / ?
gradients = self.aggrg.aggregate(
{client: result.updates for client, result in results.items()},
{client: result.n_steps for client, result in results.items()},
)
self.optim.apply_gradients(self.model, gradients)
async def evaluation_round(
self,
round_i: int,
valid_cfg: EvaluateConfig,
) -> None:
"""Orchestrate an evaluation round.
Parameters
----------
round_i: int
Index of the evaluation round.
valid_cfg: EvaluateConfig
EvaluateConfig dataclass instance wrapping data-batching
and computational effort constraints hyper-parameters.
"""
# Send evaluation requests and collect clients' replies.
self.logger.info("Initiating evaluation round %s", round_i)
clients = self._select_evaluation_round_participants()
await self._send_evaluation_instructions(clients, round_i, valid_cfg)
self.logger.info("Awaiting clients' evaluation results.")
results = await self._collect_results(
clients, messaging.EvaluationReply, "evaluation"
)
# Compute and report aggregated evaluation metrics.
self.logger.info("Aggregating evaluation results.")
loss, metrics = self._aggregate_evaluation_results(results)
self.logger.info("Averaged loss is: %s", loss)
if metrics:
self.logger.info(
"Other averaged scalar metrics are: %s",
{k: v for k, v in metrics.items() if isinstance(v, float)},
)
# Optionally checkpoint the model, optimizer and metrics.
if self.ckptr:
self._checkpoint_after_evaluation(metrics, results)
# Record the global loss, and update the kept "best" weights.
self._loss[round_i] = loss
if loss == min(self._loss.values()):
self._best = self.model.get_weights()
def _select_evaluation_round_participants(
self,
) -> Set[str]:
"""Return the names of clients that should participate in the round."""
return self.netwk.client_names
async def _send_evaluation_instructions(
self,
clients: Set[str],
round_i: int,
valid_cfg: EvaluateConfig,
) -> None:
"""Send evaluation instructions to selected clients.
Parameters
----------
clients: set[str]
Names of the clients participating in the evaluation round.
round_i: int
Index of the evaluation round.
valid_cfg: EvaluateConfig
EvaluateConfig dataclass instance wrapping data-batching
and computational effort constraints hyper-parameters.
"""
message = messaging.EvaluationRequest(
round_i=round_i,
weights=self.model.get_weights(trainable=True),
**valid_cfg.message_params,
)
await self.netwk.broadcast_message(message, clients)
def _aggregate_evaluation_results(
self,
results: Dict[str, messaging.EvaluationReply],
) -> Tuple[float, Dict[str, Union[float, np.ndarray]]]:
"""Aggregate evaluation results from clients into a global loss.
Parameters
----------
results: dict[str, EvaluationReply]
Client-wise EvaluationReply message sent after
an evaluation round.
Returns
-------
loss: float
The aggregated loss score computed from clients' ones.
metrics: dict[str, (float | np.ndarray)]
The aggregated evaluation metrics computes from clients' ones.
"""
# Reset the local MetricSet and set up ad hoc variables for the loss.
loss = 0.0
dvsr = 0.0
self.metrics.reset()
# Iteratively update the MetricSet and loss floats based on results.
for client, reply in results.items():
# Case when the client reported some metrics.
if reply.metrics:
states = reply.metrics.copy()
# Update the global metrics based on the local ones.
s_loss = states.pop("loss")
loss += s_loss["current"] # type: ignore
dvsr += s_loss["divisor"] # type: ignore
self.metrics.agg_states(states)
# Case when the client only reported the aggregated local loss.
else:
self.logger.info(
"Client %s refused to share their local metrics.", client
)
loss += reply.loss
dvsr += reply.n_steps
# Compute the final results.
metrics = self.metrics.get_result()
loss = loss / dvsr
return loss, metrics
def _checkpoint_after_evaluation(
self,
metrics: Dict[str, Union[float, np.ndarray]],
results: Dict[str, messaging.EvaluationReply],
) -> None:
"""Checkpoint the current model, optimizer and evaluation metrics.
This method is meant to be called at the end of an evaluation round.
Parameters
----------
metrics: dict[str, (float|np.ndarray)]
Aggregated evaluation metrics to checkpoint.
results: dict[str, EvaluationReply]
Client-wise EvaluationReply messages, based on which
`metrics` were already computed.
"""
# This method only works when a checkpointer is used.
if self.ckptr is None:
raise RuntimeError(
"`_checkpoint_after_evaluation` was called without "
"the FederatedServer having a Checkpointer."
)
# Checkpoint the model, optimizer and global evaluation metrics.
timestamp = self.ckptr.checkpoint(
model=self.model, optimizer=self.optim, metrics=metrics
)
# Checkpoint the client-wise metrics (or at least their loss).
# Use the same timestamp label as for global metrics and states.
local = MetricSet.from_config(self.metrics.get_config())
for client, reply in results.items():
if reply.metrics:
local.reset()
local.agg_states(reply.metrics)
metrics = local.get_result()
else:
metrics = {"loss": reply.loss}
self.ckptr.save_metrics(
metrics=local.get_result(),
prefix=f"metrics_{client}",
append=bool(self._loss),
timestamp=timestamp,
)
def _keep_training(
self,
round_i: int,
rounds: int,
early_stop: Optional[EarlyStopping],
) -> bool:
"""Decide whether training should continue.
Parameters
----------
round_i: int
Index of the latest achieved training round.
rounds: int
Maximum number of rounds that are planned.
early_stop: EarlyStopping or None
Optional EarlyStopping instance adding a stopping criterion
based on the global-evaluation-loss's evolution over rounds.
"""
if round_i >= rounds:
self.logger.info("Maximum number of training rounds reached.")
return False
if early_stop is not None:
early_stop.update(self._loss[round_i])
if not early_stop.keep_training:
self.logger.info("Early stopping criterion reached.")
return False
return True
async def stop_training(
self,
rounds: int,
) -> None:
"""Notify clients that training is over and send final information.
Parameters
----------
rounds: int
Number of training rounds taken until now.
"""
self.logger.info("Recovering weights that yielded the lowest loss.")
message = messaging.StopTraining(
weights=self._best or self.model.get_weights(),
loss=min(self._loss.values()) if self._loss else float("nan"),
rounds=rounds,
)
self.logger.info("Notifying clients that training is over.")
await self.netwk.broadcast_message(message)
if self.ckptr:
path = f"{self.ckptr.folder}/model_state_best.json"
self.logger.info("Checkpointing final weights under %s.", path)
self.model.set_weights(message.weights)
self.ckptr.save_model(self.model, timestamp="best")
|