https://github.com/halide/Halide
Revision 2cac3cc1c6ff53996e481d4ced6ce51d575a8b30 authored by Steven Johnson on 14 February 2023, 00:51:36 UTC, committed by Steven Johnson on 14 February 2023, 00:51:36 UTC
In PyBind11, if you specify a default argument for a method, it is evaluated when the Python module is initialized, *not* when the method is called (as you might expect in C++). For defaults that are just constants/literals, this is no big deal, but when calling get_*_target_from_environment, this means it is called at module init time -- also normally not a big deal (since the values ~never change at runtime anyway), with one big exception (no pun intended): if the function throws an exception (e.g. via calling user_assert() or similar), that exception is thrown at Module-initialization time, which is a much more inscrutable crash, and one that is very hard to recover from.

This may seem unlikely, but can happen pretty easily if you set (say) HL_JIT_TARGET=host-cuda (or other gpu) and the given GPU runtime isn't present on the given system; the current behavior is basically "make if impossible for the libHalidePython bindings to run", whereas what we want is "runtime exception thrown when you call the method".

This changes the relevant methods to use `Target()` as the default value, and inside the method wrapper, if the value passed equals `Target()`, it replaces the value with the righ `get_*_target_from_environment()` call.

(This turned up while doing some testing of https://github.com/halide/Halide/pull/6924 on a system without Vulkan available)
1 parent 22aed20
History
Tip revision: 2cac3cc1c6ff53996e481d4ced6ce51d575a8b30 authored by Steven Johnson on 14 February 2023, 00:51:36 UTC
Change early-bound default args in Python bindings to late-bound
Tip revision: 2cac3cc
File Mode Size
.github
apps
cmake
dependencies
doc
packaging
python_bindings
src
test
tools
tutorial
util
.clang-format -rw-r--r-- 1.4 KB
.clang-format-ignore -rw-r--r-- 286 bytes
.clang-tidy -rw-r--r-- 2.2 KB
.gitattributes -rw-r--r-- 342 bytes
.gitignore -rw-r--r-- 5.0 KB
.gitmodules -rw-r--r-- 0 bytes
CMakeLists.txt -rw-r--r-- 5.5 KB
CMakePresets.json -rw-r--r-- 5.6 KB
CODE_OF_CONDUCT.md -rw-r--r-- 3.5 KB
LICENSE.txt -rw-r--r-- 13.8 KB
MANIFEST.in -rw-r--r-- 159 bytes
Makefile -rw-r--r-- 103.3 KB
README.md -rw-r--r-- 16.4 KB
README_cmake.md -rw-r--r-- 76.2 KB
README_python.md -rw-r--r-- 32.1 KB
README_rungen.md -rw-r--r-- 12.1 KB
README_webassembly.md -rw-r--r-- 10.5 KB
pyproject.toml -rw-r--r-- 195 bytes
requirements.txt -rw-r--r-- 129 bytes
run-clang-format.sh -rwxr-xr-x 1.4 KB
run-clang-tidy.sh -rwxr-xr-x 3.1 KB
setup.py -rw-r--r-- 1.2 KB

README.md

back to top