flwr 1.21.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 (97) hide show
  1. flwr/cli/app.py +2 -0
  2. flwr/cli/new/new.py +9 -7
  3. flwr/cli/new/templates/app/README.flowertune.md.tpl +1 -1
  4. flwr/cli/new/templates/app/code/client.baseline.py.tpl +64 -47
  5. flwr/cli/new/templates/app/code/client.huggingface.py.tpl +68 -30
  6. flwr/cli/new/templates/app/code/client.jax.py.tpl +63 -42
  7. flwr/cli/new/templates/app/code/client.mlx.py.tpl +80 -51
  8. flwr/cli/new/templates/app/code/client.numpy.py.tpl +36 -13
  9. flwr/cli/new/templates/app/code/client.pytorch.py.tpl +71 -46
  10. flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +55 -0
  11. flwr/cli/new/templates/app/code/client.sklearn.py.tpl +75 -30
  12. flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +69 -44
  13. flwr/cli/new/templates/app/code/client.xgboost.py.tpl +110 -0
  14. flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +56 -90
  15. flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +1 -23
  16. flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +37 -58
  17. flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +39 -44
  18. flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -14
  19. flwr/cli/new/templates/app/code/server.baseline.py.tpl +27 -29
  20. flwr/cli/new/templates/app/code/server.huggingface.py.tpl +23 -19
  21. flwr/cli/new/templates/app/code/server.jax.py.tpl +27 -14
  22. flwr/cli/new/templates/app/code/server.mlx.py.tpl +29 -19
  23. flwr/cli/new/templates/app/code/server.numpy.py.tpl +30 -17
  24. flwr/cli/new/templates/app/code/server.pytorch.py.tpl +36 -26
  25. flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +31 -0
  26. flwr/cli/new/templates/app/code/server.sklearn.py.tpl +29 -21
  27. flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +28 -19
  28. flwr/cli/new/templates/app/code/server.xgboost.py.tpl +56 -0
  29. flwr/cli/new/templates/app/code/task.huggingface.py.tpl +16 -20
  30. flwr/cli/new/templates/app/code/task.jax.py.tpl +1 -1
  31. flwr/cli/new/templates/app/code/task.numpy.py.tpl +1 -1
  32. flwr/cli/new/templates/app/code/task.pytorch.py.tpl +14 -27
  33. flwr/cli/new/templates/app/code/{task.pytorch_msg_api.py.tpl → task.pytorch_legacy_api.py.tpl} +27 -14
  34. flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +1 -2
  35. flwr/cli/new/templates/app/code/task.xgboost.py.tpl +67 -0
  36. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +4 -4
  37. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +2 -2
  38. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +4 -4
  39. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
  40. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +2 -2
  41. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
  42. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +3 -3
  43. flwr/cli/new/templates/app/{pyproject.pytorch_msg_api.toml.tpl → pyproject.pytorch_legacy_api.toml.tpl} +3 -3
  44. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
  45. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
  46. flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +61 -0
  47. flwr/cli/pull.py +100 -0
  48. flwr/cli/utils.py +17 -0
  49. flwr/clientapp/mod/__init__.py +4 -1
  50. flwr/clientapp/mod/centraldp_mods.py +156 -40
  51. flwr/clientapp/mod/localdp_mod.py +169 -0
  52. flwr/clientapp/typing.py +22 -0
  53. flwr/common/constant.py +3 -0
  54. flwr/common/exit/exit_code.py +4 -0
  55. flwr/common/record/typeddict.py +12 -0
  56. flwr/proto/control_pb2.py +7 -3
  57. flwr/proto/control_pb2.pyi +24 -0
  58. flwr/proto/control_pb2_grpc.py +34 -0
  59. flwr/proto/control_pb2_grpc.pyi +13 -0
  60. flwr/server/app.py +13 -0
  61. flwr/serverapp/strategy/__init__.py +26 -0
  62. flwr/serverapp/strategy/bulyan.py +238 -0
  63. flwr/serverapp/strategy/dp_adaptive_clipping.py +335 -0
  64. flwr/serverapp/strategy/dp_fixed_clipping.py +71 -49
  65. flwr/serverapp/strategy/fedadagrad.py +0 -3
  66. flwr/serverapp/strategy/fedadam.py +0 -3
  67. flwr/serverapp/strategy/fedavg.py +89 -64
  68. flwr/serverapp/strategy/fedavgm.py +198 -0
  69. flwr/serverapp/strategy/fedmedian.py +105 -0
  70. flwr/serverapp/strategy/fedprox.py +174 -0
  71. flwr/serverapp/strategy/fedtrimmedavg.py +176 -0
  72. flwr/serverapp/strategy/fedxgb_bagging.py +117 -0
  73. flwr/serverapp/strategy/fedxgb_cyclic.py +220 -0
  74. flwr/serverapp/strategy/fedyogi.py +0 -3
  75. flwr/serverapp/strategy/krum.py +112 -0
  76. flwr/serverapp/strategy/multikrum.py +247 -0
  77. flwr/serverapp/strategy/qfedavg.py +252 -0
  78. flwr/serverapp/strategy/strategy_utils.py +48 -0
  79. flwr/simulation/app.py +1 -1
  80. flwr/simulation/run_simulation.py +25 -30
  81. flwr/supercore/cli/flower_superexec.py +26 -1
  82. flwr/supercore/constant.py +19 -0
  83. flwr/supercore/superexec/plugin/exec_plugin.py +11 -1
  84. flwr/supercore/superexec/run_superexec.py +16 -2
  85. flwr/superlink/artifact_provider/__init__.py +22 -0
  86. flwr/superlink/artifact_provider/artifact_provider.py +37 -0
  87. flwr/superlink/servicer/control/control_grpc.py +3 -0
  88. flwr/superlink/servicer/control/control_servicer.py +59 -2
  89. {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/METADATA +6 -16
  90. {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/RECORD +93 -74
  91. flwr/cli/new/templates/app/code/client.pytorch_msg_api.py.tpl +0 -80
  92. flwr/cli/new/templates/app/code/server.pytorch_msg_api.py.tpl +0 -41
  93. flwr/serverapp/dp_fixed_clipping.py +0 -352
  94. flwr/serverapp/strategy/strategy_utils_tests.py +0 -304
  95. /flwr/cli/new/templates/app/code/{__init__.pytorch_msg_api.py.tpl → __init__.pytorch_legacy_api.py.tpl} +0 -0
  96. {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/WHEEL +0 -0
  97. {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/entry_points.txt +0 -0
@@ -15,6 +15,11 @@
15
15
  """ServerApp strategies."""
16
16
 
17
17
 
18
+ from .bulyan import Bulyan
19
+ from .dp_adaptive_clipping import (
20
+ DifferentialPrivacyClientSideAdaptiveClipping,
21
+ DifferentialPrivacyServerSideAdaptiveClipping,
22
+ )
18
23
  from .dp_fixed_clipping import (
19
24
  DifferentialPrivacyClientSideFixedClipping,
20
25
  DifferentialPrivacyServerSideFixedClipping,
@@ -22,17 +27,38 @@ from .dp_fixed_clipping import (
22
27
  from .fedadagrad import FedAdagrad
23
28
  from .fedadam import FedAdam
24
29
  from .fedavg import FedAvg
30
+ from .fedavgm import FedAvgM
31
+ from .fedmedian import FedMedian
32
+ from .fedprox import FedProx
33
+ from .fedtrimmedavg import FedTrimmedAvg
34
+ from .fedxgb_bagging import FedXgbBagging
35
+ from .fedxgb_cyclic import FedXgbCyclic
25
36
  from .fedyogi import FedYogi
37
+ from .krum import Krum
38
+ from .multikrum import MultiKrum
39
+ from .qfedavg import QFedAvg
26
40
  from .result import Result
27
41
  from .strategy import Strategy
28
42
 
29
43
  __all__ = [
44
+ "Bulyan",
45
+ "DifferentialPrivacyClientSideAdaptiveClipping",
30
46
  "DifferentialPrivacyClientSideFixedClipping",
47
+ "DifferentialPrivacyServerSideAdaptiveClipping",
31
48
  "DifferentialPrivacyServerSideFixedClipping",
32
49
  "FedAdagrad",
33
50
  "FedAdam",
34
51
  "FedAvg",
52
+ "FedAvgM",
53
+ "FedMedian",
54
+ "FedProx",
55
+ "FedTrimmedAvg",
56
+ "FedXgbBagging",
57
+ "FedXgbCyclic",
35
58
  "FedYogi",
59
+ "Krum",
60
+ "MultiKrum",
61
+ "QFedAvg",
36
62
  "Result",
37
63
  "Strategy",
38
64
  ]
@@ -0,0 +1,238 @@
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
+ """Bulyan [El Mhamdi et al., 2018] strategy.
16
+
17
+ Paper: arxiv.org/abs/1802.07927
18
+ """
19
+
20
+
21
+ from collections import OrderedDict
22
+ from collections.abc import Iterable
23
+ from logging import INFO, WARN
24
+ from typing import Callable, Optional, cast
25
+
26
+ import numpy as np
27
+
28
+ from flwr.common import (
29
+ Array,
30
+ ArrayRecord,
31
+ Message,
32
+ MetricRecord,
33
+ NDArrays,
34
+ RecordDict,
35
+ log,
36
+ )
37
+
38
+ from .fedavg import FedAvg
39
+ from .multikrum import select_multikrum
40
+
41
+
42
+ # pylint: disable=too-many-instance-attributes
43
+ class Bulyan(FedAvg):
44
+ """Bulyan strategy.
45
+
46
+ Implementation based on https://arxiv.org/abs/1802.07927.
47
+
48
+ Parameters
49
+ ----------
50
+ fraction_train : float (default: 1.0)
51
+ Fraction of nodes used during training. In case `min_train_nodes`
52
+ is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
53
+ will still be sampled.
54
+ fraction_evaluate : float (default: 1.0)
55
+ Fraction of nodes used during validation. In case `min_evaluate_nodes`
56
+ is larger than `fraction_evaluate * total_connected_nodes`,
57
+ `min_evaluate_nodes` will still be sampled.
58
+ min_train_nodes : int (default: 2)
59
+ Minimum number of nodes used during training.
60
+ min_evaluate_nodes : int (default: 2)
61
+ Minimum number of nodes used during validation.
62
+ min_available_nodes : int (default: 2)
63
+ Minimum number of total nodes in the system.
64
+ num_malicious_nodes : int (default: 0)
65
+ Number of malicious nodes in the system.
66
+ weighted_by_key : str (default: "num-examples")
67
+ The key within each MetricRecord whose value is used as the weight when
68
+ computing weighted averages for MetricRecords.
69
+ arrayrecord_key : str (default: "arrays")
70
+ Key used to store the ArrayRecord when constructing Messages.
71
+ configrecord_key : str (default: "config")
72
+ Key used to store the ConfigRecord when constructing Messages.
73
+ train_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
+ evaluate_metrics_aggr_fn : Optional[callable] (default: None)
79
+ Function with signature (list[RecordDict], str) -> MetricRecord,
80
+ used to aggregate MetricRecords from training round replies.
81
+ If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
82
+ average using the provided weight factor key.
83
+ selection_rule : Optional[Callable] (default: None)
84
+ Function with signature (list[RecordDict], int, int) -> list[RecordDict].
85
+ The inputs are:
86
+ - a list of contents from reply messages,
87
+ - the assumed number of malicious nodes (`num_malicious_nodes`),
88
+ - the number of nodes to select (`num_nodes_to_select`).
89
+
90
+ The function should implement a Byzantine-resilient selection rule that
91
+ serves as the first step of Bulyan. If None, defaults to `select_multikrum`,
92
+ which selects nodes according to the Multi-Krum algorithm.
93
+ """
94
+
95
+ # pylint: disable=too-many-arguments,too-many-positional-arguments
96
+ def __init__(
97
+ self,
98
+ fraction_train: float = 1.0,
99
+ fraction_evaluate: float = 1.0,
100
+ min_train_nodes: int = 2,
101
+ min_evaluate_nodes: int = 2,
102
+ min_available_nodes: int = 2,
103
+ num_malicious_nodes: int = 0,
104
+ weighted_by_key: str = "num-examples",
105
+ arrayrecord_key: str = "arrays",
106
+ configrecord_key: str = "config",
107
+ train_metrics_aggr_fn: Optional[
108
+ Callable[[list[RecordDict], str], MetricRecord]
109
+ ] = None,
110
+ evaluate_metrics_aggr_fn: Optional[
111
+ Callable[[list[RecordDict], str], MetricRecord]
112
+ ] = None,
113
+ selection_rule: Optional[
114
+ Callable[[list[RecordDict], int, int], list[RecordDict]]
115
+ ] = None,
116
+ ) -> None:
117
+ super().__init__(
118
+ fraction_train=fraction_train,
119
+ fraction_evaluate=fraction_evaluate,
120
+ min_train_nodes=min_train_nodes,
121
+ min_evaluate_nodes=min_evaluate_nodes,
122
+ min_available_nodes=min_available_nodes,
123
+ weighted_by_key=weighted_by_key,
124
+ arrayrecord_key=arrayrecord_key,
125
+ configrecord_key=configrecord_key,
126
+ train_metrics_aggr_fn=train_metrics_aggr_fn,
127
+ evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
128
+ )
129
+ self.num_malicious_nodes = num_malicious_nodes
130
+ self.selection_rule = selection_rule or select_multikrum
131
+
132
+ def summary(self) -> None:
133
+ """Log summary configuration of the strategy."""
134
+ log(INFO, "\t├──> Bulyan settings:")
135
+ log(INFO, "\t│\t├── Number of malicious nodes: %d", self.num_malicious_nodes)
136
+ log(INFO, "\t│\t└── Selection rule: %s", self.selection_rule.__name__)
137
+ super().summary()
138
+
139
+ def aggregate_train(
140
+ self,
141
+ server_round: int,
142
+ replies: Iterable[Message],
143
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
144
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
145
+ valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
146
+
147
+ # Check if sufficient replies have been received
148
+ if len(valid_replies) < 4 * self.num_malicious_nodes + 3:
149
+ log(
150
+ WARN,
151
+ "Insufficient replies, skipping Bulyan aggregation: "
152
+ "Required at least %d (4*num_malicious_nodes + 3), but received %d.",
153
+ 4 * self.num_malicious_nodes + 3,
154
+ len(valid_replies),
155
+ )
156
+ return None, None
157
+
158
+ reply_contents = [msg.content for msg in valid_replies]
159
+
160
+ # Compute theta and beta
161
+ theta = len(valid_replies) - 2 * self.num_malicious_nodes
162
+ beta = theta - 2 * self.num_malicious_nodes
163
+
164
+ # Byzantine-resilient selection rule
165
+ selected_contents = self.selection_rule(
166
+ reply_contents, self.num_malicious_nodes, theta
167
+ )
168
+
169
+ # Convert each ArrayRecord to a list of NDArray for easier computation
170
+ key = list(selected_contents[0].array_records.keys())[0]
171
+ array_keys = list(selected_contents[0][key].keys())
172
+ selected_ndarrays = [
173
+ cast(ArrayRecord, ctnt[key]).to_numpy_ndarrays(keep_input=False)
174
+ for ctnt in selected_contents
175
+ ]
176
+
177
+ # Compute median
178
+ median_ndarrays = [np.median(arr, axis=0) for arr in zip(*selected_ndarrays)]
179
+
180
+ # Aggregate the beta closest weights element-wise
181
+ aggregated_ndarrays = aggregate_n_closest_weights(
182
+ median_ndarrays, selected_ndarrays, beta
183
+ )
184
+
185
+ # Convert to ArrayRecord
186
+ arrays = ArrayRecord(
187
+ OrderedDict(zip(array_keys, map(Array, aggregated_ndarrays)))
188
+ )
189
+
190
+ # Aggregate MetricRecords
191
+ metrics = self.train_metrics_aggr_fn(
192
+ selected_contents,
193
+ self.weighted_by_key,
194
+ )
195
+ return arrays, metrics
196
+
197
+
198
+ def aggregate_n_closest_weights(
199
+ ref_weights: NDArrays, weights_list: list[NDArrays], beta: int
200
+ ) -> NDArrays:
201
+ """Compute the element-wise mean of the `beta` closest weight arrays.
202
+
203
+ For each element (i-th coordinate), the output is the average of the
204
+ `beta` weight arrays that are closest to the reference weights.
205
+
206
+ Parameters
207
+ ----------
208
+ ref_weights : NDArrays
209
+ Reference weights used to compute distances.
210
+ weights_list : list[NDArrays]
211
+ List of weight arrays (e.g., from selected nodes).
212
+ beta : int
213
+ Number of closest weight arrays to include in the averaging.
214
+
215
+ Returns
216
+ -------
217
+ aggregated_weights : NDArrays
218
+ Element-wise average of the `beta` closest weight arrays to the
219
+ reference weights.
220
+ """
221
+ aggregated_weights = []
222
+ for layer_id, ref_layer in enumerate(ref_weights):
223
+ # Shape: (n_models, *layer_shape)
224
+ layer_stack = np.stack([weights[layer_id] for weights in weights_list])
225
+
226
+ # Compute absolute differences: shape (n_models, *layer_shape)
227
+ diffs = np.abs(layer_stack - ref_layer)
228
+
229
+ # Find indices of `beta` smallest per coordinate
230
+ idx = np.argpartition(diffs, beta - 1, axis=0)[:beta]
231
+
232
+ # Gather the closest weights
233
+ closest = np.take_along_axis(layer_stack, idx, axis=0)
234
+
235
+ # Average them
236
+ aggregated_weights.append(np.mean(closest, axis=0))
237
+
238
+ return aggregated_weights
@@ -0,0 +1,335 @@
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 adaptive clipping.
16
+
17
+ Paper (Andrew et al.): https://arxiv.org/abs/1905.03871
18
+ """
19
+
20
+ import math
21
+ from abc import ABC
22
+ from collections import OrderedDict
23
+ from collections.abc import Iterable
24
+ from logging import INFO
25
+ from typing import Optional
26
+
27
+ import numpy as np
28
+
29
+ from flwr.common import Array, ArrayRecord, ConfigRecord, Message, MetricRecord, log
30
+ from flwr.common.differential_privacy import (
31
+ adaptive_clip_inputs_inplace,
32
+ add_gaussian_noise_inplace,
33
+ compute_adaptive_noise_params,
34
+ compute_stdv,
35
+ )
36
+ from flwr.common.differential_privacy_constants import KEY_CLIPPING_NORM, KEY_NORM_BIT
37
+ from flwr.server import Grid
38
+ from flwr.serverapp.exception import AggregationError
39
+
40
+ from .dp_fixed_clipping import validate_replies
41
+ from .strategy import Strategy
42
+
43
+
44
+ class DifferentialPrivacyAdaptiveBase(Strategy, ABC):
45
+ """Base class for DP strategies with adaptive clipping."""
46
+
47
+ # pylint: disable=too-many-arguments,too-many-instance-attributes,too-many-positional-arguments
48
+ def __init__(
49
+ self,
50
+ strategy: Strategy,
51
+ noise_multiplier: float,
52
+ num_sampled_clients: int,
53
+ initial_clipping_norm: float = 0.1,
54
+ target_clipped_quantile: float = 0.5,
55
+ clip_norm_lr: float = 0.2,
56
+ clipped_count_stddev: Optional[float] = None,
57
+ ) -> None:
58
+ super().__init__()
59
+
60
+ if strategy is None:
61
+ raise ValueError("The passed strategy is None.")
62
+ if noise_multiplier < 0:
63
+ raise ValueError("The noise multiplier should be a non-negative value.")
64
+ if num_sampled_clients <= 0:
65
+ raise ValueError(
66
+ "The number of sampled clients should be a positive value."
67
+ )
68
+ if initial_clipping_norm <= 0:
69
+ raise ValueError("The initial clipping norm should be a positive value.")
70
+ if not 0 <= target_clipped_quantile <= 1:
71
+ raise ValueError("The target clipped quantile must be in [0, 1].")
72
+ if clip_norm_lr <= 0:
73
+ raise ValueError("The learning rate must be positive.")
74
+ if clipped_count_stddev is not None and clipped_count_stddev < 0:
75
+ raise ValueError("The `clipped_count_stddev` must be non-negative.")
76
+
77
+ self.strategy = strategy
78
+ self.num_sampled_clients = num_sampled_clients
79
+ self.clipping_norm = initial_clipping_norm
80
+ self.target_clipped_quantile = target_clipped_quantile
81
+ self.clip_norm_lr = clip_norm_lr
82
+ (
83
+ self.clipped_count_stddev,
84
+ self.noise_multiplier,
85
+ ) = compute_adaptive_noise_params(
86
+ noise_multiplier,
87
+ num_sampled_clients,
88
+ clipped_count_stddev,
89
+ )
90
+
91
+ def _add_noise_to_aggregated_arrays(self, aggregated: ArrayRecord) -> ArrayRecord:
92
+ nds = aggregated.to_numpy_ndarrays()
93
+ stdv = compute_stdv(
94
+ self.noise_multiplier, self.clipping_norm, self.num_sampled_clients
95
+ )
96
+ add_gaussian_noise_inplace(nds, stdv)
97
+ log(INFO, "aggregate_fit: central DP noise with %.4f stdev added", stdv)
98
+ return ArrayRecord(
99
+ OrderedDict({k: Array(v) for k, v in zip(aggregated.keys(), nds)})
100
+ )
101
+
102
+ def _noisy_fraction(self, count: int, total: int) -> float:
103
+ return float(np.random.normal(count, self.clipped_count_stddev)) / float(total)
104
+
105
+ def _geometric_update(self, clipped_fraction: float) -> None:
106
+ self.clipping_norm *= math.exp(
107
+ -self.clip_norm_lr * (clipped_fraction - self.target_clipped_quantile)
108
+ )
109
+
110
+ def configure_evaluate(
111
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
112
+ ) -> Iterable[Message]:
113
+ """Configure the next round of federated evaluation."""
114
+ return self.strategy.configure_evaluate(server_round, arrays, config, grid)
115
+
116
+ def aggregate_evaluate(
117
+ self, server_round: int, replies: Iterable[Message]
118
+ ) -> Optional[MetricRecord]:
119
+ """Aggregate MetricRecords in the received Messages."""
120
+ return self.strategy.aggregate_evaluate(server_round, replies)
121
+
122
+ def summary(self) -> None:
123
+ """Log summary configuration of the strategy."""
124
+ self.strategy.summary()
125
+
126
+
127
+ class DifferentialPrivacyServerSideAdaptiveClipping(DifferentialPrivacyAdaptiveBase):
128
+ """Message-based central DP with server-side adaptive clipping."""
129
+
130
+ # pylint: disable=too-many-arguments,too-many-locals,too-many-positional-arguments
131
+ def __init__(
132
+ self,
133
+ strategy: Strategy,
134
+ noise_multiplier: float,
135
+ num_sampled_clients: int,
136
+ initial_clipping_norm: float = 0.1,
137
+ target_clipped_quantile: float = 0.5,
138
+ clip_norm_lr: float = 0.2,
139
+ clipped_count_stddev: Optional[float] = None,
140
+ ) -> None:
141
+ super().__init__(
142
+ strategy,
143
+ noise_multiplier,
144
+ num_sampled_clients,
145
+ initial_clipping_norm,
146
+ target_clipped_quantile,
147
+ clip_norm_lr,
148
+ clipped_count_stddev,
149
+ )
150
+ self.current_arrays: ArrayRecord = ArrayRecord()
151
+
152
+ def __repr__(self) -> str:
153
+ """Compute a string representation of the strategy."""
154
+ return "Differential Privacy Strategy Wrapper (Server-Side Adaptive Clipping)"
155
+
156
+ def summary(self) -> None:
157
+ """Log summary configuration of the strategy."""
158
+ log(INFO, "\t├──> DP settings:")
159
+ log(INFO, "\t│\t├── Noise multiplier: %s", self.noise_multiplier)
160
+ log(INFO, "\t│\t├── Clipping norm: %s", self.clipping_norm)
161
+ log(INFO, "\t│\t├── Target clipped quantile: %s", self.target_clipped_quantile)
162
+ log(INFO, "\t│\t└── Clip norm learning rate: %s", self.clip_norm_lr)
163
+ super().summary()
164
+
165
+ def configure_train(
166
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
167
+ ) -> Iterable[Message]:
168
+ """Configure the next round of training."""
169
+ self.current_arrays = arrays
170
+ return self.strategy.configure_train(server_round, arrays, config, grid)
171
+
172
+ def aggregate_train(
173
+ self, server_round: int, replies: Iterable[Message]
174
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
175
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
176
+ if not validate_replies(replies, self.num_sampled_clients):
177
+ return None, None
178
+
179
+ current_nd = self.current_arrays.to_numpy_ndarrays()
180
+ clipped_indicator_count = 0
181
+ replies_list = list(replies)
182
+
183
+ for reply in replies_list:
184
+ for arr_name, record in reply.content.array_records.items():
185
+ reply_nd = record.to_numpy_ndarrays()
186
+ model_update = [
187
+ np.subtract(x, y) for (x, y) in zip(reply_nd, current_nd)
188
+ ]
189
+ norm_bit = adaptive_clip_inputs_inplace(
190
+ model_update, self.clipping_norm
191
+ )
192
+ clipped_indicator_count += int(norm_bit)
193
+ # reconstruct array using clipped contribution from current round
194
+ restored = [c + u for c, u in zip(current_nd, model_update)]
195
+ reply.content[arr_name] = ArrayRecord(
196
+ OrderedDict({k: Array(v) for k, v in zip(record.keys(), restored)})
197
+ )
198
+ log(
199
+ INFO,
200
+ "aggregate_train: arrays in `ArrayRecord` are clipped by value: %.4f.",
201
+ self.clipping_norm,
202
+ )
203
+
204
+ clipped_fraction = self._noisy_fraction(
205
+ clipped_indicator_count, len(replies_list)
206
+ )
207
+ self._geometric_update(clipped_fraction)
208
+
209
+ aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
210
+ server_round, replies_list
211
+ )
212
+
213
+ if aggregated_arrays:
214
+ aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
215
+
216
+ return aggregated_arrays, aggregated_metrics
217
+
218
+
219
+ class DifferentialPrivacyClientSideAdaptiveClipping(DifferentialPrivacyAdaptiveBase):
220
+ """Strategy wrapper for central DP with client-side adaptive clipping.
221
+
222
+ Use `adaptiveclipping_mod` modifier at the client side.
223
+
224
+ In comparison to `DifferentialPrivacyServerSideAdaptiveClipping`,
225
+ which performs clipping on the server-side,
226
+ `DifferentialPrivacyClientSideAdaptiveClipping`
227
+ expects clipping to happen on the client-side, usually by using the built-in
228
+ `adaptiveclipping_mod`.
229
+
230
+ Parameters
231
+ ----------
232
+ strategy : Strategy
233
+ The strategy to which DP functionalities will be added by this wrapper.
234
+ noise_multiplier : float
235
+ The noise multiplier for the Gaussian mechanism for model updates.
236
+ num_sampled_clients : int
237
+ The number of clients that are sampled on each round.
238
+ initial_clipping_norm : float
239
+ The initial value of clipping norm. Defaults to 0.1.
240
+ Andrew et al. recommends to set to 0.1.
241
+ target_clipped_quantile : float
242
+ The desired quantile of updates which should be clipped. Defaults to 0.5.
243
+ clip_norm_lr : float
244
+ The learning rate for the clipping norm adaptation. Defaults to 0.2.
245
+ Andrew et al. recommends to set to 0.2.
246
+ clipped_count_stddev : float
247
+ The stddev of the noise added to the count of
248
+ updates currently below the estimate.
249
+ Andrew et al. recommends to set to `expected_num_records/20`
250
+
251
+ Examples
252
+ --------
253
+ Create a strategy::
254
+
255
+ strategy = fl.serverapp.FedAvg(...)
256
+
257
+ Wrap the strategy with the `DifferentialPrivacyClientSideAdaptiveClipping` wrapper::
258
+
259
+ dp_strategy = DifferentialPrivacyClientSideAdaptiveClipping(
260
+ strategy, cfg.noise_multiplier, cfg.num_sampled_clients, ...
261
+ )
262
+
263
+ On the client, add the `adaptiveclipping_mod` to the client-side mods::
264
+
265
+ app = fl.client.ClientApp(mods=[adaptiveclipping_mod])
266
+ """
267
+
268
+ def __repr__(self) -> str:
269
+ """Compute a string representation of the strategy."""
270
+ return "Differential Privacy Strategy Wrapper (Client-Side Adaptive Clipping)"
271
+
272
+ def summary(self) -> None:
273
+ """Log summary configuration of the strategy."""
274
+ log(INFO, "\t├──> DP settings:")
275
+ log(INFO, "\t│\t├── Noise multiplier: %s", self.noise_multiplier)
276
+ log(INFO, "\t│\t├── Clipping norm: %s", self.clipping_norm)
277
+ log(INFO, "\t│\t├── Target clipped quantile: %s", self.target_clipped_quantile)
278
+ log(INFO, "\t│\t└── Clip norm learning rate: %s", self.clip_norm_lr)
279
+ super().summary()
280
+
281
+ def configure_train(
282
+ self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
283
+ ) -> Iterable[Message]:
284
+ """Configure the next round of training."""
285
+ config[KEY_CLIPPING_NORM] = self.clipping_norm
286
+ return self.strategy.configure_train(server_round, arrays, config, grid)
287
+
288
+ def aggregate_train(
289
+ self, server_round: int, replies: Iterable[Message]
290
+ ) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
291
+ """Aggregate ArrayRecords and MetricRecords in the received Messages."""
292
+ if not validate_replies(replies, self.num_sampled_clients):
293
+ return None, None
294
+
295
+ replies_list = list(replies)
296
+
297
+ # validate that KEY_NORM_BIT is present in all replies
298
+ for msg in replies_list:
299
+ for _, mrec in msg.content.metric_records.items():
300
+ if KEY_NORM_BIT not in mrec:
301
+ raise AggregationError(
302
+ f"KEY_NORM_BIT ('{KEY_NORM_BIT}') not found"
303
+ f" in MetricRecord or metrics for reply."
304
+ )
305
+
306
+ aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
307
+ server_round, replies_list
308
+ )
309
+
310
+ self._update_clip_norm_from_replies(replies_list)
311
+
312
+ if aggregated_arrays:
313
+ aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
314
+
315
+ return aggregated_arrays, aggregated_metrics
316
+
317
+ def _update_clip_norm_from_replies(self, replies: list[Message]) -> None:
318
+ total = len(replies)
319
+ clipped_count = 0
320
+
321
+ for msg in replies:
322
+ # KEY_NORM_BIT is guaranteed to be present
323
+ for _, mrec in msg.content.metric_records.items():
324
+ if KEY_NORM_BIT in mrec:
325
+ clipped_count += int(bool(mrec[KEY_NORM_BIT]))
326
+ break
327
+ else:
328
+ # Check fallback location
329
+ if hasattr(msg.content, "metrics") and isinstance(
330
+ msg.content.metrics, dict
331
+ ):
332
+ clipped_count += int(bool(msg.content.metrics[KEY_NORM_BIT]))
333
+
334
+ clipped_fraction = self._noisy_fraction(clipped_count, total)
335
+ self._geometric_update(clipped_fraction)