flwr 1.21.0__py3-none-any.whl → 1.23.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 (175) hide show
  1. flwr/cli/app.py +17 -1
  2. flwr/cli/auth_plugin/__init__.py +15 -6
  3. flwr/cli/auth_plugin/auth_plugin.py +95 -0
  4. flwr/cli/auth_plugin/noop_auth_plugin.py +58 -0
  5. flwr/cli/auth_plugin/oidc_cli_plugin.py +16 -25
  6. flwr/cli/build.py +118 -47
  7. flwr/cli/{cli_user_auth_interceptor.py → cli_account_auth_interceptor.py} +6 -5
  8. flwr/cli/log.py +2 -2
  9. flwr/cli/login/login.py +34 -23
  10. flwr/cli/ls.py +13 -9
  11. flwr/cli/new/new.py +196 -42
  12. flwr/cli/new/templates/app/README.flowertune.md.tpl +1 -1
  13. flwr/cli/new/templates/app/code/client.baseline.py.tpl +64 -47
  14. flwr/cli/new/templates/app/code/client.huggingface.py.tpl +68 -30
  15. flwr/cli/new/templates/app/code/client.jax.py.tpl +63 -42
  16. flwr/cli/new/templates/app/code/client.mlx.py.tpl +80 -51
  17. flwr/cli/new/templates/app/code/client.numpy.py.tpl +36 -13
  18. flwr/cli/new/templates/app/code/client.pytorch.py.tpl +71 -46
  19. flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +55 -0
  20. flwr/cli/new/templates/app/code/client.sklearn.py.tpl +75 -30
  21. flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +69 -44
  22. flwr/cli/new/templates/app/code/client.xgboost.py.tpl +110 -0
  23. flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +56 -90
  24. flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +1 -23
  25. flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +37 -58
  26. flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +39 -44
  27. flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -14
  28. flwr/cli/new/templates/app/code/server.baseline.py.tpl +27 -29
  29. flwr/cli/new/templates/app/code/server.huggingface.py.tpl +23 -19
  30. flwr/cli/new/templates/app/code/server.jax.py.tpl +27 -14
  31. flwr/cli/new/templates/app/code/server.mlx.py.tpl +29 -19
  32. flwr/cli/new/templates/app/code/server.numpy.py.tpl +30 -17
  33. flwr/cli/new/templates/app/code/server.pytorch.py.tpl +36 -26
  34. flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +31 -0
  35. flwr/cli/new/templates/app/code/server.sklearn.py.tpl +29 -21
  36. flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +28 -19
  37. flwr/cli/new/templates/app/code/server.xgboost.py.tpl +56 -0
  38. flwr/cli/new/templates/app/code/task.huggingface.py.tpl +16 -20
  39. flwr/cli/new/templates/app/code/task.jax.py.tpl +1 -1
  40. flwr/cli/new/templates/app/code/task.numpy.py.tpl +1 -1
  41. flwr/cli/new/templates/app/code/task.pytorch.py.tpl +14 -27
  42. flwr/cli/new/templates/app/code/{task.pytorch_msg_api.py.tpl → task.pytorch_legacy_api.py.tpl} +27 -14
  43. flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +1 -2
  44. flwr/cli/new/templates/app/code/task.xgboost.py.tpl +67 -0
  45. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +4 -4
  46. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +2 -2
  47. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +4 -4
  48. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
  49. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +2 -2
  50. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
  51. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +3 -3
  52. flwr/cli/new/templates/app/{pyproject.pytorch_msg_api.toml.tpl → pyproject.pytorch_legacy_api.toml.tpl} +3 -3
  53. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
  54. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
  55. flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +61 -0
  56. flwr/cli/pull.py +100 -0
  57. flwr/cli/run/run.py +11 -7
  58. flwr/cli/stop.py +2 -2
  59. flwr/cli/supernode/__init__.py +25 -0
  60. flwr/cli/supernode/ls.py +260 -0
  61. flwr/cli/supernode/register.py +185 -0
  62. flwr/cli/supernode/unregister.py +138 -0
  63. flwr/cli/utils.py +109 -69
  64. flwr/client/__init__.py +2 -1
  65. flwr/client/grpc_adapter_client/connection.py +6 -8
  66. flwr/client/grpc_rere_client/connection.py +59 -31
  67. flwr/client/grpc_rere_client/grpc_adapter.py +28 -12
  68. flwr/client/grpc_rere_client/{client_interceptor.py → node_auth_client_interceptor.py} +3 -6
  69. flwr/client/mod/secure_aggregation/secaggplus_mod.py +7 -5
  70. flwr/client/rest_client/connection.py +82 -37
  71. flwr/clientapp/__init__.py +1 -2
  72. flwr/clientapp/mod/__init__.py +4 -1
  73. flwr/clientapp/mod/centraldp_mods.py +156 -40
  74. flwr/clientapp/mod/localdp_mod.py +169 -0
  75. flwr/clientapp/typing.py +22 -0
  76. flwr/{client/clientapp → clientapp}/utils.py +1 -1
  77. flwr/common/constant.py +56 -13
  78. flwr/common/exit/exit_code.py +24 -10
  79. flwr/common/inflatable_utils.py +10 -10
  80. flwr/common/record/array.py +3 -3
  81. flwr/common/record/arrayrecord.py +10 -1
  82. flwr/common/record/typeddict.py +12 -0
  83. flwr/common/secure_aggregation/crypto/symmetric_encryption.py +1 -89
  84. flwr/common/serde.py +4 -2
  85. flwr/common/typing.py +7 -6
  86. flwr/compat/client/app.py +1 -1
  87. flwr/compat/client/grpc_client/connection.py +2 -2
  88. flwr/proto/control_pb2.py +48 -31
  89. flwr/proto/control_pb2.pyi +95 -5
  90. flwr/proto/control_pb2_grpc.py +136 -0
  91. flwr/proto/control_pb2_grpc.pyi +52 -0
  92. flwr/proto/fab_pb2.py +11 -7
  93. flwr/proto/fab_pb2.pyi +21 -1
  94. flwr/proto/fleet_pb2.py +31 -23
  95. flwr/proto/fleet_pb2.pyi +63 -23
  96. flwr/proto/fleet_pb2_grpc.py +98 -28
  97. flwr/proto/fleet_pb2_grpc.pyi +45 -13
  98. flwr/proto/node_pb2.py +3 -1
  99. flwr/proto/node_pb2.pyi +48 -0
  100. flwr/server/app.py +152 -114
  101. flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +17 -7
  102. flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +132 -38
  103. flwr/server/superlink/fleet/grpc_rere/{server_interceptor.py → node_auth_server_interceptor.py} +27 -51
  104. flwr/server/superlink/fleet/message_handler/message_handler.py +67 -22
  105. flwr/server/superlink/fleet/rest_rere/rest_api.py +52 -31
  106. flwr/server/superlink/fleet/vce/backend/backend.py +1 -1
  107. flwr/server/superlink/fleet/vce/backend/raybackend.py +1 -1
  108. flwr/server/superlink/fleet/vce/vce_api.py +18 -5
  109. flwr/server/superlink/linkstate/in_memory_linkstate.py +167 -73
  110. flwr/server/superlink/linkstate/linkstate.py +107 -24
  111. flwr/server/superlink/linkstate/linkstate_factory.py +2 -1
  112. flwr/server/superlink/linkstate/sqlite_linkstate.py +306 -255
  113. flwr/server/superlink/linkstate/utils.py +3 -54
  114. flwr/server/superlink/serverappio/serverappio_servicer.py +2 -2
  115. flwr/server/superlink/simulation/simulationio_servicer.py +1 -1
  116. flwr/server/utils/validator.py +2 -3
  117. flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +4 -2
  118. flwr/serverapp/strategy/__init__.py +26 -0
  119. flwr/serverapp/strategy/bulyan.py +238 -0
  120. flwr/serverapp/strategy/dp_adaptive_clipping.py +335 -0
  121. flwr/serverapp/strategy/dp_fixed_clipping.py +71 -49
  122. flwr/serverapp/strategy/fedadagrad.py +0 -3
  123. flwr/serverapp/strategy/fedadam.py +0 -3
  124. flwr/serverapp/strategy/fedavg.py +89 -64
  125. flwr/serverapp/strategy/fedavgm.py +198 -0
  126. flwr/serverapp/strategy/fedmedian.py +105 -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 +0 -3
  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/strategy_utils.py +48 -0
  136. flwr/simulation/app.py +1 -1
  137. flwr/simulation/ray_transport/ray_actor.py +1 -1
  138. flwr/simulation/ray_transport/ray_client_proxy.py +1 -1
  139. flwr/simulation/run_simulation.py +28 -32
  140. flwr/supercore/cli/flower_superexec.py +26 -1
  141. flwr/supercore/constant.py +41 -0
  142. flwr/supercore/object_store/in_memory_object_store.py +0 -4
  143. flwr/supercore/object_store/object_store_factory.py +26 -6
  144. flwr/supercore/object_store/sqlite_object_store.py +252 -0
  145. flwr/{client/clientapp → supercore/primitives}/__init__.py +1 -1
  146. flwr/supercore/primitives/asymmetric.py +117 -0
  147. flwr/supercore/primitives/asymmetric_ed25519.py +165 -0
  148. flwr/supercore/sqlite_mixin.py +156 -0
  149. flwr/supercore/superexec/plugin/exec_plugin.py +11 -1
  150. flwr/supercore/superexec/run_superexec.py +16 -2
  151. flwr/supercore/utils.py +20 -0
  152. flwr/superlink/artifact_provider/__init__.py +22 -0
  153. flwr/superlink/artifact_provider/artifact_provider.py +37 -0
  154. flwr/{common → superlink}/auth_plugin/__init__.py +6 -6
  155. flwr/superlink/auth_plugin/auth_plugin.py +91 -0
  156. flwr/superlink/auth_plugin/noop_auth_plugin.py +87 -0
  157. flwr/superlink/servicer/control/{control_user_auth_interceptor.py → control_account_auth_interceptor.py} +19 -19
  158. flwr/superlink/servicer/control/control_event_log_interceptor.py +1 -1
  159. flwr/superlink/servicer/control/control_grpc.py +16 -11
  160. flwr/superlink/servicer/control/control_servicer.py +207 -58
  161. flwr/supernode/cli/flower_supernode.py +19 -26
  162. flwr/supernode/runtime/run_clientapp.py +2 -2
  163. flwr/supernode/servicer/clientappio/clientappio_servicer.py +1 -1
  164. flwr/supernode/start_client_internal.py +17 -9
  165. {flwr-1.21.0.dist-info → flwr-1.23.0.dist-info}/METADATA +6 -16
  166. {flwr-1.21.0.dist-info → flwr-1.23.0.dist-info}/RECORD +170 -140
  167. flwr/cli/new/templates/app/code/client.pytorch_msg_api.py.tpl +0 -80
  168. flwr/cli/new/templates/app/code/server.pytorch_msg_api.py.tpl +0 -41
  169. flwr/common/auth_plugin/auth_plugin.py +0 -149
  170. flwr/serverapp/dp_fixed_clipping.py +0 -352
  171. flwr/serverapp/strategy/strategy_utils_tests.py +0 -304
  172. /flwr/cli/new/templates/app/code/{__init__.pytorch_msg_api.py.tpl → __init__.pytorch_legacy_api.py.tpl} +0 -0
  173. /flwr/{client → clientapp}/client_app.py +0 -0
  174. {flwr-1.21.0.dist-info → flwr-1.23.0.dist-info}/WHEEL +0 -0
  175. {flwr-1.21.0.dist-info → flwr-1.23.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,174 @@
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
+ """Federated Optimization (FedProx) [Li et al., 2018] strategy.
16
+
17
+ Paper: arxiv.org/abs/1812.06127
18
+ """
19
+
20
+
21
+ from collections.abc import Iterable
22
+ from logging import INFO, WARN
23
+ from typing import Callable, Optional
24
+
25
+ from flwr.common import (
26
+ ArrayRecord,
27
+ ConfigRecord,
28
+ Message,
29
+ MetricRecord,
30
+ RecordDict,
31
+ log,
32
+ )
33
+ from flwr.server import Grid
34
+
35
+ from .fedavg import FedAvg
36
+
37
+
38
+ class FedProx(FedAvg):
39
+ r"""Federated Optimization strategy.
40
+
41
+ Implementation based on https://arxiv.org/abs/1812.06127
42
+
43
+ FedProx extends FedAvg by introducing a proximal term into the client-side
44
+ optimization objective. The strategy itself behaves identically to FedAvg
45
+ on the server side, but each client **MUST** add a proximal regularization
46
+ term to its local loss function during training:
47
+
48
+ .. math::
49
+ \frac{\mu}{2} || w - w^t ||^2
50
+
51
+ Where $w^t$ denotes the global parameters and $w$ denotes the local weights
52
+ being optimized.
53
+
54
+ This strategy sends the proximal term inside the ``ConfigRecord`` as part of the
55
+ ``configure_train`` method under key ``"proximal-mu"``. The client can then use this
56
+ value to add the proximal term to the loss function.
57
+
58
+ In PyTorch, for example, the loss would go from:
59
+
60
+ .. code:: python
61
+ loss = criterion(net(inputs), labels)
62
+
63
+ To:
64
+
65
+ .. code:: python
66
+ # Get proximal term weight from message
67
+ mu = msg.content["config"]["proximal-mu"]
68
+
69
+ # Compute proximal term
70
+ proximal_term = 0.0
71
+ for local_weights, global_weights in zip(net.parameters(), global_params):
72
+ proximal_term += (local_weights - global_weights).norm(2)
73
+
74
+ # Update loss
75
+ loss = criterion(net(inputs), labels) + (mu / 2) * proximal_term
76
+
77
+ With ``global_params`` being a copy of the model parameters, created **after**
78
+ applying the received global weights but **before** local training begins.
79
+
80
+ .. code:: python
81
+ global_params = copy.deepcopy(net).parameters()
82
+
83
+ Parameters
84
+ ----------
85
+ fraction_train : float (default: 1.0)
86
+ Fraction of nodes used during training. In case `min_train_nodes`
87
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
88
+ will still be sampled.
89
+ fraction_evaluate : float (default: 1.0)
90
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
91
+ is larger than `fraction_evaluate * total_connected_nodes`,
92
+ `min_evaluate_nodes` will still be sampled.
93
+ min_train_nodes : int (default: 2)
94
+ Minimum number of nodes used during training.
95
+ min_evaluate_nodes : int (default: 2)
96
+ Minimum number of nodes used during validation.
97
+ min_available_nodes : int (default: 2)
98
+ Minimum number of total nodes in the system.
99
+ weighted_by_key : str (default: "num-examples")
100
+ The key within each MetricRecord whose value is used as the weight when
101
+ computing weighted averages for both ArrayRecords and MetricRecords.
102
+ arrayrecord_key : str (default: "arrays")
103
+ Key used to store the ArrayRecord when constructing Messages.
104
+ configrecord_key : str (default: "config")
105
+ Key used to store the ConfigRecord when constructing Messages.
106
+ train_metrics_aggr_fn : Optional[callable] (default: None)
107
+ Function with signature (list[RecordDict], str) -> MetricRecord,
108
+ used to aggregate MetricRecords from training round replies.
109
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
110
+ average using the provided weight factor key.
111
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
112
+ Function with signature (list[RecordDict], str) -> MetricRecord,
113
+ used to aggregate MetricRecords from training round replies.
114
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
115
+ average using the provided weight factor key.
116
+ proximal_mu : float (default: 0.0)
117
+ The weight of the proximal term used in the optimization. 0.0 makes
118
+ this strategy equivalent to FedAvg, and the higher the coefficient, the more
119
+ regularization will be used (that is, the client parameters will need to be
120
+ closer to the server parameters during training).
121
+ """
122
+
123
+ def __init__( # pylint: disable=R0913, R0917
124
+ self,
125
+ fraction_train: float = 1.0,
126
+ fraction_evaluate: float = 1.0,
127
+ min_train_nodes: int = 2,
128
+ min_evaluate_nodes: int = 2,
129
+ min_available_nodes: int = 2,
130
+ weighted_by_key: str = "num-examples",
131
+ arrayrecord_key: str = "arrays",
132
+ configrecord_key: str = "config",
133
+ train_metrics_aggr_fn: Optional[
134
+ Callable[[list[RecordDict], str], MetricRecord]
135
+ ] = None,
136
+ evaluate_metrics_aggr_fn: Optional[
137
+ Callable[[list[RecordDict], str], MetricRecord]
138
+ ] = None,
139
+ proximal_mu: float = 0.0,
140
+ ) -> None:
141
+ super().__init__(
142
+ fraction_train=fraction_train,
143
+ fraction_evaluate=fraction_evaluate,
144
+ min_train_nodes=min_train_nodes,
145
+ min_evaluate_nodes=min_evaluate_nodes,
146
+ min_available_nodes=min_available_nodes,
147
+ weighted_by_key=weighted_by_key,
148
+ arrayrecord_key=arrayrecord_key,
149
+ configrecord_key=configrecord_key,
150
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
151
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
152
+ )
153
+ self.proximal_mu = proximal_mu
154
+
155
+ if self.proximal_mu == 0.0:
156
+ log(
157
+ WARN,
158
+ "FedProx initialized with `proximal_mu=0.0`. "
159
+ "This makes the strategy equivalent to FedAvg.",
160
+ )
161
+
162
+ def summary(self) -> None:
163
+ """Log summary configuration of the strategy."""
164
+ log(INFO, "\t├──> FedProx settings:")
165
+ log(INFO, "\t│\t└── Proximal mu: %s", self.proximal_mu)
166
+ super().summary()
167
+
168
+ def configure_train(
169
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
170
+ ) -> Iterable[Message]:
171
+ """Configure the next round of federated training."""
172
+ # Inject proximal term weight into config
173
+ config["proximal-mu"] = self.proximal_mu
174
+ return super().configure_train(server_round, arrays, config, grid)
@@ -0,0 +1,176 @@
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
+ """Federated Averaging with Trimmed Mean [Dong Yin, et al., 2021].
16
+
17
+ Paper: arxiv.org/abs/1803.01498
18
+ """
19
+
20
+
21
+ from collections.abc import Iterable
22
+ from logging import INFO
23
+ from typing import Callable, Optional, cast
24
+
25
+ import numpy as np
26
+
27
+ from flwr.common import Array, ArrayRecord, Message, MetricRecord, NDArray, RecordDict
28
+ from flwr.common.logger import log
29
+
30
+ from ..exception import AggregationError
31
+ from .fedavg import FedAvg
32
+
33
+
34
+ class FedTrimmedAvg(FedAvg):
35
+ """Federated Averaging with Trimmed Mean [Dong Yin, et al., 2021].
36
+
37
+ Implemented based on: https://arxiv.org/abs/1803.01498
38
+
39
+ Parameters
40
+ ----------
41
+ fraction_train : float (default: 1.0)
42
+ Fraction of nodes used during training. In case `min_train_nodes`
43
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
44
+ will still be sampled.
45
+ fraction_evaluate : float (default: 1.0)
46
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
47
+ is larger than `fraction_evaluate * total_connected_nodes`,
48
+ `min_evaluate_nodes` will still be sampled.
49
+ min_train_nodes : int (default: 2)
50
+ Minimum number of nodes used during training.
51
+ min_evaluate_nodes : int (default: 2)
52
+ Minimum number of nodes used during validation.
53
+ min_available_nodes : int (default: 2)
54
+ Minimum number of total nodes in the system.
55
+ weighted_by_key : str (default: "num-examples")
56
+ The key within each MetricRecord whose value is used as the weight when
57
+ computing weighted averages for both ArrayRecords and MetricRecords.
58
+ arrayrecord_key : str (default: "arrays")
59
+ Key used to store the ArrayRecord when constructing Messages.
60
+ configrecord_key : str (default: "config")
61
+ Key used to store the ConfigRecord when constructing Messages.
62
+ train_metrics_aggr_fn : Optional[callable] (default: None)
63
+ Function with signature (list[RecordDict], str) -> MetricRecord,
64
+ used to aggregate MetricRecords from training round replies.
65
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
66
+ average using the provided weight factor key.
67
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
68
+ Function with signature (list[RecordDict], str) -> MetricRecord,
69
+ used to aggregate MetricRecords from training round replies.
70
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
71
+ average using the provided weight factor key.
72
+ beta : float (default: 0.2)
73
+ Fraction to cut off of both tails of the distribution.
74
+ """
75
+
76
+ def __init__( # pylint: disable=R0913, R0917
77
+ self,
78
+ fraction_train: float = 1.0,
79
+ fraction_evaluate: float = 1.0,
80
+ min_train_nodes: int = 2,
81
+ min_evaluate_nodes: int = 2,
82
+ min_available_nodes: int = 2,
83
+ weighted_by_key: str = "num-examples",
84
+ arrayrecord_key: str = "arrays",
85
+ configrecord_key: str = "config",
86
+ train_metrics_aggr_fn: Optional[
87
+ Callable[[list[RecordDict], str], MetricRecord]
88
+ ] = None,
89
+ evaluate_metrics_aggr_fn: Optional[
90
+ Callable[[list[RecordDict], str], MetricRecord]
91
+ ] = None,
92
+ beta: float = 0.2,
93
+ ) -> None:
94
+ super().__init__(
95
+ fraction_train=fraction_train,
96
+ fraction_evaluate=fraction_evaluate,
97
+ min_train_nodes=min_train_nodes,
98
+ min_evaluate_nodes=min_evaluate_nodes,
99
+ min_available_nodes=min_available_nodes,
100
+ weighted_by_key=weighted_by_key,
101
+ arrayrecord_key=arrayrecord_key,
102
+ configrecord_key=configrecord_key,
103
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
104
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
105
+ )
106
+ self.beta = beta
107
+
108
+ def summary(self) -> None:
109
+ """Log summary configuration of the strategy."""
110
+ log(INFO, "\t├──> FedTrimmedAvg settings:")
111
+ log(INFO, "\t│\t└── beta: %s", self.beta)
112
+ super().summary()
113
+
114
+ def aggregate_train(
115
+ self,
116
+ server_round: int,
117
+ replies: Iterable[Message],
118
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
119
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
120
+ # Call FedAvg aggregate_train to perform validation and aggregation
121
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
122
+
123
+ if not valid_replies:
124
+ return None, None
125
+
126
+ # Aggregate ArrayRecords using trimmed mean
127
+ # Get the key for the only ArrayRecord from the first Message
128
+ record_key = list(valid_replies[0].content.array_records.keys())[0]
129
+ # Preserve keys for arrays in ArrayRecord
130
+ array_keys = list(valid_replies[0].content[record_key].keys())
131
+
132
+ # Compute trimmed mean for each layer and construct ArrayRecord
133
+ arrays = ArrayRecord()
134
+ for array_key in array_keys:
135
+ # Get the corresponding layer from each client
136
+ layers = [
137
+ cast(ArrayRecord, msg.content[record_key]).pop(array_key).numpy()
138
+ for msg in valid_replies
139
+ ]
140
+ # Compute trimmed mean and save as Array in ArrayRecord
141
+ try:
142
+ arrays[array_key] = Array(trim_mean(np.stack(layers), self.beta))
143
+ except ValueError as e:
144
+ raise AggregationError(
145
+ f"Trimmed mean could not be computed. "
146
+ f"Likely cause: beta={self.beta} is too large."
147
+ ) from e
148
+
149
+ # Aggregate MetricRecords
150
+ metrics = self.train_metrics_aggr_fn(
151
+ [msg.content for msg in valid_replies],
152
+ self.weighted_by_key,
153
+ )
154
+ return arrays, metrics
155
+
156
+
157
+ def trim_mean(array: NDArray, cut_fraction: float) -> NDArray:
158
+ """Compute trimmed mean along axis=0.
159
+
160
+ It is based on the scipy implementation:
161
+
162
+ https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.trim_mean.html
163
+ """
164
+ axis = 0
165
+ nobs = array.shape[0]
166
+ lowercut = int(cut_fraction * nobs)
167
+ uppercut = nobs - lowercut
168
+ if lowercut > uppercut:
169
+ raise ValueError("Fraction too big.")
170
+
171
+ atmp = np.partition(array, (lowercut, uppercut - 1), axis)
172
+
173
+ slice_list = [slice(None)] * atmp.ndim
174
+ slice_list[axis] = slice(lowercut, uppercut)
175
+ result: NDArray = np.mean(atmp[tuple(slice_list)], axis=axis)
176
+ return result
@@ -0,0 +1,117 @@
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
+ """Flower message-based FedXgbBagging strategy."""
16
+ from collections.abc import Iterable
17
+ from typing import Optional, cast
18
+
19
+ import numpy as np
20
+
21
+ from flwr.common import ArrayRecord, ConfigRecord, Message, MetricRecord
22
+ from flwr.server import Grid
23
+
24
+ from ..exception import InconsistentMessageReplies
25
+ from .fedavg import FedAvg
26
+ from .strategy_utils import aggregate_bagging
27
+
28
+
29
+ # pylint: disable=line-too-long
30
+ class FedXgbBagging(FedAvg):
31
+ """Configurable FedXgbBagging strategy implementation.
32
+
33
+ Parameters
34
+ ----------
35
+ fraction_train : float (default: 1.0)
36
+ Fraction of nodes used during training. In case `min_train_nodes`
37
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
38
+ will still be sampled.
39
+ fraction_evaluate : float (default: 1.0)
40
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
41
+ is larger than `fraction_evaluate * total_connected_nodes`,
42
+ `min_evaluate_nodes` will still be sampled.
43
+ min_train_nodes : int (default: 2)
44
+ Minimum number of nodes used during training.
45
+ min_evaluate_nodes : int (default: 2)
46
+ Minimum number of nodes used during validation.
47
+ min_available_nodes : int (default: 2)
48
+ Minimum number of total nodes in the system.
49
+ weighted_by_key : str (default: "num-examples")
50
+ The key within each MetricRecord whose value is used as the weight when
51
+ computing weighted averages for MetricRecords.
52
+ arrayrecord_key : str (default: "arrays")
53
+ Key used to store the ArrayRecord when constructing Messages.
54
+ configrecord_key : str (default: "config")
55
+ Key used to store the ConfigRecord when constructing Messages.
56
+ train_metrics_aggr_fn : Optional[callable] (default: None)
57
+ Function with signature (list[RecordDict], str) -> MetricRecord,
58
+ used to aggregate MetricRecords from training round replies.
59
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
60
+ average using the provided weight factor key.
61
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
62
+ Function with signature (list[RecordDict], str) -> MetricRecord,
63
+ used to aggregate MetricRecords from training round replies.
64
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
65
+ average using the provided weight factor key.
66
+ """
67
+
68
+ current_bst: Optional[bytes] = None
69
+
70
+ def _ensure_single_array(self, arrays: ArrayRecord) -> None:
71
+ """Check that ensures there's only one Array in the ArrayRecord."""
72
+ n = len(arrays)
73
+ if n != 1:
74
+ raise InconsistentMessageReplies(
75
+ reason="Expected exactly one Array in ArrayRecord. "
76
+ "Skipping aggregation."
77
+ )
78
+
79
+ def configure_train(
80
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
81
+ ) -> Iterable[Message]:
82
+ """Configure the next round of federated training."""
83
+ self._ensure_single_array(arrays)
84
+ # Keep track of array record being communicated
85
+ self.current_bst = arrays["0"].numpy().tobytes()
86
+ return super().configure_train(server_round, arrays, config, grid)
87
+
88
+ def aggregate_train(
89
+ self,
90
+ server_round: int,
91
+ replies: Iterable[Message],
92
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
93
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
94
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
95
+
96
+ arrays, metrics = None, None
97
+ if valid_replies:
98
+ reply_contents = [msg.content for msg in valid_replies]
99
+ array_record_key = next(iter(reply_contents[0].array_records.keys()))
100
+
101
+ # Aggregate ArrayRecords
102
+ for content in reply_contents:
103
+ self._ensure_single_array(cast(ArrayRecord, content[array_record_key]))
104
+ bst = content[array_record_key]["0"].numpy().tobytes() # type: ignore[union-attr]
105
+
106
+ if self.current_bst is not None:
107
+ self.current_bst = aggregate_bagging(self.current_bst, bst)
108
+
109
+ if self.current_bst is not None:
110
+ arrays = ArrayRecord([np.frombuffer(self.current_bst, dtype=np.uint8)])
111
+
112
+ # Aggregate MetricRecords
113
+ metrics = self.train_metrics_aggr_fn(
114
+ reply_contents,
115
+ self.weighted_by_key,
116
+ )
117
+ return arrays, metrics
@@ -0,0 +1,220 @@
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
+ """Flower message-based FedXgbCyclic strategy."""
16
+
17
+
18
+ from collections.abc import Iterable
19
+ from logging import INFO
20
+ from typing import Callable, Optional, cast
21
+
22
+ from flwr.common import (
23
+ ArrayRecord,
24
+ ConfigRecord,
25
+ Message,
26
+ MessageType,
27
+ MetricRecord,
28
+ RecordDict,
29
+ log,
30
+ )
31
+ from flwr.server import Grid
32
+
33
+ from .fedavg import FedAvg
34
+ from .strategy_utils import sample_nodes
35
+
36
+
37
+ # pylint: disable=line-too-long
38
+ class FedXgbCyclic(FedAvg):
39
+ """Configurable FedXgbCyclic strategy implementation.
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_available_nodes : int (default: 2)
52
+ Minimum number of total nodes in the system.
53
+ weighted_by_key : str (default: "num-examples")
54
+ The key within each MetricRecord whose value is used as the weight when
55
+ computing weighted averages for MetricRecords.
56
+ arrayrecord_key : str (default: "arrays")
57
+ Key used to store the ArrayRecord when constructing Messages.
58
+ configrecord_key : str (default: "config")
59
+ Key used to store the ConfigRecord when constructing Messages.
60
+ train_metrics_aggr_fn : Optional[callable] (default: None)
61
+ Function with signature (list[RecordDict], str) -> MetricRecord,
62
+ used to aggregate MetricRecords from training round replies.
63
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
64
+ average using the provided weight factor key.
65
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
66
+ Function with signature (list[RecordDict], str) -> MetricRecord,
67
+ used to aggregate MetricRecords from training round replies.
68
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
69
+ average using the provided weight factor key.
70
+ """
71
+
72
+ # pylint: disable=too-many-arguments,too-many-positional-arguments
73
+ def __init__(
74
+ self,
75
+ fraction_train: float = 1.0,
76
+ fraction_evaluate: float = 1.0,
77
+ min_available_nodes: int = 2,
78
+ weighted_by_key: str = "num-examples",
79
+ arrayrecord_key: str = "arrays",
80
+ configrecord_key: str = "config",
81
+ train_metrics_aggr_fn: Optional[
82
+ Callable[[list[RecordDict], str], MetricRecord]
83
+ ] = None,
84
+ evaluate_metrics_aggr_fn: Optional[
85
+ Callable[[list[RecordDict], str], MetricRecord]
86
+ ] = None,
87
+ ) -> None:
88
+ super().__init__(
89
+ fraction_train=fraction_train,
90
+ fraction_evaluate=fraction_evaluate,
91
+ min_train_nodes=2,
92
+ min_evaluate_nodes=2,
93
+ min_available_nodes=min_available_nodes,
94
+ weighted_by_key=weighted_by_key,
95
+ arrayrecord_key=arrayrecord_key,
96
+ configrecord_key=configrecord_key,
97
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
98
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
99
+ )
100
+
101
+ self.registered_nodes: dict[int, int] = {}
102
+
103
+ if fraction_train not in (0.0, 1.0):
104
+ raise ValueError(
105
+ "fraction_train can only be set to 1.0 or 0.0 for FedXgbCyclic."
106
+ )
107
+ if fraction_evaluate not in (0.0, 1.0):
108
+ raise ValueError(
109
+ "fraction_evaluate can only be set to 1.0 or 0.0 for FedXgbCyclic."
110
+ )
111
+
112
+ def _reorder_nodes(self, node_ids: list[int]) -> list[int]:
113
+ """Re-order node ids based on registered nodes.
114
+
115
+ Each node ID is assigned a persistent index in `self.registered_nodes`
116
+ the first time it appears. The input list is then reordered according
117
+ to these stored indices, and the result is compacted into ascending
118
+ order (1..N) for the current call.
119
+ """
120
+ # Assign new indices to unknown nodes
121
+ next_index = max(self.registered_nodes.values(), default=0) + 1
122
+ for nid in node_ids:
123
+ if nid not in self.registered_nodes:
124
+ self.registered_nodes[nid] = next_index
125
+ next_index += 1
126
+
127
+ # Sort node_ids by their stored indices
128
+ sorted_by_index = sorted(node_ids, key=lambda x: self.registered_nodes[x])
129
+
130
+ # Compact re-map of indices just for this output list
131
+ unique_indices = sorted(self.registered_nodes[nid] for nid in sorted_by_index)
132
+ remap = {old: new for new, old in enumerate(unique_indices, start=1)}
133
+
134
+ # Build the result list ordered by compact indices
135
+ result_list = [
136
+ nid
137
+ for _, nid in sorted(
138
+ (remap[self.registered_nodes[nid]], nid) for nid in sorted_by_index
139
+ )
140
+ ]
141
+ return result_list
142
+
143
+ def _make_sampling(
144
+ self, grid: Grid, server_round: int, configure_type: str
145
+ ) -> list[int]:
146
+ """Sample nodes using the Grid."""
147
+ # Sample nodes
148
+ num_nodes = int(len(list(grid.get_node_ids())) * self.fraction_train)
149
+ sample_size = max(num_nodes, self.min_train_nodes)
150
+ node_ids, _ = sample_nodes(grid, self.min_available_nodes, sample_size)
151
+
152
+ # Re-order node_ids
153
+ node_ids = self._reorder_nodes(node_ids)
154
+
155
+ # Sample the clients sequentially given server_round
156
+ sampled_idx = (server_round - 1) % len(node_ids)
157
+ sampled_node_id = [node_ids[sampled_idx]]
158
+
159
+ log(
160
+ INFO,
161
+ f"{configure_type}: Sampled %s nodes (out of %s)",
162
+ len(sampled_node_id),
163
+ len(node_ids),
164
+ )
165
+ return sampled_node_id
166
+
167
+ def configure_train(
168
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
169
+ ) -> Iterable[Message]:
170
+ """Configure the next round of federated training."""
171
+ # Sample one node
172
+ sampled_node_id = self._make_sampling(grid, server_round, "configure_train")
173
+
174
+ # Always inject current server round
175
+ config["server-round"] = server_round
176
+
177
+ # Construct messages
178
+ record = RecordDict(
179
+ {self.arrayrecord_key: arrays, self.configrecord_key: config}
180
+ )
181
+ return self._construct_messages(record, sampled_node_id, MessageType.TRAIN)
182
+
183
+ def aggregate_train(
184
+ self,
185
+ server_round: int,
186
+ replies: Iterable[Message],
187
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
188
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
189
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
190
+
191
+ arrays, metrics = None, None
192
+ if valid_replies:
193
+ reply_contents = [msg.content for msg in valid_replies]
194
+ array_record_key = next(iter(reply_contents[0].array_records.keys()))
195
+
196
+ # Fetch the client model from current round as global model
197
+ arrays = cast(ArrayRecord, reply_contents[0][array_record_key])
198
+
199
+ # Aggregate MetricRecords
200
+ metrics = self.train_metrics_aggr_fn(
201
+ reply_contents,
202
+ self.weighted_by_key,
203
+ )
204
+ return arrays, metrics
205
+
206
+ def configure_evaluate(
207
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
208
+ ) -> Iterable[Message]:
209
+ """Configure the next round of federated evaluation."""
210
+ # Sample one node
211
+ sampled_node_id = self._make_sampling(grid, server_round, "configure_evaluate")
212
+
213
+ # Always inject current server round
214
+ config["server-round"] = server_round
215
+
216
+ # Construct messages
217
+ record = RecordDict(
218
+ {self.arrayrecord_key: arrays, self.configrecord_key: config}
219
+ )
220
+ return self._construct_messages(record, sampled_node_id, MessageType.EVALUATE)