Ejemplo n.º 1
0
def _make_session(model, target, zephyr_board, west_cmd, mod, build_config):
    parent_dir = os.path.dirname(__file__)
    filename = os.path.splitext(os.path.basename(__file__))[0]
    prev_build = f"{os.path.join(parent_dir, 'archive')}_{filename}_{zephyr_board}_last_build.micro"
    workspace_root = os.path.join(
        f"{os.path.join(parent_dir, 'workspace')}_{filename}_{zephyr_board}",
        datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S"),
    )
    workspace_parent = os.path.dirname(workspace_root)
    if not os.path.exists(workspace_parent):
        os.makedirs(workspace_parent)
    workspace = tvm.micro.Workspace(debug=True, root=workspace_root)

    test_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
    tvm_source_dir = os.path.join(test_dir, "..", "..", "..")
    runtime_path = os.path.join(tvm_source_dir, "apps", "microtvm", "zephyr",
                                "host_driven")
    compiler = zephyr.ZephyrCompiler(
        project_dir=runtime_path,
        board=zephyr_board,
        zephyr_toolchain_variant="zephyr",
        west_cmd=west_cmd,
    )

    opts = tvm.micro.default_options(os.path.join(runtime_path, "crt"))
    # TODO(weberlo) verify this is necessary
    opts["bin_opts"]["ccflags"] = ["-std=gnu++14"]
    opts["lib_opts"]["ccflags"] = ["-std=gnu++14"]

    flasher_kw = {}
    if build_config["debug"]:
        flasher_kw["debug_rpc_session"] = tvm.rpc.connect("127.0.0.1", 9090)

    session_kw = {
        "flasher": compiler.flasher(**flasher_kw),
    }

    if not build_config["skip_build"]:
        session_kw["binary"] = tvm.micro.build_static_runtime(
            # the x86 compiler *expects* you to give the exact same dictionary for both
            # lib_opts and bin_opts. so the library compiler is mutating lib_opts and
            # the binary compiler is expecting those mutations to be in bin_opts.
            # TODO(weberlo) fix this very bizarre behavior
            workspace,
            compiler,
            mod,
            opts,
        )
        if os.path.exists(prev_build):
            os.unlink(prev_build)
        session_kw["binary"].archive(prev_build, metadata_only=True)
    else:
        unarchive_dir = utils.tempdir()
        session_kw["binary"] = tvm.micro.MicroBinary.unarchive(
            prev_build, unarchive_dir.relpath("binary"))

    return tvm.micro.Session(**session_kw)
Ejemplo n.º 2
0
def _make_session(model, target, zephyr_board, west_cmd, mod):
    test_name = f"{os.path.splitext(os.path.abspath(__file__))[0]}-{model}"
    prev_build = f"{test_name}-last-build.micro-binary"
    workspace_root = (
        f'{test_name}-workspace/{datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")}'
    )
    workspace_parent = os.path.dirname(workspace_root)
    if not os.path.exists(workspace_parent):
        os.makedirs(workspace_parent)
    workspace = tvm.micro.Workspace(debug=True, root=workspace_root)

    project_dir = os.path.join(
        os.path.dirname(__file__) or ".", "zephyr-runtime")
    compiler = zephyr.ZephyrCompiler(
        project_dir=project_dir,
        board=zephyr_board,
        zephyr_toolchain_variant="zephyr",
        west_cmd=west_cmd,
    )

    opts = tvm.micro.default_options(f"{project_dir}/crt")
    # TODO(weberlo) verify this is necessary
    opts["bin_opts"]["ccflags"] = ["-std=gnu++14"]
    opts["lib_opts"]["ccflags"] = ["-std=gnu++14"]

    flasher_kw = {}
    if DEBUG:
        flasher_kw["debug_rpc_session"] = tvm.rpc.connect("127.0.0.1", 9090)

    session_kw = {
        "flasher": compiler.flasher(**flasher_kw),
    }

    if BUILD:
        session_kw["binary"] = tvm.micro.build_static_runtime(
            # the x86 compiler *expects* you to give the exact same dictionary for both
            # lib_opts and bin_opts. so the library compiler is mutating lib_opts and
            # the binary compiler is expecting those mutations to be in bin_opts.
            # TODO(weberlo) fix this very bizarre behavior
            workspace,
            compiler,
            mod,
            lib_opts=opts["lib_opts"],
            bin_opts=opts["bin_opts"],
        )
        if os.path.exists(prev_build):
            os.unlink(prev_build)
        session_kw["binary"].archive(prev_build, metadata_only=True)
    else:
        unarchive_dir = utils.tempdir()
        session_kw["binary"] = tvm.micro.MicroBinary.unarchive(
            prev_build, unarchive_dir.relpath("binary"))

    return tvm.micro.Session(**session_kw)
