-
Notifications
You must be signed in to change notification settings - Fork 13
/
federated.py
149 lines (124 loc) · 5.28 KB
/
federated.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
import threading
import datetime
import torch
import time
import numpy as np
from BResidual import BResidual
from optimiser import SGD
from util import sd_matrixing, PiecewiseLinear, trainable_params, StatsLogger
class Cifar10FedEngine:
def __init__(self, args, dataloader, global_param, server_param, local_param,
outputs, cid, tid, mode, server_state, client_states):
self.args = args
self.dataloader = dataloader
self.global_param = global_param
self.server_param = server_param
self.local_param = local_param
self.server_state = server_state
self.client_state = client_states
self.client_id = cid
self.outputs = outputs
self.thread = tid
self.mode = mode
self.model = self.prepare_model()
# self.threadLock = threading.Lock()
self.m1, self.m2, self.m3, self.reg1, self.reg2 = None, None, None, None, None
def prepare_model(self):
if self.args.dataset == "cifar10":
model = BResidual(3)
elif self.args.dataset == "mnist":
model = BResidual(1)
else:
print("Unknown model type ... ")
model = None
model.set_state(self.global_param, self.local_param)
return model
def run(self):
self.model.to(self.args.device)
output = self.client_run()
self.free_memory()
return output
def client_run(self):
lr_schedule = PiecewiseLinear([0, 5, self.args.client_epochs], [0, 0.4, 0.001])
lr = lambda step: lr_schedule(step / len(self.dataloader)) / self.args.batch_size
opt = SGD(trainable_params(self.model), lr=lr, momentum=0.9, weight_decay=5e-4
* self.args.batch_size, nesterov=True)
mean_loss = []
mean_acc = []
t1 = time.time()
c_state = None
if self.mode == "Train":
# training process
for epoch in range(self.args.client_epochs):
stats = self.batch_run(True, opt.step)
mean_loss.append(stats.mean('loss'))
mean_acc.append(stats.mean('correct'))
# log = "Train - Epoch: " + str(epoch) + ' train loss: ' + str(stats.mean('loss')) +\
# ' train acc: ' + str(stats.mean('correct'))
# self.logger(log, True)
elif self.mode == "Test":
# validation process
stats = self.batch_run(False)
mean_loss.append(stats.mean('loss'))
mean_acc.append(stats.mean('correct'))
# log = 'Test - test loss: ' + str(stats.mean('loss')) + ' test acc: ' \
# + str(stats.mean('correct'))
# self.logger(log)
time_cost = time.time() - t1
log = self.mode + ' - Thread: {:03d}, Client: {:03d}. Average Loss: {:.4f},' \
' Average Accuracy: {:.4f}, Total Time Cost: {:.4f}'
self.logger(log.format(self.thread, self.client_id, np.mean(mean_loss), np.mean(mean_acc),
time_cost), True)
self.model.to("cpu")
output = {"params": self.model.get_state(),
"time": time_cost,
"loss": np.mean(mean_loss),
"acc": np.mean(mean_acc),
"client_state": self.client_state,
"c_state": c_state}
# self.outputs[self.thread] = output
return output
def batch_run(self, training, optimizer_step=None, stats=None):
stats = stats or StatsLogger(('loss', 'correct'))
self.model.train(training)
for batch in self.dataloader:
output = self.model(batch)
output['loss'] = self.criterion(output['loss'], self.mode)
stats.append(output)
if training:
output['loss'].sum().backward()
optimizer_step()
self.model.zero_grad()
batch["input"].to("cpu")
batch["target"].to("cpu")
return stats
def criterion(self, loss, mode):
if self.args.agg == "avg":
pass
elif self.args.reg > 0 and mode != "PerTrain" and self.args.clients != 1:
self.m1 = sd_matrixing(self.model.get_state()[0]).reshape(1, -1).to(self.args.device)
self.m2 = sd_matrixing(self.server_param).reshape(1, -1).to(self.args.device)
self.m3 = sd_matrixing(self.global_param).reshape(1, -1).to(self.args.device)
self.reg1 = torch.nn.functional.pairwise_distance(self.m1, self.m2, p=2)
self.reg2 = torch.nn.functional.pairwise_distance(self.m1, self.m3, p=2)
loss = loss + 0.3 * self.reg1 + 0.3 * self.reg2
return loss
def free_memory(self):
if self.m1 is not None:
self.m1.to("cpu")
if self.m2 is not None:
self.m2.to("cpu")
if self.m3 is not None:
self.m3.to("cpu")
if self.reg1 is not None:
self.reg1.to("cpu")
if self.reg2 is not None:
self.reg2.to("cpu")
torch.cuda.empty_cache()
def logger(self, buf, p=False):
if p:
print(buf)
# self.threadLock.acquire()
with open(self.args.logDir, 'a+') as f:
f.write(str(datetime.datetime.now()) + '\t' + buf + '\n')
# self.threadLock.release()