Skip to content

BUG: Show warning for ignored CPU feature names - #33027

Open
mikamikasuki wants to merge 2 commits into
numpy:mainfrom
mikamikasuki:fix/visible-legacy-cpu-feature-warning-33016
Open

mikamikasuki wants to merge 2 commits into
numpy:mainfrom
mikamikasuki:fix/visible-legacy-cpu-feature-warning-33016

Conversation

@mikamikasuki

Copy link
Copy Markdown

PR summary
Fixes #33016.
Previously, when NPY_DISABLE_CPU_FEATURES contained an unavailable or unrecognized CPU feature name, NumPy emitted an ImportWarning, which is normally hidden by Python's default warning filters. As a result, users could mistakenly believe that their requested CPU feature configuration had been applied.
This PR changes the warning category to UserWarning, making the ignored feature names visible under Python's default warning settings. The change is limited to warning visibility and does not alter CPU feature detection or dispatch behavior.
Validation: 12 CPU feature tests passed, with 6 platform-specific tests skipped.
First time contributor introduction
Hi! I'm a computer science student interested in scientific computing and open-source software. I use NumPy in machine learning and research projects, and I enjoy investigating small issues that can make numerical software more reliable and easier to use.
AI disclosure
Parts of this change were developed with AI assistance. I reviewed the changes and validated the behavior with the relevant tests.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like @seiko2plus intentionally suggested this (although visibility also came up) in #22137 (comment). Maybe he has an opinion here? That said, I think it does make sense to make a visible warning about this.

I think it also probably needs a release note.

Comment thread numpy/_core/tests/test_cpu_features.py Outdated
Comment on lines +290 to +299
@pytest.mark.skipif(
not __cpu_dispatch__ or "AVX512F" in __cpu_dispatch__,
reason="AVX512F is still a dispatched feature in this build",
)
def test_legacy_feature_name_warning_is_visible(self):
"""Legacy feature names that are no longer dispatched are visible."""
self.env['NPY_DISABLE_CPU_FEATURES'] = "AVX512F"
out = self._run()
assert "UserWarning" in out.stderr
assert "AVX512F" in out.stderr

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we use a nonexistent feature name and cover both environment variables? Something like this (I used an AI model):

Suggested change
@pytest.mark.skipif(
not __cpu_dispatch__ or "AVX512F" in __cpu_dispatch__,
reason="AVX512F is still a dispatched feature in this build",
)
def test_legacy_feature_name_warning_is_visible(self):
"""Legacy feature names that are no longer dispatched are visible."""
self.env['NPY_DISABLE_CPU_FEATURES'] = "AVX512F"
out = self._run()
assert "UserWarning" in out.stderr
assert "AVX512F" in out.stderr
@pytest.mark.skipif(
not __cpu_dispatch__,
reason=(
"NPY_*_CPU_FEATURES only parsed if "
"`__cpu_dispatch__` is non-empty"
)
)
@pytest.mark.parametrize("action", ["ENABLE", "DISABLE"])
def test_unknown_feature_warning_is_visible(self, action):
feature = "NOT_A_CPU_FEATURE"
self.env[f"NPY_{action}_CPU_FEATURES"] = feature
self.env.pop("PYTHONWARNINGS", None)
out = self._run()
assert re.search(
rf"UserWarning:.*{re.escape(feature)}",
out.stderr,
flags=re.DOTALL,
)

@seiko2plus seiko2plus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

At first sight (#33016 (comment)), I didn't like the idea but after re-considering yes indeed it should be treated as UserWarning. However, you missed a case here that needs to be changed too:

if (PyErr_WarnFormat(PyExc_ImportWarning, 1,
"%s"
"You cannot use environment variable '%s', since "
#ifdef NPY_DISABLE_OPTIMIZATION

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Legacy NPY_DISABLE_CPU_FEATURES names are ignored without a visible warning

3 participants