Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
watcag-public
fpga-syspart
Commits
e5bba5ce
Commit
e5bba5ce
authored
Oct 09, 2020
by
harry1080ti
Browse files
Justin, your tuen
parent
43e1b652
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
34 deletions
+152
-34
optimization_algo/approaches/cma_approach_square_size.py
optimization_algo/approaches/cma_approach_square_size.py
+15
-6
optimization_algo/approaches/packing_penalty.py
optimization_algo/approaches/packing_penalty.py
+62
-0
optimization_algo/approaches/packing_playground.py
optimization_algo/approaches/packing_playground.py
+48
-0
optimization_algo/approaches/sq_approach_faster.py
optimization_algo/approaches/sq_approach_faster.py
+27
-28
No files found.
optimization_algo/approaches/cma_approach_square_size.py
View file @
e5bba5ce
...
@@ -3,6 +3,7 @@ from multiprocessing import Pool
...
@@ -3,6 +3,7 @@ from multiprocessing import Pool
from
os
import
cpu_count
from
os
import
cpu_count
import
time
import
time
import
path_constant
as
pc
import
path_constant
as
pc
import
packing_penalty
as
pp
class
cma_approach
(
object
):
class
cma_approach
(
object
):
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -21,7 +22,8 @@ class cma_approach(object):
...
@@ -21,7 +22,8 @@ class cma_approach(object):
penalty_offest
=
10000000000
,
penalty_offest
=
10000000000
,
seeding_type
=
"optimised"
,
seeding_type
=
"optimised"
,
hybird
=
True
,
hybird
=
True
,
print_to_csv
=
True
print_to_csv
=
True
,
max_pack_size
=
129
):
):
self
.
target_col
=
target_col
self
.
target_col
=
target_col
self
.
start
=
time
.
time
()
self
.
start
=
time
.
time
()
...
@@ -50,6 +52,8 @@ class cma_approach(object):
...
@@ -50,6 +52,8 @@ class cma_approach(object):
self
.
max_res_available
=
max_res_unit
self
.
max_res_available
=
max_res_unit
self
.
print_to_csv
=
print_to_csv
self
.
print_to_csv
=
print_to_csv
self
.
max_pack_size
=
max_pack_size
def
parse_topology_file
(
self
):
def
parse_topology_file
(
self
):
layers
=
[]
layers
=
[]
with
open
(
pc
.
TOPOLOGIES_PATH
+
self
.
topology_file
,
'r'
)
as
f
:
with
open
(
pc
.
TOPOLOGIES_PATH
+
self
.
topology_file
,
'r'
)
as
f
:
...
@@ -167,7 +171,10 @@ class cma_approach(object):
...
@@ -167,7 +171,10 @@ class cma_approach(object):
# latencies, max_idx = self.find_max_latency(layer, res)
# latencies, max_idx = self.find_max_latency(layer, res)
# res[max_idx] += self.res_step
# res[max_idx] += self.res_step
return
latencies
[
max_idx
],
latencies
,
res
,
layer
if
pp
.
packingPenalty
(
res
,
self
.
max_pack_size
):
return
latencies
[
max_idx
],
latencies
,
res
,
layer
return
self
.
penalty_offest
,
latencies
,
res
,
layer
def
evaluation_top_level
(
self
,
in_val
):
def
evaluation_top_level
(
self
,
in_val
):
pid
,
sampling
=
in_val
pid
,
sampling
=
in_val
...
@@ -206,8 +213,6 @@ class cma_approach(object):
...
@@ -206,8 +213,6 @@ class cma_approach(object):
else
:
else
:
raise
ValueError
(
'Invalid Seeding Strategy'
)
raise
ValueError
(
'Invalid Seeding Strategy'
)
self
.
es
=
cma
.
CMAEvolutionStrategy
(
self
.
seed
,
self
.
sigma
,
\
self
.
es
=
cma
.
CMAEvolutionStrategy
(
self
.
seed
,
self
.
sigma
,
\
{
'popsize'
:
self
.
population_size
})
{
'popsize'
:
self
.
population_size
})
...
@@ -301,7 +306,7 @@ class cma_approach(object):
...
@@ -301,7 +306,7 @@ class cma_approach(object):
latencies
,
max_idx
=
self
.
find_max_latency
(
layer
,
self
.
best_res
)
latencies
,
max_idx
=
self
.
find_max_latency
(
layer
,
self
.
best_res
)
max_latency
=
latencies
[
max_idx
]
max_latency
=
latencies
[
max_idx
]
# generate data for mapping the full array
# generate data for mapping the full array
(129 * 129)
full_latency
,
full_max_idx
=
self
.
find_max_latency
([
self
.
layers
],
[
129
]
*
len
(
self
.
layers
))
full_latency
,
full_max_idx
=
self
.
find_max_latency
([
self
.
layers
],
[
129
]
*
len
(
self
.
layers
))
# PLEASE UNCOMMENT OUT THIS PART IF YOU NOT USING THE BASH SCRIPT WE HAVE PROVIDED
# PLEASE UNCOMMENT OUT THIS PART IF YOU NOT USING THE BASH SCRIPT WE HAVE PROVIDED
...
@@ -325,7 +330,11 @@ class cma_approach(object):
...
@@ -325,7 +330,11 @@ class cma_approach(object):
writer
=
csv
.
writer
(
csvFile
,
delimiter
=
','
,
lineterminator
=
"
\n
"
)
writer
=
csv
.
writer
(
csvFile
,
delimiter
=
','
,
lineterminator
=
"
\n
"
)
writer
.
writerow
([
self
.
target_col
,
self
.
i
,
self
.
k
,
self
.
topology_file
,
1
,(
1
/
max_latency
),
max_latency
*
self
.
k
,
1
/
full_latency
[
full_max_idx
],
full_latency
[
full_max_idx
],
(
1
/
max_latency
)
/
(
1
/
full_latency
[
full_max_idx
]),
(
max_latency
*
self
.
k
)
/
full_latency
[
full_max_idx
],
layer
,
res
,
self
.
end
-
self
.
start
,
self
.
es
.
sigma
,
self
.
seed_od
,
self
.
valid_sampling_percentage
,
self
.
trial
,
self
.
population_size
,
self
.
max_res_unit
,
self
.
seeding_type
])
writer
.
writerow
([
self
.
target_col
,
self
.
i
,
self
.
k
,
self
.
topology_file
,
1
,(
1
/
max_latency
),
max_latency
*
self
.
k
,
1
/
full_latency
[
full_max_idx
],
full_latency
[
full_max_idx
],
(
1
/
max_latency
)
/
(
1
/
full_latency
[
full_max_idx
]),
(
max_latency
*
self
.
k
)
/
full_latency
[
full_max_idx
],
layer
,
res
,
self
.
end
-
self
.
start
,
self
.
es
.
sigma
,
self
.
seed_od
,
self
.
valid_sampling_percentage
,
self
.
trial
,
self
.
population_size
,
self
.
max_res_unit
,
self
.
seeding_type
])
csvFile
.
close
csvFile
.
close
if
self
.
valid_sampling_percentage
>
0
:
pngFileName
=
str
(
self
.
topology_file
.
replace
(
".csv"
,
""
))
+
"_k="
+
str
(
self
.
k
)
+
"_max="
+
str
(
self
.
max_res_unit
)
+
"_packSize="
+
str
(
self
.
max_pack_size
)
+
".png"
pp
.
printPNG
(
res
,
self
.
max_pack_size
,
pngFileName
)
result
=
[
self
.
target_col
,
self
.
i
,
self
.
k
,
self
.
topology_file
,
1
,(
1
/
max_latency
),
max_latency
*
self
.
k
,
1
/
full_latency
[
full_max_idx
],
full_latency
[
full_max_idx
],
(
1
/
max_latency
)
/
(
1
/
full_latency
[
full_max_idx
]),
(
max_latency
*
self
.
k
)
/
full_latency
[
full_max_idx
],
layer
,
res
,
self
.
end
-
self
.
start
,
self
.
es
.
sigma
,
self
.
seed_od
,
self
.
valid_sampling_percentage
,
self
.
trial
,
self
.
population_size
,
self
.
max_res_unit
,
self
.
seeding_type
]
result
=
[
self
.
target_col
,
self
.
i
,
self
.
k
,
self
.
topology_file
,
1
,(
1
/
max_latency
),
max_latency
*
self
.
k
,
1
/
full_latency
[
full_max_idx
],
full_latency
[
full_max_idx
],
(
1
/
max_latency
)
/
(
1
/
full_latency
[
full_max_idx
]),
(
max_latency
*
self
.
k
)
/
full_latency
[
full_max_idx
],
layer
,
res
,
self
.
end
-
self
.
start
,
self
.
es
.
sigma
,
self
.
seed_od
,
self
.
valid_sampling_percentage
,
self
.
trial
,
self
.
population_size
,
self
.
max_res_unit
,
self
.
seeding_type
]
return
True
,
result
return
True
,
result
...
...
optimization_algo/approaches/packing_penalty.py
0 → 100644
View file @
e5bba5ce
from
rectpack
import
newPacker
from
rectpack
import
MaxRectsBssf
from
matplotlib
import
pyplot
as
plt
import
matplotlib.patches
as
patches
def
packingPenalty
(
partitions
,
full_array
):
# print(partitions, full_array)
packer
=
newPacker
(
pack_algo
=
MaxRectsBssf
)
for
p
in
partitions
:
packer
.
add_rect
(
p
,
p
)
packer
.
add_bin
(
full_array
,
full_array
)
packer
.
pack
()
return
len
(
packer
[
0
])
==
len
(
partitions
)
def
printPNG
(
partitions
,
full_array
,
filename
=
"sth.png"
):
# print(partitions, full_array)
packer
=
newPacker
(
pack_algo
=
MaxRectsBssf
)
for
p
in
partitions
:
packer
.
add_rect
(
p
,
p
)
packer
.
add_bin
(
full_array
,
full_array
)
packer
.
pack
()
for
index
,
abin
in
enumerate
(
packer
):
bw
,
bh
=
abin
.
width
,
abin
.
height
# print('bin', bw, bh, "nr of rectangles in bin", len(abin))
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
,
aspect
=
'equal'
)
for
rect
in
abin
:
x
,
y
,
w
,
h
=
rect
.
x
,
rect
.
y
,
rect
.
width
,
rect
.
height
plt
.
axis
([
0
,
bw
,
0
,
bh
])
# print('rectangle', w,h)
patch
=
patches
.
Rectangle
(
(
x
,
y
),
# (x,y)
w
,
# width
h
,
# height
facecolor
=
"#00ffff"
,
edgecolor
=
"black"
,
linewidth
=
3
)
ax
.
add_patch
(
patch
)
rx
,
ry
=
patch
.
get_xy
()
cx
=
rx
+
patch
.
get_width
()
/
2.0
cy
=
ry
+
patch
.
get_height
()
/
2.0
ax
.
annotate
(
f
'w:
{
w
}
\n
h:
{
h
}
'
,
(
cx
,
cy
),
color
=
'b'
,
weight
=
'bold'
,
fontsize
=
4
,
ha
=
'center'
,
va
=
'center'
)
fig
.
savefig
(
filename
,
dpi
=
500
,
papertype
=
'letter'
,
bbox_inches
=
'tight'
)
fig
.
clear
()
if
__name__
==
"__main__"
:
assert
(
packingPenalty
([
2
,
2
],
5
)
==
True
)
assert
(
packingPenalty
([
2
,
4
],
5
)
==
False
)
\ No newline at end of file
optimization_algo/approaches/packing_playground.py
0 → 100644
View file @
e5bba5ce
from
rectpack
import
newPacker
from
rectpack
import
MaxRectsBl
from
matplotlib
import
pyplot
as
plt
import
matplotlib.patches
as
patches
cajas
=
[(
100
,
30
),
(
40
,
60
),
(
30
,
30
),
(
70
,
70
),
(
100
,
50
),
(
30
,
30
)]
contenedores
=
[(
300
,
450
)]
packer
=
newPacker
(
pack_algo
=
MaxRectsBl
)
for
c
in
cajas
:
packer
.
add_rect
(
*
c
)
for
con
in
contenedores
:
packer
.
add_bin
(
*
con
)
packer
.
pack
()
for
index
,
abin
in
enumerate
(
packer
):
bw
,
bh
=
abin
.
width
,
abin
.
height
# print('bin', bw, bh, "nr of rectangles in bin", len(abin))
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
,
aspect
=
'equal'
)
for
rect
in
abin
:
x
,
y
,
w
,
h
=
rect
.
x
,
rect
.
y
,
rect
.
width
,
rect
.
height
plt
.
axis
([
0
,
bw
,
0
,
bh
])
# print('rectangle', w,h)
patch
=
patches
.
Rectangle
(
(
x
,
y
),
# (x,y)
w
,
# width
h
,
# height
facecolor
=
"#00ffff"
,
edgecolor
=
"black"
,
linewidth
=
3
)
ax
.
add_patch
(
patch
)
rx
,
ry
=
patch
.
get_xy
()
cx
=
rx
+
patch
.
get_width
()
/
2.0
cy
=
ry
+
patch
.
get_height
()
/
2.0
ax
.
annotate
(
f
'w:
{
w
}
\n
h:
{
h
}
'
,
(
cx
,
cy
),
color
=
'b'
,
weight
=
'bold'
,
fontsize
=
4
,
ha
=
'center'
,
va
=
'center'
)
fig
.
savefig
(
f
"./
{
abin
.
bid
}
_
{
bw
}
x
{
bh
}
_
{
index
+
1
}
.png"
,
dpi
=
1200
,
papertype
=
'letter'
,
bbox_inches
=
'tight'
)
\ No newline at end of file
optimization_algo/approaches/sq_approach_faster.py
View file @
e5bba5ce
...
@@ -5,29 +5,24 @@ import path_constant as pc
...
@@ -5,29 +5,24 @@ import path_constant as pc
import
cma_approach_square_size
as
ap
import
cma_approach_square_size
as
ap
nets
=
[
nets
=
[
'FasterRCNN'
,
# ('FasterRCNN', 46, 23),
'mobilenet'
,
# ('mobilenet', 27, 14),
'yolo_tiny'
,
# ('yolo_tiny', 10, 9),
'googlenet'
,
(
'googlenet'
,
58
,
29
),
'alexnet'
,
# ('alexnet', 8, 8),
'AlphaGoZero'
,
# ('AlphaGoZero', 8, 8),
'ncf_rec'
,
# ('ncf_rec', 8, 8),
'resnet_50_v1'
]
# ('resnet_50_v1', 53, 27)
layers
=
[
46
,
27
,
10
,
58
,
8
,
8
,
8
,
53
]
max_partitions
=
[
23
,
14
,
9
,
29
,
8
,
8
,
8
,
27
,
13
]
]
max_res_sizes
=
[
max_res_sizes
=
[
1920
*
9
*
1
,
1920
*
9
*
2
,
1920
*
9
*
3
1920
*
9
*
1
# 1920*9*2, 1920*9*3
]
]
pop_size
=
[
pop_size
=
[
100
,
200
,
500
100
# 200, 500
]
]
targets
=
[
targets
=
[
...
@@ -39,6 +34,7 @@ seedings = [
...
@@ -39,6 +34,7 @@ seedings = [
]
]
MAX_TRIAL
=
20
MAX_TRIAL
=
20
MAX_PACK_SIZE
=
129
def
trial
(
prop
):
def
trial
(
prop
):
trials
=
1
trials
=
1
...
@@ -57,7 +53,8 @@ def trial(prop):
...
@@ -57,7 +53,8 @@ def trial(prop):
penalty_offest
=
100000000000
,
penalty_offest
=
100000000000
,
seeding_type
=
seed
,
seeding_type
=
seed
,
hybird
=
True
,
hybird
=
True
,
print_to_csv
=
False
print_to_csv
=
False
,
max_pack_size
=
MAX_PACK_SIZE
)
)
process_out
=
instance
.
run
()
process_out
=
instance
.
run
()
...
@@ -69,32 +66,34 @@ def trial(prop):
...
@@ -69,32 +66,34 @@ def trial(prop):
process_out
.
append
(
out
)
process_out
.
append
(
out
)
return
process_out
return
process_out
topology
=
sys
.
argv
[
1
]
#
topology = sys.argv[1]
k
=
int
(
sys
.
argv
[
2
])
#
k = int(sys.argv[2])
population_size
=
int
(
sys
.
argv
[
3
])
#
population_size = int(sys.argv[3])
max_res_unit
=
int
(
sys
.
argv
[
4
])
#
max_res_unit = int(sys.argv[4])
seeding_type
=
sys
.
argv
[
5
]
#
seeding_type = sys.argv[5]
target_col
=
sys
.
argv
[
6
]
#
target_col = sys.argv[6]
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
import
sys
import
sys
import
csv
import
csv
MAX_TRIAL
=
sys
.
argv
[
1
]
MAX_TRIAL
=
int
(
sys
.
argv
[
1
])
MAX_PACK_SIZE
=
int
(
sys
.
argv
[
2
])
approaches
=
[]
approaches
=
[]
for
idx
,
n
in
enumerate
(
nets
):
for
idx
,
n
in
enumerate
(
nets
):
for
part
in
range
(
3
,
max_partitions
[
idx
]
+
1
):
for
part
in
range
(
3
,
n
[
2
]
+
1
):
# for part in range(n[2] - 2, n[2] + 1):
for
seed
in
seedings
:
for
seed
in
seedings
:
for
pop
in
pop_size
:
for
pop
in
pop_size
:
for
tar
in
targets
:
for
tar
in
targets
:
for
res
in
max_res_sizes
:
for
res
in
max_res_sizes
:
# print(n, part, pop, tar, res, seed)
# print(n, part, pop, tar, res, seed)
approaches
.
append
((
n
,
part
,
pop
,
tar
,
res
,
seed
))
approaches
.
append
((
n
[
0
]
,
part
,
pop
,
tar
,
res
,
seed
))
pool
=
Pool
(
processes
=
cpu_count
()
-
2
)
pool
=
Pool
(
processes
=
cpu_count
()
-
2
)
for
result
in
pool
.
imap_unordered
(
trial
,
approaches
):
for
result
in
pool
.
imap_unordered
(
trial
,
approaches
):
with
open
(
pc
.
RESULT_CSV_PATH
+
'cma_logmore_sq_full_evo_
2
.csv'
,
'a'
)
as
csvFile
:
with
open
(
pc
.
RESULT_CSV_PATH
+
'cma_logmore_sq_full_evo_
packing
.csv'
,
'a'
)
as
csvFile
:
for
evo
in
result
:
for
evo
in
result
:
writer
=
csv
.
writer
(
csvFile
,
delimiter
=
','
,
lineterminator
=
"
\n
"
)
writer
=
csv
.
writer
(
csvFile
,
delimiter
=
','
,
lineterminator
=
"
\n
"
)
writer
.
writerow
(
evo
)
writer
.
writerow
(
evo
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment