-
Notifications
You must be signed in to change notification settings - Fork 11
/
concat_tiles.py
148 lines (122 loc) · 5.5 KB
/
concat_tiles.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
import numpy as np
import json
import sys
import os
import subprocess
def concat_files(input_files, output_file_path, unroll=1, strategy="single"):
contents = []
assert len(input_files) > 0
stream_id = 0
try:
str_stream = ""
for file_path in input_files:
with open(file_path, 'rb') as file:
cur_id = stream_id.to_bytes(2, byteorder='big')
contents.append(cur_id)
contents.append(file.read())
str_stream += file_path + " "
stream_id += 1
stream_id = stream_id % unroll
with open(output_file_path, 'wb') as output_file:
for content in contents:
output_file.write(content)
# print(f"Files {str_stream} successfully concatenated to {output_file_path}")
except Exception as e:
print(f"Error: {str(e)}")
def add_extents(json_tiles, output_file_path):
tiles = []
for json_tile in json_tiles:
with open(json_tile, "r") as f_tile:
tile = json.load(f_tile)
tiles.append(tile)
num_input = len(tiles[0]['IOs']["inputs"])
num_output = len(tiles[0]['IOs']["outputs"])
# check extents output meaning
for i in range(num_input):
# print(i)
tile_base = tiles[0]['IOs']["inputs"][i]["shape"]
# print(tile_base[0])
for j in range(1, len(tiles)):
tile_base[0] += tiles[j]['IOs']["inputs"][i]["shape"][0]
# print(tiles[j]['IOs']["inputs"][i]["shape"][0])
# workaround for the different shape of the input and output,
tile_base[0] += len(tiles)
tiles[0]['IOs']["inputs"][i]["shape"] = tile_base
# print(tile_base[0])
tiles[0]['IOs']["inputs"][i]["io_tiles"][0]["addr"]["extent"] = tile_base
for i in range(num_output):
tiles[0]['IOs']["outputs"][i]["io_tiles"][0]["num_blocks"] *= len(tiles)
for i in range(num_output):
tile_base = tiles[0]['IOs']["outputs"][i]["shape"]
for j in range(1, len(tiles)):
tile_base[0] += tiles[j]['IOs']["outputs"][i]["shape"][0]
tiles[0]['IOs']["outputs"][i]["shape"] = tile_base
tiles[0]['IOs']["outputs"][i]["extents"] = tile_base
json_string = json.dumps(tiles[0])
new_f = open(output_file_path, "w+")
new_f.write(json_string)
new_f.close()
def create_concate(app_name = "matmul_ijk", kernel_name = "football", tiles = [0, 2, 3], concat_name = "concat", unroll=1):
base_dir_list = []
for tile in tiles:
base_dir_list.append(f"SPARSE_TESTS/{app_name}_{tile}/GLB_DIR/{app_name}_combined_seed_{tile}/bin/")
output_tile_name = f"{app_name}_{kernel_name}-tile_{concat_name}"
output_tile_bin_name = f"{app_name}_combined_seed_{kernel_name}-tile_{concat_name}"
output_base_dir = f"SPARSE_TESTS/{output_tile_name}/GLB_DIR/{output_tile_bin_name}/bin/"
t_base = "/aha/garnet/"
if os.path.exists(t_base + f"SPARSE_TESTS/{output_tile_name}"):
subprocess.run(["rm", "-rf", t_base + f"SPARSE_TESTS/{output_tile_name}"])
subprocess.run(["cp", "-rf", t_base + f"SPARSE_TESTS/{app_name}_{tiles[0]}/", t_base + f"SPARSE_TESTS/{output_tile_name}/"])
output_base_dir_s = t_base + f"SPARSE_TESTS/{output_tile_name}/GLB_DIR/{output_tile_bin_name}"
output_base_dir_s_pre = t_base + f"SPARSE_TESTS/{output_tile_name}/GLB_DIR/{app_name}_combined_seed_{tiles[0]}"
subprocess.run(["mv", output_base_dir_s_pre, output_base_dir_s])
design_meta = base_dir_list[0] + "design_meta.json"
with open(design_meta, "r") as f_tile:
meta_f = json.load(f_tile)
f_tile.close()
input_t_num = len(meta_f['IOs']["inputs"])
input_files = []
for i in range(input_t_num):
input_files.append(meta_f['IOs']["inputs"][i]["datafile"])
# print(meta_f['IOs']["inputs"][i]["datafile"])
# concat input files
for input_file in input_files:
i_f = []
o_f_path = output_base_dir + input_file
for base_dir in base_dir_list:
i_f.append(base_dir + input_file)
concat_files(i_f, o_f_path, unroll)
meta_files = []
for base_dir in base_dir_list:
meta_files.append(base_dir + "design_meta.json")
o_f_path = output_base_dir + "design_meta.json"
add_extents(meta_files, o_f_path)
# this one is without bin folder
base_dir_list = []
for tile in tiles:
base_dir_list.append(f"SPARSE_TESTS/{app_name}_{tile}/GLB_DIR/{app_name}_combined_seed_{tile}/")
gold_files = []
for base_dir in base_dir_list:
gold_files.append(t_base + base_dir + "output_gold_0.npy")
output_base_dir = f"SPARSE_TESTS/{output_tile_name}/GLB_DIR/{output_tile_bin_name}/"
for i in range(len(gold_files)):
subprocess.run(["cp", gold_files[i],t_base + output_base_dir + f"output_gold_{i}.npy"])
if __name__ == "__main__":
# parse input
if len(sys.argv) < 3: # run with internal modification
create_concate()
else:
assert len(sys.argv) >= 3
if sys.argv[3].isdigit():
app_name = sys.argv[1]
kernel_name = sys.argv[2]
unroll = int(sys.argv[3])
tile_list = [int(sys.argv[4])]
create_concate(app_name, kernel_name, tile_list)
else:
app_name = sys.argv[1]
kernel_name = sys.argv[2]
concat_name = sys.argv[3]
unroll = int(sys.argv[4])
tile_list = sys.argv[5:]
create_concate(app_name, kernel_name, tile_list, concat_name=concat_name, unroll=unroll)