flwr 1.20.0__py3-none-any.whl → 1.22.0__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.
Files changed (182) hide show
  1. flwr/__init__.py +4 -1
  2. flwr/app/__init__.py +28 -0
  3. flwr/app/exception.py +31 -0
  4. flwr/cli/app.py +2 -0
  5. flwr/cli/auth_plugin/oidc_cli_plugin.py +4 -4
  6. flwr/cli/cli_user_auth_interceptor.py +1 -1
  7. flwr/cli/config_utils.py +3 -3
  8. flwr/cli/constant.py +25 -8
  9. flwr/cli/log.py +9 -9
  10. flwr/cli/login/login.py +3 -3
  11. flwr/cli/ls.py +5 -5
  12. flwr/cli/new/new.py +15 -2
  13. flwr/cli/new/templates/app/README.flowertune.md.tpl +1 -1
  14. flwr/cli/new/templates/app/code/__init__.pytorch_legacy_api.py.tpl +1 -0
  15. flwr/cli/new/templates/app/code/client.baseline.py.tpl +64 -47
  16. flwr/cli/new/templates/app/code/client.huggingface.py.tpl +68 -30
  17. flwr/cli/new/templates/app/code/client.jax.py.tpl +63 -42
  18. flwr/cli/new/templates/app/code/client.mlx.py.tpl +80 -51
  19. flwr/cli/new/templates/app/code/client.numpy.py.tpl +36 -13
  20. flwr/cli/new/templates/app/code/client.pytorch.py.tpl +71 -46
  21. flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +55 -0
  22. flwr/cli/new/templates/app/code/client.sklearn.py.tpl +75 -30
  23. flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +69 -44
  24. flwr/cli/new/templates/app/code/client.xgboost.py.tpl +110 -0
  25. flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +56 -90
  26. flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +1 -23
  27. flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +37 -58
  28. flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +39 -44
  29. flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -14
  30. flwr/cli/new/templates/app/code/server.baseline.py.tpl +27 -29
  31. flwr/cli/new/templates/app/code/server.huggingface.py.tpl +23 -19
  32. flwr/cli/new/templates/app/code/server.jax.py.tpl +27 -14
  33. flwr/cli/new/templates/app/code/server.mlx.py.tpl +29 -19
  34. flwr/cli/new/templates/app/code/server.numpy.py.tpl +30 -17
  35. flwr/cli/new/templates/app/code/server.pytorch.py.tpl +36 -26
  36. flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +31 -0
  37. flwr/cli/new/templates/app/code/server.sklearn.py.tpl +29 -21
  38. flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +28 -19
  39. flwr/cli/new/templates/app/code/server.xgboost.py.tpl +56 -0
  40. flwr/cli/new/templates/app/code/task.huggingface.py.tpl +16 -20
  41. flwr/cli/new/templates/app/code/task.jax.py.tpl +1 -1
  42. flwr/cli/new/templates/app/code/task.numpy.py.tpl +1 -1
  43. flwr/cli/new/templates/app/code/task.pytorch.py.tpl +14 -27
  44. flwr/cli/new/templates/app/code/task.pytorch_legacy_api.py.tpl +111 -0
  45. flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +1 -2
  46. flwr/cli/new/templates/app/code/task.xgboost.py.tpl +67 -0
  47. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +4 -4
  48. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +2 -2
  49. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +4 -4
  50. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
  51. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +2 -2
  52. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
  53. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +3 -3
  54. flwr/cli/new/templates/app/pyproject.pytorch_legacy_api.toml.tpl +53 -0
  55. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
  56. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
  57. flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +61 -0
  58. flwr/cli/pull.py +100 -0
  59. flwr/cli/run/run.py +9 -13
  60. flwr/cli/stop.py +7 -4
  61. flwr/cli/utils.py +36 -8
  62. flwr/client/grpc_rere_client/connection.py +1 -12
  63. flwr/client/rest_client/connection.py +3 -0
  64. flwr/clientapp/__init__.py +10 -0
  65. flwr/clientapp/mod/__init__.py +29 -0
  66. flwr/clientapp/mod/centraldp_mods.py +248 -0
  67. flwr/clientapp/mod/localdp_mod.py +169 -0
  68. flwr/clientapp/typing.py +22 -0
  69. flwr/common/args.py +20 -6
  70. flwr/common/auth_plugin/__init__.py +4 -4
  71. flwr/common/auth_plugin/auth_plugin.py +7 -7
  72. flwr/common/constant.py +26 -4
  73. flwr/common/event_log_plugin/event_log_plugin.py +1 -1
  74. flwr/common/exit/__init__.py +4 -0
  75. flwr/common/exit/exit.py +8 -1
  76. flwr/common/exit/exit_code.py +30 -7
  77. flwr/common/exit/exit_handler.py +62 -0
  78. flwr/common/{exit_handlers.py → exit/signal_handler.py} +20 -37
  79. flwr/common/grpc.py +0 -11
  80. flwr/common/inflatable_utils.py +1 -1
  81. flwr/common/logger.py +1 -1
  82. flwr/common/record/typeddict.py +12 -0
  83. flwr/common/retry_invoker.py +30 -11
  84. flwr/common/telemetry.py +4 -0
  85. flwr/compat/server/app.py +2 -2
  86. flwr/proto/appio_pb2.py +25 -17
  87. flwr/proto/appio_pb2.pyi +46 -2
  88. flwr/proto/clientappio_pb2.py +3 -11
  89. flwr/proto/clientappio_pb2.pyi +0 -47
  90. flwr/proto/clientappio_pb2_grpc.py +19 -20
  91. flwr/proto/clientappio_pb2_grpc.pyi +10 -11
  92. flwr/proto/control_pb2.py +66 -0
  93. flwr/proto/{exec_pb2.pyi → control_pb2.pyi} +24 -0
  94. flwr/proto/{exec_pb2_grpc.py → control_pb2_grpc.py} +88 -54
  95. flwr/proto/control_pb2_grpc.pyi +106 -0
  96. flwr/proto/serverappio_pb2.py +2 -2
  97. flwr/proto/serverappio_pb2_grpc.py +68 -0
  98. flwr/proto/serverappio_pb2_grpc.pyi +26 -0
  99. flwr/proto/simulationio_pb2.py +4 -11
  100. flwr/proto/simulationio_pb2.pyi +0 -58
  101. flwr/proto/simulationio_pb2_grpc.py +129 -27
  102. flwr/proto/simulationio_pb2_grpc.pyi +52 -13
  103. flwr/server/app.py +142 -152
  104. flwr/server/grid/grpc_grid.py +3 -0
  105. flwr/server/grid/inmemory_grid.py +1 -0
  106. flwr/server/serverapp/app.py +157 -146
  107. flwr/server/superlink/fleet/vce/backend/raybackend.py +3 -1
  108. flwr/server/superlink/fleet/vce/vce_api.py +6 -6
  109. flwr/server/superlink/linkstate/in_memory_linkstate.py +34 -0
  110. flwr/server/superlink/linkstate/linkstate.py +2 -1
  111. flwr/server/superlink/linkstate/sqlite_linkstate.py +45 -0
  112. flwr/server/superlink/serverappio/serverappio_grpc.py +1 -1
  113. flwr/server/superlink/serverappio/serverappio_servicer.py +61 -6
  114. flwr/server/superlink/simulation/simulationio_servicer.py +97 -21
  115. flwr/serverapp/__init__.py +12 -0
  116. flwr/serverapp/exception.py +38 -0
  117. flwr/serverapp/strategy/__init__.py +64 -0
  118. flwr/serverapp/strategy/bulyan.py +238 -0
  119. flwr/serverapp/strategy/dp_adaptive_clipping.py +335 -0
  120. flwr/serverapp/strategy/dp_fixed_clipping.py +374 -0
  121. flwr/serverapp/strategy/fedadagrad.py +159 -0
  122. flwr/serverapp/strategy/fedadam.py +178 -0
  123. flwr/serverapp/strategy/fedavg.py +320 -0
  124. flwr/serverapp/strategy/fedavgm.py +198 -0
  125. flwr/serverapp/strategy/fedmedian.py +105 -0
  126. flwr/serverapp/strategy/fedopt.py +218 -0
  127. flwr/serverapp/strategy/fedprox.py +174 -0
  128. flwr/serverapp/strategy/fedtrimmedavg.py +176 -0
  129. flwr/serverapp/strategy/fedxgb_bagging.py +117 -0
  130. flwr/serverapp/strategy/fedxgb_cyclic.py +220 -0
  131. flwr/serverapp/strategy/fedyogi.py +170 -0
  132. flwr/serverapp/strategy/krum.py +112 -0
  133. flwr/serverapp/strategy/multikrum.py +247 -0
  134. flwr/serverapp/strategy/qfedavg.py +252 -0
  135. flwr/serverapp/strategy/result.py +105 -0
  136. flwr/serverapp/strategy/strategy.py +285 -0
  137. flwr/serverapp/strategy/strategy_utils.py +299 -0
  138. flwr/simulation/app.py +161 -164
  139. flwr/simulation/run_simulation.py +25 -30
  140. flwr/supercore/app_utils.py +58 -0
  141. flwr/{supernode/scheduler → supercore/cli}/__init__.py +3 -3
  142. flwr/supercore/cli/flower_superexec.py +166 -0
  143. flwr/supercore/constant.py +19 -0
  144. flwr/supercore/{scheduler → corestate}/__init__.py +3 -3
  145. flwr/supercore/corestate/corestate.py +81 -0
  146. flwr/supercore/grpc_health/__init__.py +3 -0
  147. flwr/supercore/grpc_health/health_server.py +53 -0
  148. flwr/supercore/grpc_health/simple_health_servicer.py +2 -2
  149. flwr/{superexec → supercore/superexec}/__init__.py +1 -1
  150. flwr/supercore/superexec/plugin/__init__.py +28 -0
  151. flwr/{supernode/scheduler/simple_clientapp_scheduler_plugin.py → supercore/superexec/plugin/base_exec_plugin.py} +10 -6
  152. flwr/supercore/superexec/plugin/clientapp_exec_plugin.py +28 -0
  153. flwr/supercore/{scheduler/plugin.py → superexec/plugin/exec_plugin.py} +15 -5
  154. flwr/supercore/superexec/plugin/serverapp_exec_plugin.py +28 -0
  155. flwr/supercore/superexec/plugin/simulation_exec_plugin.py +28 -0
  156. flwr/supercore/superexec/run_superexec.py +199 -0
  157. flwr/superlink/artifact_provider/__init__.py +22 -0
  158. flwr/superlink/artifact_provider/artifact_provider.py +37 -0
  159. flwr/superlink/servicer/__init__.py +15 -0
  160. flwr/superlink/servicer/control/__init__.py +22 -0
  161. flwr/{superexec/exec_event_log_interceptor.py → superlink/servicer/control/control_event_log_interceptor.py} +7 -7
  162. flwr/{superexec/exec_grpc.py → superlink/servicer/control/control_grpc.py} +27 -29
  163. flwr/{superexec/exec_license_interceptor.py → superlink/servicer/control/control_license_interceptor.py} +6 -6
  164. flwr/{superexec/exec_servicer.py → superlink/servicer/control/control_servicer.py} +127 -31
  165. flwr/{superexec/exec_user_auth_interceptor.py → superlink/servicer/control/control_user_auth_interceptor.py} +10 -10
  166. flwr/supernode/cli/flower_supernode.py +3 -0
  167. flwr/supernode/cli/flwr_clientapp.py +18 -21
  168. flwr/supernode/nodestate/in_memory_nodestate.py +2 -2
  169. flwr/supernode/nodestate/nodestate.py +3 -59
  170. flwr/supernode/runtime/run_clientapp.py +39 -102
  171. flwr/supernode/servicer/clientappio/clientappio_servicer.py +10 -17
  172. flwr/supernode/start_client_internal.py +35 -76
  173. {flwr-1.20.0.dist-info → flwr-1.22.0.dist-info}/METADATA +9 -18
  174. {flwr-1.20.0.dist-info → flwr-1.22.0.dist-info}/RECORD +176 -128
  175. {flwr-1.20.0.dist-info → flwr-1.22.0.dist-info}/entry_points.txt +1 -0
  176. flwr/proto/exec_pb2.py +0 -62
  177. flwr/proto/exec_pb2_grpc.pyi +0 -93
  178. flwr/superexec/app.py +0 -45
  179. flwr/superexec/deployment.py +0 -191
  180. flwr/superexec/executor.py +0 -100
  181. flwr/superexec/simulation.py +0 -129
  182. {flwr-1.20.0.dist-info → flwr-1.22.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,247 @@
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, cast
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 MultiKrum(FedAvg):
37
+ """MultiKrum [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_select : int (default: 1)
60
+ Number of nodes to select before averaging.
61
+ weighted_by_key : str (default: "num-examples")
62
+ The key within each MetricRecord whose value is used as the weight when
63
+ computing weighted averages for both ArrayRecords and MetricRecords.
64
+ arrayrecord_key : str (default: "arrays")
65
+ Key used to store the ArrayRecord when constructing Messages.
66
+ configrecord_key : str (default: "config")
67
+ Key used to store the ConfigRecord when constructing Messages.
68
+ train_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
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
74
+ Function with signature (list[RecordDict], str) -> MetricRecord,
75
+ used to aggregate MetricRecords from training round replies.
76
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
77
+ average using the provided weight factor key.
78
+
79
+ Notes
80
+ -----
81
+ MultiKrum is a generalization of Krum. If `num_nodes_to_select` is set to 1,
82
+ MultiKrum will reduce to classical Krum.
83
+ """
84
+
85
+ # pylint: disable=too-many-arguments,too-many-positional-arguments
86
+ def __init__(
87
+ self,
88
+ fraction_train: float = 1.0,
89
+ fraction_evaluate: float = 1.0,
90
+ min_train_nodes: int = 2,
91
+ min_evaluate_nodes: int = 2,
92
+ min_available_nodes: int = 2,
93
+ num_malicious_nodes: int = 0,
94
+ num_nodes_to_select: int = 1,
95
+ weighted_by_key: str = "num-examples",
96
+ arrayrecord_key: str = "arrays",
97
+ configrecord_key: str = "config",
98
+ train_metrics_aggr_fn: Optional[
99
+ Callable[[list[RecordDict], str], MetricRecord]
100
+ ] = None,
101
+ evaluate_metrics_aggr_fn: Optional[
102
+ Callable[[list[RecordDict], str], MetricRecord]
103
+ ] = None,
104
+ ) -> None:
105
+ super().__init__(
106
+ fraction_train=fraction_train,
107
+ fraction_evaluate=fraction_evaluate,
108
+ min_train_nodes=min_train_nodes,
109
+ min_evaluate_nodes=min_evaluate_nodes,
110
+ min_available_nodes=min_available_nodes,
111
+ weighted_by_key=weighted_by_key,
112
+ arrayrecord_key=arrayrecord_key,
113
+ configrecord_key=configrecord_key,
114
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
115
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
116
+ )
117
+ self.num_malicious_nodes = num_malicious_nodes
118
+ self.num_nodes_to_select = num_nodes_to_select
119
+
120
+ def summary(self) -> None:
121
+ """Log summary configuration of the strategy."""
122
+ log(INFO, "\t├──> MultiKrum settings:")
123
+ log(INFO, "\t│\t├── Number of malicious nodes: %d", self.num_malicious_nodes)
124
+ log(INFO, "\t│\t└── Number of nodes to select: %d", self.num_nodes_to_select)
125
+ super().summary()
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
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
134
+
135
+ arrays, metrics = None, None
136
+ if valid_replies:
137
+ reply_contents = [msg.content for msg in valid_replies]
138
+
139
+ # Krum or MultiKrum selection
140
+ replies_to_aggregate = select_multikrum(
141
+ reply_contents,
142
+ num_malicious_nodes=self.num_malicious_nodes,
143
+ num_nodes_to_select=self.num_nodes_to_select,
144
+ )
145
+
146
+ # Aggregate ArrayRecords
147
+ arrays = aggregate_arrayrecords(
148
+ replies_to_aggregate,
149
+ self.weighted_by_key,
150
+ )
151
+
152
+ # Aggregate MetricRecords
153
+ metrics = self.train_metrics_aggr_fn(
154
+ replies_to_aggregate,
155
+ self.weighted_by_key,
156
+ )
157
+ return arrays, metrics
158
+
159
+
160
+ def compute_distances(records: list[ArrayRecord]) -> NDArray:
161
+ """Compute squared L2 distances between ArrayRecords.
162
+
163
+ Parameters
164
+ ----------
165
+ records : list[ArrayRecord]
166
+ A list of ArrayRecords (arrays received in replies)
167
+
168
+ Returns
169
+ -------
170
+ NDArray
171
+ A 2D array representing the distance matrix of squared L2 distances
172
+ between input ArrayRecords
173
+ """
174
+ # Formula: ||x - y||^2 = ||x||^2 + ||y||^2 - 2 * x.y
175
+ # Flatten records and stack them into a matrix
176
+ flat_w = np.stack(
177
+ [np.concatenate(rec.to_numpy_ndarrays(), axis=None).ravel() for rec in records],
178
+ axis=0,
179
+ ) # shape: (n, d) with n number of records and d the dimension of model
180
+
181
+ # Compute squared norms of each vector
182
+ norms: NDArray = np.square(flat_w).sum(axis=1) # shape (n,)
183
+
184
+ # Use broadcasting to compute pairwise distances
185
+ distance_matrix: NDArray = norms[:, None] + norms[None, :] - 2 * flat_w @ flat_w.T
186
+ return distance_matrix
187
+
188
+
189
+ def select_multikrum(
190
+ contents: list[RecordDict],
191
+ num_malicious_nodes: int,
192
+ num_nodes_to_select: int,
193
+ ) -> list[RecordDict]:
194
+ """Select the set of RecordDicts to aggregate using the Krum or MultiKrum algorithm.
195
+
196
+ For each node, computes the sum of squared L2 distances to its n-f-2 closest
197
+ parameter vectors, where n is the number of nodes and f is the number of
198
+ malicious nodes. The node(s) with the lowest score(s) are selected for
199
+ aggregation.
200
+
201
+ Parameters
202
+ ----------
203
+ contents : list[RecordDict]
204
+ List of contents from reply messages, where each content is a RecordDict
205
+ containing an ArrayRecord of model parameters from a node (client).
206
+ num_malicious_nodes : int
207
+ Number of malicious nodes in the system.
208
+ num_nodes_to_select : int
209
+ Number of client updates to select.
210
+ - If 1, the algorithm reduces to classical Krum (selecting a single update).
211
+ - If >1, Multi-Krum is applied (selecting multiple updates).
212
+
213
+ Returns
214
+ -------
215
+ list[RecordDict]
216
+ Selected contents following the Krum or Multi-Krum algorithm.
217
+
218
+ Notes
219
+ -----
220
+ If `num_nodes_to_select` is set to 1, Multi-Krum reduces to classical Krum
221
+ and only a single RecordDict is selected.
222
+ """
223
+ # Construct list of ArrayRecord objects from replies
224
+ record_key = list(contents[0].array_records.keys())[0]
225
+ # Recall aggregate_train first ensures replies only contain one ArrayRecord
226
+ array_records = [cast(ArrayRecord, reply[record_key]) for reply in contents]
227
+ distance_matrix = compute_distances(array_records)
228
+
229
+ # For each node, take the n-f-2 closest parameters vectors
230
+ num_closest = max(1, len(array_records) - num_malicious_nodes - 2)
231
+ closest_indices = []
232
+ for distance in distance_matrix:
233
+ closest_indices.append(
234
+ np.argsort(distance)[1 : num_closest + 1].tolist() # noqa: E203
235
+ )
236
+
237
+ # Compute the score for each node, that is the sum of the distances
238
+ # of the n-f-2 closest parameters vectors
239
+ scores = [
240
+ np.sum(distance_matrix[i, closest_indices[i]])
241
+ for i in range(len(distance_matrix))
242
+ ]
243
+
244
+ # Choose the num_nodes_to_select lowest-scoring nodes (MultiKrum)
245
+ # and return their updates
246
+ best_indices = np.argsort(scores)[:num_nodes_to_select]
247
+ return [contents[i] for i in best_indices]
@@ -0,0 +1,252 @@
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
+ """Fair Resource Allocation in Federated Learning [Li et al., 2020] strategy.
16
+
17
+ Paper: openreview.net/pdf?id=ByexElSYDr
18
+ """
19
+
20
+
21
+ from collections import OrderedDict
22
+ from collections.abc import Iterable
23
+ from logging import INFO
24
+ from typing import Callable, Optional
25
+
26
+ import numpy as np
27
+
28
+ from flwr.common import (
29
+ Array,
30
+ ArrayRecord,
31
+ ConfigRecord,
32
+ Message,
33
+ MetricRecord,
34
+ NDArray,
35
+ RecordDict,
36
+ )
37
+ from flwr.common.logger import log
38
+ from flwr.server import Grid
39
+
40
+ from ..exception import AggregationError
41
+ from .fedavg import FedAvg
42
+
43
+
44
+ class QFedAvg(FedAvg):
45
+ """Q-FedAvg strategy.
46
+
47
+ Implementation based on openreview.net/pdf?id=ByexElSYDr
48
+
49
+ Parameters
50
+ ----------
51
+ client_learning_rate : float
52
+ Local learning rate used by clients during training. This value is used by
53
+ the strategy to approximate the base Lipschitz constant L, via
54
+ L = 1 / client_learning_rate.
55
+ q : float (default: 0.1)
56
+ The parameter q that controls the degree of fairness of the algorithm. Please
57
+ tune this parameter based on your use case.
58
+ When set to 0, q-FedAvg is equivalent to FedAvg.
59
+ train_loss_key : str (default: "train_loss")
60
+ The key within the MetricRecord whose value is used as the training loss when
61
+ aggregating ArrayRecords following q-FedAvg.
62
+ fraction_train : float (default: 1.0)
63
+ Fraction of nodes used during training. In case `min_train_nodes`
64
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
65
+ will still be sampled.
66
+ fraction_evaluate : float (default: 1.0)
67
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
68
+ is larger than `fraction_evaluate * total_connected_nodes`,
69
+ `min_evaluate_nodes` will still be sampled.
70
+ min_train_nodes : int (default: 2)
71
+ Minimum number of nodes used during training.
72
+ min_evaluate_nodes : int (default: 2)
73
+ Minimum number of nodes used during validation.
74
+ min_available_nodes : int (default: 2)
75
+ Minimum number of total nodes in the system.
76
+ weighted_by_key : str (default: "num-examples")
77
+ The key within each MetricRecord whose value is used as the weight when
78
+ computing weighted averages for MetricRecords.
79
+ arrayrecord_key : str (default: "arrays")
80
+ Key used to store the ArrayRecord when constructing Messages.
81
+ configrecord_key : str (default: "config")
82
+ Key used to store the ConfigRecord when constructing Messages.
83
+ train_metrics_aggr_fn : Optional[callable] (default: None)
84
+ Function with signature (list[RecordDict], str) -> MetricRecord,
85
+ used to aggregate MetricRecords from training round replies.
86
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
87
+ average using the provided weight factor key.
88
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
89
+ Function with signature (list[RecordDict], str) -> MetricRecord,
90
+ used to aggregate MetricRecords from training round replies.
91
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
92
+ average using the provided weight factor key.
93
+ """
94
+
95
+ def __init__( # pylint: disable=R0913, R0917
96
+ self,
97
+ client_learning_rate: float,
98
+ q: float = 0.1,
99
+ train_loss_key: str = "train_loss",
100
+ fraction_train: float = 1.0,
101
+ fraction_evaluate: float = 1.0,
102
+ min_train_nodes: int = 2,
103
+ min_evaluate_nodes: int = 2,
104
+ min_available_nodes: int = 2,
105
+ weighted_by_key: str = "num-examples",
106
+ arrayrecord_key: str = "arrays",
107
+ configrecord_key: str = "config",
108
+ train_metrics_aggr_fn: Optional[
109
+ Callable[[list[RecordDict], str], MetricRecord]
110
+ ] = None,
111
+ evaluate_metrics_aggr_fn: Optional[
112
+ Callable[[list[RecordDict], str], MetricRecord]
113
+ ] = None,
114
+ ) -> None:
115
+ super().__init__(
116
+ fraction_train=fraction_train,
117
+ fraction_evaluate=fraction_evaluate,
118
+ min_train_nodes=min_train_nodes,
119
+ min_evaluate_nodes=min_evaluate_nodes,
120
+ min_available_nodes=min_available_nodes,
121
+ weighted_by_key=weighted_by_key,
122
+ arrayrecord_key=arrayrecord_key,
123
+ configrecord_key=configrecord_key,
124
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
125
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
126
+ )
127
+ self.q = q
128
+ self.client_learning_rate = client_learning_rate
129
+ self.train_loss_key = train_loss_key
130
+ self.current_arrays: Optional[ArrayRecord] = None
131
+
132
+ def summary(self) -> None:
133
+ """Log summary configuration of the strategy."""
134
+ log(INFO, "\t├──> q-FedAvg settings:")
135
+ log(INFO, "\t│\t├── client_learning_rate: %s", self.client_learning_rate)
136
+ log(INFO, "\t│\t├── q: %s", self.q)
137
+ log(INFO, "\t│\t└── train_loss_key: '%s'", self.train_loss_key)
138
+ super().summary()
139
+
140
+ def configure_train(
141
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
142
+ ) -> Iterable[Message]:
143
+ """Configure the next round of federated training."""
144
+ self.current_arrays = arrays.copy()
145
+ return super().configure_train(server_round, arrays, config, grid)
146
+
147
+ def aggregate_train( # pylint: disable=too-many-locals
148
+ self,
149
+ server_round: int,
150
+ replies: Iterable[Message],
151
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
152
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
153
+ # Call FedAvg aggregate_train to perform validation and aggregation
154
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
155
+
156
+ if not valid_replies:
157
+ return None, None
158
+
159
+ # Compute estimate of Lipschitz constant L
160
+ L = 1.0 / self.client_learning_rate # pylint: disable=C0103
161
+
162
+ # q-FedAvg aggregation
163
+ if self.current_arrays is None:
164
+ raise AggregationError(
165
+ "Current global model weights are not available. Make sure to call"
166
+ "`configure_train` before calling `aggregate_train`."
167
+ )
168
+ array_keys = list(self.current_arrays.keys()) # Preserve keys
169
+ global_weights = self.current_arrays.to_numpy_ndarrays(keep_input=False)
170
+ sum_delta = None
171
+ sum_h = 0.0
172
+
173
+ for msg in valid_replies:
174
+ # Extract local weights and training loss from Message
175
+ local_weights = get_local_weights(msg)
176
+ loss = get_train_loss(msg, self.train_loss_key)
177
+
178
+ # Compute delta and h
179
+ delta, h = compute_delta_and_h(
180
+ global_weights, local_weights, self.q, L, loss
181
+ )
182
+
183
+ # Compute sum of deltas and sum of h
184
+ if sum_delta is None:
185
+ sum_delta = delta
186
+ else:
187
+ sum_delta = [sd + d for sd, d in zip(sum_delta, delta)]
188
+ sum_h += h
189
+
190
+ # Compute new global weights and convert to Array type
191
+ # `np.asarray` can convert numpy scalars to 0-dim arrays
192
+ assert sum_delta is not None # Make mypy happy
193
+ array_list = [
194
+ Array(np.asarray(gw - (d / sum_h)))
195
+ for gw, d in zip(global_weights, sum_delta)
196
+ ]
197
+
198
+ # Aggregate MetricRecords
199
+ metrics = self.train_metrics_aggr_fn(
200
+ [msg.content for msg in valid_replies],
201
+ self.weighted_by_key,
202
+ )
203
+ return ArrayRecord(OrderedDict(zip(array_keys, array_list))), metrics
204
+
205
+
206
+ def get_train_loss(msg: Message, loss_key: str) -> float:
207
+ """Extract training loss from a Message."""
208
+ metrics = list(msg.content.metric_records.values())[0]
209
+ if (loss := metrics.get(loss_key)) is None or not isinstance(loss, (int, float)):
210
+ raise AggregationError(
211
+ "Missing or invalid training loss. "
212
+ f"The strategy expected a float value for the key '{loss_key}' "
213
+ "as the training loss in each MetricRecord from the clients. "
214
+ f"Ensure that '{loss_key}' is present and maps to a valid float."
215
+ )
216
+ return float(loss)
217
+
218
+
219
+ def get_local_weights(msg: Message) -> list[NDArray]:
220
+ """Extract local weights from a Message."""
221
+ arrays = list(msg.content.array_records.values())[0]
222
+ return arrays.to_numpy_ndarrays(keep_input=False)
223
+
224
+
225
+ def l2_norm(ndarrays: list[NDArray]) -> float:
226
+ """Compute the squared L2 norm of a list of numpy.ndarray."""
227
+ return float(sum(np.sum(np.square(g)) for g in ndarrays))
228
+
229
+
230
+ def compute_delta_and_h(
231
+ global_weights: list[NDArray],
232
+ local_weights: list[NDArray],
233
+ q: float,
234
+ L: float, # Lipschitz constant # pylint: disable=C0103
235
+ loss: float,
236
+ ) -> tuple[list[NDArray], float]:
237
+ """Compute delta and h used in q-FedAvg aggregation."""
238
+ # Compute gradient_k = L * (w - w_k)
239
+ for gw, lw in zip(global_weights, local_weights):
240
+ np.subtract(gw, lw, out=lw)
241
+ lw *= L
242
+ grad = local_weights # After in-place operations, local_weights is now grad
243
+ # Compute ||w_k - w||^2
244
+ norm = l2_norm(grad)
245
+ # Compute delta_k = loss_k^q * gradient_k
246
+ loss_pow_q: float = np.float_power(loss + 1e-10, q)
247
+ for g in grad:
248
+ g *= loss_pow_q
249
+ delta = grad # After in-place multiplication, grad is now delta
250
+ # Compute h_k
251
+ h = q * np.float_power(loss + 1e-10, q - 1) * norm + L * loss_pow_q
252
+ return delta, h
@@ -0,0 +1,105 @@
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
+ """Strategy results."""
16
+
17
+
18
+ import pprint
19
+ from dataclasses import dataclass, field
20
+
21
+ from flwr.common import ArrayRecord, MetricRecord
22
+ from flwr.common.typing import MetricRecordValues
23
+
24
+
25
+ @dataclass
26
+ class Result:
27
+ """Data class carrying records generated during the execution of a strategy.
28
+
29
+ This class encapsulates the results of a federated learning strategy execution,
30
+ including the final global model parameters and metrics collected throughout
31
+ the federated training and evaluation (both federated and centralized) stages.
32
+
33
+ Attributes
34
+ ----------
35
+ arrays : ArrayRecord
36
+ The final global model parameters. Contains the
37
+ aggregated model weights/parameters that resulted from the federated
38
+ learning process.
39
+ train_metrics_clientapp : dict[int, MetricRecord]
40
+ Training metrics collected from ClientApps, indexed by round number.
41
+ Contains aggregated metrics (e.g., loss, accuracy) from the training
42
+ phase of each federated learning round.
43
+ evaluate_metrics_clientapp : dict[int, MetricRecord]
44
+ Evaluation metrics collected from ClientApps, indexed by round number.
45
+ Contains aggregated metrics (e.g. validation loss) from the evaluation
46
+ phase where ClientApps evaluate the global model on their local
47
+ validation/test data.
48
+ evaluate_metrics_serverapp : dict[int, MetricRecord]
49
+ Evaluation metrics generated at the ServerApp, indexed by round number.
50
+ Contains metrics from centralized evaluation performed by the ServerApp
51
+ (e.g., when the server evaluates the global model on a held-out dataset).
52
+ """
53
+
54
+ arrays: ArrayRecord = field(default_factory=ArrayRecord)
55
+ train_metrics_clientapp: dict[int, MetricRecord] = field(default_factory=dict)
56
+ evaluate_metrics_clientapp: dict[int, MetricRecord] = field(default_factory=dict)
57
+ evaluate_metrics_serverapp: dict[int, MetricRecord] = field(default_factory=dict)
58
+
59
+ def __repr__(self) -> str:
60
+ """Create a representation of the Result instance."""
61
+ rep = ""
62
+ arr_size = sum(len(array.data) for array in self.arrays.values()) / (1024**2)
63
+ rep += "Global Arrays:\n" + f"\tArrayRecord ({arr_size:.3f} MB)\n" + "\n"
64
+ rep += (
65
+ "Aggregated ClientApp-side Train Metrics:\n"
66
+ + pprint.pformat(stringify_dict(self.train_metrics_clientapp), indent=2)
67
+ + "\n\n"
68
+ )
69
+
70
+ rep += (
71
+ "Aggregated ClientApp-side Evaluate Metrics:\n"
72
+ + pprint.pformat(stringify_dict(self.evaluate_metrics_clientapp), indent=2)
73
+ + "\n\n"
74
+ )
75
+
76
+ rep += (
77
+ "ServerApp-side Evaluate Metrics:\n"
78
+ + pprint.pformat(stringify_dict(self.evaluate_metrics_serverapp), indent=2)
79
+ + "\n"
80
+ )
81
+
82
+ return rep
83
+
84
+
85
+ def format_value(val: MetricRecordValues) -> str:
86
+ """Format a value as string, applying scientific notation for floats."""
87
+ if isinstance(val, float):
88
+ return f"{val:.4e}"
89
+ if isinstance(val, int):
90
+ return str(val)
91
+ if isinstance(val, list):
92
+ return str([f"{x:.4e}" if isinstance(x, float) else str(x) for x in val])
93
+ return str(val)
94
+
95
+
96
+ def stringify_dict(d: dict[int, MetricRecord]) -> dict[int, dict[str, str]]:
97
+ """Return a copy results metrics but with values converted to string and formatted
98
+ accordingtly."""
99
+ new_metrics_dict = {}
100
+ for k, inner in d.items():
101
+ new_inner = {}
102
+ for ik, iv in inner.items():
103
+ new_inner[ik] = format_value(iv)
104
+ new_metrics_dict[k] = new_inner
105
+ return new_metrics_dict