Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak python apps for better Blaze/Bazel compatibility #6823

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python_bindings/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ set(SCRIPTS
interpolate.py
local_laplacian.py)

make_shell_path(TEST_IMAGES_DIR $<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/../../apps/images>)
steven-johnson marked this conversation as resolved.
Show resolved Hide resolved

foreach (SCRIPT IN LISTS SCRIPTS)
get_filename_component(BASE ${SCRIPT} NAME_WE)
add_test(NAME python_apps_${BASE}
COMMAND Python3::Interpreter "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${SCRIPT}>")
set_tests_properties(python_apps_${BASE} PROPERTIES
LABELS python
ENVIRONMENT "PYTHONPATH=$<SHELL_PATH:$<TARGET_FILE_DIR:Halide::Python>>;HL_TARGET=${Halide_TARGET}")
ENVIRONMENT "PYTHONPATH=$<SHELL_PATH:$<TARGET_FILE_DIR:Halide::Python>>;HL_TARGET=${Halide_TARGET};TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_IMAGES_DIR=${TEST_IMAGES_DIR}")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ENVIRONMENT "PYTHONPATH=$<SHELL_PATH:$<TARGET_FILE_DIR:Halide::Python>>;HL_TARGET=${Halide_TARGET};TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_IMAGES_DIR=${TEST_IMAGES_DIR}")
ENVIRONMENT "${test_env}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

endforeach ()
7 changes: 4 additions & 3 deletions python_bindings/apps/bilateral_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def generate_compiled_file(bilateral_grid):


