Skip to content

Commit 00ae647

Browse files
authored
[FFI][ABI] Refactor the naming of DLPack speed converter (#18308)
Update the name to avoid potential confusion
1 parent 0c9e7cd commit 00ae647

File tree

6 files changed

+55
-54
lines changed

6 files changed

+55
-54
lines changed

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.0a11"
20+
version = "0.1.0a12"
2121
description = "tvm ffi"
2222

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

ffi/python/tvm_ffi/_optional_torch_c_dlpack.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ def load_torch_c_dlpack_extension():
117117
case ScalarType::Float8_e8m0fnu:
118118
dtype.code = DLDataTypeCode::kDLFloat8_e8m0fnu;
119119
break;
120+
#if TORCH_VERSION_MAJOR >= 2 && TORCH_VERSION_MINOR >= 8
120121
case ScalarType::Float4_e2m1fn_x2:
121122
dtype.code = DLDataTypeCode::kDLFloat4_e2m1fn;
122123
break;
124+
#endif
123125
default:
124126
TORCH_CHECK(false, "Unsupported scalar type: ");
125127
}
@@ -311,7 +313,7 @@ def load_torch_c_dlpack_extension():
311313
} // namespace
312314
} // namespace at
313315
314-
int TorchDLPackPyObjectExporter(void* py_obj, DLManagedTensorVersioned** out, void** env_stream) {
316+
int TorchDLPackFromPyObject(void* py_obj, DLManagedTensorVersioned** out, void** env_stream) {
315317
try {
316318
py::handle handle(static_cast<PyObject*>(py_obj));
317319
at::Tensor tensor = handle.cast<at::Tensor>();
@@ -326,7 +328,7 @@ def load_torch_c_dlpack_extension():
326328
}
327329
}
328330
329-
int TorchDLPackPyObjectImporter(DLManagedTensorVersioned* src, void** py_obj_out) {
331+
int TorchDLPackToPyObject(DLManagedTensorVersioned* src, void** py_obj_out) {
330332
try {
331333
at::Tensor tensor = at::fromDLPackImpl<DLManagedTensorVersioned>(src, nullptr);
332334
*py_obj_out = THPVariable_Wrap(tensor);
@@ -355,12 +357,12 @@ def load_torch_c_dlpack_extension():
355357
}
356358
}
357359
358-
int64_t TorchDLPackPyObjectExporterPtr() {
359-
return reinterpret_cast<int64_t>(TorchDLPackPyObjectExporter);
360+
int64_t TorchDLPackFromPyObjectPtr() {
361+
return reinterpret_cast<int64_t>(TorchDLPackFromPyObject);
360362
}
361363
362-
int64_t TorchDLPackPyObjectImporterPtr() {
363-
return reinterpret_cast<int64_t>(TorchDLPackPyObjectImporter);
364+
int64_t TorchDLPackToPyObjectPtr() {
365+
return reinterpret_cast<int64_t>(TorchDLPackToPyObject);
364366
}
365367
366368
int64_t TorchDLPackTensorAllocatorPtr() {
@@ -376,17 +378,17 @@ def load_torch_c_dlpack_extension():
376378
name="to_dlpack",
377379
cpp_sources=cpp_source,
378380
functions=[
379-
"TorchDLPackPyObjectExporterPtr",
380-
"TorchDLPackPyObjectImporterPtr",
381+
"TorchDLPackFromPyObjectPtr",
382+
"TorchDLPackToPyObjectPtr",
381383
"TorchDLPackTensorAllocatorPtr",
382384
],
383385
extra_cflags=["-O3"],
384386
extra_include_paths=libinfo.include_paths() + cpp_extension.include_paths("cuda"),
385387
verbose=True,
386388
)
387389
# set the dlpack related flags
388-
torch.Tensor.__c_dlpack_exporter__ = mod.TorchDLPackPyObjectExporterPtr()
389-
torch.Tensor.__c_dlpack_importer__ = mod.TorchDLPackPyObjectImporterPtr()
390+
torch.Tensor.__c_dlpack_from_pyobject__ = mod.TorchDLPackFromPyObjectPtr()
391+
torch.Tensor.__c_dlpack_to_pyobject__ = mod.TorchDLPackToPyObjectPtr()
390392
torch.Tensor.__c_dlpack_tensor_allocator__ = mod.TorchDLPackTensorAllocatorPtr()
391393
return mod
392394
except ImportError:

ffi/python/tvm_ffi/cython/base.pxi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ cdef extern from "tvm/ffi/extra/c_env_api.h":
247247
cdef extern from "tvm_ffi_python_helpers.h":
248248
# no need to expose fields of the call context
249249
# setter data structure
250-
ctypedef int (*DLPackPyObjectExporter)(
250+
ctypedef int (*DLPackFromPyObject)(
251251
void* py_obj, DLManagedTensorVersioned** out, TVMFFIStreamHandle* env_stream
252252
) except -1
253253

254-
ctypedef int (*DLPackPyObjectImporter)(
254+
ctypedef int (*DLPackToPyObject)(
255255
DLManagedTensorVersioned* tensor, void** py_obj_out
256256
) except -1
257257
ctypedef int (*DLPackTensorAllocator)(
@@ -263,13 +263,13 @@ cdef extern from "tvm_ffi_python_helpers.h":
263263
int device_type
264264
int device_id
265265
TVMFFIStreamHandle stream
266-
DLPackPyObjectImporter c_dlpack_importer
266+
DLPackToPyObject c_dlpack_to_pyobject
267267
DLPackTensorAllocator c_dlpack_tensor_allocator
268268

269269
ctypedef struct TVMFFIPyArgSetter:
270270
int (*func)(TVMFFIPyArgSetter* handle, TVMFFIPyCallContext* ctx, PyObject* py_arg, TVMFFIAny* out) except -1
271-
DLPackPyObjectExporter c_dlpack_exporter
272-
DLPackPyObjectImporter c_dlpack_importer
271+
DLPackFromPyObject c_dlpack_from_pyobject
272+
DLPackToPyObject c_dlpack_to_pyobject
273273
DLPackTensorAllocator c_dlpack_tensor_allocator
274274

275275
ctypedef int (*TVMFFIPyArgSetterFactory)(PyObject* value, TVMFFIPyArgSetter* out) except -1
@@ -281,7 +281,7 @@ cdef extern from "tvm_ffi_python_helpers.h":
281281
TVMFFIAny* result,
282282
int* c_api_ret_code,
283283
int release_gil,
284-
DLPackPyObjectImporter* out_dlpack_importer
284+
DLPackToPyObject* out_dlpack_importer
285285
) except -1
286286

287287
int TVMFFIPyCallFieldSetter(

ffi/python/tvm_ffi/cython/function.pxi

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ cdef inline object make_ret_small_bytes(TVMFFIAny result):
4747
return PyBytes_FromStringAndSize(bytes.data, bytes.size)
4848

4949

50-
cdef inline object make_ret(TVMFFIAny result, DLPackPyObjectImporter c_dlpack_importer = NULL):
50+
cdef inline object make_ret(TVMFFIAny result, DLPackToPyObject c_dlpack_to_pyobject = NULL):
5151
"""convert result to return value."""
5252
cdef int32_t type_index
5353
type_index = result.type_index
5454
if type_index == kTVMFFITensor:
5555
# specially handle Tensor as it needs a special dltensor field
56-
return make_tensor_from_any(result, c_dlpack_importer)
56+
return make_tensor_from_any(result, c_dlpack_to_pyobject)
5757
elif type_index == kTVMFFIOpaquePyObject:
5858
return make_ret_opaque_object(result)
5959
elif type_index >= kTVMFFIStaticObjectBegin:
@@ -121,18 +121,18 @@ cdef int TVMFFIPyArgSetterDLPackCExporter_(
121121
cdef TVMFFIObjectHandle temp_chandle
122122
cdef TVMFFIStreamHandle env_stream = NULL
123123

124-
if this.c_dlpack_importer != NULL:
125-
ctx.c_dlpack_importer = this.c_dlpack_importer
124+
if this.c_dlpack_to_pyobject != NULL:
125+
ctx.c_dlpack_to_pyobject = this.c_dlpack_to_pyobject
126126
if this.c_dlpack_tensor_allocator != NULL:
127127
ctx.c_dlpack_tensor_allocator = this.c_dlpack_tensor_allocator
128128

129129
if ctx.device_id != -1:
130130
# already queried device, do not do it again, pass NULL to stream
131-
if (this.c_dlpack_exporter)(arg, &temp_managed_tensor, NULL) != 0:
131+
if (this.c_dlpack_from_pyobject)(arg, &temp_managed_tensor, NULL) != 0:
132132
return -1
133133
else:
134134
# query string on the envrionment stream
135-
if (this.c_dlpack_exporter)(arg, &temp_managed_tensor, &env_stream) != 0:
135+
if (this.c_dlpack_from_pyobject)(arg, &temp_managed_tensor, &env_stream) != 0:
136136
return -1
137137
# If device is not CPU, we should set the device type and id
138138
if temp_managed_tensor.dl_tensor.device.device_type != kDLCPU:
@@ -148,7 +148,7 @@ cdef int TVMFFIPyArgSetterDLPackCExporter_(
148148
return 0
149149

150150

151-
cdef int TorchDLPackPyObjectImporterFallback_(
151+
cdef int TorchDLPackToPyObjectFallback_(
152152
DLManagedTensorVersioned* dltensor, void** py_obj_out
153153
) except -1:
154154
# a bit convoluted but ok as a fallback
@@ -173,7 +173,7 @@ cdef int TVMFFIPyArgSetterTorchFallback_(
173173
out.type_index = kTVMFFITensor
174174
out.v_ptr = (<Tensor>arg).chandle
175175
temp_dltensor = TVMFFITensorGetDLTensorPtr((<Tensor>arg).chandle)
176-
ctx.c_dlpack_importer = TorchDLPackPyObjectImporterFallback_
176+
ctx.c_dlpack_to_pyobject = TorchDLPackToPyObjectFallback_
177177
# record the stream and device for torch context
178178
if is_cuda and ctx.device_type != -1:
179179
ctx.device_type = temp_dltensor.device.device_type
@@ -370,15 +370,15 @@ cdef int TVMFFIPyArgSetterFactory_(PyObject* value, TVMFFIPyArgSetter* out) exce
370370
if isinstance(arg, ObjectRValueRef):
371371
out.func = TVMFFIPyArgSetterObjectRValueRef_
372372
return 0
373-
if os.environ.get("TVM_FFI_SKIP_C_DLPACK_EXPORTER", "0") != "1":
373+
if os.environ.get("TVM_FFI_SKIP_c_dlpack_from_pyobject", "0") != "1":
374374
# external tensors
375-
if hasattr(arg, "__c_dlpack_exporter__"):
375+
if hasattr(arg, "__c_dlpack_from_pyobject__"):
376376
out.func = TVMFFIPyArgSetterDLPackCExporter_
377-
temp_ptr = arg.__c_dlpack_exporter__
378-
out.c_dlpack_exporter = <DLPackPyObjectExporter>temp_ptr
379-
if hasattr(arg, "__c_dlpack_importer__"):
380-
temp_ptr = arg.__c_dlpack_importer__
381-
out.c_dlpack_importer = <DLPackPyObjectImporter>temp_ptr
377+
temp_ptr = arg.__c_dlpack_from_pyobject__
378+
out.c_dlpack_from_pyobject = <DLPackFromPyObject>temp_ptr
379+
if hasattr(arg, "__c_dlpack_to_pyobject__"):
380+
temp_ptr = arg.__c_dlpack_to_pyobject__
381+
out.c_dlpack_to_pyobject = <DLPackToPyObject>temp_ptr
382382
if hasattr(arg, "__c_dlpack_tensor_allocator__"):
383383
temp_ptr = arg.__c_dlpack_tensor_allocator__
384384
out.c_dlpack_tensor_allocator = <DLPackTensorAllocator>temp_ptr
@@ -470,7 +470,7 @@ cdef class Function(Object):
470470
def __call__(self, *args):
471471
cdef TVMFFIAny result
472472
cdef int c_api_ret_code
473-
cdef DLPackPyObjectImporter c_dlpack_importer = NULL
473+
cdef DLPackToPyObject c_dlpack_to_pyobject = NULL
474474
# IMPORTANT: caller need to initialize result->type_index to kTVMFFINone
475475
result.type_index = kTVMFFINone
476476
result.v_int64 = 0
@@ -480,12 +480,12 @@ cdef class Function(Object):
480480
&result,
481481
&c_api_ret_code,
482482
self.release_gil,
483-
&c_dlpack_importer
483+
&c_dlpack_to_pyobject
484484
)
485485
# NOTE: logic is same as check_call
486486
# directly inline here to simplify traceback
487487
if c_api_ret_code == 0:
488-
return make_ret(result, c_dlpack_importer)
488+
return make_ret(result, c_dlpack_to_pyobject)
489489
elif c_api_ret_code == -2:
490490
raise_existing_error()
491491
raise move_from_last_error().py_error()

ffi/python/tvm_ffi/cython/tensor.pxi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ _set_class_tensor(Tensor)
275275
_register_object_by_index(kTVMFFITensor, Tensor)
276276

277277

278-
cdef int _dltensor_test_wrapper_c_dlpack_exporter(
278+
cdef int _dltensor_test_wrapper_c_dlpack_from_pyobject(
279279
void* obj, DLManagedTensorVersioned** out, TVMFFIStreamHandle* env_stream
280280
) except -1:
281281
cdef PyObject* py_obj = <PyObject*>obj
@@ -291,8 +291,8 @@ cdef int _dltensor_test_wrapper_c_dlpack_exporter(
291291
return TVMFFITensorToDLPackVersioned(wrapper.tensor.chandle, out)
292292

293293

294-
def _dltensor_test_wrapper_c_dlpack_exporter_as_intptr():
295-
cdef DLPackPyObjectExporter converter_func = _dltensor_test_wrapper_c_dlpack_exporter
294+
def _dltensor_test_wrapper_c_dlpack_from_pyobject_as_intptr():
295+
cdef DLPackFromPyObject converter_func = _dltensor_test_wrapper_c_dlpack_from_pyobject
296296
cdef void* temp_ptr = <void*>converter_func
297297
cdef long long temp_int_ptr = <long long>temp_ptr
298298
return temp_int_ptr
@@ -301,7 +301,7 @@ def _dltensor_test_wrapper_c_dlpack_exporter_as_intptr():
301301
cdef class DLTensorTestWrapper:
302302
"""Wrapper of a Tensor that exposes DLPack protocol, only for testing purpose.
303303
"""
304-
__c_dlpack_exporter__ = _dltensor_test_wrapper_c_dlpack_exporter_as_intptr()
304+
__c_dlpack_from_pyobject__ = _dltensor_test_wrapper_c_dlpack_from_pyobject_as_intptr()
305305

306306
cdef Tensor tensor
307307
cdef dict __dict__
@@ -333,19 +333,19 @@ cdef inline object make_ret_dltensor(TVMFFIAny result):
333333
return tensor
334334

335335

336-
cdef inline object make_tensor_from_chandle(TVMFFIObjectHandle chandle, DLPackPyObjectImporter c_dlpack_importer = NULL):
336+
cdef inline object make_tensor_from_chandle(TVMFFIObjectHandle chandle, DLPackToPyObject c_dlpack_to_pyobject = NULL):
337337
# TODO: Implement
338338
cdef Tensor tensor
339339
cdef void* py_obj
340340
cdef DLManagedTensorVersioned* dlpack
341341

342-
if c_dlpack_importer != NULL:
342+
if c_dlpack_to_pyobject != NULL:
343343
# try convert and import into the environment array if possible
344344
if TVMFFITensorToDLPackVersioned(chandle, &dlpack) == 0:
345345
try:
346346
# note that py_obj already holds an extra reference to the tensor
347347
# so we need to decref it after the conversion
348-
c_dlpack_importer(dlpack, &py_obj)
348+
c_dlpack_to_pyobject(dlpack, &py_obj)
349349
tensor = <Tensor>(<PyObject*>py_obj)
350350
Py_DECREF(tensor)
351351
return tensor
@@ -358,5 +358,5 @@ cdef inline object make_tensor_from_chandle(TVMFFIObjectHandle chandle, DLPackPy
358358
return tensor
359359

360360

361-
cdef inline object make_tensor_from_any(TVMFFIAny any, DLPackPyObjectImporter c_dlpack_importer):
362-
return make_tensor_from_chandle(any.v_ptr, c_dlpack_importer)
361+
cdef inline object make_tensor_from_any(TVMFFIAny any, DLPackToPyObject c_dlpack_to_pyobject):
362+
return make_tensor_from_chandle(any.v_ptr, c_dlpack_to_pyobject)

ffi/python/tvm_ffi/cython/tvm_ffi_python_helpers.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
* \note We use void* to avoid dependency on Python.h so this specific type is
4545
* not dependent on Python.h and can be copied to dlpack.h
4646
*/
47-
typedef int (*DLPackPyObjectExporter)(void* py_obj, DLManagedTensorVersioned** out,
48-
void** env_stream);
47+
typedef int (*DLPackFromPyObject)(void* py_obj, DLManagedTensorVersioned** out, void** env_stream);
4948
/*!
5049
* \brief C-style function pointer to speed convert a DLManagedTensorVersioned to a PyObject Tensor.
5150
* \param tensor The DLManagedTensorVersioned to convert.
@@ -54,7 +53,7 @@ typedef int (*DLPackPyObjectExporter)(void* py_obj, DLManagedTensorVersioned** o
5453
* \note We use void* to avoid dependency on Python.h so this specific type is
5554
* not dependent on Python.h and can be copied to dlpack.h
5655
*/
57-
typedef int (*DLPackPyObjectImporter)(DLManagedTensorVersioned* tensor, void** py_obj_out);
56+
typedef int (*DLPackToPyObject)(DLManagedTensorVersioned* tensor, void** py_obj_out);
5857

5958
///--------------------------------------------------------------------------------
6059
/// We deliberately designed the data structure and function to be C-style
@@ -82,7 +81,7 @@ struct TVMFFIPyCallContext {
8281
/*! \brief the number of temporary arguments */
8382
int num_temp_py_objects = 0;
8483
/*! \brief the DLPack exporter, if any */
85-
DLPackPyObjectImporter c_dlpack_importer{nullptr};
84+
DLPackToPyObject c_dlpack_to_pyobject{nullptr};
8685
/*! \brief the DLPack allocator, if any */
8786
DLPackTensorAllocator c_dlpack_tensor_allocator{nullptr};
8887
};
@@ -102,11 +101,11 @@ struct TVMFFIPyArgSetter {
102101
/*!
103102
* \brief Optional DLPack exporter for for setters that leverages DLPack protocol.
104103
*/
105-
DLPackPyObjectExporter c_dlpack_exporter{nullptr};
104+
DLPackFromPyObject c_dlpack_from_pyobject{nullptr};
106105
/*!
107106
* \brief Optional DLPack importer for for setters that leverages DLPack protocol.
108107
*/
109-
DLPackPyObjectImporter c_dlpack_importer{nullptr};
108+
DLPackToPyObject c_dlpack_to_pyobject{nullptr};
110109
/*!
111110
* \brief Optional DLPack allocator for for setters that leverages DLPack protocol.
112111
*/
@@ -273,7 +272,7 @@ class TVMFFIPyCallManager {
273272
*/
274273
int Call(TVMFFIPyArgSetterFactory setter_factory, void* func_handle, PyObject* py_arg_tuple,
275274
TVMFFIAny* result, int* c_api_ret_code, bool release_gil,
276-
DLPackPyObjectImporter* optional_out_dlpack_importer) {
275+
DLPackToPyObject* optional_out_dlpack_importer) {
277276
int64_t num_args = PyTuple_Size(py_arg_tuple);
278277
if (num_args == -1) return -1;
279278
try {
@@ -321,8 +320,8 @@ class TVMFFIPyCallManager {
321320
c_api_ret_code[0] = TVMFFIEnvSetTensorAllocator(prev_tensor_allocator, 0, nullptr);
322321
if (c_api_ret_code[0] != 0) return 0;
323322
}
324-
if (optional_out_dlpack_importer != nullptr && ctx.c_dlpack_importer != nullptr) {
325-
*optional_out_dlpack_importer = ctx.c_dlpack_importer;
323+
if (optional_out_dlpack_importer != nullptr && ctx.c_dlpack_to_pyobject != nullptr) {
324+
*optional_out_dlpack_importer = ctx.c_dlpack_to_pyobject;
326325
}
327326
return 0;
328327
} catch (const std::exception& ex) {
@@ -430,7 +429,7 @@ class TVMFFIPyCallManager {
430429
inline int TVMFFIPyFuncCall(TVMFFIPyArgSetterFactory setter_factory, void* func_handle,
431430
PyObject* py_arg_tuple, TVMFFIAny* result, int* c_api_ret_code,
432431
bool release_gil = true,
433-
DLPackPyObjectImporter* out_dlpack_importer = nullptr) {
432+
DLPackToPyObject* out_dlpack_importer = nullptr) {
434433
return TVMFFIPyCallManager::ThreadLocal()->Call(setter_factory, func_handle, py_arg_tuple, result,
435434
c_api_ret_code, release_gil, out_dlpack_importer);
436435
}

0 commit comments

Comments
 (0)