Kathryn-cat commented on code in PR #96:
URL: https://github.com/apache/tvm-ffi/pull/96#discussion_r2422376622
##########
python/tvm_ffi/_optional_torch_c_dlpack.py:
##########
@@ -513,16 +508,66 @@ def load_torch_c_dlpack_extension() -> Any:
}
}
-int64_t TorchDLPackFromPyObjectPtr() {
- return reinterpret_cast<int64_t>(TorchDLPackFromPyObject);
+int TorchDLTensorFromPyObject(void* py_obj, DLTensor* out) {
+ try {
+ // Use handle (non-owning) to avoid unnecessary refcount operations
+ py::handle handle(static_cast<PyObject*>(py_obj));
+ at::Tensor tensor = handle.cast<at::Tensor>();
+
+ // Fill in the pre-allocated DLTensor struct with direct pointers
+ // This is a non-owning conversion - the original PyObject owns the data
+ // and is kept alive by the caller for the duration of this call
+ out->data = tensor.data_ptr();
+ out->device = at::torchDeviceToDLDeviceForDLPackv1(tensor.device());
+ out->ndim = static_cast<int32_t>(tensor.dim());
Review Comment:
updated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]