-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer_distributed.py
224 lines (171 loc) · 7.29 KB
/
trainer_distributed.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
import time
import copy
import sys
import numpy as np
import torch
from torch.utils.data import DataLoader, random_split
from torch.optim import Adam, SGD
from torchvision import transforms
from dataset import load_caltech101_pretrained, load_caltech256_pretrained, classes_101, classes_256
from classifier import DIM, DATASETS, ClassifierResnetLight, classifier_bias_key, classifier_key
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else "cpu")
LEARNING_RATE = 1e-3
class Worker():
def __init__(self, idx, loader, optimizer, loss_function, device, classes):
self.idx = idx
self.classes = classes
self.worker_model = ClassifierResnetLight(classes).to(device)
self.loader = loader
self.optimizer = optimizer
self.loss_function = loss_function
self.device = device
def load_state_dict(self, state_dict):
self.worker_model.load_state_dict(state_dict)
own_state = self.worker_model.state_dict()
for name, param in state_dict.items():
if name not in own_state:
continue
if isinstance(param, torch.nn.Parameter):
param = param.data
own_state[name].copy_(param)
return own_state
def update_state_dict(self, std_dict):
self.load_state_dict(std_dict)
self.optimizer = SGD(self.worker_model.parameters(), lr=LEARNING_RATE)
def get_state_dict(self):
return self.worker_model.state_dict()
def train(self, epochs, loss=None):
for epoch in range(epochs):
mean_loss = 0.0
start = time.time()
for batch_idx, data in enumerate(self.loader, 0):
inputs, labels = data
self.optimizer.zero_grad()
outputs = self.worker_model(inputs)
loss = self.loss_function(
outputs, labels) + 0.1 * self.worker_model.l2_norm()
loss.backward()
self.optimizer.step()
mean_loss += loss.item()
#print('[Worker {} Train {}, {}] loss: {} took {}'.format(
# self.idx, epoch + 1, batch_idx + 1, mean_loss / (batch_idx + 1),
# time.time() - start))
#sys.stdout.flush()
class Aggregator():
def __init__(self, loaders, workers, optimizer, loss_function, model,
device, classes):
self.model = model
self.classes = classes
self.pretrain_loader = loaders[0]
self.validate_loader = loaders[1]
self.workers = workers
self.optimizer = optimizer
self.loss_function = loss_function
self.device = device
self.model = model.to(self.device)
if device == torch.device('cuda:0'):
self.model.cuda()
def accuracy(self, output, labels):
with torch.no_grad():
output = torch.argmax(output.view(-1, self.classes), -1)
acc = torch.mean(torch.eq(output, labels).float())
return acc.cpu().numpy()
def pretrain(self, epoch, loss=None):
mean_loss = 0.0
mean_acc = 0.0
start = time.time()
for batch_idx, data in enumerate(self.pretrain_loader, 0):
inputs, labels = data
self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.loss_function(outputs,
labels) + 0.1 * self.model.l2_norm()
loss.backward()
self.optimizer.step()
acc = self.accuracy(outputs, labels)
mean_loss += loss.item()
mean_acc += acc
#print('[Pretrain {}, {}] loss: {} acc {} took {}'.format(
# (-1) * (epoch + 1), batch_idx + 1, mean_loss / (batch_idx + 1),
# mean_acc / (batch_idx + 1),
# time.time() - start))
def validate(self, epoch):
mean_loss = 0.0
mean_acc = 0.0
start = time.time()
for batch_idx, data in enumerate(self.validate_loader):
inputs, labels = data
with torch.no_grad():
outputs = self.model(inputs)
loss = self.loss_function(outputs, labels)
acc = self.accuracy(outputs, labels)
mean_loss += loss.item()
mean_acc += acc
print('[Validate {}, {}] loss: {} acc {} took {}'.format(
epoch + 1, batch_idx + 1, mean_loss / (batch_idx + 1),
mean_acc / (batch_idx + 1),
time.time() - start))
sys.stdout.flush()
def train(self, epochs, loss=None):
for epoch in range(epochs):
self.pretrain(epoch, loss)
original_state_dict = self.model.state_dict()
for worker in self.workers:
worker.update_state_dict(original_state_dict)
worker_weights = torch.zeros(
self.model.classifier.weight.data.shape).to(self.device)
worker_bias = torch.zeros(
self.model.classifier.bias.data.shape).to(self.device)
for worker in self.workers:
worker.train(100, loss)
worker_state_dict = worker.get_state_dict()
worker_bias += worker_state_dict[classifier_bias_key]
worker_weights += worker_state_dict[classifier_key]
weights_mean = worker_weights / len(self.workers)
bias_mean = worker_bias / len(self.workers)
self.model.classifier.weight.data = weights_mean
self.model.classifier.bias.data = bias_mean
self.validate(epoch)
def dataset_sizes(total, workers_cnt):
pretrain_size = int(0.05 * total)
validate_size = int(0.1 * total)
worker_total_size = total - pretrain_size - validate_size
per_worker = int(worker_total_size / workers_cnt)
sizes = [pretrain_size, validate_size]
allocated = pretrain_size + validate_size
for w in range(workers_cnt - 1):
allocated += per_worker
sizes.append(per_worker)
sizes.append(total - allocated)
return sizes
def main():
#workers_cnt = WORKERS_CNT
test = 256
for workers_cnt in [10, 50, 100]:
print("Training {} workers for {} testcase".format(workers_cnt, test))
model = ClassifierResnetLight(DATASETS[test]["classes"])
cd = DATASETS[test]["data"]
optimizer = SGD(model.parameters(), lr=LEARNING_RATE)
loss_function = torch.nn.CrossEntropyLoss()
sizes = dataset_sizes(len(cd), workers_cnt)
workers = []
data = random_split(cd, sizes)
aggregator_loaders = [
DataLoader(data[0], batch_size=512, shuffle=True),
DataLoader(data[1], batch_size=512, shuffle=True)
]
for idx in range(workers_cnt):
loader = DataLoader(data[idx + 2], batch_size=512, shuffle=True)
worker = Worker(idx, loader, optimizer, loss_function, DEVICE,
DATASETS[test]["classes"])
workers.append(worker)
aggregator = Aggregator(aggregator_loaders, workers, optimizer,
loss_function, model, DEVICE,
DATASETS[test]["classes"])
start = time.time()
aggregator.train(50)
print("Training with {} workers took {}".format(
workers_cnt,
time.time() - start))
if __name__ == '__main__':
main()