Skip to content

Commit faca787

Browse files
junrushaoclaude
andcommitted
[CORE] Add TypeConverter for runtime type validation/conversion
Add a TypeConverter system to the Cython FFI module that takes a TypeSchema and returns a Function wrapping a C++ conversion function (Any) -> Any. The converter validates/converts input values to match the target type, with fast-path (zero-copy) for containers when elements already match and slow-path (element-by-element conversion) otherwise. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6adc8df commit faca787

7 files changed

Lines changed: 1491 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ if (TVM_FFI_BUILD_PYTHON_MODULE)
247247
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm_ffi/cython/tensor.pxi
248248
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm_ffi/cython/object.pxi
249249
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm_ffi/cython/string.pxi
250+
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm_ffi/cython/type_converter.pxi
250251
)
251252
# Run a Python script to check for free-threaded build
252253
execute_process(

python/tvm_ffi/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
init_ffi_api,
4747
)
4848
from ._dtype import dtype
49-
from .core import Object, ObjectConvertible, Function
49+
from .core import Object, ObjectConvertible, Function, create_type_converter
5050
from ._convert import convert
5151
from .error import register_error
5252
from ._tensor import Device, device, DLDeviceType
@@ -118,6 +118,7 @@
118118
"access_path",
119119
"convert",
120120
"cpp",
121+
"create_type_converter",
121122
"dataclasses",
122123
"device",
123124
"dtype",

python/tvm_ffi/core.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,5 @@ class TypeInfo:
269269
parent_type_info: TypeInfo | None
270270

271271
def prototype_py(self) -> str: ...
272+
273+
def create_type_converter(schema: TypeSchema) -> Function: ...

python/tvm_ffi/cython/core.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ include "./tensor.pxi"
3838
_register_object_by_index(kTVMFFITensor, Tensor)
3939
include "./function.pxi"
4040
_register_object_by_index(kTVMFFIFunction, Function)
41+
include "./type_converter.pxi"
4142

4243
# Global invalid/missing object singleton
4344
MISSING = _get_global_func("ffi.GetInvalidObject", False)()

0 commit comments

Comments
 (0)