-
Notifications
You must be signed in to change notification settings - Fork 0
/
subchain_bft.py
173 lines (150 loc) · 5.65 KB
/
subchain_bft.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
import torch
import torch.nn as nn
import torch.optim as optim
from dataloader import Dataloader
from task import Task
from bft import State, Node
class Trainer:
def __init__(self, seq, dataloader):
self.task = Task(dataloader, 0, 0)
self.height = 0
self.seq = seq
self.connected = []
def connect(self, owner):
self.connected.append(owner)
def execute(self):
return self.task.train()
def update(self, model, height):
self.task.update(model)
self.height = height
def run(self, batch_amount):
for owner in self.connected:
self.update(owner.task.model, owner.height)
self.task.train_batches = owner.task.train_batches
for batch in range(batch_amount):
# Predict
predicted, Y = self.execute()
# Seedback
owner.receive(predicted, Y, self.height)
class Owner:
def __init__(self, seq, net_size, dataloader):
self.task = Task(dataloader, seq*1000, (seq+1)*1000-1)
self.height = 0
self.optimizer = optim.Adam(self.task.model.parameters(), lr=1e-2)
self.seq = seq
self.connected = []
self.node = Node(seq, net_size)
self.predicts = []
self.labels = []
def connect(self, trainer):
self.connected.append(trainer)
def execute(self, predicted, labels):
return self.task.backpropagation(self.optimizer, predicted, labels)
def update(self, model, height):
self.task.copyfrom(model)
self.height = height
def receive(self, predicted, Y, height):
if self.height == height:
self.predicts.append(predicted)
self.labels.append(Y)
def start(self, chain, connections):
# Build connection
for connection in connections:
self.connect(chain.trainers[connection])
chain.trainers[connection].connect(self)
# Owner synchronize
self.update(chain.model, chain.height)
self.node.model = self.task.model
def run(self, network, a, b, gap):
# Collect from trainers
# Owner BP
model, acc = self.execute(torch.cat(self.predicts, 0), torch.cat(self.labels, 0))
# Models aggregate
models = []
aggr_weights = []
total_weight = 0
for peer in network:
models.append(peer.model)
weight = 1
aggr_weights.append(weight)
total_weight += weight
for idx in range(len(aggr_weights)):
if total_weight == 0:
aggr_weights[idx] = 0
else:
aggr_weights[idx] /= total_weight
aggr_weights[idx] *= 1 - b * gap ** -a
models.append(model)
aggr_weights.append(b * gap ** -a)
print(aggr_weights)
self.aggregate(models, aggr_weights)
# Node update
self.node.model = self.task.model
self.node.model_seq = self.height
self.predicts.clear()
self.labels.clear()
def aggregate(self, models, weights):
self.task.model.aggregate(models, weights)
def evaluate(self):
return self.task.evaluate(self.task.model)
class Subchain:
def __init__(self, owner_size, trainer_size):
dataloader = Dataloader('SST', 40, 50, 25000, (16,32,32))
self.height = 0
self.block = []
self.ledger = []
self.owners = []
self.trainers = []
for i in range(owner_size):
self.owners.append(Owner(i, owner_size, dataloader))
for j in range(trainer_size):
self.trainers.append(Trainer(j, dataloader))
self.model = self.owners[0].task.model
def run(self, connections):
# FL settings
batch_amount = 32
a = 0.5
b = 0.8
# Build network
nodes = []
for owner in self.owners:
nodes.append(owner.node)
for owner in self.owners:
owner.node.peers = nodes
for idx in range(len(self.owners)):
owner = self.owners[idx]
owner.start(self, connections[idx])
# BFT-based Training
while self.height < 200:
heights = []
# Foreach
for idx in range(len(self.owners)):
owner = self.owners[idx]
node = owner.node
# Train
if owner.node.is_primary():
if owner.node.is_locked() and owner.node.model_seq != owner.node.height:
owner.node.release()
if not owner.node.is_locked():
gap = 1 + 1
base_model_seq = 0
if owner.seq < gap:
base_model_seq = owner.seq - gap + len(self.owners)
else:
base_model_seq = owner.seq - gap
owner.update(self.owners[base_model_seq].task.model, self.height)
# Predict
for trainer in self.trainers:
trainer.run(batch_amount)
# BP and aggregate
owner.run(nodes, a, b, gap)
# Evaluate
test_acc = owner.evaluate()
print("height={},node={},测试准确率={}".format(owner.height, idx, test_acc))
# Mark as trained
owner.node.lock()
# Push bft
owner.node.run()
owner.height = owner.node.height
heights.append(owner.height)
self.height = max(heights)