Skip to content

Comments

Python bindings: adds numpy_view() to hl.Buffer.#8953

Draft
jiawen wants to merge 1 commit intomainfrom
jiawen-hl_buffer
Draft

Python bindings: adds numpy_view() to hl.Buffer.#8953
jiawen wants to merge 1 commit intomainfrom
jiawen-hl_buffer

Conversation

@jiawen
Copy link
Contributor

@jiawen 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.
@jiawen
Copy link
Contributor Author

jiawen commented Feb 18, 2026

@alexreinking PTAL at the design. I left some ELEPHANTs as points for discussion.

.def_buffer([](Buffer<> &b) -> py::buffer_info {
return to_buffer_info(b, /*reverse_axes*/ true);

// ELEPHANT: this always reverses axes, which might be surprising?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to make this more explicit as well. Have two versions:

  • A version without reverse_axes that requires a contiguous buffer. It will auto-reverse if contiguous.
  • A version with explicit reverse_axes that accepts anything.

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