Thanks to visit codestin.com
Credit goes to github.com

Skip to content

API: Slash down errstate related API to make it sustainable #23900

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

Closed
seberg opened this issue Jun 8, 2023 · 4 comments Β· Fixed by #23922
Closed

API: Slash down errstate related API to make it sustainable #23900

seberg opened this issue Jun 8, 2023 · 4 comments Β· Fixed by #23922
Labels
62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.

Comments

@seberg
Copy link
Member

seberg commented Jun 8, 2023

For NumPy 2.0 (or well, TBH, I think we could also pull this off in a normal release, but then I might ask for a brief NEP, because I want to jump deprecations), I would like:

  • Delete any weird "log" and "callback" stuff from the errobj/errstate
  • Remove seterrobj, geterrobj, seterrcall, geterrcall and the ufunc errobj=
    • I would add an error on the last one with a note that we are willing to accept patches to restore some of its behavior if there is a real need. I don't hate allowing passing this state in, but I think it should evolve (and probably have a new name).
  • geterr() and seterr() are OK, since they take/return dicts. errstate is preferable, but they don't matter for this. (I am OK with removing them, but I think just deprecating them is also fine.)

errobj is a weird mutable list, I don't think using it is good and I suspect it will just make fixing the errstate hard.

Now the callback is probably used a couple of time in the wild, I found this: https://github.com/aglavic/genx/blob/8083fb73271f24dbf8c92433a6a86b422fefede5/genx/genx/core/custom_logging.py#L80

I think it should be fine (and backwards compatible) to ask such programs to monkey-patch warnings.showwarning instead.


Can I get a few πŸ‘ for this change? :)

EDIT: Whooops, the ufunc keyword argument is called extobj not errobj, shows how much its used ;).

@seberg seberg moved this to πŸ“‹ Candidate in NumPy 2.0 Roadmap Jun 8, 2023
@seberg seberg moved this from πŸ“‹ Candidate to πŸ”– Proposal in NumPy 2.0 Roadmap Jun 8, 2023
@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Jun 8, 2023
@seberg
Copy link
Member Author

seberg commented Jun 8, 2023

SciPy does not use any of these but numpy.linalg does. We could replace it with a try/except and the with errstate(). That is slightly noticeable in speed (<2.5us of 10us for a single 3x3 inverse) due to the errstate overhead and slightly ugly.

Maybe starting with seterrobj and geterrobj will be easier. Although, I doubt the slowdown is bad enough to worry about (and can probably be slashed down in an eventual follow-up).

@seberg
Copy link
Member Author

seberg commented Jun 9, 2023

This also requires ripping out the C-API functions PyUFunc_GetPyValues and PyUFunc_handlefperr.

They are designed in an awkward way to allow use within a ufunc inner-loop. Something that we don't do (presumably anymore). I have not found any usage of them.

If people speak up with need, we (or rather they) could add the new PyUFunc_GiveFloatingpointErrors to the public API with backport. The function squashes both versions.

@seberg
Copy link
Member Author

seberg commented Jun 9, 2023

Basically, the proposal is now:

Remove the C-API functions and at least (set|get)errobj as well as the errobj= kwarg.

  • np.errstate() supports everything linalg needs. The only caveat is overhead and I am sure that this can (and would presumably) be reduced by at least a factor of 2: it's fine.

Additionally, I would think it is a good idea to slash the API more, because few use it anyway:

  • seterr, seterrcall, setbufsize could be folded into something like np.errstate(...).apply(). (Occasionally it is useful to set things globally; this would discourage that as well as reduce API.)
  • The introspection geterr, geterrcall, and getbufsize could also be attributes on np.errstate(). I think that makes sense: they are probably practically never used, but no introspection capability would be weird.

Those changes are:

  • Low impact: You may get an error and the replacement isn't super tricky and shouldn't be impossible to find.
  • Few users affected: seterr will be used mainly, most of which should just switch to errstate anyway. A few people who set it globally may need a try/except hack. (If anyone worries about that, or people complain, we could keep seterr around or add it back.)

(The C-API change is in theory higher impact. But its hard to see why anyone needs to align with our machinery and we should be able to add work-arounds in the off-chance that someone complains. Plus, there are no known users, so it wouldn't matter even if it was higher impact.)

EDIT: I will add removing of log and print. For no particular reason beyond that call should be enough to log in practice and that print to stdout is weird and the only point for it is avoiding grabbing the GIL (which doesn't make sense in our use of this).

@mattip
Copy link
Member

mattip commented Jun 11, 2023

+1 for the cleanup. I would like to move to a contextvar as well

@github-project-automation github-project-automation bot moved this from πŸ”– Proposal to πŸ— In progress in NumPy 2.0 Roadmap Jun 14, 2023
@seberg seberg moved this from πŸ— In progress to βœ… Done in NumPy 2.0 Roadmap Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.
Projects
Status: βœ… Done
Development

Successfully merging a pull request may close this issue.

2 participants