def tune_and_evaluate(tuning_opt):
    print('Extract tasks...')
    (mod, params, input_shape, _) = get_network(network, batch_size=1)
    tasks = autotvm.task.extract_from_program(mod['main'], target=target, params=params, ops=(relay.op.get('nn.conv2d'),))
    print('Tuning...')
    tune_tasks(tasks, **tuning_opt)
    with autotvm.apply_history_best(log_file):
        print('Compile...')
        with tvm.transform.PassContext(opt_level=3):
            lib = relay.build_module.build(mod, target=target, params=params)
        tmp = tempdir()
        if use_android:
            from tvm.contrib import ndk
            filename = 'net.so'
            lib.export_library(tmp.relpath(filename), ndk.create_shared)
        else:
            filename = 'net.tar'
            lib.export_library(tmp.relpath(filename))
        print('Upload...')
        remote = autotvm.measure.request_remote(device_key, '127.0.0.1', 9190, timeout=10000)
        remote.upload(tmp.relpath(filename))
        rlib = remote.load_module(filename)
        dev = remote.device(str(target), 0)
        module = runtime.GraphModule(rlib['default'](dev))
        data_tvm = tvm.nd.array(np.random.uniform(size=input_shape).astype(dtype))
        module.set_input('data', data_tvm)
        print('Evaluate inference time cost...')
        ftimer = module.module.time_evaluator('run', dev, number=1, repeat=30)
        prof_res = (np.array(ftimer().results) * 1000)
        print(('Mean inference time (std dev): %.2f ms (%.2f ms)' % (np.mean(prof_res), np.std(prof_res))))
def tune_and_evaluate(tuning_opt):
    print('Extract tasks...')
    (mod, params, input_shape, _) = get_network(network, batch_size=1)
    tasks = autotvm.task.extract_from_program(mod['main'], target=target, params=params, ops=(relay.op.get('nn.conv2d'),))
    print('Tuning...')
    tune_tasks(tasks, **tuning_opt)
    with autotvm.apply_history_best(log_file):
        print('Compile...')
        with tvm.transform.PassContext(opt_level=3):
            lib = relay.build_module.build(mod, target=target, params=params)
        tmp = tempdir()
        if use_android:
            from tvm.contrib import ndk
            filename = 'net.so'
            lib.export_library(tmp.relpath(filename), ndk.create_shared)
        else:
            filename = 'net.tar'
            lib.export_library(tmp.relpath(filename))
        print('Upload...')
        remote = autotvm.measure.request_remote(device_key, '127.0.0.1', 9190, timeout=10000)
        remote.upload(tmp.relpath(filename))
        rlib = remote.load_module(filename)
        dev = remote.device(str(target), 0)
        module = runtime.GraphModule(rlib['default'](dev))
        data_tvm = tvm.nd.array(np.random.uniform(size=input_shape).astype(dtype))
        module.set_input('data', data_tvm)
        print('Evaluate inference time cost...')
        ftimer = module.module.time_evaluator('run', dev, number=1, repeat=30)
        prof_res = (np.array(ftimer().results) * 1000)
        print(('Mean inference time (std dev): %.2f ms (%.2f ms)' % (np.mean(prof_res), np.std(prof_res))))