-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfl_main.py
389 lines (322 loc) · 13.5 KB
/
fl_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
import copy
import os
import time
import numpy as np
import torch
from algorithm.algorithm_factory import AlgorithmFactory
from args import args_parser
from client import Client
from communication.air_comp import AirComp
from communication.channel import Channel
from communication.optimize import Gibbs
from dataset import DatasetObject
from model import Model
from server import Server
from utils import evaluate_performance, get_model_params, save_performance
def main():
# get all the constant parameters
args = args_parser()
############################################################################################################
# data for train and test
# data_obj = ShakespeareObjectCrop_noniid(storage_path, dataset_prefix)
unbalanced_sgm = 0
if args.rule == "dirichlet":
unbalanced_sgm = 1.0
data_obj = DatasetObject(
dataset=args.model_name,
n_client=args.n_clients,
unbalanced_sgm=unbalanced_sgm,
rule=args.rule,
rule_arg=0.6,
)
# data_obj = DatasetObject(dataset="CIFAR10", n_client=args.n_clients, rule="iid", unbalanced_sgm=0)
client_x_all = data_obj.clnt_x
client_y_all = data_obj.clnt_y
cent_x = np.concatenate(client_x_all, axis=0)
cent_y = np.concatenate(client_y_all, axis=0)
############################################################################################################
# the weight corresponds to the number of data that the client i has
# the more data the client has, the larger the weight is
weight_list = np.asarray([len(client_y_all[i]) for i in range(args.n_clients)])
# FedDyn and SCAFFOLD initialization
if args.algorithm_name == "FedDyn" or args.algorithm_name == "SCAFFOLD":
weight_list = weight_list / np.sum(weight_list) * args.n_clients
# global parameters
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_func = lambda: Model(args.model_name)
init_model = model_func()
init_par_list = get_model_params([init_model])[
0
] # parameters of the iargs.nitial model
n_param = len(init_par_list)
np.random.seed(args.rand_seed)
############################################################################################################
# Channel setup
# User-BS Path loss exponent
fc = 915 * 10**6 # carrier frequency, wavelength lambda=3.0*10**8/fc
BS_Gain = 10 ** (5.0 / 10) # BS antenna gain
RIS_Gain = 10 ** (5.0 / 10) # RIS antenna gain
User_Gain = 10 ** (0.0 / 10) # User antenna gain
dimen_RIS = 1.0 / 10 # dimension length of RIS element/wavelength
BS = np.array([-50, 0, 10]) # Cartesian coordinate of BS
RIS = np.array([0, 0, 10]) # Cartesian coordinate of RIS
#
x0 = np.ones([args.n_clients], dtype=int)
#
channel = Channel(
SNR=args.SNR,
n_clients=args.n_clients,
location_range=args.location_range,
fc=fc,
alpha_direct=args.alpha_direct,
n_RIS_ele=args.n_RIS_ele,
n_receive_ant=args.n_receive_ant,
User_Gain=User_Gain,
x0=x0,
BS=BS,
BS_Gain=BS_Gain,
RIS=RIS,
RIS_Gain=RIS_Gain,
dimen_RIS=dimen_RIS,
)
gibbs = Gibbs(
n_clients=args.n_clients,
n_receive_ant=args.n_receive_ant,
n_RIS_ele=args.n_RIS_ele,
Jmax=args.Jmax,
weight_list=weight_list,
tau=args.tau,
nit=args.nit,
threshold=args.threshold,
)
air_comp = AirComp(
n_receive_ant=args.n_receive_ant,
weight_list=weight_list,
transmit_power=args.transmit_power,
)
############################################################################################################
# create the algorihtm object
args.data_obj = data_obj
args.n_param = n_param
args.air_comp = air_comp
args.model_func = model_func
args.init_model = init_model
algorithm_factory = AlgorithmFactory(args)
algorithm = algorithm_factory.create_algorithm(args.algorithm_name)
clients_list = np.array(
[
Client(
algorithm=algorithm,
device=device,
weight=weight_list[i],
train_data_X=client_x_all[i],
train_data_Y=client_y_all[i],
model=init_model,
client_param=np.copy(init_par_list),
)
for i in range(args.n_clients)
]
)
############################################################################################################
if not os.path.exists(
"Output/%s/%s_init_mdl.pt" % (data_obj.name, args.model_name)
):
print("New directory!")
os.mkdir("Output/%s/" % (data_obj.name))
torch.save(
init_model.state_dict(),
"Output/%s/%s_init_mdl.pt" % (data_obj.name, args.model_name),
)
else:
# Load model
init_model.load_state_dict(
torch.load("Output/%s/%s_init_mdl.pt" % (data_obj.name, args.model_name))
)
############################################################################################################
# Model Initialization (feel free to add new parameters in this section if using this frameworks)
# For FedDyn
if args.algorithm_name == "FedDyn":
local_param_list = np.zeros((args.n_clients, n_param)).astype("float32")
# cloud (server) model (for FedDyn)
cloud_model = model_func().to(device)
cloud_model.load_state_dict(copy.deepcopy(dict(init_model.named_parameters())))
cloud_model_param = get_model_params([cloud_model], n_param)[0]
# For SCAFFOLD
elif args.algorithm_name == "SCAFFOLD":
state_param_list = np.zeros((args.n_clients + 1, n_param)).astype(
"float32"
) # including cloud state
trn_perf_sel = np.zeros((args.comm_rounds, 2))
trn_perf_all = np.zeros((args.comm_rounds, 2))
tst_perf_sel = np.zeros((args.comm_rounds, 2))
tst_perf_all = np.zeros((args.comm_rounds, 2))
# average model
avg_model = model_func().to(device)
avg_model.load_state_dict(copy.deepcopy(dict(init_model.named_parameters())))
# all clients model
all_model = model_func().to(device)
all_model.load_state_dict(copy.deepcopy(dict(init_model.named_parameters())))
server = Server(avg_model, all_model, device, algorithm)
############################################################################################################
# Start Communicatoin
print()
print("=" * 80)
print("Device: %s" % device)
print("Model: %s" % args.model_name)
print("Epochs: %d" % args.epoch)
print("Number of clients: %d" % args.n_clients)
print("Number of communication rounds: %d" % args.comm_rounds)
print("Data partition: %s" % args.rule)
print("Training starts with algorithm: %s\n" % algorithm.name)
print("The system is {}".format("noiseless" if args.noiseless else "noisy"))
print("=" * 80, end="\n\n")
for t in range(args.comm_rounds):
print("This is the {0}-th trial".format(t + 1))
############################################################################################################
if not args.noiseless:
print("\nRunning Gibbs Optimization...\n")
# generate the channel
h_d, G, x, sigma = channel.generate()
start = time.time()
# get the optimized parameters based on RIS-FL
x_optim, f_optim, h_optim = gibbs.optimize(h_d, G, x, sigma)
end = time.time()
print(
"Running time of Gibbs Optimization: {} seconds\n".format(end - start)
)
else:
print("\nRunning Random Selection...\n")
inc_seed = 0
x_optim = np.array([0])
while np.sum(x_optim) == 0:
# Fix randomness in client selection
np.random.seed(t + args.rand_seed + inc_seed)
active_clients = np.random.uniform(size=args.n_clients)
x_optim = active_clients <= args.act_prob
inc_seed += 1
x_optim = x_optim.astype(np.int8)
############################################################################################################
# get the selected clients
selected_clnts_idx = np.where(x_optim == 1)[
0
] # get the index of the selected clients
selected_clnts = clients_list[selected_clnts_idx]
print("Selected Clients Index: {}".format(x_optim))
print(
"Selected Clients: %s\n"
% (", ".join(["%2d" % clnt for clnt in selected_clnts_idx]))
)
############################################################################################################
# set up the required parameters for the algorithms
# feel free to add new parameters for your added algorithm
# FedDyn
if args.algorithm_name == "FedDyn":
cloud_model_param_tensor = torch.tensor(
cloud_model_param, dtype=torch.float32, device=device
)
# FedProx
elif args.algorithm_name == "FedProx":
avg_model_param = get_model_params([server.avg_model], n_param)[0]
avg_model_param_tensor = torch.tensor(
avg_model_param, dtype=torch.float32, device=device
)
# SCAFFOLD
elif args.algorithm_name == "SCAFFOLD":
delta_c_sum = np.zeros(n_param)
prev_params = get_model_params([server.avg_model], n_param)[0]
############################################################################################################
# clients training
for i, client in enumerate(selected_clnts):
# Train locally
print("---- Training client %d" % selected_clnts_idx[i])
inputs = {
"curr_round": t,
"avg_model": server.avg_model,
}
if args.algorithm_name == "FedDyn":
inputs["cloud_model"] = cloud_model
inputs["cloud_model_param"] = cloud_model_param
inputs["cloud_model_param_tensor"] = cloud_model_param_tensor
inputs["local_param"] = local_param_list[selected_clnts_idx[i]]
elif args.algorithm_name == "FedProx":
inputs["avg_model_param_tensor"] = avg_model_param_tensor
elif args.algorithm_name == "SCAFFOLD":
inputs["state_param"] = state_param_list[selected_clnts_idx[i]]
inputs["general_state_param"] = state_param_list[-1]
inputs["prev_params"] = prev_params
inputs["delta_c_sum"] = delta_c_sum
client.local_train(inputs)
# update the parameters (For FedDyn)
if args.algorithm_name == "FedDyn":
local_param_list[selected_clnts_idx[i]] = inputs["local_param"]
# update the parameters (For SCAFFOLD)
elif args.algorithm_name == "SCAFFOLD":
delta_c_sum = inputs["delta_c_sum"]
state_param_list[selected_clnts_idx[i]] = inputs["state_param"]
############################################################################################################
# aggregation
inputs = {
"clients_list": clients_list,
"selected_clnts_idx": selected_clnts_idx,
"weight_list": weight_list,
}
if args.algorithm_name == "FedDyn":
inputs["local_param_list"] = local_param_list
inputs["cloud_model"] = cloud_model
inputs["cloud_model_param"] = cloud_model_param
elif args.algorithm_name == "SCAFFOLD":
inputs["prev_params"] = prev_params
inputs["delta_c_sum"] = delta_c_sum
inputs["general_state_param"] = state_param_list[-1]
# pass the AirComp optimization parameters
if not args.noiseless:
print("\nStart AirComp Transmission")
clients_param_list = np.array(
[client.client_param for client in clients_list]
)
inputs["avg_mdl_param"] = air_comp.transmit(
n_param,
clients_param_list[selected_clnts_idx],
x_optim,
f_optim,
h_optim,
sigma,
)
server.aggregate(inputs)
# For FedDyn
if args.algorithm_name == "FedDyn":
cloud_model = inputs["cloud_model"]
cloud_model_param = inputs["cloud_model_param"]
# For SCAFFOLD
elif args.algorithm_name == "SCAFFOLD":
state_param_list[-1] = inputs["general_state_param"]
# get the test accuracy
evaluate_performance(
cent_x,
cent_y,
data_obj.tst_x,
data_obj.tst_y,
data_obj.dataset,
server.avg_model,
server.all_model,
device,
tst_perf_sel,
trn_perf_sel,
tst_perf_all,
trn_perf_all,
t,
)
############################################################################################################
save_performance(
args.comm_rounds,
tst_perf_all,
algorithm.name,
data_obj.name,
args.model_name,
args.n_clients,
args.noiseless,
args.rule,
)
############################################################################################################
if __name__ == "__main__":
main()