-
Notifications
You must be signed in to change notification settings - Fork 617
Add allow_subnormal
kwarg to st.floats()
#3156
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I think you're on the right track 🎉
Some small comments below. Note also that we'll want to have the same subnormal handling for numpy and the array_api extra.
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
4e3b2bb
to
1e6f9ee
Compare
1e6f9ee
to
f038b7b
Compare
@Zac-HD thanks for all your great suggestions—all code comments should be resolved :) RE the failing There was a failing codemod test... TIL about codemods heh. I think my change to the test is okay but thought I'd mention it. I'm mostly unavailable until next week so didn't want to sink time on adding NumPy/Array API |
|
453be35
to
75ebecb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking fantastic - thanks again @honno!
- I'd like to ship the array-related parts at the same time as the rest of it, and don't mind taking a few weeks longer to get it all right.
- Let me know if you'd like to try fixing the codemods, or want me to push a commit to your branch.
- For
from_dtype()
- as well as supportingallow_subnormal
as a kwarg, I think we can address data-apis/array-api-tests#42 by passingFalse
if we detect FTZ behaviour (which is cachable per module-and-bitwidth, and cheap with a one-element array). That way we can continue testing subnormals on libraries that represent them, and skip them for those which don't.
(but still add the more specific error, for manually-passed element strategies)
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
d2c0ad1
to
818fca2
Compare
@Zac-HD I've added the subnormal inference and helpful error for
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helpful error looks great!
As you note we'll need a separate FTZ check for each module and size; I'd suggest having an @lru_cache()
d function like ftz(xp, width) -> bool
for this. Some comments below, but looking very nice overall and there's nothing new to do.
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
Can't FTZ checks be done once in the |
e57562a
to
402dc78
Compare
Yeah, once-per-size in |
@Zac-HD RE With NumPy, I can ofc try building NumPy with ftz (15 min builds gets tiring fast when I inevitably screw something up and have to rebuild) and testing locally, but we'll probably want a ftz build of NumPy on CI too. With Theres a general maintenance burden of ftz inference and ... or support |
I think it's important to support You make a good point about the maintainence/testing burden for automatic FTZ detection: let's defer that to a future PR. |
313c1c0
to
1b2f811
Compare
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
No idea why the win tests fail i.e. why are no subnormals generated with |
Yeah, this is weird! I'm happy to add a |
2097a95
to
0576252
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost done! Thanks again @honno ... and remind me next time that we should chop your PRs into smaller pieces, they always end up growing into huge complicated things; I should remember this; and we can ship them in multiple easier stages instead.
d88154b
to
35d87f7
Compare
a2f7ee9
to
9a1e9eb
Compare
IMO we should xfail instead of skip the tests that look for |
Xfailing doesn't work in this case, because the tests currently pass with very low probability. So in strict mode (which we use) they'd be flaky; in permissive mode they'd act like skips but take morw compute. So we do need to skip, and I'll just remember them when the time comes. |
9a1e9eb
to
512c284
Compare
Oh right yeah flakiness 😅 Thanks for explaining |
Lets us remove the `float()` casting in `_from_dtype()`, which was previously necessary as mocking with `np.finfo()` produced a finfo object that held NumPy scalars instead of Python scalars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done a full review, and think this is substantively complete! Thanks again and congratulations on yet another large PR 🎉
If you want to tackle the final move-some-things comments feel free; otherwise I'm happy to get them and then merge tomorrow.
hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Outdated
Show resolved
Hide resolved
hypothesis-python/RELEASE.rst
Outdated
:func:`hypothesis.extra.numpy.from_dtype` and :func:`xps.from_dtype` can also | ||
accept the ``allow_subnormal`` when inferring float strategies. | ||
:func:`xps.from_dtype` specifically will disable subnormals by default if the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:func:`hypothesis.extra.numpy.from_dtype` and :func:`xps.from_dtype` can also | |
accept the ``allow_subnormal`` when inferring float strategies. | |
:func:`xps.from_dtype` specifically will disable subnormals by default if the | |
:func:`~hypothesis.extra.numpy.from_dtype` and :func:`xps.from_dtype` also | |
accept the ``allow_subnormal`` argument, and :func:`xps.from_dtype` or | |
:func:`xps.arrays` will disable subnormals by default if the |
Missing word "argument", also affects xps.arrays()
, minor sentence tweaks.
FYI you can click the readthedocs CI details to see a preview build, e.g. https://hypothesis--3156.org.readthedocs.build/en/3156/changes.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions make sense, updated accordingly... except for hypothesis.extra.numpy.from_dtype()
I've explicitly specified it as nps.from_dtype()
, to distinguish it from xps.from_dtype()
(had purposely not used the tilde, think this is a happy compromise).
Was only moved to Hypothesis proper because I had experimented using it in `st.floats()`
Co-authored-by: Zac Hatfield-Dodds <[email protected]>
de376af
to
a4c020e
Compare
@Zac-HD Thanks for all the effort you put pushing fixes and extensively reviewing for this PR! 😀 |
Should resolve #3155. I think I've covered all the tricky areas in the newly introduced tests now, so my TODO is seemingly:
test_allow_subnormal_defaults_correctly[allow_subnormal=True, min_value=-1, max_value=1]
is failingFixedBoundedFloatStrategy
st.floats()
docstirngextra.numpy.arrays()
andextra.array_api.arrays()
allow_subnormal
inextra.numpy.from_dtype()
andextra.array_api.from_dtype()
Fix and test kwarg toggling behaviour for out of range e.g.allow_subnormal=False
is invalid when(0,1)
RELEASE.rst
Making a PR now for CI + if anyone would be so kind to tell me if I'm on the right track 😅