-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfc_layer.py
155 lines (116 loc) · 4.77 KB
/
fc_layer.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
#!/usr/bin/python2.7
# public library
import math
import numpy as np
class FullyConnectedLayer(object):
# info for systolic array
A = None # systolic array dimension
# memory bandwith number of bytes can be transferred.
B = None
# on-chip buffer size
buf_size = None
# input layer dimension
Ci = None # channels for ifmap
Co = None # channels for ofmap
Num = None # number of same FC layer
# on-chip buffer size
bufi_size = None
bufo_size = None
bufw_size = None
"""docstring for MultiLayerPerceptron"""
def __init__(self, data, sys_info):
self.data = data
self.sys_info = sys_info
self.A = sys_info["sa_size"]
self.B = sys_info["memory_bandwidth"]/(sys_info["bit_width"]/8)
self.buf_size = sys_info["bufsize"]
def init_setup(self):
layer_info = self.data
# set up the new layer information
self.Ci = layer_info["in_channel"]
self.Co = layer_info["out_channel"]
self.Num = layer_info["num_of_layer"]
self.bufi_size = self.Ci
###############################################################
# general process #
###############################################################
# compute buffer utilization
def buffer_utilization(self, x):
# buffer = ofmap + weights + ifmap
return (x + self.Ci*x + self.Ci)
# (ofmap + ifmap)*total_batch + (ofmap+weights)*Co/c_0
def data_transfer(self, x):
# calculate the total batch
total_batch = math.ceil(float(self.Co)/x)
# ofmap, ifmap and kernel tile size
ofmap_tile_size = x
kernel_tile_size = x*self.Ci
# ofmap + kernels transfer
total_transfer = (ofmap_tile_size + kernel_tile_size) * total_batch
# add additional ifmap data transfer
total_transfer += self.Ci
return total_transfer
def systolic_array_utilization(self, x):
A = self.A
A_w_uiti = math.ceil(self.Co/math.ceil(float(self.Co)/A))
total_usage = x * self.Ci
round_up_val = math.ceil(float(x/A)) * A \
* math.ceil(float(self.Ci)/A)*A
# the pct of extra delay due to output-stationary
delay_pct = float(self.Ci)/(self.Ci+A_w_uiti)
return delay_pct * total_usage / round_up_val
def compute_bound_cycle(self, util_rate):
# total number of ops
total_computation = (self.Ci*self.Co)
# systolic array calculation capacity
comp_cap = (self.A*self.A) * util_rate
return total_computation / comp_cap
def process_parameter(self, x):
x = math.floor(x)
bound = "C"
# make the tile size even for every batch
x_0 = min(self.Co/math.ceil(self.Co/round(x)), self.Co)
# (ofmap + ifmap)*total_batch + weights
total_transfer = self.data_transfer(x_0)
# compute the utilization of systolic array
util_sys_arr = self.systolic_array_utilization(x_0)
# compute the utilization of buffer
util_buf = float(self.buffer_utilization(x_0))/self.buf_size
if util_buf > 1.01:
print("ERROR: the utilization of buffer is over 100%")
exit()
# calculate the amount of cycles of computing all elements.
if self.compute_bound_cycle(util_sys_arr) > total_transfer/self.B:
bound = "C"
total_cycle = self.compute_bound_cycle(util_sys_arr)
else:
bound = "M"
total_cycle = total_transfer/self.B
ret = {
"total_transfer": round(total_transfer)*self.Num,
"total_cycle": round(total_cycle)*self.Num,
"systolic_array_utilization": util_sys_arr,
"buffer_utilization": util_buf,
"buffer-partition [I,W,O]": [int(self.bufi_size),
int(self.bufw_size),
int(self.bufo_size)],
"x_0": math.floor(x_0),
"Bound" : bound
}
return ret
# optimize one layer
def optimize(self):
self.init_setup()
# if sum of bufi and bufw is over the self.buf_size
# we should skip it.
if self.bufi_size > self.buf_size:
print("FAIL: the entire weight cannot be stored in buffer")
exit()
self.bufw_size = (self.buf_size - self.bufi_size)*self.Ci/(self.Ci+1)
self.bufo_size = (self.buf_size - self.bufi_size)/(self.Ci+1)
# set the initial guess;
x0 = self.A
# let's see what percentage of ifmap can we fit into the buffer.
while x0 < self.Co and (x0+self.A)*self.Ci < self.bufw_size:
x0 = x0 + self.A
return self.process_parameter(x0)