tvm.micro#

MicroTVM module for bare-metal backends

Exceptions:

SessionTerminatedError

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

UnsupportedInModelLibraryFormatError

Raised when export_model_library_format does not support the given Module tree.

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.

Session([transport_context_manager, ...])

MicroTVM Device Session

TemplateProject(api_client)

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

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

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

Functions:

autotvm_build_func()

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

copy_crt_config_header(platform, output_path)

Copy crt_config header file for a platform to destinatin.

create_local_aot_executor(session)

Create a local AoT 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.

create_local_graph_executor(graph_json_str, ...)

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

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.

get_microtvm_template_projects(platform)

Find microTVM template project directory for specific platform.

get_standalone_crt_dir()

Find the standalone_crt directory.

exception tvm.micro.SessionTerminatedError[源代码]#

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

exception tvm.micro.UnsupportedInModelLibraryFormatError[源代码]#

Raised when export_model_library_format does not support the given Module tree.

class tvm.micro.AutoTvmModuleLoader(template_project_dir, project_options=None, project_dir=None, use_existing=False)[源代码]#

MicroTVM AutoTVM Module Loader

Parameters#

template_project_dirUnion[os.PathLike, str]

project template path

project_optionsdict

project generation option

project_dir: str

if use_existing is False: The path to save the generated microTVM Project. if use_existing is True: The path to a generated microTVM Project for debugging.

use_existing: bool

skips the project generation and opens transport to the project at the project_dir address.

参数:
class tvm.micro.GeneratedProject(api_client, options)[源代码]#

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

class tvm.micro.Session(transport_context_manager=None, session_name='micro-rpc', timeout_override=None)[源代码]#

MicroTVM Device Session

Parameters#

configdict

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)

Methods:

__enter__()

Initialize this session and establish an RPC session with the on-device RPC server.

__exit__(exc_type, exc_value, exc_traceback)

Tear down this session and associated RPC session resources.

__init__([transport_context_manager, ...])

Configure a new session.

__enter__()[源代码]#

Initialize this session and establish an RPC session with the on-device RPC server.

Returns#

Session :

Returns self.

__exit__(exc_type, exc_value, exc_traceback)[源代码]#

Tear down this session and associated RPC session resources.

__init__(transport_context_manager=None, session_name='micro-rpc', timeout_override=None)[源代码]#

Configure a new session.

Parameters#

transport_context_managerContextManager[transport.Transport]

If given, flasher and binary should not be given. On entry, this context manager should establish a transport between this TVM instance and the device.

session_namestr

Name of the session, used for debugging.

timeout_overrideTransportTimeouts

If given, TransportTimeouts that govern the way Receive() behaves. If not given, this is determined by calling has_flow_control() on the transport.

class tvm.micro.TemplateProject(api_client)[源代码]#

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

Methods:

_check_project_options(options)

Check if options are valid ProjectOptions

generate_project(graph_executor_factory, ...)

Generate a project given GraphRuntimeFactory.

generate_project_from_mlf(...)

Generate a project from MLF file.

_check_project_options(options)[源代码]#

Check if options are valid ProjectOptions

参数:

options (dict)

generate_project(graph_executor_factory, project_dir, options)[源代码]#

Generate a project given GraphRuntimeFactory.

generate_project_from_mlf(model_library_format_path, project_dir, options)[源代码]#

Generate a project from MLF file.

参数:

options (dict)

class tvm.micro.TransportLogger(name, child, logger=None, level=20)[源代码]#

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

Methods:

close()

Release resources associated with this transport.

open()

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

read(n, timeout_sec)

Read up to n bytes from the transport.

timeouts()

Return TransportTimeouts suitable for use with this transport.

write(data, timeout_sec)

Write data to the transport channel.

close()[源代码]#

Release resources associated with this transport.

open()[源代码]#

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

read(n, timeout_sec)[源代码]#

Read up to n bytes from the transport.

Parameters#

nint

Maximum number of bytes to read from the transport.

timeout_secUnion[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#

bytes :

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.

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.

timeouts()[源代码]#

Return TransportTimeouts suitable for use with this transport.

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

write(data, timeout_sec)[源代码]#

Write data to the transport channel.

Parameters#

databytes

The data to write over the channel.

timeout_secUnion[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#

int :

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).

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.

tvm.micro.autotvm_build_func()[源代码]#

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

tvm.micro.copy_crt_config_header(platform, output_path)[源代码]#

Copy crt_config header file for a platform to destinatin.

Parameters#

platformstr

Platform type which should be defined in MicroTVMTemplateProject.

output_path: Path

Output path for crt_config header file.

参数:
  • platform (str)

  • output_path (Path)

tvm.micro.create_local_aot_executor(session)[源代码]#

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

Parameters#

sessionSession

A microTVM device session.

Returns#

tvm.runtime.executor.aot_executor.AotModule :

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

参数:

session (Session)

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_strstr

A string containing the graph representation.

modtvm.runtime.Module

The remote module containing functions in graph_json_str.

devicetvm.runtime.Device

The remote CPU execution device.

dump_rootOptional[str]

If given, passed as dump_root= to GraphModuleDebug.

Returns#

tvm.contrib.GraphExecutor :

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

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_strstr

A string containing the graph representation.

modtvm.runtime.Module

The remote module containing functions in graph_json_str.

devicetvm.runtime.Device

The remote CPU execution device.

Returns#

tvm.contrib.GraphExecutor :

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

tvm.micro.export_model_library_format(mods, file_name)[源代码]#

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#

modExportableModule, List[ExportableModule]

The return value of tvm.build or tvm.relay.build.

file_namestr

Path to the .tar archive to generate.

Returns#

file_namestr

The path to the generated .tar archive.

参数:
  • mods (OperatorModule | AOTExecutorFactoryModule | GraphExecutorFactoryModule | List[OperatorModule | AOTExecutorFactoryModule | GraphExecutorFactoryModule])

  • file_name (str | Path)

tvm.micro.generate_project(template_project_dir, module, generated_project_dir, options=None)[源代码]#

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

Parameters#

template_project_pathpathlib.Path or str

Path to a template project containing a microTVM Project API server.

generated_project_pathpathlib.Path or str

Path to a directory to be created and filled with the built project.

moduleExportableModule

A runtime.Module exportable as Model Library Format. The value returned from tvm.relay.build or tvm.build.

optionsdict

If given, Project API options given to the microTVM API server found in both template_project_path and generated_project_path.

Returns#

GeneratedProject :

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

参数:
  • template_project_dir (Path | str)

  • module (OperatorModule | AOTExecutorFactoryModule | GraphExecutorFactoryModule)

  • generated_project_dir (Path | str)

  • options (dict)

tvm.micro.get_microtvm_template_projects(platform)[源代码]#

Find microTVM template project directory for specific platform.

Parameters#

platformstr

Platform type which should be defined in MicroTVMTemplateProject.

Returns#

str :

Path to template project directory for platform.

参数:

platform (str)

返回类型:

str

tvm.micro.get_standalone_crt_dir()[源代码]#

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#

str :

The path to the standalone_crt

返回类型:

str