Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read-only array exchange via the buffer protocol #217

Merged
merged 1 commit into from
May 25, 2023
Merged

Read-only array exchange via the buffer protocol #217

merged 1 commit into from
May 25, 2023

Conversation

wjakob
Copy link
Owner

@wjakob wjakob commented May 25, 2023

This PR adapts nanobind so that it can both receive and return arrays representing read-only memory. This is communicated via the standard const type modifier, e.g. by replacing nb::ndarray<float, ...> with nb::ndarray<const float, ...>.

The PR also adapts accessors (data(), operator()) so that they only return constant references/pointers in that case.

The change is for now specific to the buffer protocol used to exchange nd-arrays with NumPy. The DLPack interface (used to exchange nd-arrays with PyTorch, Tensorflow, etc.) ignores the read-only annotation. This may change at some point in the future when an upcoming DLPack version with a read-only bit is more widely deployed.

This PR only targets the ndarray class. It will be nice to also propagate it into the Eigen type caster (@WKarel ?)

This PR adapts nanobind so that it can both receive and return arrays
representing read-only memory. This is communicated via the standard
``const`` type modifier, e.g. by replacing ``nb::ndarray<float, ...>``
with ``nb::ndarray<const float, ...>``.

The PR also adapts accessors (``data()``, ``operator()``) so that they
only return constant references/pointers in that case.

The change is for now specific to the buffer protocol used to exchange
data with NumPy. The DLPack interface (with PyTorch, Tensorflow, etc.)
ignores the read-only annotation. This may change at some point in the
future when an upcoming DLPack version with a read-only bit is more
widely deployed.
@wjakob wjakob merged commit 5e6b210 into master May 25, 2023
@wjakob wjakob deleted the ro_array branch May 25, 2023 16:09
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.

1 participant