-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
DLPack Conversion API #1573
Merged
Merged
DLPack Conversion API #1573
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8c1715
temp checkin of WIP dlpack to/from
eqy 96d4076
fix for double free
eqy 02f0d5d
fix lint, add docstring
eqy b00ab85
update test
eqy 908d3a5
add description, fix lint
eqy 6194c3d
add pytorch function wrap
eqy caa85d5
update docstring
eqy 68489ba
update comments
eqy cdac190
fix lint
eqy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Wrapping functions to bridge frameworks with DLPack support to TVM""" | ||
from .. import ndarray | ||
|
||
def convert_func(tvm_func, tensor_type, to_dlpack_func): | ||
"""Convert a tvm function into one that accepts a tensor from another | ||
framework, provided the other framework supports DLPACK | ||
|
||
Parameters | ||
---------- | ||
tvm_func: Function | ||
Built tvm function operating on arrays | ||
|
||
tensor_type: Type | ||
Type of the tensors of the target framework | ||
|
||
to_dlpack_func: Function | ||
Function to convert the source tensors to DLPACK | ||
""" | ||
assert(callable(tvm_func)) | ||
|
||
def _wrapper(*args): | ||
args = tuple(ndarray.from_dlpack(to_dlpack_func(arg))\ | ||
if isinstance(arg, tensor_type) else arg for arg in args) | ||
return tvm_func(*args) | ||
|
||
return _wrapper | ||
|
||
def to_pytorch_func(tvm_func): | ||
"""Convert a tvm function into one that accepts PyTorch tensors | ||
|
||
Parameters | ||
---------- | ||
tvm_func: Function | ||
Built tvm function operating on arrays | ||
|
||
Returns | ||
------- | ||
wrapped_func: Function | ||
Wrapped tvm function that operates on PyTorch tensors | ||
""" | ||
import torch | ||
import torch.utils.dlpack | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a check to see if tvm_func is callable and raise error if it is not |
||
return convert_func(tvm_func, torch.Tensor, torch.utils.dlpack.to_dlpack) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import tvm | ||
import numpy as np | ||
from tvm.contrib.dlpack import to_pytorch_func | ||
|
||
def test(): | ||
a = np.random.randn(1337) | ||
tvm_a = tvm.nd.array(a) | ||
np.testing.assert_equal(tvm.nd.from_dlpack(tvm_a.to_dlpack()).asnumpy(), a) | ||
|
||
try: | ||
import torch | ||
import torch.utils.dlpack | ||
|
||
x = torch.rand(56, 56) | ||
tvm_x = tvm.nd.from_dlpack(torch.utils.dlpack.to_dlpack(x)) | ||
np.testing.assert_equal(x.numpy(), tvm_x.asnumpy()) | ||
y = tvm.nd.from_dlpack(tvm_x.to_dlpack()) | ||
np.testing.assert_equal(y.asnumpy(), tvm_x.asnumpy()) | ||
np.testing.assert_equal(torch.utils.dlpack.from_dlpack(y.to_dlpack()).numpy(), tvm_x.asnumpy()) | ||
|
||
n = tvm.convert(137) | ||
xx = torch.rand(137,137) | ||
yy = torch.rand(137,137) | ||
zz2 = torch.empty(137,137) | ||
zz = xx.mm(yy) | ||
XX = tvm.placeholder((n,n), name='X') | ||
YY = tvm.placeholder((n,n), name='Y') | ||
|
||
k = tvm.reduce_axis((0, n), name='k') | ||
ZZ = tvm.compute((n,n), lambda i,j : tvm.sum(XX[i,k]*YY[k,j], axis=k)) | ||
s = tvm.create_schedule(ZZ.op) | ||
f = tvm.build(s, [XX, YY, ZZ], target_host='llvm', name='f') | ||
|
||
f_pytorch = to_pytorch_func(f) | ||
zz2 = torch.empty(137,137) | ||
f_pytorch(xx, yy, zz2) | ||
np.testing.assert_allclose(zz.numpy(), zz2.numpy(), rtol=1e-6) | ||
|
||
except ImportError: | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
test() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add description