tvm_ffi.Object#
- class tvm_ffi.Object#
Bases:
CObjectBase class of all TVM FFI objects.
This is the root Python type for objects backed by the TVM FFI runtime. Each instance references a handle to a C++ runtime object. Python subclasses typically correspond to C++ runtime types and are registered via
tvm_ffi.register_object().Notes
Equality of two
Objectinstances uses underlying handle identity unless an overridden implementation is provided on the concrete type. Usesame_as()to check whether two references point to the same underlying object.Subclasses that omit
__slots__get__slots__ = ()injected automatically by the metaclass. To allow a per-instance__dict__, declare__slots__ = ("__dict__",)explicitly in the class body.Most users interact with subclasses (e.g.
Tensor,Function) rather thanObjectdirectly.
Examples
Constructing objects is typically performed by Python wrappers that call into registered constructors on the FFI side.
import tvm_ffi.testing # Acquire a testing object constructed through FFI obj = tvm_ffi.testing.create_object("testing.TestObjectBase", v_i64=12) assert isinstance(obj, tvm_ffi.Object) assert obj.same_as(obj)
Subclasses can declare explicit slots when needed.
@tvm_ffi.register_object("my.MyObject") class MyObject(tvm_ffi.Object): __slots__ = ()
Subclasses that need a per-instance
__dict__(e.g. for attribute caching) can opt in explicitly.@tvm_ffi.register_object("my.MyDynObject") class MyDynObject(tvm_ffi.Object): __slots__ = ("__dict__",)
- __init__()#
Methods
__ffi_init__(*args)Initialize the instance using the
__ffi_init__method registered on C++ side.__init_handle_by_constructor__(fconstructor, ...)Initialize the handle by calling constructor function.
__move_handle_from__(other)Steal the FFI handle from
other._move()Create an rvalue reference that transfers ownership.
same_as(other)Return
Trueif both references point to the same object.- __ffi_init__(*args)#
Initialize the instance using the
__ffi_init__method registered on C++ side.
- __init_handle_by_constructor__(fconstructor, *args)#
Initialize the handle by calling constructor function.
- Parameters:
- Return type:
Notes
We have a special calling convention to call constructor functions. So the return handle is directly set into the Node object instead of creating a new Node.
- __move_handle_from__(other)#
Steal the FFI handle from
other.Internal helper used by the runtime to implement move semantics. Users should prefer
_move().- Parameters:
other (
CObject)- Return type:
- _move()#
Create an rvalue reference that transfers ownership.
The returned
ObjectRValueRefindicates move semantics to the FFI layer, and is intended for performance-sensitive paths that wish to avoid an additional retain/release pair.Notes
After a successful move, the original object should be treated as invalid on the FFI side. Do not rely on the handle after transferring.
- Return type:
ObjectRValueRef- Returns:
ObjectRValueRef – The rvalue reference wrapper.
- same_as(other)#
Return
Trueif both references point to the same object.This checks identity of the underlying FFI handle rather than performing a structural, value-based comparison.
Examples
import tvm_ffi.testing x = tvm_ffi.testing.create_object("testing.TestObjectBase") y = x z = tvm_ffi.testing.create_object("testing.TestObjectBase") assert x.same_as(y) assert not x.same_as(z)