-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytorch.py
140 lines (103 loc) · 6.25 KB
/
mytorch.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class myconv2d(nn.Module):
def __init__(self, num_workers, device, weight_list, bias_list=[], stride=1, padding=0, dilation=1, groups=1):
super(myconv2d,self).__init__()
self.num_workers = num_workers
self.weight_list = weight_list
self.device = device
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.weight_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.weight_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
self.bias_list = bias_list
if bias_list:
self.bias_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.bias_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
def update(self, weight_list, bias_list=[]):
self.weight_list = weight_list
self.bias_list = bias_list
def forward(self, x):
weight_shape = [self.num_workers]
for size in self.weight_list[0].shape:
weight_shape.append(size)
mask1 = [torch.sigmoid(self.weight_mask[idx]) for idx in range(self.num_workers)]
weight_t = torch.div(torch.sum(torch.cat([torch.mul(mask1[idx], self.weight_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(weight_shape), dim=0) \
, torch.sum(torch.cat(mask1, dim=0).reshape(weight_shape), dim=0))
if self.bias_list:
bias_shape = [self.num_workers]
for size in self.bias_list[0].shape:
bias_shape.append(size)
mask2 = [torch.sigmoid(self.bias_mask[idx]) for idx in range(self.num_workers)]
bias_t = torch.div(torch.sum(torch.cat([torch.mul(mask2[idx], self.bias_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(bias_shape), dim=0) \
, torch.sum(torch.cat(mask2, dim=0).reshape(bias_shape), dim=0))
out = F.conv2d(x, weight_t, bias_t, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups)
else:
out = F.conv2d(x, weight_t, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups)
return out
class mylinear(nn.Module):
def __init__(self, num_workers, device, weight_list, bias_list=[]):
super(mylinear,self).__init__()
self.num_workers = num_workers
self.weight_list = weight_list
self.device = device
self.weight_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.weight_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
self.bias_list = bias_list
if bias_list:
self.bias_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.bias_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
def update(self, weight_list, bias_list=[]):
self.weight_list = weight_list
self.bias_list = bias_list
def forward(self, x):
weight_shape = [self.num_workers]
for size in self.weight_list[0].shape:
weight_shape.append(size)
mask1 = [torch.sigmoid(self.weight_mask[idx]) for idx in range(self.num_workers)]
weight_t = torch.div(torch.sum(torch.cat([torch.mul(mask1[idx], self.weight_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(weight_shape), dim=0) \
, torch.sum(torch.cat(mask1, dim=0).reshape(weight_shape), dim=0))
if self.bias_list:
bias_shape = [self.num_workers]
for size in self.bias_list[0].shape:
bias_shape.append(size)
mask2 = [torch.sigmoid(self.bias_mask[idx]) for idx in range(self.num_workers)]
bias_t = torch.div(torch.sum(torch.cat([torch.mul(mask2[idx], self.bias_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(bias_shape), dim=0) \
, torch.sum(torch.cat(mask2, dim=0).reshape(bias_shape), dim=0))
out = F.linear(x, weight_t, bias_t)
else:
out = F.linear(x, weight_t)
return out
class mybatch_norm(nn.Module):
def __init__(self, num_workers, device, weight_list, bias_list=[]):
super(mybatch_norm,self).__init__()
self.num_workers = num_workers
self.weight_list = weight_list
self.device = device
self.weight_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.weight_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
self.bias_list = bias_list
if bias_list:
self.bias_mask = nn.ParameterList([nn.Parameter(torch.ones(size=self.bias_list[0].shape, device=self.device), requires_grad= True) for i in range(self.num_workers)])
def update(self, weight_list, bias_list=[]):
self.weight_list = weight_list
self.bias_list = bias_list
def forward(self, x):
# print(x.shape)
weight_shape = [self.num_workers]
for size in self.weight_list[0].shape:
weight_shape.append(size)
mask1 = [torch.sigmoid(self.weight_mask[idx]) for idx in range(self.num_workers)]
weight_t = torch.div(torch.sum(torch.cat([torch.mul(mask1[idx], self.weight_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(weight_shape), dim=0) \
, torch.sum(torch.cat(mask1, dim=0).reshape(weight_shape), dim=0))
m = torch.zeros_like(self.weight_list[0])
v = torch.ones_like(self.weight_list[0])
if self.bias_list:
bias_shape = [self.num_workers]
for size in self.bias_list[0].shape:
bias_shape.append(size)
mask2 = [torch.sigmoid(self.bias_mask[idx]) for idx in range(self.num_workers)]
bias_t = torch.div(torch.sum(torch.cat([torch.mul(mask2[idx], self.bias_list[idx]) for idx in range(self.num_workers)], dim=0).reshape(bias_shape), dim=0) \
, torch.sum(torch.cat(mask2, dim=0).reshape(bias_shape), dim=0))
out = F.batch_norm(x, m, v, weight_t, bias_t)
else:
out = F.batch_norm(x, m, v, weight_t)
return out