Ejemplo n.º 3
0
def _build_session_kw(model, target, zephyr_board, west_cmd, mod, runtime_path,
                      build_config):
    parent_dir = os.path.dirname(__file__)
    filename = os.path.splitext(os.path.basename(__file__))[0]
    prev_build = f"{os.path.join(parent_dir, 'archive')}_{filename}_{zephyr_board}_last_build.micro"
    workspace_root = os.path.join(
        f"{os.path.join(parent_dir, 'workspace')}_{filename}_{zephyr_board}",
        datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S"),
    )
    workspace_parent = os.path.dirname(workspace_root)
    if not os.path.exists(workspace_parent):
        os.makedirs(workspace_parent)
    workspace = tvm.micro.Workspace(debug=True, root=workspace_root)

    compiler = zephyr.ZephyrCompiler(
        project_dir=runtime_path,
        board=zephyr_board,
        zephyr_toolchain_variant="zephyr",
        west_cmd=west_cmd,
        env_vars={"ZEPHYR_RUNTIME": "ZEPHYR-AOT"},
    )

    opts = tvm.micro.default_options(os.path.join(runtime_path, "crt"))
    opts["bin_opts"]["include_dirs"].append(
        os.path.join(runtime_path, "include"))
    opts["lib_opts"]["include_dirs"].append(
        os.path.join(runtime_path, "include"))

    flasher_kw = {}
    if build_config["debug"]:
        flasher_kw["debug_rpc_session"] = tvm.rpc.connect("127.0.0.1", 9090)

    session_kw = {
        "flasher": compiler.flasher(**flasher_kw),
    }

    if not build_config["skip_build"]:
        session_kw["binary"] = tvm.micro.build_static_runtime(
            workspace,
            compiler,
            mod,
            opts,
            executor="aot",
            extra_libs=[tvm.micro.get_standalone_crt_lib("memory")],
        )
        if os.path.exists(prev_build):
            os.unlink(prev_build)
        session_kw["binary"].archive(prev_build, metadata_only=True)
    else:
        unarchive_dir = utils.tempdir()
        session_kw["binary"] = tvm.micro.MicroBinary.unarchive(
            prev_build, unarchive_dir.relpath("binary"))

    return session_kw
