-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Expr constructed with numpy float32 argument gets type halide bool or int32 in Python bindings #8414
Comments
Here is a more relevant minimal example as well
|
If I rearrange the following Halide/python_bindings/src/halide/halide_/PyExpr.cpp Lines 26 to 37 in f658eec
into moving the double overload before bool overload
// Python float is implemented by double
// But Halide prohibits implicitly construct by double.
.def(py::init([](double v) {
return double_to_expr_check(v);
}))
.def(py::init([](bool b) {
return Internal::make_bool(b);
}))
// PyBind11 searches in declared order,
// int should be tried before float conversion
.def(py::init<int>())
.def(py::init<int64_t>()) then the issue goes away. I rebuilt the Python bindings and ran the above minimal example. Based on this comment, it seems like the order is intentional:
Reading the pybind11 docs it seems like I am also a bit confused by the comment in the code when I compare that to what the pybind11 docs states. According to the docs, pybind11 runs resolution order in two passes. One where it attempts to match explicitly, then a second where it chooses the first one that matches implicitly. It seems like we are hitting this issue in the second pass, where @steven-johnson: It seems like you wrote the initial comment. Do you remember why the integers are preferred? The comment is from 2017, so I completely understand if you do not remember 😃 |
This PR for pybind11 seems related: pybind/pybind11#2060 @dragly Does this link give enough info on how we can resolve the issue? |
Sadly, yeah, I spun down those brain cells a while ago. The current behavior definitely seems like a bug that needs fixing though! I wonder if just adding an overload for |
@alexreinking FYI we should try to fix this prior to the Halide 19 release |
LLVM 19.1.0 is due tomorrow and we normally trail them by a week or two anyway |
I have not been able to find a solution based on that link yet. The linked PR seems to add new types to pybind11 that could help solve the issue, but I am not sure it helps if those are not in pybind11 already. Maybe there is something in there if we try to mimick the behavior the PR implements, though.
I tried that, but for some reason Reordering to have |
Tweak the PyBind11 code so that float32 scalars created via numpy.float32() don't get converted into boolean expressions.
PTAL at #8420 -- I very much want feedback from @dragly and @runenordmo before landing that change |
With halide-19.0.0.dev36, a
numpy.float32(1.2)
as an argument tohalide.Expr
's constructor will give a resultingExpr
of typehl.Bool()
:Earlier versions like halide 13 also give unexpected results:
<halide.Expr of type int32: 4>
.I'm not sure if this is an unintended use case or unintended behavior.
I initially discovered it because
np.float32(np.pi)
was part of a calculation and it ended up being treated as an integer, giving wrong results.I would be greatful if you have any ideas to fix or avoid this type of behavior.
(Short term, we are keeping away from numpy.float32)
Here is a minimal example/test:
The text was updated successfully, but these errors were encountered: