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.
- flwr/clientapp/mod/__init__.py +2 -1
- flwr/clientapp/mod/centraldp_mods.py +155 -39
- flwr/clientapp/typing.py +22 -0
- flwr/common/constant.py +1 -0
- flwr/common/record/typeddict.py +12 -0
- flwr/serverapp/strategy/__init__.py +10 -0
- flwr/serverapp/strategy/dp_adaptive_clipping.py +335 -0
- flwr/serverapp/strategy/dp_fixed_clipping.py +71 -49
- flwr/serverapp/strategy/fedadagrad.py +0 -3
- flwr/serverapp/strategy/fedadam.py +0 -3
- flwr/serverapp/strategy/fedavgm.py +3 -3
- flwr/serverapp/strategy/fedprox.py +1 -1
- flwr/serverapp/strategy/fedtrimmedavg.py +1 -1
- flwr/serverapp/strategy/fedyogi.py +0 -3
- flwr/serverapp/strategy/krum.py +230 -0
- flwr/serverapp/strategy/qfedavg.py +252 -0
- {flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/METADATA +1 -1
- {flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/RECORD +20 -17
- flwr/serverapp/dp_fixed_clipping.py +0 -352
- {flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/WHEEL +0 -0
- {flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,252 @@
|
|
1
|
+
# Copyright 2025 Flower Labs GmbH. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
# ==============================================================================
|
15
|
+
"""Fair Resource Allocation in Federated Learning [Li et al., 2020] strategy.
|
16
|
+
|
17
|
+
Paper: openreview.net/pdf?id=ByexElSYDr
|
18
|
+
"""
|
19
|
+
|
20
|
+
|
21
|
+
from collections import OrderedDict
|
22
|
+
from collections.abc import Iterable
|
23
|
+
from logging import INFO
|
24
|
+
from typing import Callable, Optional
|
25
|
+
|
26
|
+
import numpy as np
|
27
|
+
|
28
|
+
from flwr.common import (
|
29
|
+
Array,
|
30
|
+
ArrayRecord,
|
31
|
+
ConfigRecord,
|
32
|
+
Message,
|
33
|
+
MetricRecord,
|
34
|
+
NDArray,
|
35
|
+
RecordDict,
|
36
|
+
)
|
37
|
+
from flwr.common.logger import log
|
38
|
+
from flwr.server import Grid
|
39
|
+
|
40
|
+
from ..exception import AggregationError
|
41
|
+
from .fedavg import FedAvg
|
42
|
+
|
43
|
+
|
44
|
+
class QFedAvg(FedAvg):
|
45
|
+
"""Q-FedAvg strategy.
|
46
|
+
|
47
|
+
Implementation based on openreview.net/pdf?id=ByexElSYDr
|
48
|
+
|
49
|
+
Parameters
|
50
|
+
----------
|
51
|
+
client_learning_rate : float
|
52
|
+
Local learning rate used by clients during training. This value is used by
|
53
|
+
the strategy to approximate the base Lipschitz constant L, via
|
54
|
+
L = 1 / client_learning_rate.
|
55
|
+
q : float (default: 0.1)
|
56
|
+
The parameter q that controls the degree of fairness of the algorithm. Please
|
57
|
+
tune this parameter based on your use case.
|
58
|
+
When set to 0, q-FedAvg is equivalent to FedAvg.
|
59
|
+
train_loss_key : str (default: "train_loss")
|
60
|
+
The key within the MetricRecord whose value is used as the training loss when
|
61
|
+
aggregating ArrayRecords following q-FedAvg.
|
62
|
+
fraction_train : float (default: 1.0)
|
63
|
+
Fraction of nodes used during training. In case `min_train_nodes`
|
64
|
+
is larger than `fraction_train * total_connected_nodes`, `min_train_nodes`
|
65
|
+
will still be sampled.
|
66
|
+
fraction_evaluate : float (default: 1.0)
|
67
|
+
Fraction of nodes used during validation. In case `min_evaluate_nodes`
|
68
|
+
is larger than `fraction_evaluate * total_connected_nodes`,
|
69
|
+
`min_evaluate_nodes` will still be sampled.
|
70
|
+
min_train_nodes : int (default: 2)
|
71
|
+
Minimum number of nodes used during training.
|
72
|
+
min_evaluate_nodes : int (default: 2)
|
73
|
+
Minimum number of nodes used during validation.
|
74
|
+
min_available_nodes : int (default: 2)
|
75
|
+
Minimum number of total nodes in the system.
|
76
|
+
weighted_by_key : str (default: "num-examples")
|
77
|
+
The key within each MetricRecord whose value is used as the weight when
|
78
|
+
computing weighted averages for MetricRecords.
|
79
|
+
arrayrecord_key : str (default: "arrays")
|
80
|
+
Key used to store the ArrayRecord when constructing Messages.
|
81
|
+
configrecord_key : str (default: "config")
|
82
|
+
Key used to store the ConfigRecord when constructing Messages.
|
83
|
+
train_metrics_aggr_fn : Optional[callable] (default: None)
|
84
|
+
Function with signature (list[RecordDict], str) -> MetricRecord,
|
85
|
+
used to aggregate MetricRecords from training round replies.
|
86
|
+
If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
|
87
|
+
average using the provided weight factor key.
|
88
|
+
evaluate_metrics_aggr_fn : Optional[callable] (default: None)
|
89
|
+
Function with signature (list[RecordDict], str) -> MetricRecord,
|
90
|
+
used to aggregate MetricRecords from training round replies.
|
91
|
+
If `None`, defaults to `aggregate_metricrecords`, which performs a weighted
|
92
|
+
average using the provided weight factor key.
|
93
|
+
"""
|
94
|
+
|
95
|
+
def __init__( # pylint: disable=R0913, R0917
|
96
|
+
self,
|
97
|
+
client_learning_rate: float,
|
98
|
+
q: float = 0.1,
|
99
|
+
train_loss_key: str = "train_loss",
|
100
|
+
fraction_train: float = 1.0,
|
101
|
+
fraction_evaluate: float = 1.0,
|
102
|
+
min_train_nodes: int = 2,
|
103
|
+
min_evaluate_nodes: int = 2,
|
104
|
+
min_available_nodes: int = 2,
|
105
|
+
weighted_by_key: str = "num-examples",
|
106
|
+
arrayrecord_key: str = "arrays",
|
107
|
+
configrecord_key: str = "config",
|
108
|
+
train_metrics_aggr_fn: Optional[
|
109
|
+
Callable[[list[RecordDict], str], MetricRecord]
|
110
|
+
] = None,
|
111
|
+
evaluate_metrics_aggr_fn: Optional[
|
112
|
+
Callable[[list[RecordDict], str], MetricRecord]
|
113
|
+
] = None,
|
114
|
+
) -> None:
|
115
|
+
super().__init__(
|
116
|
+
fraction_train=fraction_train,
|
117
|
+
fraction_evaluate=fraction_evaluate,
|
118
|
+
min_train_nodes=min_train_nodes,
|
119
|
+
min_evaluate_nodes=min_evaluate_nodes,
|
120
|
+
min_available_nodes=min_available_nodes,
|
121
|
+
weighted_by_key=weighted_by_key,
|
122
|
+
arrayrecord_key=arrayrecord_key,
|
123
|
+
configrecord_key=configrecord_key,
|
124
|
+
train_metrics_aggr_fn=train_metrics_aggr_fn,
|
125
|
+
evaluate_metrics_aggr_fn=evaluate_metrics_aggr_fn,
|
126
|
+
)
|
127
|
+
self.q = q
|
128
|
+
self.client_learning_rate = client_learning_rate
|
129
|
+
self.train_loss_key = train_loss_key
|
130
|
+
self.current_arrays: Optional[ArrayRecord] = None
|
131
|
+
|
132
|
+
def summary(self) -> None:
|
133
|
+
"""Log summary configuration of the strategy."""
|
134
|
+
log(INFO, "\t├──> q-FedAvg settings:")
|
135
|
+
log(INFO, "\t│\t├── client_learning_rate: %s", self.client_learning_rate)
|
136
|
+
log(INFO, "\t│\t├── q: %s", self.q)
|
137
|
+
log(INFO, "\t│\t└── train_loss_key: '%s'", self.train_loss_key)
|
138
|
+
super().summary()
|
139
|
+
|
140
|
+
def configure_train(
|
141
|
+
self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
|
142
|
+
) -> Iterable[Message]:
|
143
|
+
"""Configure the next round of federated training."""
|
144
|
+
self.current_arrays = arrays.copy()
|
145
|
+
return super().configure_train(server_round, arrays, config, grid)
|
146
|
+
|
147
|
+
def aggregate_train( # pylint: disable=too-many-locals
|
148
|
+
self,
|
149
|
+
server_round: int,
|
150
|
+
replies: Iterable[Message],
|
151
|
+
) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
|
152
|
+
"""Aggregate ArrayRecords and MetricRecords in the received Messages."""
|
153
|
+
# Call FedAvg aggregate_train to perform validation and aggregation
|
154
|
+
valid_replies, _ = self._check_and_log_replies(replies, is_train=True)
|
155
|
+
|
156
|
+
if not valid_replies:
|
157
|
+
return None, None
|
158
|
+
|
159
|
+
# Compute estimate of Lipschitz constant L
|
160
|
+
L = 1.0 / self.client_learning_rate # pylint: disable=C0103
|
161
|
+
|
162
|
+
# q-FedAvg aggregation
|
163
|
+
if self.current_arrays is None:
|
164
|
+
raise AggregationError(
|
165
|
+
"Current global model weights are not available. Make sure to call"
|
166
|
+
"`configure_train` before calling `aggregate_train`."
|
167
|
+
)
|
168
|
+
array_keys = list(self.current_arrays.keys()) # Preserve keys
|
169
|
+
global_weights = self.current_arrays.to_numpy_ndarrays(keep_input=False)
|
170
|
+
sum_delta = None
|
171
|
+
sum_h = 0.0
|
172
|
+
|
173
|
+
for msg in valid_replies:
|
174
|
+
# Extract local weights and training loss from Message
|
175
|
+
local_weights = get_local_weights(msg)
|
176
|
+
loss = get_train_loss(msg, self.train_loss_key)
|
177
|
+
|
178
|
+
# Compute delta and h
|
179
|
+
delta, h = compute_delta_and_h(
|
180
|
+
global_weights, local_weights, self.q, L, loss
|
181
|
+
)
|
182
|
+
|
183
|
+
# Compute sum of deltas and sum of h
|
184
|
+
if sum_delta is None:
|
185
|
+
sum_delta = delta
|
186
|
+
else:
|
187
|
+
sum_delta = [sd + d for sd, d in zip(sum_delta, delta)]
|
188
|
+
sum_h += h
|
189
|
+
|
190
|
+
# Compute new global weights and convert to Array type
|
191
|
+
# `np.asarray` can convert numpy scalars to 0-dim arrays
|
192
|
+
assert sum_delta is not None # Make mypy happy
|
193
|
+
array_list = [
|
194
|
+
Array(np.asarray(gw - (d / sum_h)))
|
195
|
+
for gw, d in zip(global_weights, sum_delta)
|
196
|
+
]
|
197
|
+
|
198
|
+
# Aggregate MetricRecords
|
199
|
+
metrics = self.train_metrics_aggr_fn(
|
200
|
+
[msg.content for msg in valid_replies],
|
201
|
+
self.weighted_by_key,
|
202
|
+
)
|
203
|
+
return ArrayRecord(OrderedDict(zip(array_keys, array_list))), metrics
|
204
|
+
|
205
|
+
|
206
|
+
def get_train_loss(msg: Message, loss_key: str) -> float:
|
207
|
+
"""Extract training loss from a Message."""
|
208
|
+
metrics = list(msg.content.metric_records.values())[0]
|
209
|
+
if (loss := metrics.get(loss_key)) is None or not isinstance(loss, (int, float)):
|
210
|
+
raise AggregationError(
|
211
|
+
"Missing or invalid training loss. "
|
212
|
+
f"The strategy expected a float value for the key '{loss_key}' "
|
213
|
+
"as the training loss in each MetricRecord from the clients. "
|
214
|
+
f"Ensure that '{loss_key}' is present and maps to a valid float."
|
215
|
+
)
|
216
|
+
return float(loss)
|
217
|
+
|
218
|
+
|
219
|
+
def get_local_weights(msg: Message) -> list[NDArray]:
|
220
|
+
"""Extract local weights from a Message."""
|
221
|
+
arrays = list(msg.content.array_records.values())[0]
|
222
|
+
return arrays.to_numpy_ndarrays(keep_input=False)
|
223
|
+
|
224
|
+
|
225
|
+
def l2_norm(ndarrays: list[NDArray]) -> float:
|
226
|
+
"""Compute the squared L2 norm of a list of numpy.ndarray."""
|
227
|
+
return float(sum(np.sum(np.square(g)) for g in ndarrays))
|
228
|
+
|
229
|
+
|
230
|
+
def compute_delta_and_h(
|
231
|
+
global_weights: list[NDArray],
|
232
|
+
local_weights: list[NDArray],
|
233
|
+
q: float,
|
234
|
+
L: float, # Lipschitz constant # pylint: disable=C0103
|
235
|
+
loss: float,
|
236
|
+
) -> tuple[list[NDArray], float]:
|
237
|
+
"""Compute delta and h used in q-FedAvg aggregation."""
|
238
|
+
# Compute gradient_k = L * (w - w_k)
|
239
|
+
for gw, lw in zip(global_weights, local_weights):
|
240
|
+
np.subtract(gw, lw, out=lw)
|
241
|
+
lw *= L
|
242
|
+
grad = local_weights # After in-place operations, local_weights is now grad
|
243
|
+
# Compute ||w_k - w||^2
|
244
|
+
norm = l2_norm(grad)
|
245
|
+
# Compute delta_k = loss_k^q * gradient_k
|
246
|
+
loss_pow_q: float = np.float_power(loss + 1e-10, q)
|
247
|
+
for g in grad:
|
248
|
+
g *= loss_pow_q
|
249
|
+
delta = grad # After in-place multiplication, grad is now delta
|
250
|
+
# Compute h_k
|
251
|
+
h = q * np.float_power(loss + 1e-10, q - 1) * norm + L * loss_pow_q
|
252
|
+
return delta, h
|
{flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/METADATA
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: flwr-nightly
|
3
|
-
Version: 1.22.0.
|
3
|
+
Version: 1.22.0.dev20250919
|
4
4
|
Summary: Flower: A Friendly Federated AI Framework
|
5
5
|
License: Apache-2.0
|
6
6
|
Keywords: Artificial Intelligence,Federated AI,Federated Analytics,Federated Evaluation,Federated Learning,Flower,Machine Learning
|
{flwr_nightly-1.22.0.dev20250918.dist-info → flwr_nightly-1.22.0.dev20250919.dist-info}/RECORD
RENAMED
@@ -113,15 +113,16 @@ flwr/client/rest_client/connection.py,sha256=fyiS1aXTv71jWczx7mSco94LYJTBXgTF-p2
|
|
113
113
|
flwr/client/run_info_store.py,sha256=MaJ3UQ-07hWtK67wnWu0zR29jrk0fsfgJX506dvEOfE,4042
|
114
114
|
flwr/client/typing.py,sha256=Jw3rawDzI_-ZDcRmEQcs5gZModY7oeQlEeltYsdOhlU,1048
|
115
115
|
flwr/clientapp/__init__.py,sha256=uoTjvIynfGvMhsmc7iYK-5qJ0AdKKjCbx7WTKc0KeSk,828
|
116
|
-
flwr/clientapp/mod/__init__.py,sha256=
|
117
|
-
flwr/clientapp/mod/centraldp_mods.py,sha256=
|
116
|
+
flwr/clientapp/mod/__init__.py,sha256=LZveV-U2YZVEH4FyAZMXMT-aD1Hc0I5miIkDr7p6uQQ,970
|
117
|
+
flwr/clientapp/mod/centraldp_mods.py,sha256=a-F-ELs3lt_wtmLl8900ExJiIY792cPCrmwmJKRrerI,8950
|
118
|
+
flwr/clientapp/typing.py,sha256=x1GvXWy112RqZh27liJqz-yZ7SSCOwiOSmAQsUxk9MY,853
|
118
119
|
flwr/common/__init__.py,sha256=5GCLVk399Az_rTJHNticRlL0Sl_oPw_j5_LuFKfX7-M,4171
|
119
120
|
flwr/common/address.py,sha256=9JucdTwlc-jpeJkRKeUboZoacUtErwSVtnDR9kAtLqE,4119
|
120
121
|
flwr/common/args.py,sha256=Nq2u4yePbkSY0CWFamn0hZY6Rms8G1xYDeDGIcLIITE,5849
|
121
122
|
flwr/common/auth_plugin/__init__.py,sha256=DktrRcGZrRarLf7Jb_UlHtOyLp9_-kEplyq6PS5-vOA,988
|
122
123
|
flwr/common/auth_plugin/auth_plugin.py,sha256=mM7SuphO4OsVAVJR1GErYVgYT83ZjxDzS_gha12bT9E,4855
|
123
124
|
flwr/common/config.py,sha256=glcZDjco-amw1YfQcYTFJ4S1pt9APoexT-mf1QscuHs,13960
|
124
|
-
flwr/common/constant.py,sha256=
|
125
|
+
flwr/common/constant.py,sha256=y3yKgr1UxAWveUkw29z8KM2hKsZJqhHUqPPhKQeef80,9045
|
125
126
|
flwr/common/context.py,sha256=Be8obQR_OvEDy1OmshuUKxGRQ7Qx89mf5F4xlhkR10s,2407
|
126
127
|
flwr/common/date.py,sha256=1ZT2cRSpC2DJqprOVTLXYCR_O2_OZR0zXO_brJ3LqWc,1554
|
127
128
|
flwr/common/differential_privacy.py,sha256=FdlpdpPl_H_2HJa8CQM1iCUGBBQ5Dc8CzxmHERM-EoE,6148
|
@@ -152,7 +153,7 @@ flwr/common/record/configrecord.py,sha256=G7U0q39kB0Kyi0zMxFmPxcVemL9NgwVS1qjvI4
|
|
152
153
|
flwr/common/record/conversion_utils.py,sha256=wbNCzy7oAqaA3-arhls_EqRZYXRC4YrWIoE-Gy82fJ0,1191
|
153
154
|
flwr/common/record/metricrecord.py,sha256=KOyJjJbvFV6IwBPbgm92FZ_0_hXpMHuwfCi1rh5Zddk,8954
|
154
155
|
flwr/common/record/recorddict.py,sha256=p7hBimFpKM1XKUe6OAkR_7pYGzGL_EwUJUvJ8odZEcY,14986
|
155
|
-
flwr/common/record/typeddict.py,sha256=
|
156
|
+
flwr/common/record/typeddict.py,sha256=NkHvzTEiicrLstryvTb-2O-sMvtsLgsOjAbostSf1z0,4102
|
156
157
|
flwr/common/recorddict_compat.py,sha256=D5SqXWkqBddn5b6K_5UoH7aZ11UaN3lDTlzvHx3-rqk,14119
|
157
158
|
flwr/common/retry_invoker.py,sha256=uQeDcgoTgmFwhJ0mkDE2eNz2acF9eShaqMOO5boGrPQ,15285
|
158
159
|
flwr/common/secure_aggregation/__init__.py,sha256=MgW6uHGhyFLBAYQqa1Vzs5n2Gc0d4yEw1_NmerFir70,731
|
@@ -335,21 +336,23 @@ flwr/server/workflow/secure_aggregation/__init__.py,sha256=vGkycLb65CxdaMkKsANxQ
|
|
335
336
|
flwr/server/workflow/secure_aggregation/secagg_workflow.py,sha256=b_pKk7gmbahwyj0ftOOLXvu-AMtRHEc82N9PJTEO8dc,5839
|
336
337
|
flwr/server/workflow/secure_aggregation/secaggplus_workflow.py,sha256=DkayCsnlAya6Y2PZsueLgoUCMRtV-GbnW08RfWx_SXM,29460
|
337
338
|
flwr/serverapp/__init__.py,sha256=ZujKNXULwhWYQhFnxOOT5Wi9MRq2JCWFhAAj7ouiQ78,884
|
338
|
-
flwr/serverapp/dp_fixed_clipping.py,sha256=wbP4W7CaUHXdll8ZSVUnTBSEWrnWM00CGk63rOR-Q2s,12133
|
339
339
|
flwr/serverapp/exception.py,sha256=5cuH-2AafvihzosWDdDjuMmHdDqZ1XxHvCqZXNBVklw,1334
|
340
|
-
flwr/serverapp/strategy/__init__.py,sha256=
|
341
|
-
flwr/serverapp/strategy/
|
342
|
-
flwr/serverapp/strategy/
|
343
|
-
flwr/serverapp/strategy/
|
340
|
+
flwr/serverapp/strategy/__init__.py,sha256=QQFa0uMXWrSCTVbd7Ixk_48U6o3K-g4nLYYJUhEVbfo,1877
|
341
|
+
flwr/serverapp/strategy/dp_adaptive_clipping.py,sha256=mssiVGMgfJw8DeP6_pBSZUKWmaXvYeG-B-p7RSt2tAU,13600
|
342
|
+
flwr/serverapp/strategy/dp_fixed_clipping.py,sha256=C_faT0ggzeUB2bGv_r1Vss-fv7-UhDrpmfiHATESI0w,12832
|
343
|
+
flwr/serverapp/strategy/fedadagrad.py,sha256=faFsuKZziPTCLeNrJOyKbPTNo-1xrIZOz7SWT5rdjJs,6269
|
344
|
+
flwr/serverapp/strategy/fedadam.py,sha256=NsY_V6TGFAfCeA9vmqaLpvB_T5siJEtKozKGdxJssAI,7064
|
344
345
|
flwr/serverapp/strategy/fedavg.py,sha256=Bq_nlmngzJbjqX1fF1mevXGVN6-pwglHv-6yNrs6lkA,12035
|
345
|
-
flwr/serverapp/strategy/fedavgm.py,sha256=
|
346
|
+
flwr/serverapp/strategy/fedavgm.py,sha256=FtFmBGLzuUQ_7JWk85Xh19d8sP0YDwqczGTliGzZyGs,8333
|
346
347
|
flwr/serverapp/strategy/fedmedian.py,sha256=b31Dk0LQBbQxi_f-jeSbWHI7iOBugcuBSN2Az-_a75E,2596
|
347
348
|
flwr/serverapp/strategy/fedopt.py,sha256=kqT0uV2IUE93O72XEVa1JJo61dcwbZEoT9KmYTjR2tE,8477
|
348
|
-
flwr/serverapp/strategy/fedprox.py,sha256=
|
349
|
-
flwr/serverapp/strategy/fedtrimmedavg.py,sha256=
|
349
|
+
flwr/serverapp/strategy/fedprox.py,sha256=J1KrcE5DFko6i4608iICv1G0t9MPXspjibPd-SF_HT8,7028
|
350
|
+
flwr/serverapp/strategy/fedtrimmedavg.py,sha256=58xDPc_YO41QM8jXn0gZ79PFzO8zo3Mh3UlkF0UBbIA,7168
|
350
351
|
flwr/serverapp/strategy/fedxgb_bagging.py,sha256=ktDjzov4y0BRecioq788umCEtcuwElou9olBizQKOnM,3282
|
351
352
|
flwr/serverapp/strategy/fedxgb_cyclic.py,sha256=8H8WoLdG4Fy1_dtLLE4AYiidC-Cvaw2GxySfzAb7Xj0,8774
|
352
|
-
flwr/serverapp/strategy/fedyogi.py,sha256=
|
353
|
+
flwr/serverapp/strategy/fedyogi.py,sha256=Y9RFBQaNch3fPgGXF7OfnTH6eOpavZxpMWxWVIC9_SY,6579
|
354
|
+
flwr/serverapp/strategy/krum.py,sha256=iUM7MFCKQcSqUO3Eu4JKWnMc8NV0WMQW9dZXm4onQ-s,9490
|
355
|
+
flwr/serverapp/strategy/qfedavg.py,sha256=EM1tO_ovkybOBeW-h1PYX0lszCUAVHT6hUpwXykAEps,10204
|
353
356
|
flwr/serverapp/strategy/result.py,sha256=E0Hl2VLnZAgQJjE2GDoKsK7JX-kPPU2KXc47Axt6hGw,4295
|
354
357
|
flwr/serverapp/strategy/strategy.py,sha256=8uJGGm1ROLZERQ_dkRS7Z_rs-yK6XCE0UxXtIdFiEWk,10789
|
355
358
|
flwr/serverapp/strategy/strategy_utils.py,sha256=hiwS7k-Hx6_c4NZXoKpHucS5CBKb7f8GppXRBSMt3Us,10851
|
@@ -416,7 +419,7 @@ flwr/supernode/servicer/__init__.py,sha256=lucTzre5WPK7G1YLCfaqg3rbFWdNSb7ZTt-ca
|
|
416
419
|
flwr/supernode/servicer/clientappio/__init__.py,sha256=7Oy62Y_oijqF7Dxi6tpcUQyOpLc_QpIRZ83NvwmB0Yg,813
|
417
420
|
flwr/supernode/servicer/clientappio/clientappio_servicer.py,sha256=nIHRu38EWK-rpNOkcgBRAAKwYQQWFeCwu0lkO7OPZGQ,10239
|
418
421
|
flwr/supernode/start_client_internal.py,sha256=Y9S1-QlO2WP6eo4JvWzIpfaCoh2aoE7bjEYyxNNnlyg,20777
|
419
|
-
flwr_nightly-1.22.0.
|
420
|
-
flwr_nightly-1.22.0.
|
421
|
-
flwr_nightly-1.22.0.
|
422
|
-
flwr_nightly-1.22.0.
|
422
|
+
flwr_nightly-1.22.0.dev20250919.dist-info/METADATA,sha256=RuNCBGYrX4Df-vdIVKJ6Do1uDfeKomIIJ557ZlE8uLI,14559
|
423
|
+
flwr_nightly-1.22.0.dev20250919.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
424
|
+
flwr_nightly-1.22.0.dev20250919.dist-info/entry_points.txt,sha256=hxHD2ixb_vJFDOlZV-zB4Ao32_BQlL34ftsDh1GXv14,420
|
425
|
+
flwr_nightly-1.22.0.dev20250919.dist-info/RECORD,,
|
@@ -1,352 +0,0 @@
|
|
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 fixed clipping.
|
16
|
-
|
17
|
-
Papers: https://arxiv.org/abs/1712.07557, https://arxiv.org/abs/1710.06963
|
18
|
-
"""
|
19
|
-
|
20
|
-
from abc import ABC
|
21
|
-
from collections import OrderedDict
|
22
|
-
from collections.abc import Iterable
|
23
|
-
from logging import INFO, WARNING
|
24
|
-
from typing import Optional
|
25
|
-
|
26
|
-
from flwr.common import Array, ArrayRecord, ConfigRecord, Message, MetricRecord, log
|
27
|
-
from flwr.common.differential_privacy import (
|
28
|
-
add_gaussian_noise_inplace,
|
29
|
-
compute_clip_model_update,
|
30
|
-
compute_stdv,
|
31
|
-
)
|
32
|
-
from flwr.common.differential_privacy_constants import (
|
33
|
-
CLIENTS_DISCREPANCY_WARNING,
|
34
|
-
KEY_CLIPPING_NORM,
|
35
|
-
)
|
36
|
-
from flwr.server import Grid
|
37
|
-
|
38
|
-
from .strategy import Strategy
|
39
|
-
|
40
|
-
|
41
|
-
class DifferentialPrivacyFixedClippingBase(Strategy, ABC):
|
42
|
-
"""Base class for DP strategies with fixed clipping.
|
43
|
-
|
44
|
-
This class contains common functionality shared between server-side and
|
45
|
-
client-side fixed clipping implementations.
|
46
|
-
|
47
|
-
Parameters
|
48
|
-
----------
|
49
|
-
strategy : Strategy
|
50
|
-
The strategy to which DP functionalities will be added by this wrapper.
|
51
|
-
noise_multiplier : float
|
52
|
-
The noise multiplier for the Gaussian mechanism for model updates.
|
53
|
-
A value of 1.0 or higher is recommended for strong privacy.
|
54
|
-
clipping_norm : float
|
55
|
-
The value of the clipping norm.
|
56
|
-
num_sampled_clients : int
|
57
|
-
The number of clients that are sampled on each round.
|
58
|
-
"""
|
59
|
-
|
60
|
-
# pylint: disable=too-many-arguments,too-many-instance-attributes
|
61
|
-
def __init__(
|
62
|
-
self,
|
63
|
-
strategy: Strategy,
|
64
|
-
noise_multiplier: float,
|
65
|
-
clipping_norm: float,
|
66
|
-
num_sampled_clients: int,
|
67
|
-
) -> None:
|
68
|
-
super().__init__()
|
69
|
-
|
70
|
-
self.strategy = strategy
|
71
|
-
|
72
|
-
if noise_multiplier < 0:
|
73
|
-
raise ValueError("The noise multiplier should be a non-negative value.")
|
74
|
-
|
75
|
-
if clipping_norm <= 0:
|
76
|
-
raise ValueError("The clipping norm should be a positive value.")
|
77
|
-
|
78
|
-
if num_sampled_clients <= 0:
|
79
|
-
raise ValueError(
|
80
|
-
"The number of sampled clients should be a positive value."
|
81
|
-
)
|
82
|
-
|
83
|
-
self.noise_multiplier = noise_multiplier
|
84
|
-
self.clipping_norm = clipping_norm
|
85
|
-
self.num_sampled_clients = num_sampled_clients
|
86
|
-
|
87
|
-
def _validate_replies(self, replies: Iterable[Message]) -> bool:
|
88
|
-
"""Validate replies and log errors/warnings.
|
89
|
-
|
90
|
-
Returns
|
91
|
-
-------
|
92
|
-
bool
|
93
|
-
True if replies are valid for aggregation, False otherwise.
|
94
|
-
"""
|
95
|
-
num_errors = 0
|
96
|
-
num_replies_with_content = 0
|
97
|
-
for msg in replies:
|
98
|
-
if msg.has_error():
|
99
|
-
log(
|
100
|
-
INFO,
|
101
|
-
"Received error in reply from node %d: %s",
|
102
|
-
msg.metadata.src_node_id,
|
103
|
-
msg.error,
|
104
|
-
)
|
105
|
-
num_errors += 1
|
106
|
-
else:
|
107
|
-
num_replies_with_content += 1
|
108
|
-
|
109
|
-
# Errors are not allowed
|
110
|
-
if num_errors:
|
111
|
-
log(
|
112
|
-
INFO,
|
113
|
-
"aggregate_train: Some clients reported errors. Skipping aggregation.",
|
114
|
-
)
|
115
|
-
return False
|
116
|
-
|
117
|
-
log(
|
118
|
-
INFO,
|
119
|
-
"aggregate_train: Received %s results and %s failures",
|
120
|
-
num_replies_with_content,
|
121
|
-
num_errors,
|
122
|
-
)
|
123
|
-
|
124
|
-
if num_replies_with_content != self.num_sampled_clients:
|
125
|
-
log(
|
126
|
-
WARNING,
|
127
|
-
CLIENTS_DISCREPANCY_WARNING,
|
128
|
-
num_replies_with_content,
|
129
|
-
self.num_sampled_clients,
|
130
|
-
)
|
131
|
-
|
132
|
-
return True
|
133
|
-
|
134
|
-
def _add_noise_to_aggregated_arrays(
|
135
|
-
self, aggregated_arrays: ArrayRecord
|
136
|
-
) -> ArrayRecord:
|
137
|
-
"""Add Gaussian noise to aggregated arrays.
|
138
|
-
|
139
|
-
Parameters
|
140
|
-
----------
|
141
|
-
aggregated_arrays : ArrayRecord
|
142
|
-
The aggregated arrays to add noise to.
|
143
|
-
|
144
|
-
Returns
|
145
|
-
-------
|
146
|
-
ArrayRecord
|
147
|
-
The aggregated arrays with noise added.
|
148
|
-
"""
|
149
|
-
aggregated_ndarrays = aggregated_arrays.to_numpy_ndarrays()
|
150
|
-
stdv = compute_stdv(
|
151
|
-
self.noise_multiplier, self.clipping_norm, self.num_sampled_clients
|
152
|
-
)
|
153
|
-
add_gaussian_noise_inplace(aggregated_ndarrays, stdv)
|
154
|
-
|
155
|
-
log(
|
156
|
-
INFO,
|
157
|
-
"aggregate_fit: central DP noise with %.4f stdev added",
|
158
|
-
stdv,
|
159
|
-
)
|
160
|
-
|
161
|
-
return ArrayRecord(
|
162
|
-
OrderedDict(
|
163
|
-
{
|
164
|
-
k: Array(v)
|
165
|
-
for k, v in zip(aggregated_arrays.keys(), aggregated_ndarrays)
|
166
|
-
}
|
167
|
-
)
|
168
|
-
)
|
169
|
-
|
170
|
-
def configure_evaluate(
|
171
|
-
self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
|
172
|
-
) -> Iterable[Message]:
|
173
|
-
"""Configure the next round of federated evaluation."""
|
174
|
-
return self.strategy.configure_evaluate(server_round, arrays, config, grid)
|
175
|
-
|
176
|
-
def aggregate_evaluate(
|
177
|
-
self,
|
178
|
-
server_round: int,
|
179
|
-
replies: Iterable[Message],
|
180
|
-
) -> Optional[MetricRecord]:
|
181
|
-
"""Aggregate MetricRecords in the received Messages."""
|
182
|
-
return self.strategy.aggregate_evaluate(server_round, replies)
|
183
|
-
|
184
|
-
def summary(self) -> None:
|
185
|
-
"""Log summary configuration of the strategy."""
|
186
|
-
self.strategy.summary()
|
187
|
-
|
188
|
-
|
189
|
-
class DifferentialPrivacyServerSideFixedClipping(DifferentialPrivacyFixedClippingBase):
|
190
|
-
"""Strategy wrapper for central DP with server-side fixed clipping.
|
191
|
-
|
192
|
-
Parameters
|
193
|
-
----------
|
194
|
-
strategy : Strategy
|
195
|
-
The strategy to which DP functionalities will be added by this wrapper.
|
196
|
-
noise_multiplier : float
|
197
|
-
The noise multiplier for the Gaussian mechanism for model updates.
|
198
|
-
A value of 1.0 or higher is recommended for strong privacy.
|
199
|
-
clipping_norm : float
|
200
|
-
The value of the clipping norm.
|
201
|
-
num_sampled_clients : int
|
202
|
-
The number of clients that are sampled on each round.
|
203
|
-
|
204
|
-
Examples
|
205
|
-
--------
|
206
|
-
Create a strategy::
|
207
|
-
|
208
|
-
strategy = fl.serverapp.FedAvg( ... )
|
209
|
-
|
210
|
-
Wrap the strategy with the `DifferentialPrivacyServerSideFixedClipping` wrapper::
|
211
|
-
|
212
|
-
dp_strategy = DifferentialPrivacyServerSideFixedClipping(
|
213
|
-
strategy, cfg.noise_multiplier, cfg.clipping_norm, cfg.num_sampled_clients
|
214
|
-
)
|
215
|
-
"""
|
216
|
-
|
217
|
-
def __init__(
|
218
|
-
self,
|
219
|
-
strategy: Strategy,
|
220
|
-
noise_multiplier: float,
|
221
|
-
clipping_norm: float,
|
222
|
-
num_sampled_clients: int,
|
223
|
-
) -> None:
|
224
|
-
super().__init__(strategy, noise_multiplier, clipping_norm, num_sampled_clients)
|
225
|
-
self.current_arrays: ArrayRecord = ArrayRecord()
|
226
|
-
|
227
|
-
def __repr__(self) -> str:
|
228
|
-
"""Compute a string representation of the strategy."""
|
229
|
-
return "Differential Privacy Strategy Wrapper (Server-Side Fixed Clipping)"
|
230
|
-
|
231
|
-
def configure_train(
|
232
|
-
self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
|
233
|
-
) -> Iterable[Message]:
|
234
|
-
"""Configure the next round of training."""
|
235
|
-
self.current_arrays = arrays
|
236
|
-
return self.strategy.configure_train(server_round, arrays, config, grid)
|
237
|
-
|
238
|
-
def aggregate_train(
|
239
|
-
self,
|
240
|
-
server_round: int,
|
241
|
-
replies: Iterable[Message],
|
242
|
-
) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
|
243
|
-
"""Aggregate ArrayRecords and MetricRecords in the received Messages."""
|
244
|
-
if not self._validate_replies(replies):
|
245
|
-
return None, None
|
246
|
-
|
247
|
-
# Clip arrays in replies
|
248
|
-
current_ndarrays = self.current_arrays.to_numpy_ndarrays()
|
249
|
-
for reply in replies:
|
250
|
-
for arr_name, record in reply.content.array_records.items():
|
251
|
-
# Clip
|
252
|
-
reply_ndarrays = record.to_numpy_ndarrays()
|
253
|
-
compute_clip_model_update(
|
254
|
-
param1=reply_ndarrays,
|
255
|
-
param2=current_ndarrays,
|
256
|
-
clipping_norm=self.clipping_norm,
|
257
|
-
)
|
258
|
-
# Replace content while preserving keys
|
259
|
-
reply.content[arr_name] = ArrayRecord(
|
260
|
-
OrderedDict(
|
261
|
-
{k: Array(v) for k, v in zip(record.keys(), reply_ndarrays)}
|
262
|
-
)
|
263
|
-
)
|
264
|
-
log(
|
265
|
-
INFO,
|
266
|
-
"aggregate_fit: parameters are clipped by value: %.4f.",
|
267
|
-
self.clipping_norm,
|
268
|
-
)
|
269
|
-
|
270
|
-
# Pass the new parameters for aggregation
|
271
|
-
aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
|
272
|
-
server_round, replies
|
273
|
-
)
|
274
|
-
|
275
|
-
# Add Gaussian noise to the aggregated arrays
|
276
|
-
if aggregated_arrays:
|
277
|
-
aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
|
278
|
-
|
279
|
-
return aggregated_arrays, aggregated_metrics
|
280
|
-
|
281
|
-
|
282
|
-
class DifferentialPrivacyClientSideFixedClipping(DifferentialPrivacyFixedClippingBase):
|
283
|
-
"""Strategy wrapper for central DP with client-side fixed clipping.
|
284
|
-
|
285
|
-
Use `fixedclipping_mod` modifier at the client side.
|
286
|
-
|
287
|
-
In comparison to `DifferentialPrivacyServerSideFixedClipping`,
|
288
|
-
which performs clipping on the server-side,
|
289
|
-
`DifferentialPrivacyClientSideFixedClipping` expects clipping to happen
|
290
|
-
on the client-side, usually by using the built-in `fixedclipping_mod`.
|
291
|
-
|
292
|
-
Parameters
|
293
|
-
----------
|
294
|
-
strategy : Strategy
|
295
|
-
The strategy to which DP functionalities will be added by this wrapper.
|
296
|
-
noise_multiplier : float
|
297
|
-
The noise multiplier for the Gaussian mechanism for model updates.
|
298
|
-
A value of 1.0 or higher is recommended for strong privacy.
|
299
|
-
clipping_norm : float
|
300
|
-
The value of the clipping norm.
|
301
|
-
num_sampled_clients : int
|
302
|
-
The number of clients that are sampled on each round.
|
303
|
-
|
304
|
-
Examples
|
305
|
-
--------
|
306
|
-
Create a strategy::
|
307
|
-
|
308
|
-
strategy = fl.serverapp.FedAvg(...)
|
309
|
-
|
310
|
-
Wrap the strategy with the `DifferentialPrivacyClientSideFixedClipping` wrapper::
|
311
|
-
|
312
|
-
dp_strategy = DifferentialPrivacyClientSideFixedClipping(
|
313
|
-
strategy, cfg.noise_multiplier, cfg.clipping_norm, cfg.num_sampled_clients
|
314
|
-
)
|
315
|
-
|
316
|
-
On the client, add the `fixedclipping_mod` to the client-side mods::
|
317
|
-
|
318
|
-
app = fl.client.ClientApp(mods=[fixedclipping_mod])
|
319
|
-
"""
|
320
|
-
|
321
|
-
def __repr__(self) -> str:
|
322
|
-
"""Compute a string representation of the strategy."""
|
323
|
-
return "Differential Privacy Strategy Wrapper (Client-Side Fixed Clipping)"
|
324
|
-
|
325
|
-
def configure_train(
|
326
|
-
self, server_round: int, arrays: ArrayRecord, config: ConfigRecord, grid: Grid
|
327
|
-
) -> Iterable[Message]:
|
328
|
-
"""Configure the next round of training."""
|
329
|
-
# Inject clipping norm in config
|
330
|
-
config[KEY_CLIPPING_NORM] = self.clipping_norm
|
331
|
-
# Call parent method
|
332
|
-
return self.strategy.configure_train(server_round, arrays, config, grid)
|
333
|
-
|
334
|
-
def aggregate_train(
|
335
|
-
self,
|
336
|
-
server_round: int,
|
337
|
-
replies: Iterable[Message],
|
338
|
-
) -> tuple[Optional[ArrayRecord], Optional[MetricRecord]]:
|
339
|
-
"""Aggregate ArrayRecords and MetricRecords in the received Messages."""
|
340
|
-
if not self._validate_replies(replies):
|
341
|
-
return None, None
|
342
|
-
|
343
|
-
# Aggregate
|
344
|
-
aggregated_arrays, aggregated_metrics = self.strategy.aggregate_train(
|
345
|
-
server_round, replies
|
346
|
-
)
|
347
|
-
|
348
|
-
# Add Gaussian noise to the aggregated arrays
|
349
|
-
if aggregated_arrays:
|
350
|
-
aggregated_arrays = self._add_noise_to_aggregated_arrays(aggregated_arrays)
|
351
|
-
|
352
|
-
return aggregated_arrays, aggregated_metrics
|