def get_input_data():
image_path = os.path.join(os.path.dirname(__file__), "../../apps/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
steven-johnson marked this conversation as resolved.
Show resolved Hide resolved
assert os.path.exists(image_path), \
"Could not find %s" % image_path
rgb_data = imageio.imread(image_path)
Expand Down Expand Up @@ -133,8 +133,9 @@ def filter_test_image(bilateral_grid, input):
output_image.copy_to_host()

# save results
input_path = "bilateral_grid_input.png"
output_path = "bilateral_grid.png"
tmpdir = os.environ["TEST_TMPDIR"]
input_path = os.path.join(tmpdir, "bilateral_grid_input.png")
output_path = os.path.join(tmpdir, "bilateral_grid.png")
imageio.imsave(input_path, input_data)
imageio.imsave(output_path, output_data)
print("\nbilateral_grid realized on output_image.")
Expand Down
7 changes: 4 additions & 3 deletions python_bindings/apps/blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_blur(input):


def get_input_data():
image_path = os.path.join(os.path.dirname(__file__), "../../apps/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
assert os.path.exists(image_path), \
"Could not find %s" % image_path
rgb_data = imageio.imread(image_path)
Expand Down Expand Up @@ -63,8 +63,9 @@ def main():
blur.realize(output_image)

# save results
input_path = "blur_input.png"
output_path = "blur_result.png"
tmpdir = os.environ["TEST_TMPDIR"]
input_path = os.path.join(tmpdir, "blur_input.png")
output_path = os.path.join(tmpdir, "blur_result.png")
imageio.imsave(input_path, input_data)
imageio.imsave(output_path, output_data)
print("\nblur realized on output image.",
Expand Down
2 changes: 1 addition & 1 deletion python_bindings/apps/erode.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_erode(input):

def get_input_data():

image_path = os.path.join(os.path.dirname(__file__), "../../apps/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
assert os.path.exists(image_path), \
"Could not find %s" % image_path
rgb_data = imageio.imread(image_path)
Expand Down
7 changes: 4 additions & 3 deletions python_bindings/apps/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def get_interpolate(input, levels):


def get_input_data():
image_path = os.path.join(os.path.dirname(__file__), "../../apps/images/rgba.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgba.png")
assert os.path.exists(image_path), "Could not find %s" % image_path

rgba_data = imageio.imread(image_path)
Expand Down Expand Up @@ -187,8 +187,9 @@ def main():
output_data = (output_data * 255).astype(np.uint8)

# save results
input_path = "interpolate_input.png"
output_path = "interpolate_result.png"
tmpdir = os.environ["TEST_TMPDIR"]
input_path = os.path.join(tmpdir, "interpolate_input.png")
output_path = os.path.join(tmpdir, "interpolate_result.png")
imageio.imsave(input_path, input_data)
imageio.imsave(output_path, output_data)

Expand Down
7 changes: 4 additions & 3 deletions python_bindings/apps/local_laplacian.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def upsample2D(f):


def get_input_data():
image_path = os.path.join(os.path.dirname(__file__), "../../apps/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
assert os.path.exists(image_path), "Could not find {}".format(image_path)

rgb_data = imageio.imread(image_path)
Expand Down Expand Up @@ -205,8 +205,9 @@ def filter_test_image(local_laplacian, input):
output_data = (output_data >> 8).astype(np.uint8)

# save results
input_path = "local_laplacian_input.png"
output_path = "local_laplacian.png"
tmpdir = os.environ["TEST_TMPDIR"]
input_path = os.path.join(tmpdir, "local_laplacian_input.png")
output_path = os.path.join(tmpdir, "local_laplacian.png")

imageio.imsave(input_path, input_data)
imageio.imsave(output_path, output_data)
Expand Down
3 changes: 2 additions & 1 deletion python_bindings/tutorial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(TESTS
)

make_shell_path(PYTHONPATH "$<TARGET_FILE_DIR:lesson_10_halide>" "$<TARGET_FILE_DIR:Halide::Python>")
make_shell_path(TEST_IMAGES_DIR $<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/../../tutorial/images>)
steven-johnson marked this conversation as resolved.
Show resolved Hide resolved

foreach (TEST IN LISTS TESTS)
get_filename_component(TEST_NAME ${TEST} NAME_WE)
Expand All @@ -25,7 +26,7 @@ foreach (TEST IN LISTS TESTS)

set_tests_properties(python_tutorial_${TEST_NAME} PROPERTIES
LABELS python
ENVIRONMENT "PYTHONPATH=${PYTHONPATH};HL_TARGET=${Halide_TARGET}")
ENVIRONMENT "PYTHONPATH=${PYTHONPATH};HL_TARGET=${Halide_TARGET};TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_IMAGES_DIR=${TEST_IMAGES_DIR}")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ENVIRONMENT "PYTHONPATH=${PYTHONPATH};HL_TARGET=${Halide_TARGET};TEST_TMPDIR=${CMAKE_CURRENT_BINARY_DIR};TEST_IMAGES_DIR=${TEST_IMAGES_DIR}")
ENVIRONMENT "${test_env}")

endforeach ()

## Add some hacks for getting CMake to delay compiling lesson_10_halide until after the test has run. The "better" way
Expand Down
8 changes: 5 additions & 3 deletions python_bindings/tutorial/lesson_02_input_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main():
# brightens an image.

# First we'll load the input image we wish to brighten.
image_path = os.path.join(os.path.dirname(__file__), "../../tutorial/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
Copy link
Member

Choose a reason for hiding this comment

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

The intended usage for these tutorials is someone typing "python lesson_02_input_image.py" at the shell. Won't this break that?


# We create a hl.Buffer object to wrap the numpy array
input = hl.Buffer(imageio.imread(image_path))
Expand Down Expand Up @@ -87,8 +87,10 @@ def main():

# Save the output for inspection. It should look like a bright parrot.
# python3-imageio versions <2.5 expect a numpy array
imageio.imsave("brighter.png", np.asanyarray(output_image))
print("Created brighter.png result file.")
tmpdir = os.environ["TEST_TMPDIR"]
output_path = os.path.join(tmpdir, "brighter.png")
imageio.imsave(output_path, np.asanyarray(output_image))
print("Created result file %s" % output_path)

print("Success!")
return 0
Expand Down
14 changes: 9 additions & 5 deletions python_bindings/tutorial/lesson_07_multi_stage_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def main():
# First we'll declare some Vars to use below.
x, y, c = hl.Var("x"), hl.Var("y"), hl.Var("c")

image_path = os.path.join(os.path.dirname(__file__), "../../tutorial/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")

# Now we'll express a multi-stage pipeline that blurs an image
# first horizontally, and then vertically.
Expand Down Expand Up @@ -84,8 +84,10 @@ def main():
# shorter than the input image.

# python3-imageio versions <2.5 expect a numpy array
imageio.imsave("blurry_parrot_1.png", np.asanyarray(result))
print("Created blurry_parrot_1.png")
tmpdir = os.environ["TEST_TMPDIR"]
output_path = os.path.join(tmpdir, "blurry_parrot_1.png")
imageio.imsave(output_path, np.asanyarray(result))
print("Created %s" % output_path)

# This is usually the fastest way to deal with boundaries:
# don't write code that reads out of bounds :) The more
Expand Down Expand Up @@ -155,8 +157,10 @@ def main():
# input.

# python3-imageio versions <2.5 expect a numpy array
imageio.imsave("blurry_parrot_2.png", np.asanyarray(result))
print("Created blurry_parrot_2.png")
tmpdir = os.environ["TEST_TMPDIR"]
output_path = os.path.join(tmpdir, "blurry_parrot_2.png")
imageio.imsave(output_path, np.asanyarray(result))
print("Created %s" % output_path)

print("Success!")
return 0
Expand Down
2 changes: 1 addition & 1 deletion python_bindings/tutorial/lesson_09_update_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def main():
x, y = hl.Var("x"), hl.Var("y")

# Load a grayscale image to use as an input.
image_path = os.path.join(os.path.dirname(__file__), "../../tutorial/images/gray.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "gray.png")
input_data = imageio.imread(image_path)
if True:
# making the image smaller to go faster
Expand Down
17 changes: 10 additions & 7 deletions python_bindings/tutorial/lesson_11_cross_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# in a shell with the current directory at python_bindings/

import halide as hl
import os.path
from struct import unpack


Expand All @@ -18,6 +19,8 @@ def main():
brighter = hl.Func("brighter")
x, y = hl.Var("x"), hl.Var("y")

tmpdir = os.environ["TEST_TMPDIR"]
steven-johnson marked this conversation as resolved.
Show resolved Hide resolved

# Declare the arguments.
offset = hl.Param(hl.UInt(8))
input = hl.ImageParam(hl.UInt(8), 2)
Expand All @@ -34,7 +37,7 @@ def main():
# program on. For example, if you compile and run this file on
# 64-bit linux on an x86 cpu with sse4.1, then the generated code
# will be suitable for 64-bit linux on x86 with sse4.1.
brighter.compile_to_file("lesson_11_host", args, "lesson_11_host")
brighter.compile_to_file(os.path.join(tmpdir, "lesson_11_host"), args, "lesson_11_host")

# We can also compile object files suitable for other cpus and
# operating systems. You do this with an optional third argument
Expand All @@ -53,7 +56,7 @@ def main():
arm_features = [] # A list of features to set
target.set_features(arm_features)
# Pass the target as the last argument.
brighter.compile_to_file("lesson_11_arm_32_android", args,
brighter.compile_to_file(os.path.join(tmpdir, "lesson_11_arm_32_android"), args,
"lesson_11_arm_32_android", target)

if create_windows:
Expand All @@ -63,7 +66,7 @@ def main():
target.arch = hl.TargetArch.X86
target.bits = 64
target.set_features([hl.TargetFeature.AVX, hl.TargetFeature.SSE41])
brighter.compile_to_file("lesson_11_x86_64_windows", args,
brighter.compile_to_file(os.path.join(tmpdir, "lesson_11_x86_64_windows"), args,
"lesson_11_x86_64_windows", target)

if create_ios:
Expand All @@ -78,7 +81,7 @@ def main():
target.arch = hl.TargetArch.ARM
target.bits = 32
target.set_features([hl.TargetFeature.ARMv7s])
brighter.compile_to_file("lesson_11_arm_32_ios", args,
brighter.compile_to_file(os.path.join(tmpdir, "lesson_11_arm_32_ios"), args,
"lesson_11_arm_32_ios", target)

# Now let's check these files are what they claim, by examining
Expand All @@ -93,7 +96,7 @@ def main():
1] # Current version of elf

length = len(arm_32_android_magic)
f = open("lesson_11_arm_32_android.o", "rb")
f = open(os.path.join(tmpdir, "lesson_11_arm_32_android.o"), "rb")
Copy link
Member

Choose a reason for hiding this comment

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

If tmpdir were a Path variable (from the standard pathlib module) then you could just write: tmpdir / "lesson_11_arm_32_android.o"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is zero other usage of that in our current python; if we want to convert, let's do it all at once, not piecewise.

try:
header_bytes = f.read(length)
except:
Expand All @@ -111,7 +114,7 @@ def main():
# uint8_t []
win_64_magic = [0x64, 0x86]

f = open("lesson_11_x86_64_windows.obj", "rb")
f = open(os.path.join(tmpdir, "lesson_11_x86_64_windows.obj"), "rb")
try:
header_bytes = f.read(2)
except:
Expand All @@ -130,7 +133,7 @@ def main():
12, # CPU type is ARM
11, # CPU subtype is ARMv7s
1] # It's a relocatable object file.
f = open("lesson_11_arm_32_ios.o", "rb")
f = open(os.path.join(tmpdir, "lesson_11_arm_32_ios.o"), "rb")
try:
header_bytes = f.read(4 * 4)
except:
Expand Down
2 changes: 1 addition & 1 deletion python_bindings/tutorial/lesson_12_using_the_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_correctness(self, reference_output):

def main():
# Load an input image.
image_path = os.path.join(os.path.dirname(__file__), "../../tutorial/images/rgb.png")
image_path = os.path.join(os.environ["TEST_IMAGES_DIR"], "rgb.png")
input = hl.Buffer(imageio.imread(image_path))

# Allocated an image that will store the correct output
Expand Down
1 change: 0 additions & 1 deletion src/CodeGen_LLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ void CodeGen_LLVM::initialize_llvm() {
#define LLVM_ASM_PRINTER(target) \
Initialize##target##AsmPrinter();
#include <llvm/Config/AsmPrinters.def>
#include <utility>
#undef LLVM_ASM_PRINTER
});
}
Expand Down