-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make from_dlpack handle cuda synchronization implicitly for input ten…
…sors that have __dlpack__ and __dlpack_device__ attributes. (#7125)
- Loading branch information
1 parent
aeed61a
commit daada22
Showing
6 changed files
with
78 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -1,10 +1,36 @@ | ||
from typing import Any | ||
import enum | ||
import torch_xla | ||
|
||
|
||
def to_dlpack(xla_tensor: Any): | ||
return torch_xla._XLAC._to_dlpack(xla_tensor) | ||
|
||
|
||
class DLDeviceType(enum.IntEnum): | ||
# Enums as in DLPack specification (aten/src/ATen/dlpack.h) | ||
kDLCPU = 1, | ||
kDLGPU = 2, | ||
kDLCPUPinned = 3, | ||
kDLOpenCL = 4, | ||
kDLVulkan = 7, | ||
kDLMetal = 8, | ||
kDLVPI = 9, | ||
kDLROCM = 10, | ||
kDLExtDev = 12, | ||
kDLOneAPI = 14, | ||
|
||
|
||
def from_dlpack(ext_tensor: Any): | ||
return torch_xla._XLAC._from_dlpack(ext_tensor) | ||
if hasattr(ext_tensor, '__dlpack_device__') and hasattr( | ||
ext_tensor, '__dlpack__'): | ||
device_type, device_id = ext_tensor.__dlpack_device__() | ||
if device_type == DLDeviceType.kDLGPU: | ||
stream = torch_xla._XLAC._get_stream_for_cuda_device(device_id) | ||
dlpack = ext_tensor.__dlpack__(stream=stream) | ||
else: | ||
dlpack = ext_tensor.__dlpack__() | ||
else: | ||
dlpack = ext_tensor | ||
|
||
return torch_xla._XLAC._from_dlpack(dlpack) |