-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[FFI][FEAT] AutoDLPack for taking external tensor objects #17927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BenchmarkEnv CPU: AMD Ryzen 9 7950X > python ffi/scripts/benchmark_dlpack.py
-----------------------------
Benchmark f(x, y, z) overhead
-----------------------------
numpy.add 1.921653747558594e-07 sec/call
torch.add[cpu] 6.330013275146484e-07 sec/call
torch.add[cuda] 2.330756187438965e-06 sec/call
tvm.ffi.nop 3.983736038208008e-07 sec/call
tvm.ffi.nop+from_dlpack(torch) 4.368019104003906e-06 sec/call
tvm.ffi.nop+from_dlpack(numpy) 1.1694192886352538e-06 sec/call
tvm.ffi.nop+from_dlpack(tvm) 1.4580249786376954e-06 sec/call
tvm.ffi.nop+from_dlpack(torch.utils) 3.2754182815551756e-06 sec/call
tvm.ffi.nop.autodlpack(torch[cpu]) 3.567361831665039e-06 sec/call
tvm.ffi.nop.autodlpack(torch[cuda]) 3.5606861114501952e-06 sec/call
tvm.ffi.nop.autodlpack(numpy) 1.6696929931640624e-06 sec/call
-------------------------------
Benchmark x.__dlpack__ overhead
-------------------------------
torch.utils.dlpack.to_dlpack 4.5762062072753906e-07 sec/call
torch.__dlpack__ 9.840965270996094e-07 sec/call
numpy.__dlpack__ 5.011558532714844e-08 sec/call
tvm.__dlpack__ 1.5852451324462892e-07 sec/call
---------------------------------------------------
Benchmark x.__dlpack__(max_version=(1,1)) overhead
---------------------------------------------------
torch.__dlpack__(max_version=(1,1)) Tensor.__dlpack__() got an unexpected keyword 'max_version'
numpy.__dlpack__(max_version=(1,1)) 6.172657012939454e-08 sec/call
tvm.__dlpack__(max_version=(1,1)) 1.720428466796875e-07 sec/call Discussions
|
This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument. The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes. When a function returns NDArray, the return value still needs to be converted back via torch.from_dlpack. However, a common use case is the destination passing, where all inputs outputs are pre-allocated and passed into the function. AutoDLPack effectively enables zero overhead support for a wide range of python arrays. We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.
Hzfengsy
approved these changes
May 8, 2025
ShiboXing
pushed a commit
to ShiboXing/tvm
that referenced
this pull request
Aug 10, 2025
[FFI][FEAT] AutoDLPack to enable external tensor args. This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument. The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes. When a function returns NDArray, the return value still needs to be converted back via torch.from_dlpack. However, a common use case is the destination passing, where all inputs outputs are pre-allocated and passed into the function. AutoDLPack effectively enables zero overhead support for a wide range of python arrays. We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.
tqchen
added a commit
to tqchen/tvm
that referenced
this pull request
Sep 13, 2025
[FFI][FEAT] AutoDLPack to enable external tensor args. This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument. The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes. When a function returns NDArray, the return value still needs to be converted back via torch.from_dlpack. However, a common use case is the destination passing, where all inputs outputs are pre-allocated and passed into the function. AutoDLPack effectively enables zero overhead support for a wide range of python arrays. We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.
tqchen
added a commit
to tqchen/tvm
that referenced
this pull request
Sep 13, 2025
[FFI][FEAT] AutoDLPack to enable external tensor args. This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument. The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes. When a function returns NDArray, the return value still needs to be converted back via torch.from_dlpack. However, a common use case is the destination passing, where all inputs outputs are pre-allocated and passed into the function. AutoDLPack effectively enables zero overhead support for a wide range of python arrays. We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.
tqchen
added a commit
to tqchen/tvm
that referenced
this pull request
Sep 13, 2025
[FFI][FEAT] AutoDLPack to enable external tensor args. This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument. The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes. When a function returns NDArray, the return value still needs to be converted back via torch.from_dlpack. However, a common use case is the destination passing, where all inputs outputs are pre-allocated and passed into the function. AutoDLPack effectively enables zero overhead support for a wide range of python arrays. We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces autodlpack feature to the tvm ffi. When an ffi Function takes Tensor argument that conforms to DLPack it automatically imports into NDArray and pass as argument.
The feature will allow compiled function to directly take torch.Tensor as input argument without extra set of changes.
We also added a benchmark script to measure the overall ffi overhead. One thing to note is that there is still continuguous and alignment requirement that is needed by underlying DSL compiler, as of now we use a global value. So x.continugous is still needed before passing the argument if tranpose or other ops are performed.