tvm.module

Container of compiled functions of TVM.

class tvm.module.Module(handle)

Module container of all TVM generated functions

export_library(file_name, fcompile=None, **kwargs)

Export the module and its imported device code one library.

This function only works on host llvm modules. It will pack all the imported modules

Parameters:
  • file_name (str) – The name of the shared library.
  • fcompile (function(target, file_list, kwargs), optional) – Compilation function to use create dynamic library. If fcompile has attribute object_format, will compile host library to that format. Otherwise, will use default format “o”.
  • kwargs (dict, optional) – Additional arguments passed to fcompile
get_source(fmt='')

Get source code from module, if available.

Parameters:fmt (str, optional) – The specified format.
Returns:source – The result source code.
Return type:str
imported_modules

Get imported modules

Returns:modules – The module
Return type:list of Module
save(file_name, fmt='')

Save the module to file.

This do not save the dependent device modules. See also export_shared

Parameters:
  • file_name (str) – The name of the file.
  • fmt (str) – The format of the file.

See also

Module.export_library()
export the module to shared library.
time_evaluator(func_name, ctx, number=10, repeat=1, min_repeat_ms=0)

Get an evaluator that measures time cost of running function.

Parameters:
  • func_name (str) – The name of the function in the module.
  • ctx (TVMContext) – The context we should run this function on.
  • number (int) – The number of times to run this function for taking average. We call these runs as one repeat of measurement.
  • repeat (int, optional) – The number of times to repeat the measurement. In total, the function will be invoked (1 + number x repeat) times, where the first one is warm up and will be discarded. The returned result contains repeat costs, each of which is an average of number costs.
  • min_repeat_ms (int, optional) – The minimum duration of one repeat in milliseconds. By default, one repeat contains number runs. If this parameter is set, the parameters number will be dynamically adjusted to meet the minimum duration requirement of one repeat. i.e., When the run time of one repeat falls below this time, the number parameter will be automatically increased.

Note

The function will be invoked (1 + number x repeat) times, with the first call discarded in case there is lazy initialization.

Returns:ftimer – The function that takes same argument as func and returns a ProfileResult. The ProfileResult reports repeat time costs in seconds.
Return type:Function
type_key

Get type key of the module.

class tvm.module.ProfileResult(mean, results)
mean

Alias for field number 0

results

Alias for field number 1

tvm.module.enabled(target)

Whether module runtime is enabled for target

Parameters:target (str) – The target device type.
Returns:enabled – Whether runtime is enabled.
Return type:bool

Examples

The following code checks if gpu is enabled.

>>> tvm.module.enabled("gpu")
tvm.module.load(path, fmt='')

Load module from file.

Parameters:
  • path (str) – The path to the module file.
  • fmt (str, optional) – The format of the file, if not specified it will be inferred from suffix of the file.
Returns:

module – The loaded module

Return type:

Module

Note

This function will automatically call cc.create_shared if the path is in format .o or .tar

tvm.module.system_lib()

Get system-wide library module singleton.

System lib is a global module that contains self register functions in startup. Unlike normal dso modules which need to be loaded explicitly. It is useful in environments where dynamic loading api like dlopen is banned.

To build system lib function, simply specify target option `llvm --system-lib` The system lib will be available as long as the result code is linked by the program.

The system lib is intended to be linked and loaded during the entire life-cyle of the program. If you want dynamic loading features, use dso modules instead.

Returns:module – The system-wide library module.
Return type:Module