-
Notifications
You must be signed in to change notification settings - Fork 52
Description
Background
As of now we have standard interface for exchanging tensors through DLPack. One minor but still useful thing is to also provide a mechanism for solutions. Motivating example
import numpy
import torch
# results in error
torch.empty((1, 2), dtype=numpy.float16)
# results in error
numpy.empty((1, 2), dtype=torch.float16)
It could be useful for the package.dtype
and package.Device
to be able to provide an interface for exchange such information. Given we already have DLDataType and DLDevice in dlpack, it might be helpful to bringup interfaces to transparently expose them. Such information can also be helpful for bindings to implement generic support for these types
Proposal
# defined in dtype
class dtype:
def __dlpack_data_type__():
"""Return a tuple (code, bits, lanes) in DLDataType format
"""
# defined in Device
class Device:
def __dlpack_device__():
"""Return a tuple of (device_type, device_id) """
Then if desirable, the interface could implement something like
def empty(shape, dtype):
if hasattr(dtype, "___dlpack_data_type__"):
convert
There is also possibility of trying to run general value conversion. I do not think we have to update from_dlpack to also support these values, but there is an option to do so
def from_dlpack(src):
if hasattr(src, "__dlpack__"):
# this is an array
if hasattr(src, "__dlpack_data_type__"):
# this is a dtype
if hasattr(src, "__dlpack_device__"):
# this is a device