forked from TsingZ0/PFLlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
487 lines (414 loc) · 20.3 KB
/
main.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# PFLlib: Personalized Federated Learning Algorithm Library
# Copyright (C) 2021 Jianqing Zhang
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#!/usr/bin/env python
import copy
# import imp
import torch
import argparse
import os
import time
import warnings
import numpy as np
import torchvision
import logging
import sys
sys.path.append('../')
sys.path.append('system/')
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from flcore.trainmodel.utils import get_model
from dataset.generate_cifar10 import generate_cifar10
from dataset.generate_cifar100 import generate_cifar100
from dataset.generate_mnist import generate_mnist
from flcore.servers.serveravg import FedAvg
from flcore.servers.serverpFedMe import pFedMe
from flcore.servers.serverperavg import PerAvg
from flcore.servers.serverprox import FedProx
from flcore.servers.serverfomo import FedFomo
from flcore.servers.serveramp import FedAMP
from flcore.servers.servermtl import FedMTL
from flcore.servers.serverlocal import Local
from flcore.servers.serverper import FedPer
from flcore.servers.serverapfl import APFL
from flcore.servers.serverditto import Ditto
from flcore.servers.serverrep import FedRep
from flcore.servers.serverphp import FedPHP
from flcore.servers.serverbn import FedBN
from flcore.servers.serverrod import FedROD
from flcore.servers.serverproto import FedProto
from flcore.servers.serverdyn import FedDyn
from flcore.servers.servermoon import MOON
from flcore.servers.serverbabu import FedBABU
from flcore.servers.serverapple import APPLE
from flcore.servers.servergen import FedGen
from flcore.servers.serverscaffold import SCAFFOLD
from flcore.servers.serverdistill import FedDistill
from flcore.servers.serverala import FedALA
from flcore.servers.serverpac import FedPAC
from flcore.servers.serverlg import LG_FedAvg
from flcore.servers.servergc import FedGC
from flcore.servers.serverfml import FML
from flcore.servers.serverkd import FedKD
from flcore.servers.serverpcl import FedPCL
from flcore.servers.servercp import FedCP
from flcore.servers.servergpfl import GPFL
from flcore.servers.serverntd import FedNTD
from flcore.servers.servergh import FedGH
from flcore.servers.serveravgDBE import FedAvgDBE
from flcore.servers.serverpFedCon import pFedCon
from flcore.servers.serverpFedConSingle import pFedConSingle
from flcore.trainmodel.models import *
from flcore.trainmodel.bilstm import *
from flcore.trainmodel.resnet import *
from flcore.trainmodel.alexnet import *
from flcore.trainmodel.mobilenet_v2 import *
from flcore.trainmodel.transformer import *
from utils.result_utils import average_data
from utils.mem_utils import MemReporter
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
warnings.simplefilter("ignore")
torch.manual_seed(0)
def run(args, dataset_path = None):
time_list = []
reporter = MemReporter()
model_str = args.model
for i in range(args.prev, args.times):
print(f"============= Running time: {i}th =============\n")
print("Creating server and clients ...")
start = time.time()
# Generate args.model
args.model = get_model(args)
# dataset path
print(args.model)
if dataset_path:
args.dataset = dataset_path
# select algorithm
if args.algorithm == "FedAvg":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedAvg(args, i)
elif args.algorithm == "Local":
server = Local(args, i)
elif args.algorithm == "FedMTL":
server = FedMTL(args, i)
elif args.algorithm == "PerAvg":
server = PerAvg(args, i)
elif args.algorithm == "pFedMe":
server = pFedMe(args, i)
elif args.algorithm == "FedProx":
server = FedProx(args, i)
elif args.algorithm == "FedFomo":
server = FedFomo(args, i)
elif args.algorithm == "FedAMP":
server = FedAMP(args, i)
elif args.algorithm == "APFL":
server = APFL(args, i)
elif args.algorithm == "FedPer":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedPer(args, i)
elif args.algorithm == "Ditto":
server = Ditto(args, i)
elif args.algorithm == "FedRep":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedRep(args, i)
elif args.algorithm == "FedPHP":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedPHP(args, i)
elif args.algorithm == "FedBN":
server = FedBN(args, i)
elif args.algorithm == "FedROD":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedROD(args, i)
elif args.algorithm == "FedProto":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedProto(args, i)
elif args.algorithm == "FedDyn":
server = FedDyn(args, i)
elif args.algorithm == "MOON":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = MOON(args, i)
elif args.algorithm == "FedBABU":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedBABU(args, i)
elif args.algorithm == "APPLE":
server = APPLE(args, i)
elif args.algorithm == "FedGen":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedGen(args, i)
elif args.algorithm == "SCAFFOLD":
server = SCAFFOLD(args, i)
elif args.algorithm == "FedDistill":
server = FedDistill(args, i)
elif args.algorithm == "FedALA":
server = FedALA(args, i)
elif args.algorithm == "FedPAC":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedPAC(args, i)
elif args.algorithm == "LG-FedAvg":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = LG_FedAvg(args, i)
elif args.algorithm == "FedGC":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedGC(args, i)
elif args.algorithm == "FML":
server = FML(args, i)
elif args.algorithm == "FedKD":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedKD(args, i)
elif args.algorithm == "FedPCL":
args.model.fc = nn.Identity()
server = FedPCL(args, i)
elif args.algorithm == "FedCP":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedCP(args, i)
elif args.algorithm == "GPFL":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = GPFL(args, i)
elif args.algorithm == "FedNTD":
server = FedNTD(args, i)
elif args.algorithm == "FedGH":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedGH(args, i)
elif args.algorithm == "FedAvgDBE":
args.head = copy.deepcopy(args.model.fc)
args.model.fc = nn.Identity()
args.model = BaseHeadSplit(args.model, args.head)
server = FedAvgDBE(args, i)
elif args.algorithm == "pFedCon":
server = pFedCon(args, i)
elif args.algorithm == "pFedConSingle":
server = pFedConSingle(args, i)
else:
raise NotImplementedError
server.train()
time_list.append(time.time()-start)
print(f"\nAverage time cost: {round(np.average(time_list), 2)}s.")
# Global average
average_data(dataset=args.dataset, algorithm=args.algorithm, goal=args.goal, times=args.times)
print("All done!")
reporter.report()
if __name__ == "__main__":
total_start = time.time()
parser = argparse.ArgumentParser()
# general
parser.add_argument('-go', "--goal", type=str, default="test",
help="The goal for this experiment")
parser.add_argument('-dev', "--device", type=str, default="cuda",
choices=["cpu", "cuda"])
parser.add_argument('-did', "--device_id", type=str, default="0")
parser.add_argument('-data', "--dataset", type=str, default="mnist")
parser.add_argument('-nb', "--num_classes", type=int, default=10)
parser.add_argument('-m', "--model", type=str, default="cnn")
parser.add_argument('-lbs', "--batch_size", type=int, default=10)
parser.add_argument('-lr', "--local_learning_rate", type=float, default=0.005,
help="Local learning rate")
parser.add_argument('-ld', "--learning_rate_decay", type=bool, default=False)
parser.add_argument('-ldg', "--learning_rate_decay_gamma", type=float, default=0.99)
parser.add_argument('-gr', "--global_rounds", type=int, default=2000)
parser.add_argument('-ls', "--local_epochs", type=int, default=1,
help="Multiple update steps in one local epoch.")
parser.add_argument('-algo', "--algorithm", type=str, default="FedAvg")
parser.add_argument('-jr', "--join_ratio", type=float, default=1.0,
help="Ratio of clients per round")
parser.add_argument('-rjr', "--random_join_ratio", type=bool, default=False,
help="Random ratio of clients per round")
parser.add_argument('-nc', "--num_clients", type=int, default=20,
help="Total number of clients")
parser.add_argument('-pv', "--prev", type=int, default=0,
help="Previous Running times")
parser.add_argument('-t', "--times", type=int, default=1,
help="Running times")
parser.add_argument('-eg', "--eval_gap", type=int, default=1,
help="Rounds gap for evaluation")
parser.add_argument('-dp', "--privacy", type=bool, default=False,
help="differential privacy")
parser.add_argument('-dps', "--dp_sigma", type=float, default=0.0)
parser.add_argument('-sfn', "--save_folder_name", type=str, default='items')
parser.add_argument('-ab', "--auto_break", type=bool, default=False)
parser.add_argument('-dlg', "--dlg_eval", type=bool, default=False)
parser.add_argument('-dlgg', "--dlg_gap", type=int, default=100)
parser.add_argument('-bnpc', "--batch_num_per_client", type=int, default=2)
parser.add_argument('-nnc', "--num_new_clients", type=int, default=0)
parser.add_argument('-ften', "--fine_tuning_epoch_new", type=int, default=0)
# practical
parser.add_argument('-cdr', "--client_drop_rate", type=float, default=0.0,
help="Rate for clients that train but drop out")
parser.add_argument('-tsr', "--train_slow_rate", type=float, default=0.0,
help="The rate for slow clients when training locally")
parser.add_argument('-ssr', "--send_slow_rate", type=float, default=0.0,
help="The rate for slow clients when sending global model")
parser.add_argument('-ts', "--time_select", type=bool, default=False,
help="Whether to group and select clients at each round according to time cost")
parser.add_argument('-tth', "--time_threthold", type=float, default=10000,
help="The threthold for droping slow clients")
# pFedMe / PerAvg / FedProx / FedAMP / FedPHP / GPFL
parser.add_argument('-bt', "--beta", type=float, default=0.0)
parser.add_argument('-lam', "--lamda", type=float, default=1.0,
help="Regularization weight")
parser.add_argument('-mu', "--mu", type=float, default=0.0)
parser.add_argument('-K', "--K", type=int, default=5,
help="Number of personalized training steps for pFedMe")
parser.add_argument('-lrp', "--p_learning_rate", type=float, default=0.01,
help="personalized learning rate to caculate theta aproximately using K steps")
# FedFomo
parser.add_argument('-M', "--M", type=int, default=5,
help="Server only sends M client models to one client at each round")
# FedMTL
parser.add_argument('-itk', "--itk", type=int, default=4000,
help="The iterations for solving quadratic subproblems")
# FedAMP
parser.add_argument('-alk', "--alphaK", type=float, default=1.0,
help="lambda/sqrt(GLOABL-ITRATION) according to the paper")
parser.add_argument('-sg', "--sigma", type=float, default=1.0)
# APFL
parser.add_argument('-al', "--alpha", type=float, default=1.0)
# Ditto / FedRep
parser.add_argument('-pls', "--plocal_epochs", type=int, default=1)
# MOON
parser.add_argument('-tau', "--tau", type=float, default=1.0)
# FedBABU
parser.add_argument('-fte', "--fine_tuning_epochs", type=int, default=10)
# APPLE
parser.add_argument('-dlr', "--dr_learning_rate", type=float, default=0.0)
parser.add_argument('-L', "--L", type=float, default=1.0)
# FedGen
parser.add_argument('-nd', "--noise_dim", type=int, default=512)
parser.add_argument('-glr', "--generator_learning_rate", type=float, default=0.005)
parser.add_argument('-hd', "--hidden_dim", type=int, default=512)
parser.add_argument('-se', "--server_epochs", type=int, default=1000)
parser.add_argument('-lf', "--localize_feature_extractor", type=bool, default=False)
# SCAFFOLD / FedGH
parser.add_argument('-slr', "--server_learning_rate", type=float, default=1.0)
# FedALA
parser.add_argument('-et', "--eta", type=float, default=1.0)
parser.add_argument('-s', "--rand_percent", type=int, default=80)
parser.add_argument('-p', "--layer_idx", type=int, default=2,
help="More fine-graind than its original paper.")
# FedKD
parser.add_argument('-mlr', "--mentee_learning_rate", type=float, default=0.005)
parser.add_argument('-Ts', "--T_start", type=float, default=0.95)
parser.add_argument('-Te', "--T_end", type=float, default=0.98)
# FedAvgDBE
parser.add_argument('-mo', "--momentum", type=float, default=0.1)
parser.add_argument('-klw', "--kl_weight", type=float, default=0.0)
#data
parser.add_argument('--niid', type=bool, default=True)
parser.add_argument('--balance', type=bool, default=True)
parser.add_argument('--partition', type=str, default="dir")
parser.add_argument('--niid_alpha', type=float, default=0.1)
parser.add_argument('--seed', type=int, default=1)
#Con
parser.add_argument('--feature_dim', type=int, default=128)
parser.add_argument('--temperature', type=float, default=0.5)
parser.add_argument('--is_con', type=int, default=0)
# hyper-params for Text tasks
parser.add_argument('--vocab_size', type=int, default=98635)
parser.add_argument('--max_len', type=int, default=200)
parser.add_argument('--emb_dim', type=int, default=32)
args = parser.parse_args()
# os.environ["CUDA_VISIBLE_DEVICES"] = args.device_id
if args.device == "cuda" and not torch.cuda.is_available():
print("\ncuda is not avaiable.\n")
args.device = "cpu"
print("device: ",args.device)
print("=" * 50)
print("Algorithm: {}".format(args.algorithm))
print("Local batch size: {}".format(args.batch_size))
print("Local epochs: {}".format(args.local_epochs))
print("Local learing rate: {}".format(args.local_learning_rate))
print("Local learing rate decay: {}".format(args.learning_rate_decay))
if args.learning_rate_decay:
print("Local learing rate decay gamma: {}".format(args.learning_rate_decay_gamma))
print("Total number of clients: {}".format(args.num_clients))
print("Clients join in each round: {}".format(args.join_ratio))
print("Clients randomly join: {}".format(args.random_join_ratio))
print("Client drop rate: {}".format(args.client_drop_rate))
print("Client select regarding time: {}".format(args.time_select))
if args.time_select:
print("Time threthold: {}".format(args.time_threthold))
print("Running times: {}".format(args.times))
print("Dataset: {}".format(args.dataset))
print("Number of classes: {}".format(args.num_classes))
print("Backbone: {}".format(args.model))
print("Using device: {}".format(args.device))
print("Using DP: {}".format(args.privacy))
if args.privacy:
print("Sigma for DP: {}".format(args.dp_sigma))
print("Auto break: {}".format(args.auto_break))
if not args.auto_break:
print("Global rounds: {}".format(args.global_rounds))
# if args.device == "cuda":
# print("Cuda device id: {}".format(os.environ["CUDA_VISIBLE_DEVICES"]))
print("DLG attack: {}".format(args.dlg_eval))
if args.dlg_eval:
print("DLG attack round gap: {}".format(args.dlg_gap))
print("Total number of new clients: {}".format(args.num_new_clients))
print("Fine tuning epoches on new clients: {}".format(args.fine_tuning_epoch_new))
print("=" * 50)
print(args.balance)
if args.dataset == "mnist" or args.dataset == "fmnist":
train_path, test_path = generate_mnist('data/fmnist/', args.num_clients, args.num_classes, args.niid, args.balance, args.partition, args.niid_alpha, args.seed)
elif args.dataset == "Cifar10" :
train_path, test_path = generate_cifar10('data/Cifar10/', args.num_clients, args.num_classes, args.niid, args.balance, args.partition, args.niid_alpha, args.seed, args.is_con)
elif args.dataset == "Cifar100":
train_path, test_path = generate_cifar100('data/Cifar100/', args.num_clients, args.num_classes, args.niid, args.balance, args.partition, args.niid_alpha, args.seed)
# else:
# generate_synthetic('../dataset/synthetic/', args.num_clients, 10, args.niid)
# with torch.profiler.profile(
# activities=[
# torch.profiler.ProfilerActivity.CPU,
# torch.profiler.ProfilerActivity.CUDA],
# profile_memory=True,
# on_trace_ready=torch.profiler.tensorboard_trace_handler('./log')
# ) as prof:
# with torch.autograd.profiler.profile(profile_memory=True) as prof:
# accelerator.wait_for_everyone()
if train_path:
dataset_path = os.path.abspath(os.path.join(train_path, os.path.pardir))
else: dataset_path = None
run(args, dataset_path)
sys.exit(521)
# print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=20))
# print(f"\nTotal time cost: {round(time.time()-total_start, 2)}s.")