flwr-nightly 1.22.0.dev20250918__py3-none-any.whl → 1.22.0.dev20250919__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,9 +17,10 @@
17
17
 
18
18
  from flwr.client.mod.comms_mods import arrays_size_mod, message_size_mod
19
19
 
20
- from .centraldp_mods import fixedclipping_mod
20
+ from .centraldp_mods import adaptiveclipping_mod, fixedclipping_mod
21
21
 
22
22
  __all__ = [
23
+ "adaptiveclipping_mod",
23
24
  "arrays_size_mod",
24
25
  "fixedclipping_mod",
25
26
  "message_size_mod",
@@ -16,13 +16,26 @@
16
16
 
17
17
 
18
18
  from collections import OrderedDict
19
- from logging import INFO, WARN
19
+ from logging import ERROR, INFO
20
20
  from typing import cast
21
21
 
22
- from flwr.client.typing import ClientAppCallable
23
- from flwr.common import Array, ArrayRecord, Context, Message, MessageType, log
24
- from flwr.common.differential_privacy import compute_clip_model_update
25
- from flwr.common.differential_privacy_constants import KEY_CLIPPING_NORM
22
+ from flwr.app import Error
23
+ from flwr.clientapp.typing import ClientAppCallable
24
+ from flwr.common import (
25
+ Array,
26
+ ArrayRecord,
27
+ ConfigRecord,
28
+ Context,
29
+ Message,
30
+ MetricRecord,
31
+ log,
32
+ )
33
+ from flwr.common.constant import ErrorCode
34
+ from flwr.common.differential_privacy import (
35
+ compute_adaptive_clip_model_update,
36
+ compute_clip_model_update,
37
+ )
38
+ from flwr.common.differential_privacy_constants import KEY_CLIPPING_NORM, KEY_NORM_BIT
26
39
 
27
40
 
28
41
  # pylint: disable=too-many-return-statements
@@ -46,33 +59,15 @@ def fixedclipping_mod(
46
59
 
47
60
  Typically, fixedclipping_mod should be the last to operate on params.
48
61
  """
49
- if msg.metadata.message_type != MessageType.TRAIN:
50
- return call_next(msg, ctxt)
51
-
52
62
  if len(msg.content.array_records) != 1:
53
- log(
54
- WARN,
55
- "fixedclipping_mod is designed to work with a single ArrayRecord. "
56
- "Skipping.",
57
- )
58
- return call_next(msg, ctxt)
59
-
63
+ return _handle_multi_record_err("fixedclipping_mod", msg, ArrayRecord)
60
64
  if len(msg.content.config_records) != 1:
61
- log(
62
- WARN,
63
- "fixedclipping_mod is designed to work with a single ConfigRecord. "
64
- "Skipping.",
65
- )
66
- return call_next(msg, ctxt)
65
+ return _handle_multi_record_err("fixedclipping_mod", msg, ConfigRecord)
67
66
 
68
67
  # Get keys in the single ConfigRecord
69
68
  keys_in_config = set(next(iter(msg.content.config_records.values())).keys())
70
69
  if KEY_CLIPPING_NORM not in keys_in_config:
71
- raise KeyError(
72
- f"The {KEY_CLIPPING_NORM} value is not supplied by the "
73
- f"`DifferentialPrivacyClientSideFixedClipping` wrapper at"
74
- f" the server side."
75
- )
70
+ return _handle_no_key_err("fixedclipping_mod", msg)
76
71
  # Record array record communicated to client and clipping norm
77
72
  original_array_record = next(iter(msg.content.array_records.values()))
78
73
  clipping_norm = cast(
@@ -86,26 +81,16 @@ def fixedclipping_mod(
86
81
  if out_msg.has_error():
87
82
  return out_msg
88
83
 
89
- # Ensure there is a single ArrayRecord
84
+ # Ensure reply has a single ArrayRecord
90
85
  if len(out_msg.content.array_records) != 1:
91
- log(
92
- WARN,
93
- "fixedclipping_mod is designed to work with a single ArrayRecord. "
94
- "Skipping.",
95
- )
96
- return out_msg
86
+ return _handle_multi_record_err("fixedclipping_mod", out_msg, ArrayRecord)
97
87
 
98
88
  new_array_record_key, client_to_server_arrecord = next(
99
89
  iter(out_msg.content.array_records.items())
100
90
  )
101
91
  # Ensure keys in returned ArrayRecord match those in the one sent from server
102
92
  if set(original_array_record.keys()) != set(client_to_server_arrecord.keys()):
103
- log(
104
- WARN,
105
- "fixedclipping_mod: Keys in ArrayRecord must match those from the model "
106
- "that the ClientApp received. Skipping.",
107
- )
108
- return out_msg
93
+ return _handle_array_key_mismatch_err("fixedclipping_mod", out_msg)
109
94
 
110
95
  client_to_server_ndarrays = client_to_server_arrecord.to_numpy_ndarrays()
111
96
  # Clip the client update
@@ -130,3 +115,134 @@ def fixedclipping_mod(
130
115
  )
131
116
  )
132
117
  return out_msg
118
+
119
+
120
+ def adaptiveclipping_mod(
121
+ msg: Message, ctxt: Context, call_next: ClientAppCallable
122
+ ) -> Message:
123
+ """Client-side adaptive clipping modifier.
124
+
125
+ This mod needs to be used with the DifferentialPrivacyClientSideAdaptiveClipping
126
+ server-side strategy wrapper.
127
+
128
+ The wrapper sends the clipping_norm value to the client.
129
+
130
+ This mod clips the client model updates before sending them to the server.
131
+
132
+ It also sends KEY_NORM_BIT to the server for computing the new clipping value.
133
+
134
+ It operates on messages of type `MessageType.TRAIN`.
135
+
136
+ Notes
137
+ -----
138
+ Consider the order of mods when using multiple.
139
+
140
+ Typically, adaptiveclipping_mod should be the last to operate on params.
141
+ """
142
+ if len(msg.content.array_records) != 1:
143
+ return _handle_multi_record_err("adaptiveclipping_mod", msg, ArrayRecord)
144
+ if len(msg.content.config_records) != 1:
145
+ return _handle_multi_record_err("adaptiveclipping_mod", msg, ConfigRecord)
146
+
147
+ # Get keys in the single ConfigRecord
148
+ keys_in_config = set(next(iter(msg.content.config_records.values())).keys())
149
+ if KEY_CLIPPING_NORM not in keys_in_config:
150
+ return _handle_no_key_err("adaptiveclipping_mod", msg)
151
+
152
+ # Record array record communicated to client and clipping norm
153
+ original_array_record = next(iter(msg.content.array_records.values()))
154
+ clipping_norm = cast(
155
+ float, next(iter(msg.content.config_records.values()))[KEY_CLIPPING_NORM]
156
+ )
157
+
158
+ # Call inner app
159
+ out_msg = call_next(msg, ctxt)
160
+
161
+ # Ensure reply has a single ArrayRecord
162
+ if len(out_msg.content.array_records) != 1:
163
+ return _handle_multi_record_err("adaptiveclipping_mod", out_msg, ArrayRecord)
164
+
165
+ # Ensure reply has a single MetricRecord
166
+ if len(out_msg.content.metric_records) != 1:
167
+ return _handle_multi_record_err("adaptiveclipping_mod", out_msg, MetricRecord)
168
+
169
+ # Check if the msg has error
170
+ if out_msg.has_error():
171
+ return out_msg
172
+
173
+ new_array_record_key, client_to_server_arrecord = next(
174
+ iter(out_msg.content.array_records.items())
175
+ )
176
+
177
+ # Ensure keys in returned ArrayRecord match those in the one sent from server
178
+ if set(original_array_record.keys()) != set(client_to_server_arrecord.keys()):
179
+ return _handle_array_key_mismatch_err("adaptiveclipping_mod", out_msg)
180
+
181
+ client_to_server_ndarrays = client_to_server_arrecord.to_numpy_ndarrays()
182
+ # Clip the client update
183
+ norm_bit = compute_adaptive_clip_model_update(
184
+ client_to_server_ndarrays,
185
+ original_array_record.to_numpy_ndarrays(),
186
+ clipping_norm,
187
+ )
188
+ log(
189
+ INFO,
190
+ "adaptiveclipping_mod: ndarrays are clipped by value: %.4f.",
191
+ clipping_norm,
192
+ )
193
+ # Replace outgoing ArrayRecord's Array while preserving their keys
194
+ out_msg.content.array_records[new_array_record_key] = ArrayRecord(
195
+ OrderedDict(
196
+ {
197
+ k: Array(v)
198
+ for k, v in zip(
199
+ client_to_server_arrecord.keys(), client_to_server_ndarrays
200
+ )
201
+ }
202
+ )
203
+ )
204
+ # Add to the MetricRecords the norm bit (recall reply messages only contain
205
+ # one MetricRecord)
206
+ metric_record_key = list(out_msg.content.metric_records.keys())[0]
207
+ # We cast it to `int` because MetricRecord does not support `bool` values
208
+ out_msg.content.metric_records[metric_record_key][KEY_NORM_BIT] = int(norm_bit)
209
+ return out_msg
210
+
211
+
212
+ def _handle_err(msg: Message, reason: str) -> Message:
213
+ """Log and return error message."""
214
+ log(ERROR, reason)
215
+ return Message(
216
+ Error(code=ErrorCode.MOD_FAILED_PRECONDITION, reason=reason),
217
+ reply_to=msg,
218
+ )
219
+
220
+
221
+ def _handle_multi_record_err(mod_name: str, msg: Message, record_type: type) -> Message:
222
+ """Log and return multi-record error."""
223
+ cnt = sum(isinstance(_, record_type) for _ in msg.content.values())
224
+ return _handle_err(
225
+ msg,
226
+ f"{mod_name} expects exactly one {record_type.__name__}, "
227
+ f"but found {cnt} {record_type.__name__}(s).",
228
+ )
229
+
230
+
231
+ def _handle_no_key_err(mod_name: str, msg: Message) -> Message:
232
+ """Log and return no-key error."""
233
+ return _handle_err(
234
+ msg,
235
+ f"{mod_name} requires the key '{KEY_CLIPPING_NORM}' to be present in the "
236
+ "ConfigRecord, but it was not found. "
237
+ "Please ensure the `DifferentialPrivacyClientSideFixedClipping` wrapper "
238
+ "is used in the ServerApp.",
239
+ )
240
+
241
+
242
+ def _handle_array_key_mismatch_err(mod_name: str, msg: Message) -> Message:
243
+ """Create array-key-mismatch error reasons."""
244
+ return _handle_err(
245
+ msg,
246
+ f"{mod_name} expects the keys in the ArrayRecord of the reply message to match "
247
+ "those from the ArrayRecord that the ClientApp received, but they do not.",
248
+ )
@@ -0,0 +1,22 @@
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
+ """Custom types for Flower clients."""
16
+
17
+
18
+ from typing import Callable
19
+
20
+ from flwr.common import Context, Message
21
+
22
+ ClientAppCallable = Callable[[Message, Context], Message]
flwr/common/constant.py CHANGED
@@ -202,6 +202,7 @@ class ErrorCode:
202
202
  MESSAGE_UNAVAILABLE = 3
203
203
  REPLY_MESSAGE_UNAVAILABLE = 4
204
204
  NODE_UNAVAILABLE = 5
205
+ MOD_FAILED_PRECONDITION = 6
205
206
 
206
207
  def __new__(cls) -> ErrorCode:
207
208
  """Prevent instantiation."""
@@ -18,6 +18,8 @@
18
18
  from collections.abc import ItemsView, Iterator, KeysView, MutableMapping, ValuesView
19
19
  from typing import Callable, Generic, TypeVar, cast
20
20
 
21
+ from typing_extensions import Self
22
+
21
23
  K = TypeVar("K") # Key type
22
24
  V = TypeVar("V") # Value type
23
25
 
@@ -86,3 +88,13 @@ class TypedDict(MutableMapping[K, V], Generic[K, V]):
86
88
  def items(self) -> ItemsView[K, V]:
87
89
  """D.items() -> a set-like object providing a view on D's items."""
88
90
  return cast(dict[K, V], self.__dict__["_data"]).items()
91
+
92
+ def copy(self) -> Self:
93
+ """Return a shallow copy of the dictionary."""
94
+ # Allocate instance without going through __init__
95
+ new = self.__class__.__new__(type(self))
96
+ # Copy internal state
97
+ new.__dict__["_check_key_fn"] = self.__dict__["_check_key_fn"]
98
+ new.__dict__["_check_value_fn"] = self.__dict__["_check_value_fn"]
99
+ new.__dict__["_data"] = cast(dict[K, V], self.__dict__["_data"]).copy()
100
+ return new
@@ -15,6 +15,10 @@
15
15
  """ServerApp strategies."""
16
16
 
17
17
 
18
+ from .dp_adaptive_clipping import (
19
+ DifferentialPrivacyClientSideAdaptiveClipping,
20
+ DifferentialPrivacyServerSideAdaptiveClipping,
21
+ )
18
22
  from .dp_fixed_clipping import (
19
23
  DifferentialPrivacyClientSideFixedClipping,
20
24
  DifferentialPrivacyServerSideFixedClipping,
@@ -29,11 +33,15 @@ from .fedtrimmedavg import FedTrimmedAvg
29
33
  from .fedxgb_bagging import FedXgbBagging
30
34
  from .fedxgb_cyclic import FedXgbCyclic
31
35
  from .fedyogi import FedYogi
36
+ from .krum import Krum
37
+ from .qfedavg import QFedAvg
32
38
  from .result import Result
33
39
  from .strategy import Strategy
34
40
 
35
41
  __all__ = [
42
+ "DifferentialPrivacyClientSideAdaptiveClipping",
36
43
  "DifferentialPrivacyClientSideFixedClipping",
44
+ "DifferentialPrivacyServerSideAdaptiveClipping",
37
45
  "DifferentialPrivacyServerSideFixedClipping",
38
46
  "FedAdagrad",
39
47
  "FedAdam",
@@ -45,6 +53,8 @@ __all__ = [
45
53
  "FedXgbBagging",
46
54
  "FedXgbCyclic",
47
55
  "FedYogi",
56
+ "Krum",
57
+ "QFedAvg",
48
58
  "Result",
49
59
  "Strategy",
50
60
  ]
@@ -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)