Unverified Commit e33de246 by Cody Yu Committed by GitHub

[Doc][AutoTVM] Fix bugs that override n_trials (#4842)

parent 0186ca49
...@@ -299,13 +299,14 @@ def tune_tasks(tasks, ...@@ -299,13 +299,14 @@ def tune_tasks(tasks,
tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))
# do tuning # do tuning
n_trial = min(n_trial, len(tsk.config_space)) tsk_trial = min(n_trial, len(tsk.config_space))
tuner_obj.tune(n_trial=n_trial, tuner_obj.tune(n_trial=tsk_trial,
early_stopping=early_stopping, early_stopping=early_stopping,
measure_option=measure_option, measure_option=measure_option,
callbacks=[ callbacks=[
autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.progress_bar(tsk_trial, prefix=prefix),
autotvm.callback.log_to_file(tmp_log_file)]) autotvm.callback.log_to_file(tmp_log_file)
])
# pick best records to a cache file # pick best records to a cache file
autotvm.record.pick_best(tmp_log_file, log_filename) autotvm.record.pick_best(tmp_log_file, log_filename)
......
...@@ -201,13 +201,14 @@ def tune_tasks(tasks, ...@@ -201,13 +201,14 @@ def tune_tasks(tasks,
tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))
# do tuning # do tuning
n_trial = min(n_trial, len(tsk.config_space)) tsk_trial = min(n_trial, len(tsk.config_space))
tuner_obj.tune(n_trial=n_trial, tuner_obj.tune(n_trial=tsk_trial,
early_stopping=early_stopping, early_stopping=early_stopping,
measure_option=measure_option, measure_option=measure_option,
callbacks=[ callbacks=[
autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.progress_bar(tsk_trial, prefix=prefix),
autotvm.callback.log_to_file(tmp_log_file)]) autotvm.callback.log_to_file(tmp_log_file)
])
# pick best records to a cache file # pick best records to a cache file
autotvm.record.pick_best(tmp_log_file, log_filename) autotvm.record.pick_best(tmp_log_file, log_filename)
......
...@@ -283,13 +283,14 @@ def tune_tasks(tasks, ...@@ -283,13 +283,14 @@ def tune_tasks(tasks,
tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))
# do tuning # do tuning
n_trial = min(n_trial, len(tsk.config_space)) tsk_trial = min(n_trial, len(tsk.config_space))
tuner_obj.tune(n_trial=n_trial, tuner_obj.tune(n_trial=tsk_trial,
early_stopping=early_stopping, early_stopping=early_stopping,
measure_option=measure_option, measure_option=measure_option,
callbacks=[ callbacks=[
autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.progress_bar(tsk_trial, prefix=prefix),
autotvm.callback.log_to_file(tmp_log_file)]) autotvm.callback.log_to_file(tmp_log_file)
])
# pick best records to a cache file # pick best records to a cache file
autotvm.record.pick_best(tmp_log_file, log_filename) autotvm.record.pick_best(tmp_log_file, log_filename)
......
...@@ -74,6 +74,7 @@ from vta.top import graph_pack ...@@ -74,6 +74,7 @@ from vta.top import graph_pack
# --------------- # ---------------
# Perform vta-specific compilation with Relay from a Gluon model # Perform vta-specific compilation with Relay from a Gluon model
def compile_network(env, target, model, start_pack, stop_pack): def compile_network(env, target, model, start_pack, stop_pack):
# Populate the shape and data type dictionary # Populate the shape and data type dictionary
...@@ -91,20 +92,18 @@ def compile_network(env, target, model, start_pack, stop_pack): ...@@ -91,20 +92,18 @@ def compile_network(env, target, model, start_pack, stop_pack):
# Perform quantization in Relay # Perform quantization in Relay
# Note: We set opt_level to 3 in order to fold batch norm # Note: We set opt_level to 3 in order to fold batch norm
with relay.build_config(opt_level=3): with relay.build_config(opt_level=3):
with relay.quantize.qconfig(global_scale=8.0, with relay.quantize.qconfig(global_scale=8.0, skip_conv_layers=[0]):
skip_conv_layers=[0]):
mod = relay.quantize.quantize(mod, params=params) mod = relay.quantize.quantize(mod, params=params)
# Perform graph packing and constant folding for VTA target # Perform graph packing and constant folding for VTA target
if target.device_name == "vta": if target.device_name == "vta":
assert env.BLOCK_IN == env.BLOCK_OUT assert env.BLOCK_IN == env.BLOCK_OUT
relay_prog = graph_pack( relay_prog = graph_pack(mod["main"],
mod["main"], env.BATCH,
env.BATCH, env.BLOCK_OUT,
env.BLOCK_OUT, env.WGT_WIDTH,
env.WGT_WIDTH, start_name=start_pack,
start_name=start_pack, stop_name=stop_pack)
stop_name=stop_pack)
return relay_prog, params return relay_prog, params
...@@ -195,8 +194,8 @@ target = env.target if device == "vta" else env.target_vta_cpu ...@@ -195,8 +194,8 @@ target = env.target if device == "vta" else env.target_vta_cpu
# to start and end the graph packing relay pass: in other words # to start and end the graph packing relay pass: in other words
# where to start and finish offloading to VTA. # where to start and finish offloading to VTA.
network = "resnet18_v1" network = "resnet18_v1"
start_pack="nn.max_pool2d" start_pack = "nn.max_pool2d"
stop_pack="nn.global_avg_pool2d" stop_pack = "nn.global_avg_pool2d"
# Tuning option # Tuning option
log_file = "%s.%s.log" % (device, network) log_file = "%s.%s.log" % (device, network)
...@@ -209,12 +208,12 @@ tuning_option = { ...@@ -209,12 +208,12 @@ tuning_option = {
'measure_option': autotvm.measure_option( 'measure_option': autotvm.measure_option(
builder=autotvm.LocalBuilder(), builder=autotvm.LocalBuilder(),
runner=autotvm.RPCRunner( runner=autotvm.RPCRunner(env.TARGET,
env.TARGET, host=tracker_host, port=tracker_port, host=tracker_host,
number=5, port=tracker_port,
timeout=60, number=5,
check_correctness=True timeout=60,
), check_correctness=True),
), ),
} }
...@@ -240,6 +239,7 @@ tuning_option = { ...@@ -240,6 +239,7 @@ tuning_option = {
# Given that the tuning will be done on Pynq FPGA boards, make sure that # Given that the tuning will be done on Pynq FPGA boards, make sure that
# the ```TARGET`` entry in the ``vta_config.json`` file is set to ``pynq``. # the ```TARGET`` entry in the ``vta_config.json`` file is set to ``pynq``.
# You can skip the implementation of this function for this tutorial. # You can skip the implementation of this function for this tutorial.
def tune_tasks(tasks, def tune_tasks(tasks,
measure_option, measure_option,
...@@ -255,7 +255,7 @@ def tune_tasks(tasks, ...@@ -255,7 +255,7 @@ def tune_tasks(tasks,
os.remove(tmp_log_file) os.remove(tmp_log_file)
for i, tsk in enumerate(reversed(tasks)): for i, tsk in enumerate(reversed(tasks)):
prefix = "[Task %2d/%2d] " % (i+1, len(tasks)) prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
# create tuner # create tuner
if tuner == 'xgb' or tuner == 'xgb-rank': if tuner == 'xgb' or tuner == 'xgb-rank':
...@@ -276,23 +276,24 @@ def tune_tasks(tasks, ...@@ -276,23 +276,24 @@ def tune_tasks(tasks,
tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file)) tuner_obj.load_history(autotvm.record.load_from_file(tmp_log_file))
# do tuning # do tuning
n_trial = min(n_trial, len(tsk.config_space)) tsk_trial = min(n_trial, len(tsk.config_space))
tuner_obj.tune(n_trial=n_trial, tuner_obj.tune(n_trial=tsk_trial,
early_stopping=early_stopping, early_stopping=early_stopping,
measure_option=measure_option, measure_option=measure_option,
callbacks=[ callbacks=[
autotvm.callback.progress_bar(n_trial, prefix=prefix), autotvm.callback.progress_bar(tsk_trial, prefix=prefix),
autotvm.callback.log_to_file(tmp_log_file)]) autotvm.callback.log_to_file(tmp_log_file)
])
# pick best records to a cache file # pick best records to a cache file
autotvm.record.pick_best(tmp_log_file, log_filename) autotvm.record.pick_best(tmp_log_file, log_filename)
os.remove(tmp_log_file) os.remove(tmp_log_file)
######################################################################## ########################################################################
# Register VTA-specific tuning tasks # Register VTA-specific tuning tasks
def register_vta_tuning_tasks(): def register_vta_tuning_tasks():
from tvm.autotvm.task.topi_integration import TaskExtractEnv, deserialize_args from tvm.autotvm.task.topi_integration import TaskExtractEnv, deserialize_args
...@@ -330,11 +331,15 @@ def register_vta_tuning_tasks(): ...@@ -330,11 +331,15 @@ def register_vta_tuning_tasks():
######################################################################## ########################################################################
# Finally, we launch tuning jobs and evaluate the end-to-end performance. # Finally, we launch tuning jobs and evaluate the end-to-end performance.
def tune_and_evaluate(tuning_opt): def tune_and_evaluate(tuning_opt):
if env.TARGET != "sim": if env.TARGET != "sim":
# Get remote from fleet node # Get remote from fleet node
remote = autotvm.measure.request_remote(env.TARGET, tracker_host, tracker_port, timeout=10000) remote = autotvm.measure.request_remote(env.TARGET,
tracker_host,
tracker_port,
timeout=10000)
# Reconfigure the JIT runtime and FPGA. # Reconfigure the JIT runtime and FPGA.
vta.reconfig_runtime(remote) vta.reconfig_runtime(remote)
vta.program_fpga(remote, bitstream=None) vta.program_fpga(remote, bitstream=None)
...@@ -351,7 +356,7 @@ def tune_and_evaluate(tuning_opt): ...@@ -351,7 +356,7 @@ def tune_and_evaluate(tuning_opt):
mod = relay.Module.from_expr(relay_prog) mod = relay.Module.from_expr(relay_prog)
tasks = autotvm.task.extract_from_program(mod, tasks = autotvm.task.extract_from_program(mod,
params=params, params=params,
ops=(tvm.relay.op.nn.conv2d,), ops=(tvm.relay.op.nn.conv2d, ),
target=target, target=target,
target_host=env.target_host) target_host=env.target_host)
...@@ -361,17 +366,16 @@ def tune_and_evaluate(tuning_opt): ...@@ -361,17 +366,16 @@ def tune_and_evaluate(tuning_opt):
for tsk in tasks: for tsk in tasks:
inp = tsk.args[0][1] inp = tsk.args[0][1]
wgt = tsk.args[1][1] wgt = tsk.args[1][1]
batch = inp[0]*inp[4] batch = inp[0] * inp[4]
in_filter = inp[1]*inp[5] in_filter = inp[1] * inp[5]
out_filter = wgt[0]*wgt[4] out_filter = wgt[0] * wgt[4]
height, width = inp[2], inp[3] height, width = inp[2], inp[3]
hkernel, wkernel = wgt[2], wgt[3] hkernel, wkernel = wgt[2], wgt[3]
hstride, wstride = tsk.args[2][0], tsk.args[2][1] hstride, wstride = tsk.args[2][0], tsk.args[2][1]
hpad, wpad = tsk.args[3][0], tsk.args[3][1] hpad, wpad = tsk.args[3][0], tsk.args[3][1]
print("({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})".format( print("({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {})".format(
batch, height, width, in_filter, out_filter, batch, height, width, in_filter, out_filter, hkernel, wkernel,
hkernel, wkernel, hpad, wpad, hstride, wstride hpad, wpad, hstride, wstride))
))
# We do not run the tuning in our webpage server since it takes too long. # We do not run the tuning in our webpage server since it takes too long.
# Comment the following line to run it by yourself. # Comment the following line to run it by yourself.
...@@ -387,14 +391,17 @@ def tune_and_evaluate(tuning_opt): ...@@ -387,14 +391,17 @@ def tune_and_evaluate(tuning_opt):
print("Compile...") print("Compile...")
with relay.build_config(opt_level=3, disabled_pass={"AlterOpLayout"}): with relay.build_config(opt_level=3, disabled_pass={"AlterOpLayout"}):
if target.device_name != "vta": if target.device_name != "vta":
graph, lib, params = relay.build( graph, lib, params = relay.build(relay_prog,
relay_prog, target=target, target=target,
params=params, target_host=env.target_host) params=params,
target_host=env.target_host)
else: else:
with vta.build_config(): with vta.build_config():
graph, lib, params = relay.build( graph, lib, params = relay.build(
relay_prog, target=target, relay_prog,
params=params, target_host=env.target_host) target=target,
params=params,
target_host=env.target_host)
# Export library # Export library
print("Upload...") print("Upload...")
...@@ -421,6 +428,7 @@ def tune_and_evaluate(tuning_opt): ...@@ -421,6 +428,7 @@ def tune_and_evaluate(tuning_opt):
print("Mean inference time (std dev): %.2f ms (%.2f ms)" % print("Mean inference time (std dev): %.2f ms (%.2f ms)" %
(np.mean(prof_res), np.std(prof_res))) (np.mean(prof_res), np.std(prof_res)))
# Run the tuning and evaluate the results # Run the tuning and evaluate the results
tune_and_evaluate(tuning_option) tune_and_evaluate(tuning_option)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment