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

Add allow_subnormal kwarg to floats() #3155

Closed
honno opened this issue Nov 17, 2021 · 2 comments · Fixed by #3156
Closed

Add allow_subnormal kwarg to floats() #3155

honno opened this issue Nov 17, 2021 · 2 comments · Fixed by #3156
Assignees
Labels
enhancement it's not broken, but we want it to be better

Comments

@honno
Copy link
Member

honno commented Nov 17, 2021

Currently floats() generates subnormal floats, which would not be desired when testing "flush-to-zero" libraries (the one concrete use case being CuPy in data-apis/array-api-tests#42). The subnormals generated could be filtered out, but it would be nice if there was first-party support from Hypothesis, especially as how tricky such filtering could be for an end user.

I can take a look at doing this myself tomorrow, but mind I've never touched floats() before heh. I guess I'd need to figure out how to calculate the normal-subnormal boundary with width via the IEEE 754 standard.

@Zac-HD
Copy link
Member

Zac-HD commented Nov 18, 2021

This will be, effectively, a filter in FloatStrategy.permitted() for unbounded floats - for bounded floats since they're strictly positive at this low level you can adjust the lower bound.

To get the bounds, see sys.float_info.min and the note. For 32bit and 16bit floats I think you'll just need to calculate the bounds as module-constants.

@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label Nov 18, 2021
@Zac-HD
Copy link
Member

Zac-HD commented Nov 18, 2021

We might also add a more specific error message for this case, e.g.

if (
    np.issubdtype(self.dtype, np.floating)
    and result[idx] == 0
    and 0 < abs(val) < np.finfo(self.dtype).tiny
):
    raise InvalidArgument(
        f"Generated subnormal float {val}, but adding it to the array "
        f"resulted in {result[idx]!r}, probably as a result of IEEE-754-"
        "violating compiler options like -ffast-math.  Consider passing "
        "allow_subnormal=False."
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants