-
-
Notifications
You must be signed in to change notification settings - Fork 11k
BUG: Address interaction between SME and FPSR #29223
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
Merged
charris
merged 2 commits into
numpy:main
from
Developer-Ecosystem-Engineering:patch_sme_fpsr_interop
Jun 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#ifdef __APPLE__ | ||
#include <sys/sysctl.h> | ||
#endif | ||
|
||
#include "numpy/numpyconfig.h" // NPY_VISIBILITY_HIDDEN | ||
#include "numpy/npy_math.h" // npy_get_floatstatus_barrier | ||
#include "blas_utils.h" | ||
|
||
#if NPY_BLAS_CHECK_FPE_SUPPORT | ||
|
||
/* Return whether we're running on macOS 15.4 or later | ||
*/ | ||
static inline bool | ||
is_macOS_version_15_4_or_later(void){ | ||
#if !defined(__APPLE__) | ||
return false; | ||
#else | ||
char *osProductVersion = NULL; | ||
size_t size = 0; | ||
bool ret = false; | ||
|
||
// Query how large OS version string should be | ||
if(-1 == sysctlbyname("kern.osproductversion", NULL, &size, NULL, 0)){ | ||
goto cleanup; | ||
} | ||
|
||
osProductVersion = malloc(size + 1); | ||
|
||
// Get the OS version string | ||
if(-1 == sysctlbyname("kern.osproductversion", osProductVersion, &size, NULL, 0)){ | ||
goto cleanup; | ||
} | ||
|
||
osProductVersion[size] = '\0'; | ||
|
||
// Parse the version string | ||
int major = 0, minor = 0; | ||
if(2 > sscanf(osProductVersion, "%d.%d", &major, &minor)) { | ||
goto cleanup; | ||
} | ||
|
||
if(major >= 15 && minor >= 4){ | ||
ret = true; | ||
} | ||
|
||
cleanup: | ||
if(osProductVersion){ | ||
free(osProductVersion); | ||
} | ||
|
||
return ret; | ||
#endif | ||
} | ||
|
||
/* ARM Scalable Matrix Extension (SME) raises all floating-point error flags | ||
* when it's used regardless of values or operations. As a consequence, | ||
* when SME is used, all FPE state is lost and special handling is needed. | ||
* | ||
* For NumPy, SME is not currently used directly, but can be used via | ||
* BLAS / LAPACK libraries. This function does a runtime check for whether | ||
* BLAS / LAPACK can use SME and special handling around FPE is required. | ||
*/ | ||
static inline bool | ||
BLAS_can_use_ARM_SME(void) | ||
{ | ||
#if defined(__APPLE__) && defined(__aarch64__) && defined(ACCELERATE_NEW_LAPACK) | ||
// ARM SME can be used by Apple's Accelerate framework for BLAS / LAPACK | ||
// - macOS 15.4+ | ||
// - Apple silicon M4+ | ||
|
||
// Does OS / Accelerate support ARM SME? | ||
if(!is_macOS_version_15_4_or_later()){ | ||
return false; | ||
} | ||
|
||
// Does hardware support SME? | ||
int has_SME = 0; | ||
size_t size = sizeof(has_SME); | ||
if(-1 == sysctlbyname("hw.optional.arm.FEAT_SME", &has_SME, &size, NULL, 0)){ | ||
return false; | ||
} | ||
|
||
if(has_SME){ | ||
return true; | ||
} | ||
#endif | ||
|
||
// default assume SME is not used | ||
return false; | ||
} | ||
|
||
/* Static variable to cache runtime check of BLAS FPE support. | ||
*/ | ||
static bool blas_supports_fpe = true; | ||
|
||
#endif // NPY_BLAS_CHECK_FPE_SUPPORT | ||
|
||
|
||
NPY_VISIBILITY_HIDDEN bool | ||
npy_blas_supports_fpe(void) | ||
{ | ||
#if NPY_BLAS_CHECK_FPE_SUPPORT | ||
return blas_supports_fpe; | ||
#else | ||
return true; | ||
#endif | ||
} | ||
|
||
NPY_VISIBILITY_HIDDEN void | ||
npy_blas_init(void) | ||
{ | ||
#if NPY_BLAS_CHECK_FPE_SUPPORT | ||
blas_supports_fpe = !BLAS_can_use_ARM_SME(); | ||
#endif | ||
} | ||
|
||
NPY_VISIBILITY_HIDDEN int | ||
npy_get_floatstatus_after_blas(void) | ||
{ | ||
#if NPY_BLAS_CHECK_FPE_SUPPORT | ||
if(!blas_supports_fpe){ | ||
// BLAS does not support FPE and we need to return FPE state. | ||
// Instead of clearing and then grabbing state, just return | ||
// that no flags are set. | ||
return 0; | ||
} | ||
#endif | ||
char *param = NULL; | ||
return npy_get_floatstatus_barrier(param); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdbool.h> | ||
|
||
#include "numpy/numpyconfig.h" // for NPY_VISIBILITY_HIDDEN | ||
|
||
/* NPY_BLAS_CHECK_FPE_SUPPORT controls whether we need a runtime check | ||
* for floating-point error (FPE) support in BLAS. | ||
*/ | ||
#if defined(__APPLE__) && defined(__aarch64__) && defined(ACCELERATE_NEW_LAPACK) | ||
#define NPY_BLAS_CHECK_FPE_SUPPORT 1 | ||
#else | ||
#define NPY_BLAS_CHECK_FPE_SUPPORT 0 | ||
#endif | ||
|
||
/* Initialize BLAS environment, if needed | ||
*/ | ||
NPY_VISIBILITY_HIDDEN void | ||
npy_blas_init(void); | ||
|
||
/* Runtime check if BLAS supports floating-point errors. | ||
* true - BLAS supports FPE and one can rely on them to indicate errors | ||
* false - BLAS does not support FPE. Special handling needed for FPE state | ||
*/ | ||
NPY_VISIBILITY_HIDDEN bool | ||
npy_blas_supports_fpe(void); | ||
|
||
/* If BLAS supports FPE, exactly the same as npy_get_floatstatus_barrier(). | ||
* Otherwise, we can't rely on FPE state and need special handling. | ||
*/ | ||
NPY_VISIBILITY_HIDDEN int | ||
npy_get_floatstatus_after_blas(void); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
As written, this comparison will return false for e.g.
26.0
.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 fixed this in the backport.