Skip to content

[RFC]: DLPack based exchange of dtype and Device #972

@tqchen

Description

@tqchen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    API extensionAdds new functions or objects to the API.Needs DiscussionNeeds further discussion.RFCRequest for comments. Feature requests and proposed changes.topic: DLPackDLPack.topic: Device HandlingDevice handling.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions