-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
311 lines (247 loc) · 10.4 KB
/
solver.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python2.7
# own module
# from memory_controller import *
# from systolic_array import *
# from onchip_buffer import *
# public library
import cv2 as cv
import math
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import minimize
# info for systolic array
A = 16.0 # systolic array dimension
# info for weights
K = 3.0 # kernel size
# input layer dimension
H = 512.0 # height of ofmap
W = 512.0 # width of ifmap
Ci = 512.0 # channels for weights
Co = 512.0 # channels for ofmap
# memory bandwith number of bytes can be trasferred.
B = 2.0/4
# on-chip buffer size
buffer_size = 1.0*1024.0*1024.0
# variables for optimization
# this two has been encodes as x[2]; = {c_0, h_0xw_0};
# c_0 # number of channels per batch;
# h_0xw_0 # size of tile per batch;
# calculate the latency for compute and memory;
# l_com = (K*K*c_0*h_0xw_0)/(R*R)
# # if row-major
# l_mem_r = (c_0*h_0xw_0 + C*h_0xw_0)/B
# # if channel-major
# l_mem_c = (c_0*h_0xw_0 + C*K*K*h_0xw_0)/B
###############################################################
# general process #
###############################################################
def process_parameter(x, row_major, comp_bound):
res = [math.ceil(Co/x[0]), Co/math.ceil(Co/x[0]), \
math.ceil(W*H/x[1]), H*W/math.ceil(W*H/x[1])]
print(math.ceil(Co/x[0]), Co/math.ceil(Co/x[0]))
print(math.ceil(W*H/x[1]), H*W/math.ceil(W*H/x[1]))
x[0] = A*math.floor(x[0]/A)
x[1] = A*math.floor(x[1]/A)
print(math.ceil(Co/x[0]), Co/math.ceil(Co/x[0]))
print(math.ceil(W*H/x[1]), H*W/math.ceil(W*H/x[1]))
if (row_major):
total_transfer = (res[1]*res[3]+res[3]*Ci)*res[2]*res[0]\
+(res[1]*res[3]+K*K*Ci*res[3])*res[0]
else:
total_transfer = (res[1]*res[3]+K*K*Ci*res[1])*res[0]*res[2]\
+(res[1]*res[3]+res[3]*Ci)*res[2]
if comp_bound:
total_cycle = (res[0]/A*res[1]/A)*(Ci*K*K)*res[2]*res[3]
else:
total_cycle = total_transfer/B
print("total_transfer", total_transfer)
print("total_cycle", total_cycle)
return [res, total_transfer]
# this function is to verifer if a given hardware
# configuration is able to realize in given hardware
# constraints.
# return the result and total
# def verifier(x, row_major):
###############################################################
# general constraints #
###############################################################
# the low bound of buffer size;
# make sure the buffer utilization is always larger than 0
def buffer_constraint1(x):
# buffer = ofmap + weights + ifmap
return x[0]*x[1] + Ci*K*K*x[0] + Ci*x[1]
# the upper bound of the buffer size;
# make sure the buffer utilization is
# always smaller than buffer size;
def buffer_constraint2(x):
return buffer_size - (x[0]*x[1]+Ci*K*K*x[0]+Ci*x[1])
###############################################################
# row-major constraint solving obj and constraints #
###############################################################
# the minimization objective of row-major
def row_major_obj(x):
return H*W/(x[0]*x[1]) + K*K/x[0]
# make sure the load for row-major is always less than
# load for channel-major, range : [0, +inf]
def row_major_constraint(x):
# simplified from K^2*C*x[0] > C*x[1]
return K*K*x[0] - x[1];
# make sure the process is always memory-bound;
# which is the latency for memory access is always
# greater than lantecy of compute;
# (x[0]+C)*x[1]/B >= (K^2*C/A^2)*x[0]*x[1]
# range : [0, +inf]
def row_major_mem_bound_constraint(x):
return (x[0]+Ci)/B - K*K*Ci/(A*A)*x[0]
# the main optimization of memory-bound and row-major case;
def opti_mem_row_major():
# set the initial guess;
x0 = [A, A]
# for row_major_constraint1
con1 = {'type': 'ineq', 'fun': row_major_constraint}
# for mem_bound_constraint
con2 = {'type': 'ineq', 'fun': row_major_mem_bound_constraint}
# for the buffer_constraint
con3 = {'type': 'ineq', 'fun': buffer_constraint1}
con4 = {'type': 'ineq', 'fun': buffer_constraint2}
# summery all the bounds and constraints
bnds = ((A, Co), (A, H*W))
cons= ([con1, con2, con3, con4])
# call the external solver to solve the solution
solution = minimize(row_major_obj, x0, method='SLSQP',\
bounds=bnds,constraints=cons)
print("row major", solution.x, row_major_obj(solution.x))
print("row major constraint", row_major_constraint(solution.x))
print("buffer size", buffer_constraint1(solution.x))
print("buffer constraint", buffer_constraint2(solution.x))
print(row_major_mem_bound_constraint(solution.x))
process_parameter(solution.x, True, False)
# make sure the process is always compute-bound;
# which is the latency for compute is always
# greater than lantecy of memory access;
# (x[0]+C)*x[1]/B <= (K^2*C/A^2)*x[0]*x[1]
# range : [0, +inf]
def row_major_comp_bound_constraint(x):
return K*K*Ci/(A*A)*x[0]-(Ci+x[0])/B
# the main optimization of compute-bound and row-major case;
def opti_comp_row_major():
# set the initial guess;
x0 = [A,A]
# for row_major_constraint1
con1 = {'type': 'ineq', 'fun': row_major_constraint}
# for mem_bound_constraint
con2 = {'type': 'ineq', 'fun': row_major_comp_bound_constraint}
# for the buffer_constraint
con3 = {'type': 'ineq', 'fun': buffer_constraint1}
con4 = {'type': 'ineq', 'fun': buffer_constraint2}
# summery all the bounds and constraints
bnds = ((A, Co), (A, H*W))
cons= ([con1, con2, con3, con4])
# call the external solver to solve the solution
solution = minimize(row_major_obj,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
print("row major", solution.x, row_major_obj(solution.x))
print("row major constraint", row_major_constraint(solution.x))
print("buffer size", buffer_constraint1(solution.x))
print("buffer constraint", buffer_constraint2(solution.x))
print(row_major_comp_bound_constraint(solution.x))
process_parameter(solution.x, True, True)
###############################################################
# channel-major constraint solving obj and constraints #
###############################################################
# the minimization objective of channel-major
def channel_major_obj(x):
# simplified from H*W*C/x[0] + K*K*C*C*W*H/x[1]
return 1/x[1] + K*K*Co/(x[0]*x[1])
# make sure the load for channel-major is always less than
# load for row-major, range : [0, +inf]
def channel_major_constraint(x):
# simplified from K^2*C*x[0] < C*x[1]
return x[1] - K*K*x[0];
# make sure the process is always memory-bound;
# which is the latency for memory access is always
# greater than lantecy of compute;
# x[0]*(x[1]+K^2*C)/B >= (K^2*C/A^2)*x[0]*x[1]
# range : [0, +inf]
def channel_major_mem_bound_constraint(x):
return (x[1]+K*K*Ci)/B - K*K*Ci/(A*A)*x[1]
# the main optimization of memory-bound and channel-major case;
def opti_mem_channel_major():
# set the initial guess;
x0 = [A,A]
# for row_major_constraint1
con1 = {'type': 'ineq', 'fun': channel_major_constraint}
# for mem_bound_constraint
con2 = {'type': 'ineq', 'fun': channel_major_mem_bound_constraint}
# for the buffer_constraint
con3 = {'type': 'ineq', 'fun': buffer_constraint1}
con4 = {'type': 'ineq', 'fun': buffer_constraint2}
# summery all the bounds and constraints
bnds = ((A, Co), (A, H*W))
cons= ([con1, con2, con3, con4])
# call the external solver to solve the solution
solution = minimize(channel_major_obj,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
print("channel major",solution.x, channel_major_obj(solution.x))
print("channel major constraint", channel_major_constraint(solution.x))
print("buffer size", buffer_constraint1(solution.x))
print("buffer constraint", buffer_constraint2(solution.x))
print(channel_major_mem_bound_constraint(solution.x))
process_parameter(solution.x, False, False)
# make sure the process is always memory-bound;
# which is the latency for memory access is always
# greater than lantecy of compute;
# x[0]*(x[1]+K^2*C)/B <= (K^2*C/A^2)*x[0]*x[1]
# range : [0, +inf]
def channel_major_comp_bound_constraint(x):
return K*K*Co/(A*A)*x[1] - (x[1]+K*K*Co)/B
# the main optimization of compute-bound and channel-major case;
def opti_comp_channel_major():
# set the initial guess;
x0 = [A,A]
# for row_major_constraint1
con1 = {'type': 'ineq', 'fun': channel_major_constraint}
# for mem_bound_constraint
con2 = {'type': 'ineq', 'fun': channel_major_comp_bound_constraint}
# for the buffer_constraint
con3 = {'type': 'ineq', 'fun': buffer_constraint1}
con4 = {'type': 'ineq', 'fun': buffer_constraint2}
# summery all the bounds and constraints
bnds = ((A, Co), (A, H*W))
cons= ([con1, con2, con3, con4])
# call the external solver to solve the solution
solution = minimize(channel_major_obj,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
print("channel major",solution.x, channel_major_obj(solution.x))
print("channel major constraint", channel_major_constraint(solution.x))
print("buffer size", buffer_constraint1(solution.x))
print("buffer constraint", buffer_constraint2(solution.x))
print(channel_major_comp_bound_constraint(solution.x))
process_parameter(solution.x, False, True)
def opti_mem():
print("=================================")
print("======= Memory Bound ==========")
# optimization for row-major;
opti_mem_row_major();
# optimization for channel-major;
opti_mem_channel_major();
print("=================================\n")
def opti_comp():
print("=================================")
print("====== Compute Bound ==========")
# optimization for row-major;
opti_comp_row_major();
# optimization for channel-major;
opti_comp_channel_major();
print("=================================\n")
def optimizeLayer(height, width, channel, w_number):
# if it is possible to be memory-bound only;
if (K*K*Ci)/(A*A) < B or B/((K*K*Ci)/(A*A) - B) > 1:
opti_mem() # only possible to be memory-bound;
else:
# both cases are possible;
opti_mem()
opti_comp()
if __name__== '__main__':
optimizeLayer(H, W, Ci, Co)