-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfedavg.py
375 lines (331 loc) · 14.9 KB
/
fedavg.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import pickle
from argparse import Namespace
from collections import OrderedDict
from copy import deepcopy
from typing import Dict, List, Tuple, Union
from pathlib import Path
import torch
from torch.utils.data import DataLoader, Subset
from torchvision import transforms
PROJECT_DIR = Path(__file__).parent.parent.parent.absolute()
from src.utils.tools import trainable_params, evalutate_model, Logger
from src.utils.models import DecoupledModel
from src.utils.constants import DATA_MEAN, DATA_STD
from data.utils.datasets import DATASETS
class FedAvgClient:
def __init__(
self,
model: DecoupledModel,
args: Namespace,
logger: Logger,
device: torch.device,
):
self.args = args
self.device = device
self.client_id: int = None
# load dataset and clients' data indices
try:
partition_path = PROJECT_DIR / "data" / self.args.dataset / "partition.pkl"
with open(partition_path, "rb") as f:
partition = pickle.load(f)
except:
raise FileNotFoundError(f"Please partition {args.dataset} first.")
self.data_indices: List[List[int]] = partition["data_indices"]
# --------- you can define your own data transformation strategy here ------------
general_data_transform = transforms.Compose(
[
transforms.Normalize(
DATA_MEAN[self.args.dataset], DATA_STD[self.args.dataset]
)
]
if self.args.dataset in DATA_MEAN and self.args.dataset in DATA_STD
else []
)
general_target_transform = transforms.Compose([])
train_data_transform = transforms.Compose([])
train_target_transform = transforms.Compose([])
# --------------------------------------------------------------------------------
self.dataset = DATASETS[self.args.dataset](
root=PROJECT_DIR / "data" / args.dataset,
args=args.dataset_args,
general_data_transform=general_data_transform,
general_target_transform=general_target_transform,
train_data_transform=train_data_transform,
train_target_transform=train_target_transform,
)
self.trainloader: DataLoader = None
self.valloader: DataLoader = None
self.testloader: DataLoader = None
self.trainset: Subset = Subset(self.dataset, indices=[])
self.valset: Subset = Subset(self.dataset, indices=[])
self.testset: Subset = Subset(self.dataset, indices=[])
self.model = model.to(self.device)
self.local_epoch = self.args.local_epoch
self.criterion = torch.nn.CrossEntropyLoss().to(self.device)
self.logger = logger
self.personal_params_dict: Dict[int, Dict[str, torch.Tensor]] = {}
self.personal_params_name: List[str] = []
self.init_personal_params_dict: Dict[str, torch.Tensor] = {
key: param.clone().detach()
for key, param in self.model.state_dict(keep_vars=True).items()
if not param.requires_grad
}
self.opt_state_dict = {}
if self.args.optimizer == "sgd":
self.optimizer = torch.optim.SGD(
params=trainable_params(self.model),
lr=self.args.local_lr,
momentum=self.args.momentum,
weight_decay=self.args.weight_decay,
)
elif self.args.optimizer == "adam":
self.optimizer = torch.optim.Adam(
params=trainable_params(self.model),
lr=self.args.local_lr,
weight_decay=self.args.weight_decay,
)
self.init_opt_state_dict = deepcopy(self.optimizer.state_dict())
def load_dataset(self):
"""This function is for loading data indices for No.`self.client_id` client."""
self.trainset.indices = self.data_indices[self.client_id]["train"]
self.testset.indices = self.data_indices[self.client_id]["test"]
self.valset.indices = self.data_indices[self.client_id]["val"]
self.trainloader = DataLoader(self.trainset, self.args.batch_size, shuffle=True)
self.valloader = DataLoader(self.valset, self.args.batch_size)
self.testloader = DataLoader(self.testset, self.args.batch_size)
def train_and_log(self, verbose=False) -> Dict[str, Dict[str, float]]:
"""This function includes the local training and logging process.
Args:
verbose (bool, optional): Set to `True` for print logging info onto the stdout (Controled by the server by default). Defaults to False.
Returns:
Dict[str, Dict[str, float]]: The logging info, which contains metric stats.
"""
eval_results = {
"before": {
"train": {"loss": 0, "correct": 0, "size": 0},
"val": {"loss": 0, "correct": 0, "size": 0},
"test": {"loss": 0, "correct": 0, "size": 0},
},
"after": {
"train": {"loss": 0, "correct": 0, "size": 0},
"val": {"loss": 0, "correct": 0, "size": 0},
"test": {"loss": 0, "correct": 0, "size": 0},
},
}
eval_results["before"] = self.evaluate()
if self.local_epoch > 0:
self.fit()
self.save_state()
eval_results["after"] = self.evaluate()
if verbose:
colors = {"train": "yellow", "val": "green", "test": "cyan"}
for split, flag, subset in [
["train", self.args.eval_train, self.trainset],
["val", self.args.eval_val, self.valset],
["test", self.args.eval_test, self.testset],
]:
if len(subset) > 0 and flag:
self.logger.log(
"client [{}] [{}]({}) loss: {:.4f} -> {:.4f} accuracy: {:.2f}% -> {:.2f}%".format(
self.client_id,
colors[split],
split,
eval_results["before"][split]["loss"]
/ eval_results["before"][split]["size"],
eval_results["after"][split]["loss"]
/ eval_results["after"][split]["size"],
eval_results["before"][split]["correct"]
/ eval_results["before"][split]["size"]
* 100.0,
eval_results["after"][split]["correct"]
/ eval_results["after"][split]["size"]
* 100.0,
)
)
return eval_results
def set_parameters(self, new_parameters: OrderedDict[str, torch.Tensor]):
"""Load model parameters received from the server.
Args:
new_parameters (OrderedDict[str, torch.Tensor]): Parameters of FL model.
"""
personal_parameters = self.personal_params_dict.get(
self.client_id, self.init_personal_params_dict
)
self.optimizer.load_state_dict(
self.opt_state_dict.get(self.client_id, self.init_opt_state_dict)
)
self.model.load_state_dict(new_parameters, strict=False)
# personal params would overlap the dummy params from new_parameters from the same layers
self.model.load_state_dict(personal_parameters, strict=False)
def save_state(self):
"""Save client model personal parameters and the state of optimizer at the end of local training."""
self.personal_params_dict[self.client_id] = {
key: param.clone().detach()
for key, param in self.model.state_dict(keep_vars=True).items()
if (not param.requires_grad) or (key in self.personal_params_name)
}
self.opt_state_dict[self.client_id] = deepcopy(self.optimizer.state_dict())
def train(
self,
client_id: int,
local_epoch: int,
new_parameters: OrderedDict[str, torch.Tensor],
return_diff=True,
verbose=False,
) -> Tuple[Union[OrderedDict[str, torch.Tensor], List[torch.Tensor]], int, Dict]:
"""
The funtion for including all operations in client local training phase.
If you wanna implement your method, consider to override this funciton.
Args:
client_id (int): The ID of client.
local_epoch (int): The number of epochs for performing local training.
new_parameters (OrderedDict[str, torch.Tensor]): Parameters of FL model.
return_diff (bool, optional):
Set as `True` to send the difference between FL model parameters that before and after training;
Set as `False` to send FL model parameters without any change. Defaults to True.
verbose (bool, optional): Set to `True` for print logging info onto the stdout (Controled by the server by default). Defaults to False.
Returns:
Tuple[Union[OrderedDict[str, torch.Tensor], List[torch.Tensor]], int, Dict]:
[The difference / all trainable parameters, the weight of this client, the evaluation metric stats].
"""
self.client_id = client_id
self.local_epoch = local_epoch
self.load_dataset()
self.set_parameters(new_parameters)
eval_results = self.train_and_log(verbose=verbose)
if return_diff:
delta = OrderedDict()
for (name, p0), p1 in zip(
new_parameters.items(), trainable_params(self.model)
):
delta[name] = p0 - p1
return delta, len(self.trainset), eval_results
else:
return (
trainable_params(self.model, detach=True),
len(self.trainset),
eval_results,
)
def fit(self):
"""
The function for specifying operations in local training phase.
If you wanna implement your method and your method has different local training operations to FedAvg, this method has to be overrided.
"""
self.model.train()
for _ in range(self.local_epoch):
for x, y in self.trainloader:
# When the current batch size is 1, the batchNorm2d modules in the model would raise error.
# So the latent size 1 data batches are discarded.
if len(x) <= 1:
continue
x, y = x.to(self.device), y.to(self.device)
logit = self.model(x)
loss = self.criterion(logit, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
@torch.no_grad()
def evaluate(
self, model: torch.nn.Module = None, force_eval=False
) -> Dict[str, float]:
"""The evaluation function. Would be activated before and after local training if `eval_test = True` or `eval_train = True`.
Args:
model (torch.nn.Module, optional): The target model needed evaluation (set to `None` for using `self.model`). Defaults to None.
force_eval (bool, optional): Set as `True` when the server asking client to evaluate model.
Returns:
Dict[str, float]: The evaluation metric stats.
"""
# disable train data transform while evaluating
self.dataset.enable_train_transform = False
target_model = self.model if model is None else model
target_model.eval()
train_loss, val_loss, test_loss = 0, 0, 0
train_correct, val_correct, test_correct = 0, 0, 0
train_size, val_size, test_size = 0, 0, 0
criterion = torch.nn.CrossEntropyLoss(reduction="sum")
if len(self.testset) > 0 and self.args.eval_test:
test_loss, test_correct, test_size = evalutate_model(
model=target_model,
dataloader=self.testloader,
criterion=criterion,
device=self.device,
)
if len(self.valset) > 0 and (force_eval or self.args.eval_val):
val_loss, val_correct, val_size = evalutate_model(
model=target_model,
dataloader=self.valloader,
criterion=criterion,
device=self.device,
)
if len(self.trainset) > 0 and self.args.eval_train:
train_loss, train_correct, train_size = evalutate_model(
model=target_model,
dataloader=self.trainloader,
criterion=criterion,
device=self.device,
)
self.dataset.enable_train_transform = True
return {
"train": {
"loss": train_loss,
"correct": train_correct,
"size": float(max(1, train_size)),
},
"val": {
"loss": val_loss,
"correct": val_correct,
"size": float(max(1, val_size)),
},
"test": {
"loss": test_loss,
"correct": test_correct,
"size": float(max(1, test_size)),
},
}
def test(
self, client_id: int, new_parameters: OrderedDict[str, torch.Tensor]
) -> Dict[str, Dict[str, float]]:
"""Test function. Only be activated while in FL test round.
Args:
client_id (int): The ID of client.
new_parameters (OrderedDict[str, torch.Tensor]): The FL model parameters.
Returns:
Dict[str, Dict[str, float]]: the evalutaion metrics stats.
"""
self.client_id = client_id
self.load_dataset()
self.set_parameters(new_parameters)
# set `size` as 1 for avoiding NaN.
results = {
"before": {
"train": {"loss": 0, "correct": 0, "size": 1},
"val": {"loss": 0, "correct": 0, "size": 1},
"test": {"loss": 0, "correct": 0, "size": 1},
},
"after": {
"train": {"loss": 0, "correct": 0, "size": 1},
"val": {"loss": 0, "correct": 0, "size": 1},
"test": {"loss": 0, "correct": 0, "size": 1},
},
}
results["before"] = self.evaluate(force_eval=True)
if self.args.finetune_epoch > 0:
self.finetune()
results["after"] = self.evaluate(force_eval=True)
return results
def finetune(self):
"""
The fine-tune function. If your method has different fine-tuning opeation, consider to override this.
This function will only be activated in FL test epoches.
"""
self.model.train()
for _ in range(self.args.finetune_epoch):
for x, y in self.trainloader:
if len(x) <= 1:
continue
x, y = x.to(self.device), y.to(self.device)
logit = self.model(x)
loss = self.criterion(logit, y)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()