tvm_ffi.system_lib

Contents

tvm_ffi.system_lib#

tvm_ffi.system_lib(symbol_prefix='')[source]#

Get system-wide library module singleton with functions prefixed by __tvm_ffi_{symbol_prefix}.

The library module contains symbols that are registered via TVMFFIEnvModRegisterSystemLibSymbol().

Note

The system lib is intended to be statically linked and loaded during the entire lifecycle of the program. If you want dynamic loading features, use DSO modules instead.

Parameters:

symbol_prefix (str, default: "") – Optional symbol prefix that can be used for search. When we lookup a symbol symbol_prefix + name will first be searched, then the name without symbol_prefix.

Return type:

Module

Returns:

The system-wide library module.

Examples

Register the function test_symbol_add_one in C++ with the name __tvm_ffi_test_symbol_add_one via TVMFFIEnvModRegisterSystemLibSymbol().

// A function to be registered in the system lib
static int test_symbol_add_one(void*, const TVMFFIAny* args, int32_t num_args, TVMFFIAny* ret) {
  TVM_FFI_SAFE_CALL_BEGIN();
  TVM_FFI_CHECK(num_args == 1, "Expected 1 argument, but got: " + std::to_string(num_args));
  int64_t x = reinterpret_cast<const AnyView*>(args)[0].cast<int64_t>();
  reinterpret_cast<Any*>(ret)[0] = x + 1;
  TVM_FFI_SAFE_CALL_END();
}

// Register the function with name `test_symbol_add_one` prefixed by `__tvm_ffi_`
int _ = TVMFFIEnvModRegisterSystemLibSymbol("__tvm_ffi_testing.add_one", reinterpret_cast<void*>(test_symbol_add_one));

Look up and call the function from Python:

import tvm_ffi

mod: tvm_ffi.Module = tvm_ffi.system_lib("testing.")  # symbols prefixed with `__tvm_ffi_testing.`
func: tvm_ffi.Function = mod["add_one"]  # looks up `__tvm_ffi_testing.add_one`
assert func(10) == 11