tvm.micro

MicroTVM module for bare-metal backends

Functions:

autotvm_build_func()

A dummy build function which causes autotvm to use a different export format.

get_standalone_crt_dir()

Find the standalone_crt directory.

get_microtvm_template_projects(platform)

Find microTVM template project directory for specific platform.

export_model_library_format(mods, file_name)

Export the build artifact in Model Library Format.

generate_project(template_project_dir, ...)

Generate a project for an embedded platform that contains the given model.

create_local_graph_executor(graph_json_str, ...)

Create a local graph executor driving execution on the remote CPU device given.

create_local_debug_executor(graph_json_str, ...)

Create a local debug runtime driving execution on the remote CPU device given.

Classes:

AutoTvmModuleLoader(template_project_dir[, ...])

MicroTVM AutoTVM Module Loader

GeneratedProject(api_client, options)

Defines a glue interface to interact with a generated project through the API server.

TemplateProject(api_client)

Defines a glue interface to interact with a template project through the API Server.

Session([transport_context_manager, ...])

MicroTVM Device Session

TransportLogger(name, child[, logger, level])

Wraps a Transport implementation and logs traffic to the Python logging infrastructure.

Exceptions:

UnsupportedInModelLibraryFormatError

Raised when export_model_library_format does not support the given Module tree.

SessionTerminatedError

Raised when a transport read operation discovers that the remote session is terminated.

tvm.micro.autotvm_build_func()

A dummy build function which causes autotvm to use a different export format.

class tvm.micro.AutoTvmModuleLoader(template_project_dir: Union[pathlib.Path, str], project_options: Optional[dict] = None)

MicroTVM AutoTVM Module Loader

Parameters
  • template_project_dir (Union[pathlib.Path, str]) – project template path

  • project_options (dict) – project generation option

tvm.micro.get_standalone_crt_dir() str

Find the standalone_crt directory.

Though the C runtime source lives in the tvm tree, it is intended to be distributed with any binary build of TVM. This source tree is intended to be integrated into user projects to run models targeted with –runtime=c.

Returns

The path to the standalone_crt

Return type

str

tvm.micro.get_microtvm_template_projects(platform: str) str

Find microTVM template project directory for specific platform.

Parameters

platform (str) – Platform type which should be defined in MicroTVMTemplateProject.

Returns

Path to template project directory for platform.

Return type

str

tvm.micro.export_model_library_format(mods: Union[tvm.driver.build_module.OperatorModule, tvm.relay.backend.executor_factory.AOTExecutorFactoryModule, tvm.relay.backend.executor_factory.GraphExecutorFactoryModule, List[Union[tvm.driver.build_module.OperatorModule, tvm.relay.backend.executor_factory.AOTExecutorFactoryModule, tvm.relay.backend.executor_factory.GraphExecutorFactoryModule]]], file_name: Union[str, pathlib.Path])

Export the build artifact in Model Library Format.

This function creates a .tar archive containing the build artifacts in a standardized layout. It’s intended to allow downstream automation to build TVM artifacts against the C runtime.

Parameters
  • mod (ExportableModule, List[ExportableModule]) – The return value of tvm.build or tvm.relay.build.

  • file_name (str) – Path to the .tar archive to generate.

Returns

file_name – The path to the generated .tar archive.

Return type

str

exception tvm.micro.UnsupportedInModelLibraryFormatError

Raised when export_model_library_format does not support the given Module tree.

tvm.micro.generate_project(template_project_dir: Union[pathlib.Path, str], module: Union[tvm.driver.build_module.OperatorModule, tvm.relay.backend.executor_factory.AOTExecutorFactoryModule, tvm.relay.backend.executor_factory.GraphExecutorFactoryModule], generated_project_dir: Union[pathlib.Path, str], options: Optional[dict] = None)

Generate a project for an embedded platform that contains the given model.

Parameters
  • template_project_path (pathlib.Path or str) – Path to a template project containing a microTVM Project API server.

  • generated_project_path (pathlib.Path or str) – Path to a directory to be created and filled with the built project.

  • module (ExportableModule) – A runtime.Module exportable as Model Library Format. The value returned from tvm.relay.build or tvm.build.

  • options (dict) – If given, Project API options given to the microTVM API server found in both template_project_path and generated_project_path.

Returns

A class that wraps the generated project and which can be used to further interact with it.

Return type

GeneratedProject

class tvm.micro.GeneratedProject(api_client, options)

Defines a glue interface to interact with a generated project through the API server.

class tvm.micro.TemplateProject(api_client)

Defines a glue interface to interact with a template project through the API Server.

Methods:

