tvm_ffi.Module#
- class tvm_ffi.Module(*args, **kwargs)[source]#
Bases:
ObjectModule container for dynamically loaded Module.
Examples
import tvm_ffi # Load the module from a shared library mod = tvm_ffi.load_module("path/to/library.so") # Call exported function mod.func_name(*args) # Query function metadata (type signature) metadata = mod.get_function_metadata("func_name") # Query function documentation (if available) doc = mod.get_function_doc("func_name")
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()
Methods
__ffi_init__(*args)Defined in
Objectas method__ffi_init__().__init__(*args, **kwargs)Remove all imports of the module.
get_function(name[, query_imports])Get function from the module.
get_function_doc(name[, query_imports])Get documentation string for a function exported from the module.
get_function_metadata(name[, query_imports])Get metadata for a function exported from the module.
Get the runtime module property mask.
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.
Return whether the module is binary serializable (supports save_to_bytes).
Return whether the module is compilation exportable.
Return whether the module is runnable (supports get_function).
same_as(other)write_to_file(file_name[, fmt])Write the current module to file.
Attributes
Get imported modules.
Get type key of the module.
- property imports_#
- entry_name: ClassVar[str] = 'main'#
- implements_function(name, query_imports=False)[source]#
Return True if the module defines a global function.
Notes
implements_function(name)does not guarantee thatget_function(name)will return a callable, because some module kinds (e.g. a source-only module) may not provide a packed function implementation until further compilation occurs. However, a non-null result fromget_function(name)should imply the module implements the function.
- get_function_metadata(name, query_imports=False)[source]#
Get metadata for a function exported from the module.
This retrieves metadata for functions exported via c:macro:TVM_FFI_DLL_EXPORT_TYPED_FUNC and when c:macro:TVM_FFI_DLL_EXPORT_INCLUDE_METADATA is on, which includes type schema information.
- Parameters:
- Return type:
- Returns:
metadata – A dictionary containing function metadata. The
type_schemafield encodes the callable signature.
Examples
import tvm_ffi from tvm_ffi.core import TypeSchema import json mod = tvm_ffi.load_module("add_one_cpu.so") metadata = mod.get_function_metadata("add_one_cpu") schema = TypeSchema.from_json_str(metadata["type_schema"]) print(schema) # Shows function signature
See also
tvm_ffi.get_global_func_metadata()Get metadata for global registry functions.
- get_function_doc(name, query_imports=False)[source]#
Get documentation string for a function exported from the module.
This retrieves documentation for functions exported via c:macro:TVM_FFI_DLL_EXPORT_TYPED_FUNC_DOC.
- Parameters:
- Return type:
- Returns:
doc (str or None) – The documentation string if available, None otherwise.
Examples
import tvm_ffi mod = tvm_ffi.load_module("mylib.so") doc = mod.get_function_doc("process_batch") if doc: print(doc)
See also
get_function_metadata()Get metadata including type schema.
- get_property_mask()[source]#
Get the runtime module property mask. The mapping is stated in ModulePropertyMask.
- Return type:
- Returns:
Bitmask of runtime module property
- is_binary_serializable()[source]#
Return whether the module is binary serializable (supports save_to_bytes).
- Return type:
- Returns:
True if the module is binary serializable.
- is_runnable()[source]#
Return whether the module is runnable (supports get_function).
- Return type:
- Returns:
True if the module is runnable.