Add array.data_ptr() Python binding for low-level interop#3342
Add array.data_ptr() Python binding for low-level interop#3342Aristide021 wants to merge 1 commit intoml-explore:mainfrom
Conversation
|
Thanks for the PR but conversion of arrays is already supported via buffer protocol and dlpack protocol: https://ml-explore.github.io/mlx/build/html/usage/numpy.html . We should not add another way to do the same thing. |
@zcbenz The linked documentation covers the read direction. The use case here was the write direction for zero-copy writes from external kernels into unified memory. Neither DLPack nor the buffer protocol exposes that cleanly. I also want to note that |
|
The dlpack protocol does support zero-copy writing, from the linked doc: a = mx.arange(3)
a_view = np.array(a, copy=False)
print(a_view.flags.owndata) # False
a_view[0] = 1
print(a[0].item()) # 1We don't just expose C++ interface to Python with a 1:1 mapping, for example in C++ we use |
Proposed changes
Exposes
array.data<void>()in Python as data_ptr() -> int, returning the address of the first element after evaluation.Implementation:
eval()to avoid blocking during synchronizationuintptr_tcast to PythonintTests:
test_data_ptrinpython/tests/test_array.py__array_interface__and verify pointed values viactypesChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes