Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

Issues setting stride in Halide python bindings (Error: Constraint violated: brighten.stride.0 (1) == 3 (3))

I'm trying to use a C memory layout (RGBRGBRGBRGB) instead of planar from an AOT compiled halide function (build and run with the python bindings.) Following the C++ tutorial here, I set the stride and bounds, but I still get:

Error: Constraint violated: brighten.stride.0 (1) == 3 (3)

I also tried first calling .dim(0).set_stride(hl.Expr()) like mentioned here but no luck.

Here's my full test.

import halide as hl
import os
import numpy as np
import cv2

x,y,c = hl.Var('x'), hl.Var('y'), hl.Var('c')

inputs = hl.ImageParam(hl.UInt(8), 3, 'input')

# Simple function
brighten = hl.Func('brighten')
brighten[x,y,c] = inputs[x,y,c] * 2


# Set strides
brighten_out = brighten.output_buffer()

# Remove constraints on dim(0) (doesn't appear to work)
inputs.dim(0).set_stride(hl.Expr())
brighten_out.dim(0).set_stride(hl.Expr())


# Following example from: https://github.com/halide/Halide/blob/master/tutorial/lesson_16_rgb_generate.cpp#L130
inputs.dim(0).set_stride(3)
inputs.dim(2).set_stride(1).set_bounds(0,3)

brighten_out.dim(0).set_stride(3)
brighten_out.dim(2).set_stride(1).set_bounds(0,3)

# Compile AOT
brighten.compile_to(
    {
        hl.Output.object: "aot_example.o",
        hl.Output.python_extension: "aot_example.py.cpp"
    },
    [inputs],
    "aot_example",
    target=hl.get_target_from_environment(),
)

os.system(f"g++ -shared -fPIC aot_example.py.cpp aot_example.o `python-config --cflags` -lpthread -I../Halide/src/runtime -O3  -fvisibility=hidden -fvisibility-inlines-hidden -o aot_example.so")

# Import the compiled file
import aot_example as aot_example

# Simple input/output with C memory layout
input = np.ones((200,200,3), dtype=np.uint8, order='C')
output = np.empty((input.shape[0], input.shape[1], 3), dtype=np.uint8, order='C')

# If the input/outputs are fortran arrays, and we don't set strides, things work
# (or if we run do a JIT version with .realize)
# input = np.asfortranarray(input)
# output = np.asfortranarray(output)

aot_example.aot_example(input,output)

cv2.imwrite('output.jpg', np.asarray(output))

I might be missing something, but it looks like calling .set_stride(hl.Expr()) doesn't remove the default constraint on dim(0) (python bindings issue maybe?) Or perhaps I'm missing something.

Any help would be greatly appreciated. Thanks!

question from:https://stackoverflow.com/questions/66066716/issues-setting-stride-in-halide-python-bindings-error-constraint-violated-bri

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I finally figured out there's just some issues with any non-fortran style numpy arrays being passed in to the Halide python bindings. I've got a fix and I'll submit a PR shortly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...