-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_output.py
128 lines (87 loc) · 3.34 KB
/
parse_output.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
input_offsets = []
filter_vals = []
filter_offsets = []
bias_data = []
output_multiplier = []
output_offset = []
right_shift = []
output_scale = []
output_zero_point = []
for line in open("output.txt"):
line = line.strip()
if line.startswith("NAB FULLY CONNECTED EXEC FILTER VAL "):
filter_vals.append(line.replace("NAB FULLY CONNECTED EXEC FILTER VAL ", ""))
elif line.startswith("NAB FULLY CONNECTED EXEC FILTER OFFSET "):
filter_offsets.append(line.replace("NAB FULLY CONNECTED EXEC FILTER OFFSET ", ""))
elif line.startswith("NAB FULLY CONNECTED EXEC INPUT OFFSET "):
input_offsets.append(line.replace("NAB FULLY CONNECTED EXEC INPUT OFFSET ", ""))
elif line.startswith("NAB FULLY CONNECTED EXEC BIAS DATA "):
bias_data.append(line.replace("NAB FULLY CONNECTED EXEC BIAS DATA ", ""))
elif line.startswith("NAB FULLY CONNECTED OUTPUT MULTIPLIER "):
output_multiplier.append(line.replace("NAB FULLY CONNECTED OUTPUT MULTIPLIER ", ""))
elif line.startswith("NAB FULLY CONNECTED RIGHT SHIFT "):
right_shift.append(line.replace("NAB FULLY CONNECTED RIGHT SHIFT ", ""))
elif line.startswith("NAB FULLY CONNECTED EXEC OUTPUT OFFSET "):
output_offset.append(line.replace("NAB FULLY CONNECTED EXEC OUTPUT OFFSET ", ""))
elif line.startswith("NAB OUTPUT SCALE "):
output_scale.append(line.replace("NAB OUTPUT SCALE ", ""))
elif line.startswith("NAB OUTPUT ZERO POINT "):
output_zero_point.append(line.replace("NAB OUTPUT ZERO POINT ", ""))
x = 1
l1_input = int(x / 0.024574 - 128)
l1_results = []
l2_results = []
# Layer 1
for i in range(16):
acc = (int(filter_vals[i]) + int(filter_offsets[i])) * (l1_input + int(input_offsets[i]))
acc += int(bias_data[i])
if acc > 0:
acc = (acc * int(output_multiplier[i]) + 2**30) / 2**31
else:
acc = (acc * int(output_multiplier[i]) + (1 - 2**30)) / 2**31
print(acc)
acc = int(int(acc) / 2**int(right_shift[i]))
print(acc)
acc += int(output_offset[i])
if acc < -128:
acc = -128
elif acc > 127:
acc = 127
l1_results.append(acc)
print(l1_results)
# Layer 2
for node in range(16):
acc = 0
for l1 in range(16):
offset = 16 + (node*16) + l1
acc += (int(filter_vals[offset]) + int(filter_offsets[offset])) * (l1_results[l1] + int(input_offsets[offset]))
acc += int(bias_data[node+16])
if acc > 0:
acc = (acc * int(output_multiplier[node+16]) + 2**30) / 2**31
else:
acc = (acc * int(output_multiplier[node+16]) + (1 - 2**30)) / 2**31
acc = int(int(acc) / 2**int(right_shift[node+16]))
acc += int(output_offset[node+16])
if acc < -128:
acc = -128
elif acc > 127:
acc = 127
l2_results.append(acc)
print(l2_results)
# Layer 3
acc = 0
for l2 in range(16):
offset = 16 + (16*16) + l2
acc += (int(filter_vals[offset]) + int(filter_offsets[offset])) * (l2_results[l2] + int(input_offsets[offset]))
acc += int(bias_data[32])
if acc > 0:
acc = (acc * int(output_multiplier[32]) + 2**30) / 2**31
else:
acc = (acc * int(output_multiplier[32]) + (1 - 2**30)) / 2**31
acc = int(int(acc) / 2**int(right_shift[32]))
acc += int(output_offset[32])
if acc < -128:
acc = -128
elif acc > 127:
acc = 127
print((acc - int(output_zero_point[0])) * float(output_scale[0]))