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

Python bindings should preserve float32 scalar Exprs #8420

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 27 additions & 14 deletions python_bindings/src/halide/halide_/PyExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,42 @@ void define_expr(py::module &m) {

auto expr_class =
py::class_<Expr>(m, "Expr")
// Default ctor
.def(py::init<>())
.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>())
// 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<std::string>())

// for implicitly_convertible
// For implicitly_convertible
.def(py::init([](const FuncRef &f) -> Expr { return f; }))
.def(py::init([](const FuncTupleElementRef &f) -> Expr { return f; }))
.def(py::init([](const Param<> &p) -> Expr { return p; }))
.def(py::init([](const RDom &r) -> Expr { return r; }))
.def(py::init([](const RVar &r) -> Expr { return r; }))
.def(py::init([](const Var &v) -> Expr { return v; }))

// Weird types
.def(py::init<std::string>())

// Numeric types.
// This is tricky. PyBind11 tries the conversions in declared order,
// and we generally want to prefer int over float conversion (to avoid
// accidental promotion). However, we want to keep a float32 as a float32
// (e.g. specified via numpy.float32()) and the implicit Expr conversion
// will confuse PyBind, hence the apparently wrong order.
.def(py::init<float>())
.def(py::init([](bool b) {
return Internal::make_bool(b);
}))
.def(py::init<int32_t>())
.def(py::init<int64_t>())
// Most scalar fp values we get from Python will actually be doubles;
// for efficiency, we want to store these as float32 instead of float64.
// This may not always be the right decision -- e.g., if someone
// constructs something via numpy.float64() they will be unhappy --
// but changing the behavior now would likely cause lots of subtle
// regressions.
.def(py::init([](double v) {
return double_to_expr_check(v);
}))

.def("__bool__", to_bool)
.def("__nonzero__", to_bool)

Expand Down
12 changes: 12 additions & 0 deletions python_bindings/test/correctness/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,17 @@ def test_implicit_convert_int64():
assert (hl.i32(0) + (0x7fffffff+1)).type() == hl.Int(64)


def test_explicit_expr_ctors():
assert (hl.Expr(np.bool_(0))).type() == hl.Bool(), (hl.Expr(np.bool_(0))).type()
assert (hl.Expr(np.int32(0))).type() == hl.Int(32), (hl.Expr(np.int32(0))).type()
assert (hl.Expr(np.int64(0x7fffffff+1))).type() == hl.Int(64), (hl.Expr(np.int64(0x7fffffff+1))).type()
assert (hl.Expr(np.float32(0))).type() == hl.Float(32), (hl.Expr(np.float32(0))).type()
# Note that this is deliberate: we have aggressively downscaled scalar
# float64 values from Python into float32, and we aren't going to change
# that now.
assert (hl.Expr(np.float64(0))).type() == hl.Float(32), (hl.Expr(np.float64(0))).type()


if __name__ == "__main__":
test_compiletime_error()
test_runtime_error()
Expand All @@ -469,3 +480,4 @@ def test_implicit_convert_int64():
test_bool_conversion()
test_requirements()
test_implicit_convert_int64()
test_explicit_expr_ctors()
Loading