tvm_ffi.Module#

class tvm_ffi.Module(*args, **kwargs)[source]#

Bases: Object

Module container for dynamically loaded Module.

Examples

import tvm_ffi

# load the module from a tvm-ffi shared library
mod : tvm_ffi.Module = tvm_ffi.load_module("path/to/library.so")
# you can use mod.func_name to call the exported function
mod.func_name(*args)

Notes

If you load a module within a local scope, be careful when any called function creates and returns an object. The memory deallocation routines are part of the library’s code. If the module is unloaded before the object is destroyed, the deleter may call an invalid address. Keep the module loaded until all returned objects are deleted. You can safely use returned objects inside a nested function that finishes before the module goes out of scope. When possible, consider keeping the module alive in a long-lived/global scope (for example, in a global state) to avoid premature unloading.

def bad_pattern(x):
    # Bad: unload order of `tensor` and `mod` is not guaranteed
    mod = tvm_ffi.load_module("path/to/library.so")
    # ... do something with the tensor
    tensor = mod.func_create_and_return_tensor(x)

def good_pattern(x):
    # Good: `tensor` is freed before `mod` goes out of scope
    mod = tvm_ffi.load_module("path/to/library.so")
    def run_some_tests():
        tensor = mod.func_create_and_return_tensor(x)
        # ... do something with the tensor
    run_some_tests()
Parameters:
__init__(*args, **kwargs)#
Parameters:
Return type:

None

Methods

__ffi_init__(*args)

Defined in Object as method __ffi_init__().

__init__(*args, **kwargs)

clear_imports()

Remove all imports of the module.

get_function(name[, query_imports])

Get function from the module.

get_property_mask()

Get the runtime module property mask.

get_write_formats()

Get the format of the module.

implements_function(name[, query_imports])

Return True if the module defines a global function.

import_module(module)

Add module to the import list of current one.

inspect_source([fmt])

Get source code from module, if available.

is_binary_serializable()

Return whether the module is binary serializable (supports save_to_bytes).

is_compilation_exportable()

Return whether the module is compilation exportable.

is_runnable()

Return whether the module is runnable (supports get_function).

same_as(other)

Defined in Object as method same_as().

write_to_file(file_name[, fmt])

Write the current module to file.

Attributes

entry_name

imports

Get imported modules.

imports_

kind

Get type key of the module.

property imports_#
entry_name: ClassVar[str] = 'main'#
property kind: str#

Get type key of the module.

property imports: list[Module]#

Get imported modules.

Returns:

modules – The module

implements_function(name, query_imports=False)[source]#

Return True if the module defines a global function.

Note

that has_function(name) does not imply get_function(name) is non-null since the module that has_function(name) does not imply get_function(name) is non-null since the module may be, eg, a CSourceModule which cannot supply a packed-func implementation of the function without further compilation. However, get_function(name) non null should always imply has_function(name).

Parameters:
  • name (str) – The name of the function

  • query_imports (bool, default: False) – Whether to also query modules imported by this module.

Return type:

bool

Returns:

b – True if module (or one of its imports) has a definition for name.

get_function(name, query_imports=False)[source]#

Get function from the module.

Parameters:
  • name (str) – The name of the function

  • query_imports (bool, default: False) – Whether also query modules imported by this module.

Return type:

Function

Returns:

f – The result function.

import_module(module)[source]#

Add module to the import list of current one.

Parameters:

module (Module) – The other module.

Return type:

None

inspect_source(fmt='')[source]#

Get source code from module, if available.

Parameters:

fmt (str, default: "") – The specified format.

Return type:

str

Returns:

source – The result source code.

get_write_formats()[source]#

Get the format of the module.

Return type:

Sequence[str]

get_property_mask()[source]#

Get the runtime module property mask. The mapping is stated in ModulePropertyMask.

Return type:

int

Returns:

mask – Bitmask of runtime module property

is_binary_serializable()[source]#

Return whether the module is binary serializable (supports save_to_bytes).

Return type:

bool

Returns:

b – True if the module is binary serializable.

is_runnable()[source]#

Return whether the module is runnable (supports get_function).

Return type:

bool

Returns:

b – True if the module is runnable.

is_compilation_exportable()[source]#

Return whether the module is compilation exportable.

write_to_file is supported for object or source.

Return type:

bool

Returns:

b – True if the module is compilation exportable.

clear_imports()[source]#

Remove all imports of the module.

Return type:

None

write_to_file(file_name, fmt='')[source]#

Write the current module to file.

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

  • fmt (str, default: "") – The format of the file.

Return type:

None

See also

runtime.Module.export_library

export the module to shared library.