-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_behaviour.py
136 lines (112 loc) · 3.87 KB
/
test_behaviour.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
import sparse_conv as sp
import torch.nn as nn
import torch
import torch.nn.functional as F
import torch.nn.utils.prune as prune
import copy
class LeNet5(nn.Module):
"""
A standard LeNet5 model
"""
def __init__(self, n_classes,sparse_conv_flag=True):
self._sparse_conv_flag=sparse_conv_flag
super(LeNet5, self).__init__()
if sparse_conv_flag:
conv = sp.SparseConv2D
else:
conv = nn.Conv2d
self.conv1 = conv(in_channels=1, out_channels=6, kernel_size=5, stride=1)
self.tanh1 = nn.Tanh()
self.pool1 = nn.AvgPool2d(kernel_size=2)
self.conv2 = conv(in_channels=6, out_channels=16, kernel_size=5, stride=1)
self.tanh2 = nn.Tanh()
self.pool2 = nn.AvgPool2d(kernel_size=2)
self.conv3 = conv(in_channels=16, out_channels=120, kernel_size=5, stride=1)
self.tanh3 = nn.Tanh()
self.linear1 = nn.Linear(in_features=120, out_features=84)
self.tanh4 = nn.Tanh()
self.linear2 = nn.Linear(in_features=84, out_features=n_classes)
def make_weights_sparse(self):
'''
Allow the convolution to compute the sparse representation of the weights
'''
print("----------------------------------------")
self.conv1.load()
print("----------------------------------------")
self.conv2.load()
print("----------------------------------------")
self.conv3.load()
print("----------------------------------------")
#Required only for benchmark Conv2d vs SparseConv
def setSparseConvUsage(self,usage=True):
self.conv1.use_sparse = usage
self.conv2.use_sparse = usage
self.conv3.use_sparse = usage
def forward(self, x):
x = self.conv1(x)
x = self.tanh1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.tanh2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.tanh3(x)
x = torch.flatten(x, 1)
x = self.linear1(x)
x = self.tanh4(x)
logits = self.linear2(x)
probs = F.softmax(logits, dim=1)
return logits, probs
def pruning_model_random(model, px):
parameters_to_prune =[]
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
parameters_to_prune.append((m,'weight'))
parameters_to_prune = tuple(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.RandomUnstructured,
amount=px,
)
RANDOM_SEED = 42
LEARNING_RATE = 0.001
BATCH_SIZE = 32
N_EPOCHS = 4
IMG_SIZE = 32
N_CLASSES = 10
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
#LOAD THE MODEL
model = LeNet5(N_CLASSES,sparse_conv_flag=True)
model.to(device)
#PRUNE THE MODEL TO ADD SPARSITY
pruning_model_random(model,0.6)
#CONVERT KERNEL TO CSR
model.conv1.training = False
model.conv1.use_sparse = True
#------------------------------------------
#------------------------------------------
#----------TESTING-------------------------
#------------------------------------------
#------------------------------------------
#Generate a dummy input to give the convolution
dummy_input = torch.randn(1, 1,IMG_SIZE,IMG_SIZE, dtype=torch.float).to(device)
dummy_input = dummy_input.cuda()
input = copy.deepcopy(dummy_input)
input = input.cuda()
#Generate sparse conv ouptput
sp_out = model.conv1.forward(dummy_input)
#Generate vanilla conv output
model.conv1.use_sparse = False
out = model.conv1.forward(input)
#TODO Compare vanilla vs sparse output
#print(f"SP_OUT: {sp_out}")
#print(f"OUT: {out}")
print("Vanilla vs SparseConv:")
if torch.all(sp_out.eq(out)):
print("\033[92mSUCCESS => Same Outputs\033[0m")
else:
print("\033[91mFAIL => Divergent Outputs\033[0m")
print(f"IN -shape: {dummy_input.shape}")
print(f"OUT-shape: {out.shape}")