generate_project_from_mlf(...)

Generate a project from MLF file.

generate_project(graph_executor_factory, ...)

Generate a project given GraphRuntimeFactory.

generate_project_from_mlf(model_library_format_path, project_dir, options: dict)

Generate a project from MLF file.

generate_project(graph_executor_factory, project_dir, options)

Generate a project given GraphRuntimeFactory.

tvm.micro.create_local_graph_executor(graph_json_str, mod, device)

Create a local graph executor driving execution on the remote CPU device given.

Parameters
  • graph_json_str (str) – A string containing the graph representation.

  • mod (tvm.runtime.Module) – The remote module containing functions in graph_json_str.

  • device (tvm.runtime.Device) – The remote CPU execution device.

Returns

A local graph executor instance that executes on the remote device.

Return type

tvm.contrib.GraphExecutor

tvm.micro.create_local_debug_executor(graph_json_str, mod, device, dump_root=None)

Create a local debug runtime driving execution on the remote CPU device given.

Parameters
  • graph_json_str (str) – A string containing the graph representation.

  • mod (tvm.runtime.Module) – The remote module containing functions in graph_json_str.

  • device (tvm.runtime.Device) – The remote CPU execution device.

  • dump_root (Optional[str]) – If given, passed as dump_root= to GraphModuleDebug.

Returns

A local graph executor instance that executes on the remote device.

Return type

tvm.contrib.GraphExecutor

class tvm.micro.Session(transport_context_manager=None, session_name='micro-rpc', timeout_override=None)

MicroTVM Device Session

Parameters

config (dict) – configuration for this session (as generated by tvm.micro.device.host.default_config(), for example)

Example

c_mod = ...  # some module generated with "c" as the target
dev_config = micro.device.arm.stm32f746xx.default_config('127.0.0.1', 6666)
with tvm.micro.Session(dev_config) as sess:
    micro_mod = sess.create_micro_mod(c_mod)
exception tvm.micro.SessionTerminatedError

Raised when a transport read operation discovers that the remote session is terminated.

class tvm.micro.TransportLogger(name, child, logger=None, level=20)

Wraps a Transport implementation and logs traffic to the Python logging infrastructure.

Methods:

timeouts()

Return TransportTimeouts suitable for use with this transport.

open()

Open any resources needed to send and receive RPC protocol data for a single session.

close()

Release resources associated with this transport.

read(n, timeout_sec)

Read up to n bytes from the transport.

write(data, timeout_sec)

Write data to the transport channel.

timeouts()

Return TransportTimeouts suitable for use with this transport.

See the TransportTimeouts documentation in python/tvm/micro/session.py.

open()

Open any resources needed to send and receive RPC protocol data for a single session.

close()

Release resources associated with this transport.

read(n, timeout_sec)

Read up to n bytes from the transport.

Parameters
  • n (int) – Maximum number of bytes to read from the transport.

  • timeout_sec (Union[float, None]) – Number of seconds to wait for all n bytes to be received before timing out. The transport can wait additional time to account for transport latency or bandwidth limitations based on the selected configuration and number of bytes being received. If timeout_sec is 0, read should attempt to service the request in a non-blocking fashion. If timeout_sec is None, read should block until at least 1 byte of data can be returned.

Returns

Data read from the channel. Less than n bytes may be returned, but 0 bytes should never be returned. If returning less than n bytes, the full timeout_sec, plus any internally-added timeout, should be waited. If a timeout or transport error occurs, an exception should be raised rather than simply returning empty bytes.

Return type

bytes

Raises
  • TransportClosedError : – When the transport layer determines that the transport can no longer send or receive data due to an underlying I/O problem (i.e. file descriptor closed, cable removed, etc).

  • IoTimeoutError : – When timeout_sec elapses without receiving any data.

write(data, timeout_sec)

Write data to the transport channel.

Parameters
  • data (bytes) – The data to write over the channel.

  • timeout_sec (Union[float, None]) – Number of seconds to wait for at least one byte to be written before timing out. The transport can wait additional time to account for transport latency or bandwidth limitations based on the selected configuration and number of bytes being received. If timeout_sec is 0, write should attempt to service the request in a non-blocking fashion. If timeout_sec is None, write should block until at least 1 byte of data can be returned.

Returns

The number of bytes written to the underlying channel. This can be less than the length of data, but cannot be 0 (raise an exception instead).

Return type

int

Raises
  • TransportClosedError : – When the transport layer determines that the transport can no longer send or receive data due to an underlying I/O problem (i.e. file descriptor closed, cable removed, etc).

  • IoTimeoutError : – When timeout_sec elapses without receiving any data.