1

Can anyone help? I've implemented a halide aot function in python to compute a histogram of a grayscale image... I create a numpy array to hold the histogram like so:

histo = np.empty((1, 2048), dtype=np.uint64)

In this case, I have:

histo.shape: (1, 2048)
histo.strides: (16384, 8)

But when I invoke the function I get:

Error: Constraint violated: histogram_pan.stride.0 (2048) == 1 (1)

The aot function is defined as:

b = hl.Var("b")
x = hl.Var("x")

input_image = hl.ImageParam(hl.UInt(16), 2)

histogram = hl.Func("histogram_pan")
histogram[b, x] = hl.u64(0)
r = hl.RDom([(0, input_image.dim(0).extent()), (0, input_image.dim(1).extent())])
histogram[0, hl.clamp(input_image[r.x, r.y], 0, 2047)] += hl.u64(1)
histogram.vectorize(x, 8)

histogram.compile_to({hl.Output.object : 'histogram_pan.o',
                      hl.Output.python_extension : 'histogram_pan.py.cpp'},
                     [input_image], 'histogram_pan')```

EDIT: Some additional info:

Someone asked: "what does the error say if you swap the dimensions of the numpy array? -> np.empty((2048, 1) ...)"

When I swap the dimensions as suggested, I have:

histo.shape: (2048, 1)
histo.strides: (8, 8)

and, I get the error:

ValueError: Invalid buffer: length 16384, but computed length 16384

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
user992113
  • 163
  • 1
  • 9
  • Did you ever find a solution, I'm running into the same thing. As far as I can tell, on the C++ side you could remove the constraint with .set_stride(0, Expr()), but doing .dim(0).set_stride(hl.Expr()) doesn't work. – Ryan Feb 05 '21 at 16:08
  • @Ryan no I never found a true solution. For now, I work around it by having dual implementations of the same algorithm: one for single band images and one for multi band images. It's not ideal. – user992113 Mar 12 '21 at 00:44

1 Answers1

0

So there is an issue with the python binding where it doesn't work with anything but planar memory. (Strides don't get set right) I've got it working with any layout using this commit: https://github.com/ryanstout/Halide/commit/fbe5f8096105e300dcd9f37871de940f32fecba5 I need to submit a PR with it, but it needs a bit more work. I still run in to the invalid buffer: length.. issue on my non-contiguous memory layouts, so I've got that check disabled at the moment (doesn't seem to cause any issues) If I can get a few, I'll try to add back that test for any memory layout and submit a PR. Hope that helps. Thanks!

Ryan
  • 956
  • 7
  • 8