Skip to content

Add array.data_ptr() Python binding for low-level interop#3342

Closed
Aristide021 wants to merge 1 commit intoml-explore:mainfrom
Aristide021:feat/mlx-array-data-ptr
Closed

Add array.data_ptr() Python binding for low-level interop#3342
Aristide021 wants to merge 1 commit intoml-explore:mainfrom
Aristide021:feat/mlx-array-data-ptr

Conversation

@Aristide021
Copy link
Copy Markdown
Contributor

Proposed changes

Exposes array.data<void>() in Python as data_ptr() -> int, returning the address of the first element after evaluation.

Implementation:

  • Release GIL before eval() to avoid blocking during synchronization
  • Return uintptr_t cast to Python int
  • Pointer validity is tied to underlying storage lifetime

Tests:

  • Add test_data_ptr in python/tests/test_array.py
  • Cover base contiguous array, positive-offset slice, transpose view, and negative-stride view
  • Validate pointers against NumPy __array_interface__ and verify pointed values via ctypes

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works

@zcbenz
Copy link
Copy Markdown
Collaborator

zcbenz commented Mar 31, 2026

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 zcbenz closed this Mar 31, 2026
@Aristide021
Copy link
Copy Markdown
Contributor Author

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 array.data<void>() already exists in the C++ API. So this isn't another way to do the same thing, it's a Python binding of something that's already there. Closing it as "use DLPack" conflates two different things.

@zcbenz
Copy link
Copy Markdown
Collaborator

zcbenz commented Mar 31, 2026

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()) # 1

We don't just expose C++ interface to Python with a 1:1 mapping, for example in C++ we use begin/end for iterators but __iter__ in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants