You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think we can just wait for dlpack support, since it should be simpler to use. The tensor objects we create are simply wrappers around arrays from different frameworks, so it matters any target framework also needs to support the protocol.
Do you need both support from dlpack and to dlpack? __dlpack__ gets you the capsule. But how do you go from dlpack to other framework? Or is that not needed?
I imagine it’s the users responsibility to choose a target framework that can accept a dlpack compatible array. Of course it would be helpful for mlx to implement from_dlpack to accept arrays produced by other frameworks.
In [4]: tensor = torch.from_dlpack(mx.array([42]))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 tensor = torch.from_dlpack(mx.array([42]))
File /opt/homebrew/lib/python3.10/site-packages/torch/utils/dlpack.py:101, in from_dlpack(ext_tensor)
50 """from_dlpack(ext_tensor) -> Tensor
51
52 Converts a tensor from an external library into a ``torch.Tensor``.
(...)
98
99 """
100 if hasattr(ext_tensor, '__dlpack__'):
--> 101 device = ext_tensor.__dlpack_device__()
102 # device is either CUDA or ROCm, we need to pass the current
103 # stream
104 if device[0] in (DLDeviceType.kDLGPU, DLDeviceType.kDLROCM):
AttributeError: 'array' object has no attribute '__dlpack_device__'
In [5]: np.from_dlpack(mx.array([42]))
Out[5]: array([42], dtype=int32)
Activity
awni commentedon May 6, 2024
MLX doesn't support the dlpack protocol yet. It does support the Python buffer protocol, could that work for you?
We could add dlpack support, I do not think it would be too difficult given that nanobind's
nd::arraysupports it.justinchuby commentedon May 6, 2024
I think we can just wait for dlpack support, since it should be simpler to use. The tensor objects we create are simply wrappers around arrays from different frameworks, so it matters any target framework also needs to support the protocol.
awni commentedon May 6, 2024
Do you need both support from dlpack and to dlpack?
__dlpack__gets you the capsule. But how do you go from dlpack to other framework? Or is that not needed?justinchuby commentedon May 6, 2024
I think allowing from_dlpack to be called on Arrays is enough. The implementation simply relays the call so the mlx array can be used by other frameworks: https://github.com/microsoft/onnxscript/blob/bc818e7ae4a4418dbc6a80da6d25311c7579e53d/onnxscript/ir/_core.py#L344
justinchuby commentedon May 6, 2024
I imagine it’s the users responsibility to choose a target framework that can accept a dlpack compatible array. Of course it would be helpful for mlx to implement from_dlpack to accept arrays produced by other frameworks.
awni commentedon May 25, 2024
How is this going? DId you need any additional dlpack support in
mx.array?justinchuby commentedon May 25, 2024
For some reason this code fails for me (linux gpu):
->
mx.__version__=='0.2.0'justinchuby commentedon May 25, 2024
When running on a Mac machine, I get
showing numpy works but torch fails.
awni commentedon May 25, 2024
__dlpack_device__justinchuby commentedon Jun 17, 2024
Just checking if it's implemented now?
justinchuby commentedon Jun 17, 2024
Just saw it. Thanks!
awni commentedon Jun 18, 2024
Let us know if you have any trouble with that.