Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ def _link_inherited_members(app, what, name, obj, options, lines) -> None: # no
# If it comes from builtins we already hide it; no link needed
if base in _py_native_classes or getattr(base, "__module__", "") == "builtins":
return
owner_fq = f"{base.__module__}.{base.__qualname__}".replace("tvm_ffi.core.", "tvm_ffi.")
owner_fq = f"{base.__module__}.{base.__qualname__}".replace(
"tvm_ffi.core.", "tvm_ffi."
).replace(".CObject", ".Object")
role = "attr" if what in {"attribute", "property"} else "meth"
lines.clear()
lines.append(
Expand Down Expand Up @@ -329,6 +331,9 @@ def _import_cls(cls_name: str) -> type | None:
"__ffi_init__",
"__from_extern_c__",
"__from_mlir_packed_safe_call__",
"_move",
"__move_handle_from__",
"__init_handle_by_constructor__",
}
# If a member method comes from one of these native types, hide it in the docs
_py_native_classes: tuple[type, ...] = (
Expand Down
6 changes: 4 additions & 2 deletions python/tvm_ffi/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _TRACEBACK_TO_BACKTRACE_STR: Callable[[types.TracebackType | None], str] | None
# DLPack protocol version (defined in tensor.pxi)
__dlpack_version__: tuple[int, int]

class Object:
class CObject:
def __ctypes_handle__(self) -> Any: ...
def __chandle__(self) -> int: ...
def __reduce__(self) -> Any: ...
Expand All @@ -46,7 +46,9 @@ class Object:
def __ffi_init__(self, *args: Any) -> None: ...
def same_as(self, other: Any) -> bool: ...
def _move(self) -> ObjectRValueRef: ...
def __move_handle_from__(self, other: Object) -> None: ...
def __move_handle_from__(self, other: CObject) -> None: ...

class Object(CObject): ...

class ObjectConvertible:
def asobject(self) -> Object: ...
Expand Down
1 change: 1 addition & 0 deletions python/tvm_ffi/cython/base.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ cdef extern from "tvm_ffi_python_helpers.h":


cdef class ByteArrayArg:
__slots__ = ()
cdef TVMFFIByteArray cdata
cdef object py_data

Expand Down
1 change: 1 addition & 0 deletions python/tvm_ffi/cython/device.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ cdef class Device:
assert str(dev) == "cuda:0"

"""
__slots__ = ()
cdef DLDevice cdevice

_DEVICE_TYPE_TO_NAME = {
Expand Down
1 change: 1 addition & 0 deletions python/tvm_ffi/cython/dtype.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ cdef class DataType:
assert str(d) == "int32"

"""
__slots__ = ()
cdef DLDataType cdtype

def __init__(self, dtype_str: str) -> None:
Expand Down
7 changes: 4 additions & 3 deletions python/tvm_ffi/cython/error.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _WITH_APPEND_BACKTRACE: Optional[Callable[[BaseException, str], BaseException]]
_TRACEBACK_TO_BACKTRACE_STR: Optional[Callable[[types.TracebackType | None], str]] = None


cdef class Error(Object):
cdef class Error(CObject):
"""Base class for FFI errors.

An :class:`Error` is a lightweight wrapper around a concrete Python
Expand All @@ -43,6 +43,7 @@ cdef class Error(Object):
Do not directly raise this object. Instead, use :py:meth:`py_error`
to convert it to a Python exception and raise that.
"""
__slots__ = ()

def __init__(self, kind: str, message: str, backtrace: str):
"""Construct an error wrapper.
Expand All @@ -66,7 +67,7 @@ cdef class Error(Object):
)
if ret != 0:
raise MemoryError("Failed to create error object")
(<Object>self).chandle = out
(<CObject>self).chandle = out

def update_backtrace(self, backtrace: str) -> None:
"""Replace the stored backtrace string with ``backtrace``.
Expand Down Expand Up @@ -107,7 +108,7 @@ cdef class Error(Object):
cdef inline Error move_from_last_error():
# raise last error
error = Error.__new__(Error)
TVMFFIErrorMoveFromRaised(&(<Object>error).chandle)
TVMFFIErrorMoveFromRaised(&(<CObject>error).chandle)
return error


Expand Down
48 changes: 24 additions & 24 deletions python/tvm_ffi/cython/function.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ cdef int TVMFFIPyArgSetterTensor_(
TVMFFIPyArgSetter* handle, TVMFFIPyCallContext* ctx,
PyObject* arg, TVMFFIAny* out
) except -1:
if (<Object>arg).chandle != NULL:
if (<CObject>arg).chandle != NULL:
out.type_index = kTVMFFITensor
out.v_ptr = (<Tensor>arg).chandle
else:
Expand All @@ -144,8 +144,8 @@ cdef int TVMFFIPyArgSetterObject_(
TVMFFIPyArgSetter* handle, TVMFFIPyCallContext* ctx,
PyObject* arg, TVMFFIAny* out
) except -1:
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
return 0


Expand Down Expand Up @@ -312,7 +312,7 @@ cdef int TVMFFIPyArgSetterFFIObjectProtocol_(
"""Setter for objects that implement the `__tvm_ffi_object__` protocol."""
cdef object arg = <object>py_arg
cdef TVMFFIObjectHandle temp_chandle
cdef Object obj = arg.__tvm_ffi_object__()
cdef CObject obj = arg.__tvm_ffi_object__()
cdef long ref_count = Py_REFCNT(obj)
temp_chandle = obj.chandle
out.type_index = TVMFFIObjectGetTypeIndex(temp_chandle)
Expand Down Expand Up @@ -418,8 +418,8 @@ cdef int TVMFFIPyArgSetterPyNativeObjectStr_(
# need to check if the arg is a large string returned from ffi
if arg._tvm_ffi_cached_object is not None:
arg = arg._tvm_ffi_cached_object
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
return 0
return TVMFFIPyArgSetterStr_(handle, ctx, py_arg, out)

Expand Down Expand Up @@ -457,8 +457,8 @@ cdef int TVMFFIPyArgSetterPyNativeObjectBytes_(
# need to check if the arg is a large bytes returned from ffi
if arg._tvm_ffi_cached_object is not None:
arg = arg._tvm_ffi_cached_object
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
return 0
return TVMFFIPyArgSetterBytes_(handle, ctx, py_arg, out)

Expand All @@ -473,8 +473,8 @@ cdef int TVMFFIPyArgSetterPyNativeObjectGeneral_(
raise ValueError(f"_tvm_ffi_cached_object is None for {type(arg)}")
assert arg._tvm_ffi_cached_object is not None
arg = arg._tvm_ffi_cached_object
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
return 0


Expand Down Expand Up @@ -507,7 +507,7 @@ cdef int TVMFFIPyArgSetterObjectRValueRef_(
"""Setter for ObjectRValueRef"""
cdef object arg = <object>py_arg
out.type_index = kTVMFFIObjectRValueRef
out.v_ptr = &((<Object>(arg.obj)).chandle)
out.v_ptr = &((<CObject>(arg.obj)).chandle)
return 0


Expand All @@ -532,8 +532,8 @@ cdef int TVMFFIPyArgSetterException_(
"""Setter for Exception"""
cdef object arg = <object>py_arg
arg = _convert_to_ffi_error(arg)
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
TVMFFIPyPushTempPyObject(ctx, <PyObject*>arg)
return 0

Expand Down Expand Up @@ -595,8 +595,8 @@ cdef int TVMFFIPyArgSetterObjectConvertible_(
# recursively construct a new map
cdef object arg = <object>py_arg
arg = arg.asobject()
out.type_index = TVMFFIObjectGetTypeIndex((<Object>arg).chandle)
out.v_ptr = (<Object>arg).chandle
out.type_index = TVMFFIObjectGetTypeIndex((<CObject>arg).chandle)
out.v_ptr = (<CObject>arg).chandle
TVMFFIPyPushTempPyObject(ctx, <PyObject*>arg)


Expand Down Expand Up @@ -727,7 +727,7 @@ cdef int TVMFFIPyArgSetterFactory_(PyObject* value, TVMFFIPyArgSetter* out) exce
if isinstance(arg, Tensor):
out.func = TVMFFIPyArgSetterTensor_
return 0
if isinstance(arg, Object):
if isinstance(arg, CObject):
out.func = TVMFFIPyArgSetterObject_
return 0
if isinstance(arg, ObjectRValueRef):
Expand Down Expand Up @@ -857,7 +857,7 @@ cdef int TVMFFIPyArgSetterFactory_(PyObject* value, TVMFFIPyArgSetter* out) exce
# ---------------------------------------------------------------------------------------------
# Implementation of function calling
# ---------------------------------------------------------------------------------------------
cdef class Function(Object):
cdef class Function(CObject):
"""Callable wrapper around a TVM FFI function.

Instances are obtained by converting Python callables with
Expand Down Expand Up @@ -908,7 +908,7 @@ cdef class Function(Object):
result.v_int64 = 0
TVMFFIPyFuncCall(
TVMFFIPyArgSetterFactory_,
(<Object>self).chandle, <PyObject*>args,
(<CObject>self).chandle, <PyObject*>args,
&result,
&c_api_ret_code,
self.release_gil,
Expand Down Expand Up @@ -972,7 +972,7 @@ cdef class Function(Object):

CHECK_CALL(ret_code)
func = Function.__new__(Function)
(<Object>func).chandle = chandle
(<CObject>func).chandle = chandle
return func

@staticmethod
Expand Down Expand Up @@ -1026,7 +1026,7 @@ cdef class Function(Object):
TVMFFIPyMLIRPackedSafeCallDeleter(mlir_packed_safe_call)
CHECK_CALL(ret_code)
func = Function.__new__(Function)
(<Object>func).chandle = chandle
(<CObject>func).chandle = chandle
return func


Expand All @@ -1039,7 +1039,7 @@ def _register_global_func(name: str, pyfunc: Callable[..., Any] | Function, over
if not isinstance(pyfunc, Function):
pyfunc = _convert_to_ffi_func(pyfunc)

CHECK_CALL(TVMFFIFunctionSetGlobal(name_arg.cptr(), (<Object>pyfunc).chandle, ioverride))
CHECK_CALL(TVMFFIFunctionSetGlobal(name_arg.cptr(), (<CObject>pyfunc).chandle, ioverride))
return pyfunc


Expand All @@ -1050,7 +1050,7 @@ def _get_global_func(name: str, allow_missing: bool):
CHECK_CALL(TVMFFIFunctionGetGlobal(name_arg.cptr(), &chandle))
if chandle != NULL:
ret = Function.__new__(Function)
(<Object>ret).chandle = chandle
(<CObject>ret).chandle = chandle
return ret

if allow_missing:
Expand Down Expand Up @@ -1105,7 +1105,7 @@ def _convert_to_ffi_func(object pyfunc: Callable[..., Any]) -> Function:
cdef TVMFFIObjectHandle chandle
_convert_to_ffi_func_handle(pyfunc, &chandle)
ret = Function.__new__(Function)
(<Object>ret).chandle = chandle
(<CObject>ret).chandle = chandle
return ret


Expand All @@ -1127,7 +1127,7 @@ def _convert_to_opaque_object(object pyobject: Any) -> OpaquePyObject:
cdef TVMFFIObjectHandle chandle
_convert_to_opaque_object_handle(pyobject, &chandle)
ret = OpaquePyObject.__new__(OpaquePyObject)
(<Object>ret).chandle = chandle
(<CObject>ret).chandle = chandle
return ret


Expand Down
Loading