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

Improve type_caster for floating-point types. #829

Merged
merged 6 commits into from
Jan 10, 2025
Merged
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
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Version TBD (not yet released)
structures to ensure correct free-threaded behavior on architectures with
weak memory ordering (specifically, ARM). (PR `#819
<https://github.com/wjakob/nanobind/pull/819>`__).

- The floating-point type_caster now only performs value-changing narrowing
conversions if the convert flag is set.
(PR `#829 <https://github.com/wjakob/nanobind/pull/829>`__)

Version 2.4.0 (Dec 6, 2024)
---------------------------
Expand Down
17 changes: 15 additions & 2 deletions include/nanobind/nb_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,23 @@ template <typename T>
struct type_caster<T, enable_if_t<std::is_arithmetic_v<T> && !is_std_char_v<T>>> {
NB_INLINE bool from_python(handle src, uint8_t flags, cleanup_list *) noexcept {
if constexpr (std::is_floating_point_v<T>) {
if constexpr (sizeof(T) == 8)
if constexpr (std::is_same_v<T, double>) {
return detail::load_f64(src.ptr(), flags, &value);
else
} else if constexpr (std::is_same_v<T, float>) {
return detail::load_f32(src.ptr(), flags, &value);
} else {
double d;
if (!detail::load_f64(src.ptr(), flags, &d))
return false;
T result = (T) d;
if ((flags & (uint8_t) cast_flags::convert)
|| (double) result == d
|| (result != result && d != d)) {
Copy link
Owner

Choose a reason for hiding this comment

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

What does result != result && d != d accomplish that result != result does not do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend for the caster to work for any floating-point type. The type T may not have Inf. If that is the case, then a double precision Inf would be converted to NaN. So, result != result but d == d. This is a value-changing conversion, so we want it to fail if noconvert() was specified. The same can happen if d is large. Then the conversion would overflow (depending on rounding mode), and although d is finite, result is NaN.
This is a possible scenario. Nvidia, Intel, Arm, Google, AMD, and Meta have "approved" an 8-bit floating-point specification E4M3 which does not have Inf but does have NaN. (E5M2 has both Inf and NaN.)
https://www.opencompute.org/documents/ocp-8-bit-floating-point-specification-ofp8-revision-1-0-2023-12-01-pdf-1

Maybe we could just check d != d. But that would be wrong if T does not support NaN. I cannot immediately think of a system relevant to nanobind that has such a type, but I'd rather play it safe. The NaN comparison check is at the end of all the short-circuiting, so I think it won't affect performance in practical usage.

value = result;
return true;
}
return false;
}
} else {
if constexpr (std::is_signed_v<T>) {
if constexpr (sizeof(T) == 8)
Expand Down
29 changes: 19 additions & 10 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept {

#if !defined(Py_LIMITED_API)
if (NB_LIKELY(is_float)) {
*out = (double) PyFloat_AS_DOUBLE(o);
*out = PyFloat_AS_DOUBLE(o);
return true;
}

Expand All @@ -915,7 +915,7 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept {
double result = PyFloat_AsDouble(o);

if (result != -1.0 || !PyErr_Occurred()) {
*out = (double) result;
*out = result;
return true;
} else {
PyErr_Clear();
Expand All @@ -927,22 +927,31 @@ bool load_f64(PyObject *o, uint8_t flags, double *out) noexcept {

bool load_f32(PyObject *o, uint8_t flags, float *out) noexcept {
bool is_float = PyFloat_CheckExact(o);
bool convert = flags & (uint8_t) cast_flags::convert;

#if !defined(Py_LIMITED_API)
if (NB_LIKELY(is_float)) {
*out = (float) PyFloat_AS_DOUBLE(o);
return true;
double d = PyFloat_AS_DOUBLE(o);
float result = (float) d;
if (convert || (double) result == d || d != d) {
*out = result;
return true;
} else {
return false;
}
}

is_float = false;
Copy link
Owner

Choose a reason for hiding this comment

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

Can you re-enable this assigment? I am not sure that all compilers will understand that is_float can only be false following this conditional. Having the assignment gurantees that constant propagation will remove the check below.

#endif

if (is_float || (flags & (uint8_t) cast_flags::convert)) {
double result = PyFloat_AsDouble(o);

if (result != -1.0 || !PyErr_Occurred()) {
*out = (float) result;
return true;
if (is_float || convert) {
double d = PyFloat_AsDouble(o);
if (d != -1.0 || !PyErr_Occurred()) {
float result = (float) d;
if (convert || (double) result == d || d != d) {
*out = result;
return true;
}
} else {
PyErr_Clear();
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ NB_MODULE(test_functions_ext, m) {
m.def("test_21_g", []() { return nb::int_(1.5); });
m.def("test_21_h", []() { return nb::int_(1e50); });

// Test floating-point
m.def("test_21_dnc", [](double d) { return d + 1.0; }, nb::arg().noconvert());
m.def("test_21_fnc", [](float f) { return f + 1.0f; }, nb::arg().noconvert());

// Test capsule wrapper
m.def("test_22", []() -> void * { return (void*) 1; });
m.def("test_23", []() -> void * { return nullptr; });
Expand Down
12 changes: 12 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ def test21_numpy_overloads():
assert t.test_11_sll(np.int32(5)) == 5
assert t.test_11_ull(np.int32(5)) == 5

with pytest.raises(TypeError) as excinfo:
t.test_21_dnc(np.float64(21.0)) # Python type is not exactly float
assert "incompatible function arguments" in str(excinfo.value)
assert t.test_21_dnc(float(np.float64(21.0))) == 22.0
assert t.test_21_dnc(float(np.float32(21.0))) == 22.0

assert t.test_21_fnc(float(np.float32(21.0))) == 22.0
with pytest.raises(TypeError) as excinfo:
t.test_21_fnc(float(np.float64(21.1))) # Inexact narrowing to float32
assert "incompatible function arguments" in str(excinfo.value)
assert t.test_21_fnc(float(np.float32(21.1))) == np.float32(22.1)


def test22_string_return():
assert t.test_12("hello") == "hello"
Expand Down
4 changes: 4 additions & 0 deletions tests/test_functions_ext.pyi.ref
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ def test_20(arg: str, /) -> object: ...

def test_21(arg: int, /) -> int: ...

def test_21_dnc(arg: float) -> float: ...

def test_21_f(arg: float, /) -> int: ...

def test_21_fnc(arg: float) -> float: ...

hpkfft marked this conversation as resolved.
Show resolved Hide resolved
def test_21_g() -> int: ...

def test_21_h() -> int: ...
Expand Down
Loading