Ejemplo n.º 4
0
def eval_micro_dev(args, model_inst, compiled_model, samples):
    opts = model_inst.get_micro_compiler_opts()
    opts['lib_opts']['cmake_args'] = ['-DCMAKE_VERBOSE_MAKEFILE=1']
    compiler = zephyr.ZephyrCompiler(util.get_zephyr_project_root(),
                                     board=args.zephyr_board,
                                     zephyr_toolchain_variant='zephyr',
                                     west_cmd=args.west_cmd)

    latest_symlink = get_latest_symlink(model_inst.config)
    micro_bin_path = os.path.join(latest_symlink, MICRO_BINARY_FILENAME)
    graph_json_path = os.path.join(latest_symlink, 'graph.json')
    if args.skip_micro_build:
        micro_bin = tvm.micro.MicroBinary.unarchive(
            micro_bin_path,
            get_micro_workspace().relpath('unarchive-micro-binary'))
        with open(graph_json_path) as json_f:
            graph_json_str = json_f.read()

    else:
        lowered = model_inst.lower_model(compiled_model)
        print('lowered', lowered.lib)

        micro_bin = tvm.micro.build_static_runtime(get_micro_workspace(),
                                                   compiler, lowered.lib, opts)
        if os.path.lexists(latest_symlink):
            os.unlink(latest_symlink)
        os.symlink(
            os.path.relpath(get_micro_workspace().tempdir.temp_dir,
                            os.path.dirname(latest_symlink)), latest_symlink)
        micro_bin.archive(micro_bin_path, metadata_only=True)
        with open(graph_json_path, 'w') as json_f:
            json_f.write(lowered.get_json())
        graph_json_str = lowered.get_json()

    with contextlib.ExitStack() as with_stack:
        debug_session = None
        if args.debug_micro_execution:
            hostport = args.microtvm_debug_shell_hostport.rsplit(':', 1)
            debug_session = with_stack.enter_context(
                connect_debug_shell(hostport[0], int(hostport[1])))

        sess = with_stack.enter_context(
            tvm.micro.Session(
                binary=micro_bin,
                flasher=compiler.flasher(debug_rpc_session=debug_session)))

        _LOG.debug('[Executing]')
        device = sess.device
        if args.use_debug_executor:
            mod = tvm.micro.session.create_local_debug_executor(
                graph_json_str,
                sess.get_system_lib(),
                device,
                #                1, 1, 1,
                dump_root=f'{util.get_repo_root()}/debug/micro')
        else:
            mod = tvm.micro.create_local_graph_executor(
                graph_json_str, sess.get_system_lib(), device)

        target_str = str(model_inst.target)
        if '-link-params=0' in target_str or '-link-params' not in target_str:
            total_bytes = 0
            for k, v in lowered.params.items():
                num_bytes = np.prod(v.shape) * 4
                _LOG.debug('input %s (%r): %d bytes', k, v.shape, num_bytes)
                total_bytes += num_bytes

            _LOG.info("total parameter bytes to set: %d", total_bytes)

            for k, v in lowered.params.items():
                _LOG.debug('set param %s', k)
                mod.set_input(k, v)

        results = []
        for i, sample in enumerate(samples):
            inputs = adapt_all_inputs(
                model_inst, sample,
                compiled_model.ir_mod[compiled_model.entry_point])

            _LOG.debug('Setting model input')
            mod.set_input(**inputs)
            _LOG.debug('ctx sync()')
            device.sync()  # Ensure all args have been pushed to the device.
            _LOG.debug('run inference')
            mod.run()
            _LOG.debug('ctx sync()')
            device.sync()  # This actually executes the on-device task queue.
            _LOG.debug('Retrieving model output')
            results.append(adapt_all_outputs(model_inst, mod, sample))
            _LOG.info('got prediction: %r', results[-1])

    return results
Ejemplo n.º 5
0
#TARGET = tvm.target.target.micro("host")
TARGET = tvm.target.target.micro("stm32f746xx")

with tvm.transform.PassContext(opt_level=3,
                               config={"tir.disable_vectorize": True},
                               disabled_pass=["FuseOps"]):
    graph, c_mod, c_params = relay.build(mod, target=TARGET, params=params)

workspace = tvm.micro.Workspace(debug=True)

# github : https://github.com/tom-gall/zephyr-runtime.git
project_dir = os.path.join("/home/tgall/tvm/", "zephyr-runtime")

compiler = zephyr.ZephyrCompiler(
    project_dir=project_dir,
    board="stm32f746g_disco",
    zephyr_toolchain_variant="zephyr",
)
opts = tvm.micro.default_options(f"{project_dir}/crt")
opts["bin_opts"]["ccflags"] = ["-std=gnu++14"]
opts["lib_opts"]["ccflags"] = ["-std=gnu++14"]

micro_binary = tvm.micro.build_static_runtime(
    # the x86 compiler *expects* you to give the exact same dictionary for both
    # lib_opts and bin_opts. so the library compiler is mutating lib_opts and
    # the binary compiler is expecting those mutations to be in bin_opts.
    # TODO(weberlo) fix this very bizarre behavior
    workspace,
    compiler,
    c_mod,
    lib_opts=opts["bin_opts"],