ENH allow for specifying CPU features to enable via NPY_ENABLE_CPU_FEATURES environment variable#22137
Conversation
NPY_ENABLE_CPU_FEATURES environment variable
|
@mattip @seiko2plus Do you know where I should add the tests for this? |
maybe within [numpy/core/tests/test_cpu_dispatcher.py] (https://github.com/numpy/numpy/blob/main/numpy/core/tests/test_cpu_dispatcher.py) but it will require loading the module in a separate process. |
|
@seiko2plus Is there a preference in NumPy for warnings vs errors? My question here is partially because of the fact that enabling a feature that doesn't exist is not equivalent to disabling a feature that doesn't exist, and so may require different ways of handling the error--a warning for the latter seems appropriate, but in the case of the former, it may be better to throw an error and exit. Thoughts? |
For reviewers and interested parties, the current PR's behavior regarding errors/warnings: numpy/numpy/core/src/common/npy_cpu_features.h Lines 108 to 123 in a6231b6 |
|
As long as the error does not affect the workflow of the following procedures, then choosing to raise runtime-warning would make sense. according to Python's warning-doc import os, warnings
os.environ['NPY_DISABLE_CPU_FEATURES'] = 'XXXX'
warnings.simplefilter("error", RuntimeWarning)
import numpy as np |
|
@seiko2plus That's perfect. Thank you! |
|
Tests are missing. For an example of tests that change the environment variables and then run python in a subprocess, see |
|
I can't reproduce these errors locally. Even when setting up a machine with |
|
@Micky774, to reproduce this error, you have to disable the dispatch-able features similar to what the smoke test does, for example: python setup.py build --cpu-baseline=native --cpu-dispatch=none install --user
python runtests.py -nor just python runtests.py --cpu-dispatch=noneIn general, you have to escape the dispatch tests if |
|
@Micky774, please let me know when it's ready for review. |
@seiko2plus Should be good now! Thanks for the help :) |
|
@seiko2plus could you take a look? I think the aarch64 failure is systematic and not connected to this PR. |
seiko2plus
left a comment
There was a problem hiding this comment.
LGTM, Just requires some simplifying.
|
@seiko2plus Should be ready when you get a chance to review. |
|
@seiko2plus Just a gentle reminder ping. Let me know if you have any other concerns to address for this PR :) |
|
@seiko2plus @seberg Just wanted to ping again regarding this PR. |
seiko2plus
left a comment
There was a problem hiding this comment.
The C implementation looks good to me, just the unit testing may need some modification as follows
|
Coverage thinks tests are missing for some of the corner cases. |
| return -1; | ||
| } | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
The return 0 is not covered, but I think that is because the warning is always turned into an error in tests. Perhaps this was a latent bug?
| return -1; | ||
| } | ||
| if (PyErr_WarnFormat(PyExc_ImportWarning, 1, NOTSUPP_BODY) < 0) { | ||
| return -1; |
There was a problem hiding this comment.
It seems these cases are not covered in tests?
There was a problem hiding this comment.
The first one (RuntimeError) is covered, however the second one (ImportWarning) wasn't. Upon closer inspection, the only time the latter would run is if:
NPY_CPU_DISABLE_FEATUREwas set to an unsupported feature- The unsupported feature is included in dispatch
From my current understanding (I am a bit rusty) an unsupported feature cannot, by default, appear in dispatch. Thus I have now removed this check
|
rebuild CI |
|
@Micky774 will you be continuing this? |
|
Admittedly this had fallen off my radar, but yes I'll resume work on it over the next few days! Thank you for the reminder :) |
|
@mattip Do you know if the failing |
No. It is one of those failures that happens now and then on Windows for unknown reasons. It is probably compiler related, you can ignore it. |
|
I think this needs to be rebased off main rather than having main merged in for the coverage report to be correct? Otherwise I don't understand why coverage is claiming so much of the code is not tested. |
|
Squashed and rebased off main |
|
Coverage is still claiming the new code is not tested. When I run them locally on my AMD machine, they are skipped since I do not have any features beyond the baseline. |
|
I have verified the coverage report is bogus and the new tests run. |
|
Thanks @Micky774 |
Related issues/PRs
Fixes #22058
What does this PR add?
This PR enables the usage of a
NPY_ENABLE_CPU_FEATURESenvironment variable whose function is described in #22058.Note that the enabling function errors when asked to enable features not supported by the CPU, or that numpy was not built with. This breaks symmetry with the disabling semantics, but I think this makes sense since it would be a drastic expectation violation otherwise.
Other comments