-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.py
213 lines (176 loc) · 5.95 KB
/
runner.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
"""GPGPU-Sim Deep Learning Runner Script
This script manages the execution of deep learning programs on GPGPU-Sim.
New deep learning programs should be first made compatible with the simulator
according to the instructions in the README, To use VF32, make sure that the PTX
for the deep learning program has been generated (by running it on the simulator
once) and that you have overriden the PTX to use VF32.
Ensure that the repo configuration parameters accurate reflect your local
project structure. The parameters under script configuration should be set
accordingly to control execution behavior. Briefly, the four stages in this
script are as follows:
BUILD:
- Compile DL program
- Apply the simulator config file
- Setup simulator environment
TRAIN:
- Executes training on the DL program
- Params:
- input weights file (optional)
- output weights file
- start epoch (optional)
- end epoch
INFER:
- Executes inferences on the DL program
- Params:
- weights file
- log file (optional)
CLEANUP:
- Cleans up temporary files
Each stage can be individually disabled.
"""
from collections import OrderedDict
from time import sleep
import subprocess
import os
# Repo configuration (modify this)
PROGRAM_DIR = "./programs"
CONFIG_DIR = "./config"
SIM_DIR = "./../gpgpu-sim_distribution"
# Script configuration (modify this)
BASE_VF_SIGNIFICAND = 8
BASE_VF_EXPONENT_MIN = -132
BASE_VF_EXPONENT_MAX = 128
PROGRAM = "MLP2"
EXECUTABLE = "mlp2_sim"
STAGE_CONFIG = {
"BUILD": {
"RUN": True,
},
"TRAIN": {
"RUN": False,
"START_EPOCH": 1,
"INPUT_WEIGHTS_FILE": "",
"END_EPOCH": 10000,
"OUTPUT_WEIGHTS_FILE": "weights10000.txt",
},
"TEST": {
"RUN": True,
"WEIGHTS_FILE": "weights10000.txt",
"LOG_FILE": "",
},
"CLEANUP": {
"RUN": True,
},
}
def stream(process):
"""
Streams a given process' stdout to this process' stdout.
"""
result = None
while result is None:
for line in process.stdout:
print(line.decode(), end="")
for line in process.stderr:
print(line.decode(), end="")
result = process.poll()
sleep(0.1)
return result
def metaprint(stage, message):
"""
Helper function to print "meta" messages about the script execution.
Messages will be printed in green.
"""
string = "[{}] {}".format(stage, message)
META_COLOR = '\033[92m'
END_COLOR = '\033[0m'
print(META_COLOR + string + END_COLOR)
def run(command, env=os.environ):
"""
Runs a given command in a subprocess.
"""
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.join(PROGRAM_DIR, PROGRAM),
shell=True,
executable='/bin/bash',
env=env)
result = stream(process)
if result != 0:
print("Error: return code {}".format(result))
exit(result)
def run_with_sim_setup(command):
"""
Runs a given command in a subprocess, with simulator setup.
"""
env = os.environ.copy()
# set path to gpgpusim.config
env["SIM_CONFIG_PATH"] = os.path.abspath(
os.path.join(CONFIG_DIR, "gpgpusim.config"))
# set base precision of VF32 type
env["VF_SIGNIFICAND"] = str(BASE_VF_SIGNIFICAND)
env["VF_EXPONENT_MIN"] = str(BASE_VF_EXPONENT_MIN)
env["VF_EXPONENT_MAX"] = str(BASE_VF_EXPONENT_MAX)
# source sim setup file
setup_file = os.path.abspath(os.path.join(SIM_DIR, "setup_environment"))
command = ". {}; {};".format(setup_file, command)
run(command, env)
def build():
"""
Invoke the pre-determined build command for DL applications.
"""
run("make")
def train():
"""
Launches training on the simulator for the configured application.
If a start epoch greater than 1 is provided, a weights input file is expected
to perform incremental training.
"""
config = STAGE_CONFIG["TRAIN"]
if config["START_EPOCH"] <= 1:
# run regular training
run_with_sim_setup("./{} -train {} {}".format(EXECUTABLE,
config["END_EPOCH"],
config["OUTPUT_WEIGHTS_FILE"]))
else:
# run incremental training, starting from an input weights file
run_with_sim_setup("./{} -train-increment {} {} {} {}".format(EXECUTABLE,
config["START_EPOCH"],
config["END_EPOCH"],
config["INPUT_WEIGHTS_FILE"],
config["OUTPUT_WEIGHTS_FILE"]))
def test():
"""
Launches inference on the simulator for the configured application.
If a non-empty log file name is provided, the simulator output is piped
to the log file.
"""
config = STAGE_CONFIG["TEST"]
command = "./{} -test {}".format(EXECUTABLE,
config["WEIGHTS_FILE"])
# pipe simulator output to log file (if any)
log_file = config["LOG_FILE"]
if log_file != "":
command = "{} > {}".format(command, log_file)
run_with_sim_setup(command)
def cleanup():
"""
Deletes temporary files created by simulator.
"""
run("rm -f _cuobjdump_list_ptx_* _app_cuda_version_*")
if __name__ == "__main__":
STAGE_FUNC = OrderedDict([
("BUILD", build),
("TRAIN", train),
("TEST", test),
("CLEANUP", cleanup),
])
assert(STAGE_FUNC.keys() == STAGE_CONFIG.keys())
for stage, func in STAGE_FUNC.items():
if STAGE_CONFIG[stage]["RUN"]:
metaprint(stage, "Starting...")
func()
metaprint(stage, "Completed!")
else:
metaprint(stage, "Skipped")