tvm.Function

class tvm.Function(handle, is_global)

The PackedFunc object used in TVM.

Function plays an key role to bridge front and backend in TVM. Function provide a type-erased interface, you can call function with positional arguments.

The compiled module returns Function. TVM backend also registers and exposes its API as Functions. For example, the developer function exposed in tvm.ir_pass are actually C++ functions that are registered as PackedFunc

The following are list of common usage scenario of tvm.Function.

  • Automatic exposure of C++ API into python
  • To call PackedFunc from python side
  • To call python callbacks to inspect results in generated code
  • Bring python hook into C++ backend

See also

tvm.register_func
How to register global function.
tvm.get_global_func
How to get global function.
tvm.register_func(func_name, f=None, override=False)

Register global function

Parameters:
  • func_name (str or function) – The function name
  • f (function, optional) – The function to be registered.
  • override (boolean optional) – Whether override existing entry.
Returns:

fregister – Register function if f is not specified.

Return type:

function

Examples

The following code registers my_packed_func as global function. Note that we simply get it back from global function table to invoke it from python side. However, we can also invoke the same function from C++ backend, or in the compiled TVM code.

targs = (10, 10.0, "hello")
@tvm.register_func
def my_packed_func(*args):
    assert(tuple(args) == targs)
    return 10
# Get it out from global function table
f = tvm.get_global_func("my_packed_func")
assert isinstance(f, tvm.nd.Function)
y = f(*targs)
assert y == 10
tvm.get_global_func(name, allow_missing=False)

Get a global function by name

Parameters:
  • name (str) – The name of the global function
  • allow_missing (bool) – Whether allow missing function or raise an error.
Returns:

func – The function to be returned, None if function is missing.

Return type:

tvm.Function