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

  • 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 code from module, if available.

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

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

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

See also

export the module to shared library.
time_evaluator(func_name, ctx, number, repeat=1)

Get an evaluator that measures time cost of running function.

  • 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 steps used in measuring each time interval
  • repeat (int, optional) – Number of times to run the timer measurement If repeat equals 3, then we will get 3 numbers in the ProfileResult.


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

Returns:ftimer – The function that takes same argument as func and return a float representing seconds per function call.
Return type:Function

Get type key of the module.

class tvm.module.ProfileResult(mean, results)

Alias for field number 0


Alias for field number 1


Whether module runtime is enabled for target

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


The following code checks if gpu is enabled.

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

Load module from file.

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

module – The loaded module

Return type:



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


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