Apache TVM FFI
apache-tvm-ffi provides the foundational Foreign Function Interface (FFI) layer for the Apache TVM deep learning compiler stack, enabling Python to bind to and interact with C++ types and functions. It is currently at version 0.1.10 and receives frequent, minor updates.
Warnings
- breaking The Python-side field descriptor infrastructure for dataclasses was removed. If you were using `tvm-ffi` to define dataclasses with specific FFI-related field descriptors, this API has been removed.
- breaking The `tvm_ffi.Function.__init__` constructor, which was briefly introduced, was reverted. If you implemented code relying on a custom `__init__` for `tvm_ffi.Function` objects, it will break.
- gotcha The `apache-tvm-ffi` library is a low-level FFI binding for TVM's C++ runtime. It is not typically used as a standalone application library but rather as a foundational component for deeper integration with TVM or custom C++ extensions. Its API surface can be complex and requires understanding of TVM's C++ backend.
Install
-
pip install apache-tvm-ffi
Imports
- tvm_ffi
import tvm_ffi
- device
from tvm_ffi import device
- Object
from tvm_ffi.runtime_base import Object
- Function
from tvm_ffi.runtime_base import Function
Quickstart
import tvm_ffi
# tvm_ffi provides low-level FFI bindings, often used with TVM itself.
# A simple interaction is with device context management.
# Create a CPU device context
cpu_ctx = tvm_ffi.device("cpu", 0)
print(f"CPU context: {cpu_ctx}")
assert str(cpu_ctx) == "cpu(0)"
# Attempt to create a GPU device context (may fail if no GPU is present)
try:
gpu_ctx = tvm_ffi.device("cuda", 0)
print(f"CUDA context: {gpu_ctx}")
assert str(gpu_ctx) == "cuda(0)"
except tvm_ffi.TVMError as e:
print(f"Could not create CUDA context: {e}")
# The tvm_ffi.Object is a base class for runtime objects
from tvm_ffi.runtime_base import Object
class MyTVMObject(Object):
# Custom runtime objects inherit from tvm_ffi.runtime_base.Object
# This is often extended in C++ and exposed via FFI.
pass
# Note: Direct instantiation of Object in Python is usually for type hinting
# or for objects returned from FFI calls.
# obj = MyTVMObject() # This would typically require C++ backend setup