Python bindings: adds numpy_view() to hl.Buffer.#8953
Draft
Conversation
Contributor
jiawen
commented
Feb 18, 2026
- numpy_view() with no arguments:
- Always tries to returns a C-contiguous view of the buffer if possible.
- If Halide Buffer is stored in the default order, will reverse axes.
- If Halide Buffer is stored in the reverse order, will preserve axes.
- numpy_view(reverse_axes: Bool):
- Requires an explicit reverse_axes argument to be passed in.
- It will do what was requested, and supports non-contiguous buffers.
- numpy_view() with no arguments: - Always tries to returns a C-contiguous view of the buffer if possible. - If Halide Buffer is stored in the default order, will reverse axes. - If Halide Buffer is stored in the reverse order, will preserve axes. - numpy_view(reverse_axes: Bool): - Requires an explicit reverse_axes argument to be passed in. - It will do what was requested, and supports non-contiguous buffers.
Contributor
Author
|
@alexreinking PTAL at the design. I left some |
jiawen
commented
Feb 18, 2026
| .def_buffer([](Buffer<> &b) -> py::buffer_info { | ||
| return to_buffer_info(b, /*reverse_axes*/ true); | ||
|
|
||
| // ELEPHANT: this always reverses axes, which might be surprising? |
Contributor
Author
There was a problem hiding this comment.
I'd like to break the interface here.
The smoothest path would be to permit this, with automatic maybe-reverse-axes-depending on whether the Buffer is contiguous, only when the Buffer is contiguous. This is analogous to the numpy_view() with no args below.
This means breaking automatic conversion from cropped Buffers to the buffer protocol - but I imagine that's not used very much. Clients can use numpy_view(reverse_axes: Bool) instead.
WDTY?
jiawen
commented
Feb 18, 2026
| // - It is possible for a Buffer to be both C and F contiguous (e.g., a scalar or a | ||
| // 1D vector), or for a Buffer to be neither (e.g., storage_order=[1, 0, 2] for a 3D | ||
| // buffer). | ||
| // ELEPHANT: maybe I should just call it [densest_first, densest_last]. But that |
Contributor
Author
There was a problem hiding this comment.
To discuss how to name this.
jiawen
commented
Feb 18, 2026
|
|
||
| // This allows us to use any buffer-like Python entity to create a Buffer<> | ||
| // (most notably, an ndarray) | ||
| .def(py::init_alias<py::buffer, const std::string &, bool>(), py::arg("buffer"), py::arg("name") = "", py::arg("reverse_axes") = true) |
Contributor
Author
There was a problem hiding this comment.
I'd like to make this more explicit as well. Have two versions:
- A version without
reverse_axesthat requires a contiguous buffer. It will auto-reverse if contiguous. - A version with explicit
reverse_axesthat accepts anything.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.