-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
143 lines (106 loc) · 4.02 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
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import numpy as np
import fmnist
from optimizers.DANE import DANE
from optimizers.ADMM import ADMM
from optimizers.GradientAvg import GradientAvg
from utils import consensus_train, evaluate
def train_dane(rank, train_loader, test_loader):
if rank == 0:
print('training with DANE:')
model = fmnist.FashionMNISTNet()
criterion = nn.NLLLoss()
opt = optim.Adam(model.parameters(), lr=3e-4)
dane = DANE(model.parameters(), opt, lr=1, mu=1e-4)
train_hist = consensus_train(model, criterion, dane, train_loader)
if rank == 0:
test_loss, test_acc = evaluate(model, criterion, test_loader)
print('Finally ::\tLoss = {},\tAccuracy = {}'.format(
test_loss, test_acc
))
return train_hist
def train_admm(rank, train_loader, test_loader):
if rank == 0:
print('training with ADMM:')
model = fmnist.FashionMNISTNet()
criterion = nn.NLLLoss()
opt = optim.Adam(model.parameters(), lr=3e-4)
admm = ADMM(model.parameters(), opt, lr=1, rho=0.1)
train_hist = consensus_train(model, criterion, admm, train_loader)
if rank == 0:
test_loss, test_acc = evaluate(model, criterion, test_loader)
print('Finally ::\tLoss = {},\tAccuracy = {}'.format(
test_loss, test_acc
))
return train_hist
def train_oneshot(rank, train_loader, test_loader):
if rank == 0:
print('training with one-shot averaging:')
model = fmnist.FashionMNISTNet()
criterion = nn.NLLLoss()
opt = optim.Adam(model.parameters(), lr=3e-4)
avg = GradientAvg(model.parameters(), opt)
train_hist = consensus_train(model, criterion, avg, train_loader)
if rank == 0:
test_loss, test_acc = evaluate(model, criterion, test_loader)
print('Finally ::\tLoss = {},\tAccuracy = {}'.format(
test_loss, test_acc
))
return train_hist
def run(rank):
torch.manual_seed(1234)
train_loader, test_loader = fmnist.load()
avg_hist = train_oneshot(rank, train_loader, test_loader)
admm_hist = train_admm(rank, train_loader, test_loader)
dane_hist = train_dane(rank, train_loader, test_loader)
if rank == 0:
fig, ax = plt.subplots(1, 3, figsize=(18, 6))
ax[0].plot(dane_hist['loss'], label='DANE')
ax[0].plot(admm_hist['loss'], label='ADMM')
ax[0].plot(avg_hist['loss'], label='gradient averaging')
ax[0].set_xticks(np.arange(1, 10))
ax[0].set_yscale('log')
ax[0].set_xlabel('epoch')
ax[0].set_ylabel('logloss')
ax[0].set_title('Loss')
ax[0].legend()
ax[1].plot(dane_hist['acc'], label='DANE')
ax[1].plot(admm_hist['acc'], label='ADMM')
ax[1].plot(avg_hist['acc'], label='gradient averaging')
ax[1].set_xticks(np.arange(1, 10))
ax[1].set_xlabel('epoch')
ax[1].set_ylabel('accuracy')
ax[1].set_title('Accuracy')
ax[1].legend()
ax[2].plot(dane_hist['interconnect'], label='DANE')
ax[2].plot(admm_hist['interconnect'], label='ADMM')
ax[2].plot(avg_hist['interconnect'], label='gradient averaging')
ax[2].set_xticks(np.arange(1, 10))
ax[2].set_xlabel('epoch')
ax[2].set_ylabel('data transferred, bytes')
ax[2].set_title('Interconnect')
ax[2].legend()
fig.suptitle('Training History')
fig.savefig('results.png')
plt.show()
def init_process(rank, size, fn, backend='gloo'):
os.environ['MASTER_ADDR'] = '127.0.0.1'
os.environ['MASTER_PORT'] = '29500'
dist.init_process_group(backend, rank=rank, world_size=size)
fn(rank)
if __name__ == "__main__":
size = 4
processes = []
mp.set_start_method("spawn")
for rank in range(size):
p = mp.Process(target=init_process, args=(rank, size, run))
p.start()
processes.append(p)
for p in processes:
p.join()