-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_alg.py
258 lines (226 loc) · 7.84 KB
/
custom_alg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
The same as the FedProx algorithm, for testing custom algorithm.
"""
import warnings
from copy import deepcopy
from typing import Any, Dict, List
import torch
from torch_ecg.utils.misc import add_docstring
from tqdm.auto import tqdm
from fl_sim.algorithms._misc import client_config_kw_doc, server_config_kw_doc
from fl_sim.algorithms._register import register_algorithm
from fl_sim.nodes import Client, ClientConfig, ClientMessage, Server, ServerConfig
__all__ = [
"CustomServer",
"CustomClient",
"CustomServerConfig",
"CustomClientConfig",
]
@register_algorithm()
@add_docstring(server_config_kw_doc, "append")
class CustomServerConfig(ServerConfig):
"""Server config for the Custom algorithm.
Parameters
----------
num_iters : int
The number of (outer) iterations.
num_clients : int
The number of clients.
clients_sample_ratio : float
The ratio of clients to sample for each iteration.
vr : bool, default False
Whether to use variance reduction.
**kwargs : dict, optional
Additional keyword arguments:
"""
__name__ = "CustomServerConfig"
def __init__(
self,
num_iters: int,
num_clients: int,
clients_sample_ratio: float,
vr: bool = False,
**kwargs: Any,
) -> None:
name = self.__name__.replace("ServerConfig", "")
if kwargs.pop("algorithm", None) is not None:
warnings.warn(
f"The `algorithm` argument is fixed to `{name}` and will be ignored.",
RuntimeWarning,
)
super().__init__(
name,
num_iters,
num_clients,
clients_sample_ratio,
vr=vr,
**kwargs,
)
@register_algorithm()
@add_docstring(client_config_kw_doc, "append")
class CustomClientConfig(ClientConfig):
"""Client config for the Custom algorithm.
Parameters
----------
batch_size : int
The batch size.
num_epochs : int
The number of epochs.
lr : float, default 1e-2
The learning rate.
mu : float, default 0.01
Coefficient for the proximal term.
vr : bool, default False
Whether to use variance reduction.
**kwargs : dict, optional
Additional keyword arguments:
"""
__name__ = "CustomClientConfig"
def __init__(
self,
batch_size: int,
num_epochs: int,
lr: float = 1e-2,
mu: float = 0.01,
vr: bool = False,
**kwargs: Any,
) -> None:
name = self.__name__.replace("ClientConfig", "")
if kwargs.pop("algorithm", None) is not None:
warnings.warn(
f"The `algorithm` argument is fixed to `{name}` and will be ignored.",
RuntimeWarning,
)
optimizer = "FedProx" if not vr else "FedProx_VR"
if kwargs.pop("optimizer", None) is not None:
warnings.warn(
"The `optimizer` argument is fixed to `FedProx` or `FedProx_VR` and will be ignored.",
RuntimeWarning,
)
super().__init__(
name,
optimizer,
batch_size,
num_epochs,
lr,
mu=mu,
vr=vr,
**kwargs,
)
@register_algorithm()
@add_docstring(
Server.__doc__.replace(
"The class to simulate the server node.",
"Server node for the Custom algorithm.",
)
.replace("ServerConfig", "CustomServerConfig")
.replace("ClientConfig", "CustomClientConfig")
)
class CustomServer(Server):
"""Server node for the Custom algorithm."""
__name__ = "CustomServer"
def _post_init(self) -> None:
"""
check if all required field in the config are set,
and check compatibility of server and client configs
"""
super()._post_init()
assert self.config.vr == self._client_config.vr
@property
def client_cls(self) -> type:
return CustomClient
@property
def required_config_fields(self) -> List[str]:
return []
def communicate(self, target: "CustomClient") -> None:
target._received_messages = {"parameters": self.get_detached_model_parameters()}
if target.config.vr:
target._received_messages["gradients"] = [
p.grad.detach().clone() if p.grad is not None else torch.zeros_like(p) for p in target.model.parameters()
]
def update(self) -> None:
# sum of received parameters, with self.model.parameters() as its container
self.avg_parameters()
if self.config.vr:
self.update_gradients()
@property
def config_cls(self) -> Dict[str, type]:
return {
"server": CustomServerConfig,
"client": CustomClientConfig,
}
@property
def doi(self) -> List[str]:
return ["10.48550/ARXIV.1812.06127"]
@register_algorithm()
@add_docstring(
Client.__doc__.replace(
"The class to simulate the client node.",
"Client node for the Custom algorithm.",
).replace("ClientConfig", "CustomClientConfig")
)
class CustomClient(Client):
"""Client node for the Custom algorithm."""
__name__ = "CustomClient"
def _post_init(self) -> None:
"""
check if all required field in the config are set,
and set attributes for maintaining itermidiate states
"""
super()._post_init()
if self.config.vr:
self._gradient_buffer = [torch.zeros_like(p) for p in self.model.parameters()]
else:
self._gradient_buffer = None
@property
def required_config_fields(self) -> List[str]:
return ["mu"]
def communicate(self, target: "CustomServer") -> None:
message = {
"client_id": self.client_id,
"parameters": self.get_detached_model_parameters(),
"train_samples": len(self.train_loader.dataset),
"metrics": self._metrics,
}
if self.config.vr:
message["gradients"] = [p.grad.detach().clone() for p in self.model.parameters()]
target._received_messages.append(ClientMessage(**message))
def update(self) -> None:
try:
self._cached_parameters = deepcopy(self._received_messages["parameters"])
except KeyError:
warnings.warn(
"No parameters received from server. " "Using current model parameters as initial parameters.",
RuntimeWarning,
)
self._cached_parameters = self.get_detached_model_parameters()
except Exception as err:
raise err
self._cached_parameters = [p.to(self.device) for p in self._cached_parameters]
if self.config.vr and self._received_messages.get("gradients", None) is not None:
self._gradient_buffer = [gd.clone().to(self.device) for gd in self._received_messages["gradients"]]
self.solve_inner() # alias of self.train()
def train(self) -> None:
self.model.train()
with tqdm(
range(self.config.num_epochs),
total=self.config.num_epochs,
mininterval=1.0,
disable=self.config.verbose < 2,
leave=False,
) as pbar:
for epoch in pbar: # local update
self.model.train()
for X, y in self.train_loader:
X, y = X.to(self.device), y.to(self.device)
self.optimizer.zero_grad()
output = self.model(X)
loss = self.criterion(output, y)
loss.backward()
self.optimizer.step(
local_weights=self._cached_parameters,
variance_buffer=self._gradient_buffer,
)
# free memory
del X, y, output, loss
self.lr_scheduler.step()