flwr-nightly 1.22.0.dev20250918__py3-none-any.whl → 1.22.0.dev20250919__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.
@@ -84,53 +84,6 @@ class DifferentialPrivacyFixedClippingBase(Strategy, ABC):
84
84
  self.clipping_norm = clipping_norm
85
85
  self.num_sampled_clients = num_sampled_clients
86
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
87
  def _add_noise_to_aggregated_arrays(
135
88
  self, aggregated_arrays: ArrayRecord
136
89
  ) -> ArrayRecord:
@@ -228,6 +181,13 @@ class DifferentialPrivacyServerSideFixedClipping(DifferentialPrivacyFixedClippin
228
181
  """Compute a string representation of the strategy."""
229
182
  return "Differential Privacy Strategy Wrapper (Server-Side Fixed Clipping)"
230
183
 
184
+ def summary(self) -> None:
185
+ """Log summary configuration of the strategy."""
186
+ log(INFO, "\t├──> DP settings:")
187
+ log(INFO, "\t│\t├── Noise multiplier: %s", self.noise_multiplier)
188
+ log(INFO, "\t│\t└── Clipping norm: %s", self.clipping_norm)
189
+ super().summary()
190
+
231
191
  def configure_train(
232
192
  self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
233
193
  ) -> Iterable[Message]:
@@ -241,7 +201,7 @@ class DifferentialPrivacyServerSideFixedClipping(DifferentialPrivacyFixedClippin
241
201
  replies: Iterable[Message],
242
202
  ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
243
203
  """Aggregate ArrayRecords and MetricRecords in the received Messages."""
244
- if not self._validate_replies(replies):
204
+ if not validate_replies(replies, self.num_sampled_clients):
245
205
  return None, None
246
206
 
247
207
  # Clip arrays in replies
@@ -322,6 +282,13 @@ class DifferentialPrivacyClientSideFixedClipping(DifferentialPrivacyFixedClippin
322
282
  """Compute a string representation of the strategy."""
323
283
  return "Differential Privacy Strategy Wrapper (Client-Side Fixed Clipping)"
324
284
 
285
+ def summary(self) -> None:
286
+ """Log summary configuration of the strategy."""
287
+ log(INFO, "\t├──> DP settings:")
288
+ log(INFO, "\t│\t├── Noise multiplier: %s", self.noise_multiplier)
289
+ log(INFO, "\t│\t└── Clipping norm: %s", self.clipping_norm)
290
+ super().summary()
291
+
325
292
  def configure_train(
326
293
  self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
327
294
  ) -> Iterable[Message]:
@@ -337,7 +304,7 @@ class DifferentialPrivacyClientSideFixedClipping(DifferentialPrivacyFixedClippin
337
304
  replies: Iterable[Message],
338
305
  ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
339
306
  """Aggregate ArrayRecords and MetricRecords in the received Messages."""
340
- if not self._validate_replies(replies):
307
+ if not validate_replies(replies, self.num_sampled_clients):
341
308
  return None, None
342
309
 
343
310
  # Aggregate
@@ -350,3 +317,58 @@ class DifferentialPrivacyClientSideFixedClipping(DifferentialPrivacyFixedClippin
350
317
  aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
351
318
 
352
319
  return aggregated_arrays, aggregated_metrics
320
+
321
+
322
+ def validate_replies(replies: Iterable[Message], num_sampled_clients: int) -> bool:
323
+ """Validate replies and log errors/warnings.
324
+
325
+ Arguments
326
+ ----------
327
+ replies : Iterable[Message]
328
+ The replies to validate.
329
+ num_sampled_clients : int
330
+ The expected number of sampled clients.
331
+
332
+ Returns
333
+ -------
334
+ bool
335
+ True if replies are valid for aggregation, False otherwise.
336
+ """
337
+ num_errors = 0
338
+ num_replies_with_content = 0
339
+ for msg in replies:
340
+ if msg.has_error():
341
+ log(
342
+ INFO,
343
+ "Received error in reply from node %d: %s",
344
+ msg.metadata.src_node_id,
345
+ msg.error,
346
+ )
347
+ num_errors += 1
348
+ else:
349
+ num_replies_with_content += 1
350
+
351
+ # Errors are not allowed
352
+ if num_errors:
353
+ log(
354
+ INFO,
355
+ "aggregate_train: Some clients reported errors. Skipping aggregation.",
356
+ )
357
+ return False
358
+
359
+ log(
360
+ INFO,
361
+ "aggregate_train: Received %s results and %s failures",
362
+ num_replies_with_content,
363
+ num_errors,
364
+ )
365
+
366
+ if num_replies_with_content != num_sampled_clients:
367
+ log(
368
+ WARNING,
369
+ CLIENTS_DISCREPANCY_WARNING,
370
+ num_replies_with_content,
371
+ num_sampled_clients,
372
+ )
373
+
374
+ return True
@@ -153,9 +153,6 @@ class FedAdagrad(FedOpt):
153
153
  for k, x in self.current_arrays.items()
154
154
  }
155
155
 
156
- # Update current arrays
157
- self.current_arrays = new_arrays
158
-
159
156
  return (
160
157
  ArrayRecord(OrderedDict({k: Array(v) for k, v in new_arrays.items()})),
161
158
  aggregated_metrics,
@@ -172,9 +172,6 @@ class FedAdam(FedOpt):
172
172
  for k, x in self.current_arrays.items()
173
173
  }
174
174
 
175
- # Update current arrays
176
- self.current_arrays = new_arrays
177
-
178
175
  return (
179
176
  ArrayRecord(OrderedDict({k: Array(v) for k, v in new_arrays.items()})),
180
177
  aggregated_metrics,
@@ -126,9 +126,9 @@ class FedAvgM(FedAvg):
126
126
  """Log summary configuration of the strategy."""
127
127
  opt_status = "ON" if self.server_opt else "OFF"
128
128
  log(INFO, "\t├──> FedAvgM settings:")
129
- log(INFO, "\t|\t├── Server optimization: %s", opt_status)
130
- log(INFO, "\t|\t├── Server learning rate: %s", self.server_learning_rate)
131
- log(INFO, "\t|\t└── Server Momentum: %s", self.server_momentum)
129
+ log(INFO, "\t│\t├── Server optimization: %s", opt_status)
130
+ log(INFO, "\t│\t├── Server learning rate: %s", self.server_learning_rate)
131
+ log(INFO, "\t│\t└── Server Momentum: %s", self.server_momentum)
132
132
  super().summary()
133
133
 
134
134
  def configure_train(
@@ -162,7 +162,7 @@ class FedProx(FedAvg):
162
162
  def summary(self) -> None:
163
163
  """Log summary configuration of the strategy."""
164
164
  log(INFO, "\t├──> FedProx settings:")
165
- log(INFO, "\t|\t└── Proximal mu: %s", self.proximal_mu)
165
+ log(INFO, "\t│\t└── Proximal mu: %s", self.proximal_mu)
166
166
  super().summary()
167
167
 
168
168
  def configure_train(
@@ -108,7 +108,7 @@ class FedTrimmedAvg(FedAvg):
108
108
  def summary(self) -> None:
109
109
  """Log summary configuration of the strategy."""
110
110
  log(INFO, "\t├──> FedTrimmedAvg settings:")
111
- log(INFO, "\t|\t└── beta: %s", self.beta)
111
+ log(INFO, "\t│\t└── beta: %s", self.beta)
112
112
  super().summary()
113
113
 
114
114
  def aggregate_train(
@@ -164,9 +164,6 @@ class FedYogi(FedOpt):
164
164
  for k, x in self.current_arrays.items()
165
165
  }
166
166
 
167
- # Update current arrays
168
- self.current_arrays = new_arrays
169
-
170
167
  return (
171
168
  ArrayRecord(OrderedDict({k: Array(v) for k, v in new_arrays.items()})),
172
169
  aggregated_metrics,
@@ -0,0 +1,230 @@
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
+ """Machine Learning with Adversaries: Byzantine Tolerant Gradient Descent.
16
+
17
+ [Blanchard et al., 2017].
18
+
19
+ Paper: proceedings.neurips.cc/paper/2017/file/f4b9ec30ad9f68f89b29639786cb62ef-Paper.pdf
20
+ """
21
+
22
+
23
+ from collections.abc import Iterable
24
+ from logging import INFO
25
+ from typing import Callable, Optional
26
+
27
+ import numpy as np
28
+
29
+ from flwr.common import ArrayRecord, Message, MetricRecord, NDArray, RecordDict, log
30
+
31
+ from .fedavg import FedAvg
32
+ from .strategy_utils import aggregate_arrayrecords
33
+
34
+
35
+ # pylint: disable=too-many-instance-attributes
36
+ class Krum(FedAvg):
37
+ """Krum [Blanchard et al., 2017] strategy.
38
+
39
+ Implementation based on https://arxiv.org/abs/1703.02757
40
+
41
+ Parameters
42
+ ----------
43
+ fraction_train : float (default: 1.0)
44
+ Fraction of nodes used during training. In case `min_train_nodes`
45
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
46
+ will still be sampled.
47
+ fraction_evaluate : float (default: 1.0)
48
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
49
+ is larger than `fraction_evaluate * total_connected_nodes`,
50
+ `min_evaluate_nodes` will still be sampled.
51
+ min_train_nodes : int (default: 2)
52
+ Minimum number of nodes used during training.
53
+ min_evaluate_nodes : int (default: 2)
54
+ Minimum number of nodes used during validation.
55
+ min_available_nodes : int (default: 2)
56
+ Minimum number of total nodes in the system.
57
+ num_malicious_nodes : int (default: 0)
58
+ Number of malicious nodes in the system. Defaults to 0.
59
+ num_nodes_to_keep : int (default: 0)
60
+ Number of nodes to keep before averaging (MultiKrum). Defaults to 0, in
61
+ that case classical Krum is applied.
62
+ weighted_by_key : str (default: "num-examples")
63
+ The key within each MetricRecord whose value is used as the weight when
64
+ computing weighted averages for both ArrayRecords and MetricRecords.
65
+ arrayrecord_key : str (default: "arrays")
66
+ Key used to store the ArrayRecord when constructing Messages.
67
+ configrecord_key : str (default: "config")
68
+ Key used to store the ConfigRecord when constructing Messages.
69
+ train_metrics_aggr_fn : Optional[callable] (default: None)
70
+ Function with signature (list[RecordDict], str) -> MetricRecord,
71
+ used to aggregate MetricRecords from training round replies.
72
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
73
+ average using the provided weight factor key.
74
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
75
+ Function with signature (list[RecordDict], str) -> MetricRecord,
76
+ used to aggregate MetricRecords from training round replies.
77
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
78
+ average using the provided weight factor key.
79
+ """
80
+
81
+ # pylint: disable=too-many-arguments,too-many-positional-arguments
82
+ def __init__(
83
+ self,
84
+ fraction_train: float = 1.0,
85
+ fraction_evaluate: float = 1.0,
86
+ min_train_nodes: int = 2,
87
+ min_evaluate_nodes: int = 2,
88
+ min_available_nodes: int = 2,
89
+ num_malicious_nodes: int = 0,
90
+ num_nodes_to_keep: int = 0,
91
+ weighted_by_key: str = "num-examples",
92
+ arrayrecord_key: str = "arrays",
93
+ configrecord_key: str = "config",
94
+ train_metrics_aggr_fn: Optional[
95
+ Callable[[list[RecordDict], str], MetricRecord]
96
+ ] = None,
97
+ evaluate_metrics_aggr_fn: Optional[
98
+ Callable[[list[RecordDict], str], MetricRecord]
99
+ ] = None,
100
+ ) -> None:
101
+ super().__init__(
102
+ fraction_train=fraction_train,
103
+ fraction_evaluate=fraction_evaluate,
104
+ min_train_nodes=min_train_nodes,
105
+ min_evaluate_nodes=min_evaluate_nodes,
106
+ min_available_nodes=min_available_nodes,
107
+ weighted_by_key=weighted_by_key,
108
+ arrayrecord_key=arrayrecord_key,
109
+ configrecord_key=configrecord_key,
110
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
111
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
112
+ )
113
+ self.num_malicious_nodes = num_malicious_nodes
114
+ self.num_nodes_to_keep = num_nodes_to_keep
115
+
116
+ def summary(self) -> None:
117
+ """Log summary configuration of the strategy."""
118
+ log(INFO, "\t├──> Krum settings:")
119
+ log(INFO, "\t│\t├── Number of malicious nodes: %d", self.num_malicious_nodes)
120
+ log(INFO, "\t│\t└── Number of nodes to keep: %d", self.num_nodes_to_keep)
121
+ super().summary()
122
+
123
+ def _compute_distances(self, records: list[ArrayRecord]) -> NDArray:
124
+ """Compute distances between ArrayRecords.
125
+
126
+ Parameters
127
+ ----------
128
+ records : list[ArrayRecord]
129
+ A list of ArrayRecords (arrays received in replies)
130
+
131
+ Returns
132
+ -------
133
+ NDArray
134
+ A 2D array representing the distance matrix of squared distances
135
+ between input ArrayRecords
136
+ """
137
+ flat_w = np.array(
138
+ [
139
+ np.concatenate(rec.to_numpy_ndarrays(), axis=None).ravel()
140
+ for rec in records
141
+ ]
142
+ )
143
+ distance_matrix = np.zeros((len(records), len(records)))
144
+ for i, flat_w_i in enumerate(flat_w):
145
+ for j, flat_w_j in enumerate(flat_w):
146
+ delta = flat_w_i - flat_w_j
147
+ norm = np.linalg.norm(delta)
148
+ distance_matrix[i, j] = norm**2
149
+ return distance_matrix
150
+
151
+ def _krum(self, replies: list[RecordDict]) -> list[RecordDict]:
152
+ """Select the set of RecordDicts to aggregate using the Krum or MultiKrum
153
+ algorithm.
154
+
155
+ For each node, computes the sum of squared distances to its n-f-2 closest
156
+ parameter vectors, where n is the number of nodes and f is the number of
157
+ malicious nodes. The node(s) with the lowest score(s) are selected for
158
+ aggregation.
159
+
160
+ Parameters
161
+ ----------
162
+ replies : list[RecordDict]
163
+ List of RecordDicts, each containing an ArrayRecord representing model
164
+ parameters from a client.
165
+
166
+ Returns
167
+ -------
168
+ list[RecordDict]
169
+ List of RecordDicts selected for aggregation. If `num_nodes_to_keep` > 0,
170
+ returns the top `num_nodes_to_keep` RecordDicts (MultiKrum); otherwise,
171
+ returns the single RecordDict with the lowest score (Krum).
172
+ """
173
+ # Construct list of ArrayRecord objects from replies
174
+ # Recall aggregate_train first ensures replies only contain one ArrayRecord
175
+ array_records = [list(reply.array_records.values())[0] for reply in replies]
176
+ distance_matrix = self._compute_distances(array_records)
177
+
178
+ # For each node, take the n-f-2 closest parameters vectors
179
+ num_closest = max(1, len(array_records) - self.num_malicious_nodes - 2)
180
+ closest_indices = []
181
+ for distance in distance_matrix:
182
+ closest_indices.append(
183
+ np.argsort(distance)[1 : num_closest + 1].tolist() # noqa: E203
184
+ )
185
+
186
+ # Compute the score for each node, that is the sum of the distances
187
+ # of the n-f-2 closest parameters vectors
188
+ scores = [
189
+ np.sum(distance_matrix[i, closest_indices[i]])
190
+ for i in range(len(distance_matrix))
191
+ ]
192
+
193
+ # Return RecordDicts that should be aggregated
194
+ if self.num_nodes_to_keep > 0:
195
+ # Choose to_keep nodes and return their average (MultiKrum)
196
+ best_indices = np.argsort(scores)[::-1][
197
+ len(scores) - self.num_nodes_to_keep :
198
+ ] # noqa: E203
199
+ return [replies[i] for i in best_indices]
200
+
201
+ # Return the RecordDict with the ArrayRecord that minimize the score (Krum)
202
+ return [replies[np.argmin(scores)]]
203
+
204
+ def aggregate_train(
205
+ self,
206
+ server_round: int,
207
+ replies: Iterable[Message],
208
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
209
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
210
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
211
+
212
+ arrays, metrics = None, None
213
+ if valid_replies:
214
+ reply_contents = [msg.content for msg in valid_replies]
215
+
216
+ # Krum
217
+ replies_to_aggregate = self._krum(reply_contents)
218
+
219
+ # Aggregate ArrayRecords
220
+ arrays = aggregate_arrayrecords(
221
+ replies_to_aggregate,
222
+ self.weighted_by_key,
223
+ )
224
+
225
+ # Aggregate MetricRecords
226
+ metrics = self.train_metrics_aggr_fn(
227
+ replies_to_aggregate,
228
+ self.weighted_by_key,
229
+ )
230
+ return arrays, metrics