Skip to content

Commit

Permalink
Python Bindings didn't allow for zero-D Funcs, ImageParams, Buffers (#…
Browse files Browse the repository at this point in the history
…6633)

* Python Bindings didn't allow for zero-D Funcs, ImageParams, Buffers

There were no overloads or tests for accessing the element of any of these in the zero-D case, and the obvious syntax (`[]`, to mirror C++ `()` in these cases) isn't legal in Python. To support this uncommon-but-necessary case, I'm proposing that we use the syntax `[None]`, which isn't pretty, but is less bad than other options I've considered so far. (Suggestions welcome.)

* Use [()] instead of [None]
  • Loading branch information
steven-johnson authored Mar 4, 2022
1 parent 86728d7 commit 3827279
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions python_bindings/correctness/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ def test_vector_tile():
p = hl.Pipeline([f, g])
p.compile_jit()

def test_scalar_funcs():
input = hl.ImageParam(hl.UInt(16), 0, 'input')

f = hl.Func('f')
g = hl.Func('g')

input[()]

(input[()]+input[()]) / 2
f[()]
g[()]

f[()] = (input[()]+input[()]+input[()])/3
g[()] = (f[()]+f[()]+f[()])/3

g.compile_jit()


if __name__ == "__main__":
Expand All @@ -300,3 +316,4 @@ def test_vector_tile():
test_basics3()
test_basics4()
test_basics5()
test_scalar_funcs()
14 changes: 14 additions & 0 deletions python_bindings/correctness/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ def test_buffer_to_str():
b = hl.Buffer(hl.Int(32), [128, 256])
assert str(b) == '<halide.Buffer of type int32 shape:[[0,128,1],[0,256,128]]>'

def test_scalar_buffers():
buf = hl.Buffer.make_scalar(hl.Float(32))

assert buf.dimensions() == 0

buf.fill(0)
buf[()] = 2.5

assert buf[()] == 2.5

buf.fill(32)
assert buf[()] == 32

if __name__ == "__main__":
test_make_interleaved()
test_interleaved_ndarray()
Expand All @@ -271,3 +284,4 @@ def test_buffer_to_str():
test_reorder()
test_overflow()
test_buffer_to_str()
test_scalar_buffers()
4 changes: 3 additions & 1 deletion python_bindings/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ with some differences where the C++ idiom is either inappropriate or impossible:
offers variadic and list versions:
`Buffer<>(Type t, int extent_dim_0, int extent_dim_1, ...., extent_dim_N, string name = "") Buffer<>(Type t, vector<int> extents, string name = "")`
in Python, only the second variant is provided.
- `Func` and `Buffer` access is done using `[]` rather than `()`
- `Func` and `Buffer` access is done using `[]` rather than `()`.
- For zero-dimensional `Func` and `Buffer`, you must explicitly specify `[()]` -- that is, use an empty tuple as the index" --
as `[]` is not syntactically acceptable in Python.
- Some classes in the Halide API aren't provided because they are 'wrapped' with
standard Python idioms:
- `Halide::Tuple` doesn't exist in the Python bindings; an ordinary Python
Expand Down

0 comments on commit 3827279

Please sign in to comment.