flwr-nightly 1.21.0.dev20250902__py3-none-any.whl → 1.21.0.dev20250904__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,352 @@
1
+ # Copyright 2025 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Message-based Central differential privacy with fixed clipping.
16
+
17
+ Papers: https://arxiv.org/abs/1712.07557, https://arxiv.org/abs/1710.06963
18
+ """
19
+
20
+ from abc import ABC
21
+ from collections import OrderedDict
22
+ from collections.abc import Iterable
23
+ from logging import INFO, WARNING
24
+ from typing import Optional
25
+
26
+ from flwr.common import Array, ArrayRecord, ConfigRecord, Message, MetricRecord, log
27
+ from flwr.common.differential_privacy import (
28
+ add_gaussian_noise_inplace,
29
+ compute_clip_model_update,
30
+ compute_stdv,
31
+ )
32
+ from flwr.common.differential_privacy_constants import (
33
+ CLIENTS_DISCREPANCY_WARNING,
34
+ KEY_CLIPPING_NORM,
35
+ )
36
+ from flwr.server import Grid
37
+
38
+ from .strategy import Strategy
39
+
40
+
41
+ class DifferentialPrivacyFixedClippingBase(Strategy, ABC):
42
+ """Base class for DP strategies with fixed clipping.
43
+
44
+ This class contains common functionality shared between server-side and
45
+ client-side fixed clipping implementations.
46
+
47
+ Parameters
48
+ ----------
49
+ strategy : Strategy
50
+ The strategy to which DP functionalities will be added by this wrapper.
51
+ noise_multiplier : float
52
+ The noise multiplier for the Gaussian mechanism for model updates.
53
+ A value of 1.0 or higher is recommended for strong privacy.
54
+ clipping_norm : float
55
+ The value of the clipping norm.
56
+ num_sampled_clients : int
57
+ The number of clients that are sampled on each round.
58
+ """
59
+
60
+ # pylint: disable=too-many-arguments,too-many-instance-attributes
61
+ def __init__(
62
+ self,
63
+ strategy: Strategy,
64
+ noise_multiplier: float,
65
+ clipping_norm: float,
66
+ num_sampled_clients: int,
67
+ ) -> None:
68
+ super().__init__()
69
+
70
+ self.strategy = strategy
71
+
72
+ if noise_multiplier < 0:
73
+ raise ValueError("The noise multiplier should be a non-negative value.")
74
+
75
+ if clipping_norm <= 0:
76
+ raise ValueError("The clipping norm should be a positive value.")
77
+
78
+ if num_sampled_clients <= 0:
79
+ raise ValueError(
80
+ "The number of sampled clients should be a positive value."
81
+ )
82
+
83
+ self.noise_multiplier = noise_multiplier
84
+ self.clipping_norm = clipping_norm
85
+ self.num_sampled_clients = num_sampled_clients
86
+
87
+ def _validate_replies(self, replies: Iterable[Message]) -> bool:
88
+ """Validate replies and log errors/warnings.
89
+
90
+ Returns
91
+ -------
92
+ bool
93
+ True if replies are valid for aggregation, False otherwise.
94
+ """
95
+ num_errors = 0
96
+ num_replies_with_content = 0
97
+ for msg in replies:
98
+ if msg.has_error():
99
+ log(
100
+ INFO,
101
+ "Received error in reply from node %d: %s",
102
+ msg.metadata.src_node_id,
103
+ msg.error,
104
+ )
105
+ num_errors += 1
106
+ else:
107
+ num_replies_with_content += 1
108
+
109
+ # Errors are not allowed
110
+ if num_errors:
111
+ log(
112
+ INFO,
113
+ "aggregate_train: Some clients reported errors. Skipping aggregation.",
114
+ )
115
+ return False
116
+
117
+ log(
118
+ INFO,
119
+ "aggregate_train: Received %s results and %s failures",
120
+ num_replies_with_content,
121
+ num_errors,
122
+ )
123
+
124
+ if num_replies_with_content != self.num_sampled_clients:
125
+ log(
126
+ WARNING,
127
+ CLIENTS_DISCREPANCY_WARNING,
128
+ num_replies_with_content,
129
+ self.num_sampled_clients,
130
+ )
131
+
132
+ return True
133
+
134
+ def _add_noise_to_aggregated_arrays(
135
+ self, aggregated_arrays: ArrayRecord
136
+ ) -> ArrayRecord:
137
+ """Add Gaussian noise to aggregated arrays.
138
+
139
+ Parameters
140
+ ----------
141
+ aggregated_arrays : ArrayRecord
142
+ The aggregated arrays to add noise to.
143
+
144
+ Returns
145
+ -------
146
+ ArrayRecord
147
+ The aggregated arrays with noise added.
148
+ """
149
+ aggregated_ndarrays = aggregated_arrays.to_numpy_ndarrays()
150
+ stdv = compute_stdv(
151
+ self.noise_multiplier, self.clipping_norm, self.num_sampled_clients
152
+ )
153
+ add_gaussian_noise_inplace(aggregated_ndarrays, stdv)
154
+
155
+ log(
156
+ INFO,
157
+ "aggregate_fit: central DP noise with %.4f stdev added",
158
+ stdv,
159
+ )
160
+
161
+ return ArrayRecord(
162
+ OrderedDict(
163
+ {
164
+ k: Array(v)
165
+ for k, v in zip(aggregated_arrays.keys(), aggregated_ndarrays)
166
+ }
167
+ )
168
+ )
169
+
170
+ def configure_evaluate(
171
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
172
+ ) -> Iterable[Message]:
173
+ """Configure the next round of federated evaluation."""
174
+ return self.strategy.configure_evaluate(server_round, arrays, config, grid)
175
+
176
+ def aggregate_evaluate(
177
+ self,
178
+ server_round: int,
179
+ replies: Iterable[Message],
180
+ ) -> Optional[MetricRecord]:
181
+ """Aggregate MetricRecords in the received Messages."""
182
+ return self.strategy.aggregate_evaluate(server_round, replies)
183
+
184
+ def summary(self) -> None:
185
+ """Log summary configuration of the strategy."""
186
+ self.strategy.summary()
187
+
188
+
189
+ class DifferentialPrivacyServerSideFixedClipping(DifferentialPrivacyFixedClippingBase):
190
+ """Strategy wrapper for central DP with server-side fixed clipping.
191
+
192
+ Parameters
193
+ ----------
194
+ strategy : Strategy
195
+ The strategy to which DP functionalities will be added by this wrapper.
196
+ noise_multiplier : float
197
+ The noise multiplier for the Gaussian mechanism for model updates.
198
+ A value of 1.0 or higher is recommended for strong privacy.
199
+ clipping_norm : float
200
+ The value of the clipping norm.
201
+ num_sampled_clients : int
202
+ The number of clients that are sampled on each round.
203
+
204
+ Examples
205
+ --------
206
+ Create a strategy::
207
+
208
+ strategy = fl.serverapp.FedAvg( ... )
209
+
210
+ Wrap the strategy with the `DifferentialPrivacyServerSideFixedClipping` wrapper::
211
+
212
+ dp_strategy = DifferentialPrivacyServerSideFixedClipping(
213
+ strategy, cfg.noise_multiplier, cfg.clipping_norm, cfg.num_sampled_clients
214
+ )
215
+ """
216
+
217
+ def __init__(
218
+ self,
219
+ strategy: Strategy,
220
+ noise_multiplier: float,
221
+ clipping_norm: float,
222
+ num_sampled_clients: int,
223
+ ) -> None:
224
+ super().__init__(strategy, noise_multiplier, clipping_norm, num_sampled_clients)
225
+ self.current_arrays: ArrayRecord = ArrayRecord()
226
+
227
+ def __repr__(self) -> str:
228
+ """Compute a string representation of the strategy."""
229
+ return "Differential Privacy Strategy Wrapper (Server-Side Fixed Clipping)"
230
+
231
+ def configure_train(
232
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
233
+ ) -> Iterable[Message]:
234
+ """Configure the next round of training."""
235
+ self.current_arrays = arrays
236
+ return self.strategy.configure_train(server_round, arrays, config, grid)
237
+
238
+ def aggregate_train(
239
+ self,
240
+ server_round: int,
241
+ replies: Iterable[Message],
242
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
243
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
244
+ if not self._validate_replies(replies):
245
+ return None, None
246
+
247
+ # Clip arrays in replies
248
+ current_ndarrays = self.current_arrays.to_numpy_ndarrays()
249
+ for reply in replies:
250
+ for arr_name, record in reply.content.array_records.items():
251
+ # Clip
252
+ reply_ndarrays = record.to_numpy_ndarrays()
253
+ compute_clip_model_update(
254
+ param1=reply_ndarrays,
255
+ param2=current_ndarrays,
256
+ clipping_norm=self.clipping_norm,
257
+ )
258
+ # Replace content while preserving keys
259
+ reply.content[arr_name] = ArrayRecord(
260
+ OrderedDict(
261
+ {k: Array(v) for k, v in zip(record.keys(), reply_ndarrays)}
262
+ )
263
+ )
264
+ log(
265
+ INFO,
266
+ "aggregate_fit: parameters are clipped by value: %.4f.",
267
+ self.clipping_norm,
268
+ )
269
+
270
+ # Pass the new parameters for aggregation
271
+ aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
272
+ server_round, replies
273
+ )
274
+
275
+ # Add Gaussian noise to the aggregated arrays
276
+ if aggregated_arrays:
277
+ aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
278
+
279
+ return aggregated_arrays, aggregated_metrics
280
+
281
+
282
+ class DifferentialPrivacyClientSideFixedClipping(DifferentialPrivacyFixedClippingBase):
283
+ """Strategy wrapper for central DP with client-side fixed clipping.
284
+
285
+ Use `fixedclipping_mod` modifier at the client side.
286
+
287
+ In comparison to `DifferentialPrivacyServerSideFixedClipping`,
288
+ which performs clipping on the server-side,
289
+ `DifferentialPrivacyClientSideFixedClipping` expects clipping to happen
290
+ on the client-side, usually by using the built-in `fixedclipping_mod`.
291
+
292
+ Parameters
293
+ ----------
294
+ strategy : Strategy
295
+ The strategy to which DP functionalities will be added by this wrapper.
296
+ noise_multiplier : float
297
+ The noise multiplier for the Gaussian mechanism for model updates.
298
+ A value of 1.0 or higher is recommended for strong privacy.
299
+ clipping_norm : float
300
+ The value of the clipping norm.
301
+ num_sampled_clients : int
302
+ The number of clients that are sampled on each round.
303
+
304
+ Examples
305
+ --------
306
+ Create a strategy::
307
+
308
+ strategy = fl.serverapp.FedAvg(...)
309
+
310
+ Wrap the strategy with the `DifferentialPrivacyClientSideFixedClipping` wrapper::
311
+
312
+ dp_strategy = DifferentialPrivacyClientSideFixedClipping(
313
+ strategy, cfg.noise_multiplier, cfg.clipping_norm, cfg.num_sampled_clients
314
+ )
315
+
316
+ On the client, add the `fixedclipping_mod` to the client-side mods::
317
+
318
+ app = fl.client.ClientApp(mods=[fixedclipping_mod])
319
+ """
320
+
321
+ def __repr__(self) -> str:
322
+ """Compute a string representation of the strategy."""
323
+ return "Differential Privacy Strategy Wrapper (Client-Side Fixed Clipping)"
324
+
325
+ def configure_train(
326
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
327
+ ) -> Iterable[Message]:
328
+ """Configure the next round of training."""
329
+ # Inject clipping norm in config
330
+ config[KEY_CLIPPING_NORM] = self.clipping_norm
331
+ # Call parent method
332
+ return self.strategy.configure_train(server_round, arrays, config, grid)
333
+
334
+ def aggregate_train(
335
+ self,
336
+ server_round: int,
337
+ replies: Iterable[Message],
338
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
339
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
340
+ if not self._validate_replies(replies):
341
+ return None, None
342
+
343
+ # Aggregate
344
+ aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
345
+ server_round, replies
346
+ )
347
+
348
+ # Add Gaussian noise to the aggregated arrays
349
+ if aggregated_arrays:
350
+ aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
351
+
352
+ return aggregated_arrays, aggregated_metrics
@@ -0,0 +1,162 @@
1
+ # Copyright 2025 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """FedAdagrad [Reddi et al., 2020] strategy.
16
+
17
+ Adaptive Federated Optimization using Adagrad.
18
+
19
+ Paper: arxiv.org/abs/2003.00295
20
+ """
21
+
22
+ from collections import OrderedDict
23
+ from collections.abc import Iterable
24
+ from typing import Callable, Optional
25
+
26
+ import numpy as np
27
+
28
+ from flwr.common import Array, ArrayRecord, Message, MetricRecord, RecordDict
29
+
30
+ from .fedopt import FedOpt
31
+ from .strategy_utils import AggregationError
32
+
33
+
34
+ # pylint: disable=line-too-long
35
+ class FedAdagrad(FedOpt):
36
+ """FedAdagrad strategy - Adaptive Federated Optimization using Adagrad.
37
+
38
+ Implementation based on https://arxiv.org/abs/2003.00295v5
39
+
40
+ Parameters
41
+ ----------
42
+ fraction_train : float (default: 1.0)
43
+ Fraction of nodes used during training. In case `min_train_nodes`
44
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
45
+ will still be sampled.
46
+ fraction_evaluate : float (default: 1.0)
47
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
48
+ is larger than `fraction_evaluate * total_connected_nodes`,
49
+ `min_evaluate_nodes` will still be sampled.
50
+ min_train_nodes : int (default: 2)
51
+ Minimum number of nodes used during training.
52
+ min_evaluate_nodes : int (default: 2)
53
+ Minimum number of nodes used during validation.
54
+ min_available_nodes : int (default: 2)
55
+ Minimum number of total nodes in the system.
56
+ weighted_by_key : str (default: "num-examples")
57
+ The key within each MetricRecord whose value is used as the weight when
58
+ computing weighted averages for both ArrayRecords and MetricRecords.
59
+ arrayrecord_key : str (default: "arrays")
60
+ Key used to store the ArrayRecord when constructing Messages.
61
+ configrecord_key : str (default: "config")
62
+ Key used to store the ConfigRecord when constructing Messages.
63
+ train_metrics_aggr_fn : Optional[callable] (default: None)
64
+ Function with signature (list[RecordDict], str) -> MetricRecord,
65
+ used to aggregate MetricRecords from training round replies.
66
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
67
+ average using the provided weight factor key.
68
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
69
+ Function with signature (list[RecordDict], str) -> MetricRecord,
70
+ used to aggregate MetricRecords from training round replies.
71
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
72
+ average using the provided weight factor key.
73
+ eta : float, optional
74
+ Server-side learning rate. Defaults to 1e-1.
75
+ eta_l : float, optional
76
+ Client-side learning rate. Defaults to 1e-1.
77
+ tau : float, optional
78
+ Controls the algorithm's degree of adaptability. Defaults to 1e-3.
79
+ """
80
+
81
+ # pylint: disable=too-many-arguments
82
+ def __init__(
83
+ self,
84
+ *,
85
+ fraction_train: float = 1.0,
86
+ fraction_evaluate: float = 1.0,
87
+ min_train_nodes: int = 2,
88
+ min_evaluate_nodes: int = 2,
89
+ min_available_nodes: int = 2,
90
+ weighted_by_key: str = "num-examples",
91
+ arrayrecord_key: str = "arrays",
92
+ configrecord_key: str = "config",
93
+ train_metrics_aggr_fn: Optional[
94
+ Callable[[list[RecordDict], str], MetricRecord]
95
+ ] = None,
96
+ evaluate_metrics_aggr_fn: Optional[
97
+ Callable[[list[RecordDict], str], MetricRecord]
98
+ ] = None,
99
+ eta: float = 1e-1,
100
+ eta_l: float = 1e-1,
101
+ tau: float = 1e-3,
102
+ ) -> None:
103
+ super().__init__(
104
+ fraction_train=fraction_train,
105
+ fraction_evaluate=fraction_evaluate,
106
+ min_train_nodes=min_train_nodes,
107
+ min_evaluate_nodes=min_evaluate_nodes,
108
+ min_available_nodes=min_available_nodes,
109
+ weighted_by_key=weighted_by_key,
110
+ arrayrecord_key=arrayrecord_key,
111
+ configrecord_key=configrecord_key,
112
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
113
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
114
+ eta=eta,
115
+ eta_l=eta_l,
116
+ beta_1=0.0,
117
+ beta_2=0.0,
118
+ tau=tau,
119
+ )
120
+
121
+ def aggregate_train(
122
+ self,
123
+ server_round: int,
124
+ replies: Iterable[Message],
125
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
126
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
127
+ aggregated_arrayrecord, aggregated_metrics = super().aggregate_train(
128
+ server_round, replies
129
+ )
130
+
131
+ if aggregated_arrayrecord is None:
132
+ return aggregated_arrayrecord, aggregated_metrics
133
+
134
+ if self.current_arrays is None:
135
+ reason = (
136
+ "Current arrays not set. Ensure that `configure_train` has been "
137
+ "called before aggregation."
138
+ )
139
+ raise AggregationError(reason=reason)
140
+
141
+ # Compute intermediate variables
142
+ delta_t, m_t, aggregated_ndarrays = self._compute_deltat_and_mt(
143
+ aggregated_arrayrecord
144
+ )
145
+
146
+ # v_t
147
+ if not self.v_t:
148
+ self.v_t = {k: np.zeros_like(v) for k, v in aggregated_ndarrays.items()}
149
+ self.v_t = {k: v + (delta_t[k] ** 2) for k, v in self.v_t.items()}
150
+
151
+ new_arrays = {
152
+ k: x + self.eta * m_t[k] / (np.sqrt(self.v_t[k]) + self.tau)
153
+ for k, x in self.current_arrays.items()
154
+ }
155
+
156
+ # Update current arrays
157
+ self.current_arrays = new_arrays
158
+
159
+ return (
160
+ ArrayRecord(OrderedDict({k: Array(v) for k, v in new_arrays.items()})),
161
+ aggregated_metrics,
162
+ )
@@ -0,0 +1,181 @@
1
+ # Copyright 2025 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Adaptive Federated Optimization using Adam (FedAdam) strategy.
16
+
17
+ [Reddi et al., 2020]
18
+
19
+ Paper: arxiv.org/abs/2003.00295
20
+ """
21
+
22
+ from collections import OrderedDict
23
+ from collections.abc import Iterable
24
+ from typing import Callable, Optional
25
+
26
+ import numpy as np
27
+
28
+ from flwr.common import Array, ArrayRecord, Message, MetricRecord, RecordDict
29
+
30
+ from .fedopt import FedOpt
31
+ from .strategy_utils import AggregationError
32
+
33
+
34
+ # pylint: disable=line-too-long
35
+ class FedAdam(FedOpt):
36
+ """FedAdam - Adaptive Federated Optimization using Adam.
37
+
38
+ Implementation based on https://arxiv.org/abs/2003.00295v5
39
+
40
+ Parameters
41
+ ----------
42
+ fraction_train : float (default: 1.0)
43
+ Fraction of nodes used during training. In case `min_train_nodes`
44
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
45
+ will still be sampled.
46
+ fraction_evaluate : float (default: 1.0)
47
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
48
+ is larger than `fraction_evaluate * total_connected_nodes`,
49
+ `min_evaluate_nodes` will still be sampled.
50
+ min_train_nodes : int (default: 2)
51
+ Minimum number of nodes used during training.
52
+ min_evaluate_nodes : int (default: 2)
53
+ Minimum number of nodes used during validation.
54
+ min_available_nodes : int (default: 2)
55
+ Minimum number of total nodes in the system.
56
+ weighted_by_key : str (default: "num-examples")
57
+ The key within each MetricRecord whose value is used as the weight when
58
+ computing weighted averages for both ArrayRecords and MetricRecords.
59
+ arrayrecord_key : str (default: "arrays")
60
+ Key used to store the ArrayRecord when constructing Messages.
61
+ configrecord_key : str (default: "config")
62
+ Key used to store the ConfigRecord when constructing Messages.
63
+ train_metrics_aggr_fn : Optional[callable] (default: None)
64
+ Function with signature (list[RecordDict], str) -> MetricRecord,
65
+ used to aggregate MetricRecords from training round replies.
66
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
67
+ average using the provided weight factor key.
68
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
69
+ Function with signature (list[RecordDict], str) -> MetricRecord,
70
+ used to aggregate MetricRecords from training round replies.
71
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
72
+ average using the provided weight factor key.
73
+ eta : float, optional
74
+ Server-side learning rate. Defaults to 1e-1.
75
+ eta_l : float, optional
76
+ Client-side learning rate. Defaults to 1e-1.
77
+ beta_1 : float, optional
78
+ Momentum parameter. Defaults to 0.9.
79
+ beta_2 : float, optional
80
+ Second moment parameter. Defaults to 0.99.
81
+ tau : float, optional
82
+ Controls the algorithm's degree of adaptability. Defaults to 1e-3.
83
+ """
84
+
85
+ # pylint: disable=too-many-arguments, too-many-locals
86
+ def __init__(
87
+ self,
88
+ *,
89
+ fraction_train: float = 1.0,
90
+ fraction_evaluate: float = 1.0,
91
+ min_train_nodes: int = 2,
92
+ min_evaluate_nodes: int = 2,
93
+ min_available_nodes: int = 2,
94
+ weighted_by_key: str = "num-examples",
95
+ arrayrecord_key: str = "arrays",
96
+ configrecord_key: str = "config",
97
+ train_metrics_aggr_fn: Optional[
98
+ Callable[[list[RecordDict], str], MetricRecord]
99
+ ] = None,
100
+ evaluate_metrics_aggr_fn: Optional[
101
+ Callable[[list[RecordDict], str], MetricRecord]
102
+ ] = None,
103
+ eta: float = 1e-1,
104
+ eta_l: float = 1e-1,
105
+ beta_1: float = 0.9,
106
+ beta_2: float = 0.99,
107
+ tau: float = 1e-3,
108
+ ) -> None:
109
+ super().__init__(
110
+ fraction_train=fraction_train,
111
+ fraction_evaluate=fraction_evaluate,
112
+ min_train_nodes=min_train_nodes,
113
+ min_evaluate_nodes=min_evaluate_nodes,
114
+ min_available_nodes=min_available_nodes,
115
+ weighted_by_key=weighted_by_key,
116
+ arrayrecord_key=arrayrecord_key,
117
+ configrecord_key=configrecord_key,
118
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
119
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
120
+ eta=eta,
121
+ eta_l=eta_l,
122
+ beta_1=beta_1,
123
+ beta_2=beta_2,
124
+ tau=tau,
125
+ )
126
+
127
+ def aggregate_train(
128
+ self,
129
+ server_round: int,
130
+ replies: Iterable[Message],
131
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
132
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
133
+ aggregated_arrayrecord, aggregated_metrics = super().aggregate_train(
134
+ server_round, replies
135
+ )
136
+
137
+ if aggregated_arrayrecord is None:
138
+ return aggregated_arrayrecord, aggregated_metrics
139
+
140
+ if self.current_arrays is None:
141
+ reason = (
142
+ "Current arrays not set. Ensure that `configure_train` has been "
143
+ "called before aggregation."
144
+ )
145
+ raise AggregationError(reason=reason)
146
+
147
+ # Compute intermediate variables
148
+ delta_t, m_t, aggregated_ndarrays = self._compute_deltat_and_mt(
149
+ aggregated_arrayrecord
150
+ )
151
+
152
+ # v_t
153
+ if not self.v_t:
154
+ self.v_t = {k: np.zeros_like(v) for k, v in aggregated_ndarrays.items()}
155
+ self.v_t = {
156
+ k: self.beta_2 * v + (1 - self.beta_2) * (delta_t[k] ** 2)
157
+ for k, v in self.v_t.items()
158
+ }
159
+
160
+ # Compute the bias-corrected learning rate, `eta_norm` for improving convergence
161
+ # in the early rounds of FL training. This `eta_norm` is `\alpha_t` in Kingma &
162
+ # Ba, 2014 (http://arxiv.org/abs/1412.6980) "Adam: A Method for Stochastic
163
+ # Optimization" in the formula line right before Section 2.1.
164
+ eta_norm = (
165
+ self.eta
166
+ * np.sqrt(1 - np.power(self.beta_2, server_round + 1.0))
167
+ / (1 - np.power(self.beta_1, server_round + 1.0))
168
+ )
169
+
170
+ new_arrays = {
171
+ k: x + eta_norm * m_t[k] / (np.sqrt(self.v_t[k]) + self.tau)
172
+ for k, x in self.current_arrays.items()
173
+ }
174
+
175
+ # Update current arrays
176
+ self.current_arrays = new_arrays
177
+
178
+ return (
179
+ ArrayRecord(OrderedDict({k: Array(v) for k, v in new_arrays.items()})),
180
+ aggregated_metrics,
181
+ )