-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions_not_used.py
285 lines (236 loc) · 7.38 KB
/
functions_not_used.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#Library
import sys
import os
import os.path as pth
#!pip install torchplot
import torch
import torch.nn as nn
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.nn.init as init
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.backends.cudnn as cudnn
import torchplot as plt
import nets
import datasets
import tools
import layers as L
import train
from io import BytesIO
from datetime import datetime
from pytz import timezone
from slacker import Slacker
from quantization import *
import matplotlib.pyplot as plt
import numpy as np
import argparse
import pickle
import random
import itertools
import math
from tqdm import tqdm
from numba import jit
import warnings
warnings.simplefilter("ignore")
# Function for integer operation -> Not completed
@jit(nopython=True)
def partial_sum_int_fa(original, bit=5):
a = original
bit = bit - 2
result = np.zeros(a.shape[0])
for d in range(a.shape[0]):
partial = a[d].sum()
if partial > 15:
partial = 15
elif partial < -15:
partial = -15
else:
partial = partial
result[d] = math.trunc(partial)
return result.sum()
@jit(cache=True)
def conv_custom_fa_int_plot(x_original, w, b):
filt = w.shape[0]
depth = x_original.shape[0]
row = x_original.shape[1] - 2
col = x_original.shape[2] - 2
c = np.zeros((filt,row,col))
one_layer = np.zeros((row,col))
for f in range(filt):
for i in range(row):
for j in range(col):
r = x_original[:,i:i+3,j:j+3] * w[f]
one_layer[i,j] = partial_sum_int_fa(r)
c[f,:,:] = one_layer + b[f]
return c, one_layer
# Partial_sum_fa_version2 -> Completed & Not used
@jit(nopython=True)
def partial_sum_fa2(original, bit=5):
bit = bit - 2
result = np.zeros(original.shape[0])
for d in range(original.shape[0]):
result[d] = original[d].sum()
result = np.clip(result, -1.875, 1.875)
for d in range(original.shape[0]):
result[d] = math.trunc(result[d]* (2**bit)) / (2**bit)
return result.sum()
# Extra Convolution function - Completed
@jit(cache=True)
def conv_custom_fa_plot(x_original, w, b):
filt = w.shape[0]
depth = x_original.shape[0]
row = x_original.shape[1] - 2
col = x_original.shape[2] - 2
c = np.zeros((filt,row,col))
one_layer = np.zeros((row,col))
before_sum = np.zeros((row,col,filt,3,3))
for f in range(filt):
for i in range(row):
for j in range(col):
r = x_original[:,i:i+3,j:j+3] * w[f]
#print(r.shape)
before_sum[i][j][f] = r
one_layer[i,j] = partial_sum_fa_conv(r)
c[f,:,:] = one_layer + b[f]
return c, one_layer, before_sum
@jit(cache=True)
def conv_custom_fa_test(x_original, w, b):
filt = w.shape[0]
depth = x_original.shape[0]
row = x_original.shape[1] - 2
col = x_original.shape[2] - 2
c = np.zeros((filt,row,col))
one_layer = np.zeros((row,col))
for f in range(filt):
for i in range(row):
for j in range(col):
r = x_original[:,i:i+3,j:j+3] * w[f]
re = 0
re1 = partial_sum_fa_conv(r)
for d1 in range(depth):
partial_sum = r[d1].sum()
re = re + quant_signed_15_1(partial_sum)
if (re != re1):
print("wrong")
print(re, re1)
one_layer[i,j] = re
c[f,:,:] = one_layer + b[f]
return c
@jit
def quant_signed_15_1(original, bit=5):
bit = bit -2
original = np.clip(original, -1.875, 1.875)
original = original * (2**bit)
result = math.trunc(original)/ (2**bit)
return result
# Partial Sum Fast - Completed & Not used
# Fixed point quantization , Not dynamically quantized
@jit(cache=True)
def partial_sum_fa_fc(original, bit=5):
a = original
bit = bit - 2
result = np.zeros(a.shape[0])
for d in range(a.shape[0]):
partial = a[d].sum()
if partial > 1.875:
partial = 1.875
elif partial < -1.875:
partial = -1.875
else:
partial = partial
result[d] = math.trunc(partial* (2**bit)) / (2**bit)
return result.sum()
@jit(nopython=True)
def partial_sum_fa_conv(original, bit=5):
a = original
bit = bit - 2
result = np.zeros(a.shape[0])
for d in range(a.shape[0]):
partial = a[d].sum()
if partial > 1.875:
partial = 1.875
elif partial < -1.875:
partial = -1.875
else:
partial = partial
result[d] = math.trunc(partial* (2**bit)) / (2**bit)
return result.sum()
# FC & Conv function without point parameters - completed
# Not dynamically quantized
@jit(cache=True)
def fc_fa_non(x_original, w, b):
filt = w.shape[0]
stage = int(x_original.shape[1]/8)
c = np.zeros((1,filt))
for f in range(filt):
re = 0
for i in range(stage):
r = x_original[0,i*8:i*8+8] * w[f,i*8:i*8+8]
re = re + r.sum()
c[0,f] = quant_signed_15_np_fc(re + b[f])
return c
@jit(cache=True)
def conv_custom_fa_non(x_original, w, b):
filt = w.shape[0]
depth = x_original.shape[0]
row = x_original.shape[1] - 2
col = x_original.shape[2] - 2
c = np.zeros((filt,row,col))
one_layer = np.zeros((row,col))
for f in range(filt):
for i in range(row):
for j in range(col):
r = x_original[:,i:i+3,j:j+3] * w[f]
one_layer[i,j] = r.sum()
c[f,:,:] = quant_signed_15_np(one_layer + b[f])
return c
# This is original function that I wrote, but does not match with our verilog model
# It saturates every single value of the input -> I don't remember why I wrote code this way
@jit(cache=True)
def partial_sum_fa_fc_point_wrong(original, bit=5, point=1):
a = original
bit = bit - 2 + (point - 1)
value = 1.875/(2**(point-1))
result = np.zeros(a.shape[0])
for d in range(a.shape[0]):
print(d, a.shape[0], a[d])
partial = a[d].sum()
if partial > value:
partial = value
elif partial < -value:
partial = -value
else:
partial = partial
result[d] = math.trunc(partial* (2**bit)) / (2**bit)
return result.sum()
# FC & Conv function with point parameters - completed
@jit(cache=True)
def fc_fa_wo_quant(x_original, w, b, p=1):
filt = w.shape[0]
stage = int(x_original.shape[1]/8)
c = np.zeros((1,filt))
for f in range(filt):
re = 0
for i in range(stage):
r = x_original[0,i*8:i*8+8] * w[f,i*8:i*8+8]
re = re + partial_sum_fa_fc_point(r, point=p)
c[0,f] = re + b[f]
return c
@jit(cache=True)
def conv_custom_fa_wo_quant(x_original, w, b, p=1):
filt = w.shape[0]
depth = x_original.shape[0]
row = x_original.shape[1] - 2
col = x_original.shape[2] - 2
c = np.zeros((filt,row,col))
one_layer = np.zeros((row,col))
for f in range(filt):
for i in range(row):
for j in range(col):
r = x_original[:,i:i+3,j:j+3] * w[f]
one_layer[i,j] = partial_sum_fa_conv_point(r, point=p)
c[f,:,:] = one_layer + b[f]
return c