Apache TVM FFI

0.1.10 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

Demonstrates basic device context creation using `tvm_ffi.device` and shows the `tvm_ffi.runtime_base.Object` base class for extending FFI objects.

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

view raw JSON →