-
Notifications
You must be signed in to change notification settings - Fork 0
/
adHocSL.py
205 lines (141 loc) · 8.51 KB
/
adHocSL.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
import models.ResNet as my_resnet
import datasets.cifar_data as cifar_data
import torch
import torchvision
import torchvision.transforms as transforms
import math
import numpy as np
import torch
import time
from torch.autograd import Function
from torch.optim import Adam
import torch.nn as nn
import copy
class define_training_param:
lr=0.001
epoch_num = 20
batch_size = 32
num_out = 10
class DataOwner:
num_of_samples = 1
def __init__(self, id, model_part_a, model_part_b, model_part_c, training_par):
self.id = id
self.model_part_a = model_part_a
self.model_part_b = model_part_b
self.model_part_c = model_part_c
self.training_par = training_par
# define a different optimizer for each model part
self.optimizer_a = Adam(self.model_part_a.parameters(), lr=training_par.lr) # NOTE: adapt accordingly
self.optimizer_b = Adam(self.model_part_b.parameters(), lr=training_par.lr)
self.optimizer_c = Adam(self.model_part_c.parameters(), lr=training_par.lr)
self.criterion = nn.CrossEntropyLoss() # NOTE: adapt accordingly
class AdHocSL:
data_owners = []
def __init__(self, pointa, pointb, num_dataowners, model_name): # TODO: pass the model definition
self.pointa = pointa
self.pointb = pointb
self.training_par = define_training_param()
self.g_model_part_a = my_resnet.get_resnet18(self.training_par.num_out, -1, self.pointa)
self.g_model_part_b = my_resnet.get_resnet18(self.training_par.num_out, self.pointa, self.pointb)
self.g_model_part_c = my_resnet.get_resnet18(self.training_par.num_out, self.pointb, -1)
for i in range(num_dataowners):
model_part_a = my_resnet.get_resnet18(self.training_par.num_out, -1, self.pointa)
model_part_b = my_resnet.get_resnet18(self.training_par.num_out, self.pointa, self.pointb)
model_part_c = my_resnet.get_resnet18(self.training_par.num_out, self.pointb, -1)
model_part_a.load_state_dict(copy.deepcopy(self.g_model_part_a.state_dict()))
model_part_b.load_state_dict(copy.deepcopy(self.g_model_part_b.state_dict()))
model_part_c.load_state_dict(copy.deepcopy(self.g_model_part_c.state_dict()))
self.data_owners.append(DataOwner(i, model_part_a, model_part_b, model_part_c, self.training_par))
def local_update(self, d_id, input, label):
self.data_owners[d_id-1].optimizer_a.zero_grad()
self.data_owners[d_id-1].optimizer_b.zero_grad()
self.data_owners[d_id-1].optimizer_c.zero_grad()
# start forward propagation
out_a = self.data_owners[d_id-1].model_part_a(input)
det_out_a = out_a.clone().detach().requires_grad_(True)
out_b = self.data_owners[d_id-1].model_part_b(det_out_a)
det_out_b = out_b.clone().detach().requires_grad_(True)
out_c = self.data_owners[d_id-1].model_part_c(det_out_b)
loss = self.data_owners[d_id-1].criterion(out_c, label) # calculates cross-entropy loss
# start backward propagation
loss.backward()
loss = loss.item()
acc = cifar_data.categorical_accuracy(out_c, label)
acc = acc.item()
self.data_owners[d_id-1].optimizer_c.step()
grad_b = det_out_b.grad.clone().detach()
out_b.backward(grad_b)
self.data_owners[d_id-1].optimizer_b.step()
grad_a = det_out_a.grad.clone().detach()
out_a.backward(grad_a)
self.data_owners[d_id-1].optimizer_a.step()
return (loss, acc)
def adHoc_update(self, source_id, destination_id, input, label):
self.data_owners[source_id-1].optimizer_a.zero_grad()
self.data_owners[source_id-1].optimizer_b.zero_grad()
self.data_owners[source_id-1].optimizer_c.zero_grad()
self.data_owners[destination_id-1].optimizer_a.zero_grad()
self.data_owners[destination_id-1].optimizer_b.zero_grad()
self.data_owners[destination_id-1].optimizer_c.zero_grad()
# start forward propagation
out_a = self.data_owners[source_id-1].model_part_a(input)
det_out_a = out_a.clone().detach().requires_grad_(True)
# TRANSFER
out_b = self.data_owners[destination_id-1].model_part_b(det_out_a)
det_out_b = out_b.clone().detach().requires_grad_(True)
# TRANSFER
out_c = self.data_owners[source_id-1].model_part_c(det_out_b)
loss = self.data_owners[source_id-1].criterion(out_c, label) # calculates cross-entropy loss
# start backward propagation
loss.backward()
loss = loss.item()
acc = cifar_data.categorical_accuracy(out_c, label)
acc = acc.item()
self.data_owners[source_id-1].optimizer_c.step()
grad_b = det_out_b.grad.clone().detach()
# TRANSFER
out_b.backward(grad_b)
self.data_owners[destination_id-1].optimizer_b.step()
grad_a = det_out_a.grad.clone().detach()
# TRANSFER
out_a.backward(grad_a)
self.data_owners[source_id-1].optimizer_a.step()
return (loss, acc)
def aggregate(self):
total_samples = 0
for i in range(len(self.data_owners)):
print(i)
total_samples += self.data_owners[i].num_of_samples
if i == 0: # just copy
k = 0
for key in self.g_model_part_a.state_dict().keys():
self.g_model_part_a.state_dict()[key] = self.data_owners[i].model_part_a.state_dict()[key].clone()
self.g_model_part_a.state_dict()[key] = torch.multiply(self.g_model_part_a.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_a.state_dict()[key] = torch.divide(self.g_model_part_a.state_dict()[key], total_samples)
for key in self.g_model_part_b.state_dict().keys():
self.g_model_part_b.state_dict()[key] = self.data_owners[i].model_part_b.state_dict()[key].clone()
self.g_model_part_b.state_dict()[key] = torch.multiply(self.g_model_part_b.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_b.state_dict()[key] = torch.divide(self.g_model_part_b.state_dict()[key], total_samples)
for key in self.g_model_part_c.state_dict().keys():
self.g_model_part_c.state_dict()[key] = self.data_owners[i].model_part_c.state_dict()[key].clone()
self.g_model_part_c.state_dict()[key] = torch.multiply(self.g_model_part_c.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_c.state_dict()[key] = torch.divide(self.g_model_part_c.state_dict()[key], total_samples)
else:
for key in self.g_model_part_a.state_dict().keys():
self.g_model_part_a.state_dict()[key] += self.data_owners[i].model_part_a.state_dict()[key].clone()
self.g_model_part_a.state_dict()[key] = torch.multiply(self.g_model_part_a.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_a.state_dict()[key] = torch.divide(self.g_model_part_a.state_dict()[key], total_samples)
for key in self.g_model_part_b.state_dict().keys():
self.g_model_part_b.state_dict()[key] += self.data_owners[i].model_part_b.state_dict()[key].clone()
self.g_model_part_b.state_dict()[key] = torch.multiply(self.g_model_part_b.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_b.state_dict()[key] = torch.divide(self.g_model_part_b.state_dict()[key], total_samples)
for key in self.g_model_part_c.state_dict().keys():
self.g_model_part_c.state_dict()[key] += self.data_owners[i].model_part_c.state_dict()[key].clone()
self.g_model_part_c.state_dict()[key] = torch.multiply(self.g_model_part_c.state_dict()[key], self.data_owners[i].num_of_samples)
if (i == len(self.data_owners) - 1):
self.g_model_part_c.state_dict()[key] = torch.divide(self.g_model_part_c.state_dict()[key], total_samples)