Python bindings: adds numpy_view() to hl.Buffer.#8953
Conversation
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.
|
@alexreinking PTAL at the design. I left some |
| .def_buffer([](Buffer<> &b) -> py::buffer_info { | ||
| return to_buffer_info(b, /*reverse_axes*/ true); | ||
|
|
||
| // ELEPHANT: this always reverses axes, which might be surprising? |
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?
| // - 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 |
There was a problem hiding this comment.
To discuss how to name this.
|
|
||
| // 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) |
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.
|
Bump |
|
@abadams @alexreinking Bump, part deux |