Skip to content

Commit 678984f

Browse files
authored
[FFI][ABI] Better String and Nested Container handling (#18311)
[FFI][ABI][REFACTOR] Better String and nested container handling This PR improves the overall String/Bytes and nested container handling It also fixes a bug for temp object recycling when temp object. - Introduce formal API for string/bytes creation - Updates the tuple/dict conversion to also preserve the torch stream - So if a function takes a list of torch.Tensor, torch stream will be setup in context - Optimizes recursive argument conversion by moving most logic into c++
1 parent 00ae647 commit 678984f

File tree

15 files changed

+432
-114
lines changed

15 files changed

+432
-114
lines changed

ffi/include/tvm/ffi/c_api.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,25 @@ TVM_FFI_DLL int TVMFFITensorFromDLPackVersioned(DLManagedTensorVersioned* from,
555555
*/
556556
TVM_FFI_DLL int TVMFFITensorToDLPackVersioned(TVMFFIObjectHandle from,
557557
DLManagedTensorVersioned** out);
558+
//---------------------------------------------------------------
559+
// Section: string/bytes support APIs.
560+
// These APIs are used to simplify the string/bytes construction
561+
//---------------------------------------------------------------
562+
/*!
563+
* \brief Reinterpret the content of TVMFFIByteArray to String.
564+
* \param input The TVMFFIByteArray to convert.
565+
* \param out The output String owned by the caller, maybe a SmallStr or a Str object.
566+
* \return 0 on success, nonzero on failure.
567+
*/
568+
TVM_FFI_DLL int TVMFFIStringFromByteArray(const TVMFFIByteArray* input, TVMFFIAny* out);
569+
570+
/*!
571+
* \brief Reinterpret the content of TVMFFIByteArray to Bytes.
572+
* \param input The TVMFFIByteArray to convert.
573+
* \param out The output Bytes owned by the caller, maybe a SmallBytes or a Bytes object.
574+
* \return 0 on success, nonzero on failure.
575+
*/
576+
TVM_FFI_DLL int TVMFFIBytesFromByteArray(const TVMFFIByteArray* input, TVMFFIAny* out);
558577

559578
//---------------------------------------------------------------
560579
// Section: dtype string support APIs.

ffi/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
[project]
1919
name = "apache-tvm-ffi"
20-
version = "0.1.0a12"
20+
version = "0.1.0a13"
2121
description = "tvm ffi"
2222

2323
authors = [{ name = "TVM FFI team" }]

ffi/python/tvm_ffi/_convert.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,9 @@ def convert(value: Any) -> Any:
4040
automatically converted. So this function is mainly
4141
only used in internal or testing scenarios.
4242
"""
43-
if isinstance(value, core.Object):
43+
if isinstance(value, (core.Object, core.PyNativeObject, bool, Number)):
4444
return value
45-
elif isinstance(value, core.PyNativeObject):
46-
return value
47-
elif isinstance(value, (bool, Number)):
48-
return value
49-
elif isinstance(value, (list, tuple)):
45+
elif isinstance(value, (tuple, list)):
5046
return container.Array(value)
5147
elif isinstance(value, dict):
5248
return container.Map(value)
@@ -67,6 +63,3 @@ def convert(value: Any) -> Any:
6763
else:
6864
# in this case, it is an opaque python object
6965
return core._convert_to_opaque_object(value)
70-
71-
72-
core._set_func_convert_to_object(convert)

ffi/python/tvm_ffi/_optional_torch_c_dlpack.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ def load_torch_c_dlpack_extension():
384384
],
385385
extra_cflags=["-O3"],
386386
extra_include_paths=libinfo.include_paths() + cpp_extension.include_paths("cuda"),
387-
verbose=True,
388387
)
389388
# set the dlpack related flags
390389
torch.Tensor.__c_dlpack_from_pyobject__ = mod.TorchDLPackFromPyObjectPtr()

ffi/python/tvm_ffi/cython/base.pxi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ cdef extern from "tvm/ffi/c_api.h":
212212
TVMFFIByteArray* traceback) nogil
213213

214214
int TVMFFITypeKeyToIndex(TVMFFIByteArray* type_key, int32_t* out_tindex) nogil
215+
int TVMFFIStringFromByteArray(TVMFFIByteArray* input_, TVMFFIAny* out) nogil
216+
int TVMFFIBytesFromByteArray(TVMFFIByteArray* input_, TVMFFIAny* out) nogil
215217
int TVMFFIDataTypeFromString(TVMFFIByteArray* str, DLDataType* out) nogil
216218
int TVMFFIDataTypeToString(const DLDataType* dtype, TVMFFIAny* out) nogil
217219
const TVMFFIByteArray* TVMFFITraceback(
@@ -284,6 +286,15 @@ cdef extern from "tvm_ffi_python_helpers.h":
284286
DLPackToPyObject* out_dlpack_importer
285287
) except -1
286288

289+
int TVMFFIPyConstructorCall(
290+
TVMFFIPyArgSetterFactory setter_factory,
291+
void* chandle,
292+
PyObject* py_arg_tuple,
293+
TVMFFIAny* result,
294+
int* c_api_ret_code,
295+
TVMFFIPyCallContext* parent_ctx
296+
) except -1
297+
287298
int TVMFFIPyCallFieldSetter(
288299
TVMFFIPyArgSetterFactory setter_factory,
289300
TVMFFIFieldSetter field_setter,

0 commit comments

Comments
 (0)