8000 ENH, SIMD: Initial implementation of Highway wrapper by seiko2plus · Pull Request #28622 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH, SIMD: Initial implementation of Highway wrapper #28622

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
merged 7 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
SIMD: Rename NPY_SIMDX to NPY_HWY
Rename Highway wrapper macros for clarity:
- NPY_SIMDX → NPY_HWY
- NPY_SIMDX_F16 → NPY_HWY_F16
- NPY_SIMDX_F64 → NPY_HWY_F64
- NPY_SIMDX_FMA → NPY_HWY_FMA

To avoids confusion with legacy SIMD macros.
  • Loading branch information
seiko2plus committed May 19, 2025
commit 6519b288d66dcc7ed27cfeb664a1eb2d279d8492
16 changes: 8 additions & 8 deletions numpy/_core/src/common/simd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ StoreU(v128, data);
#include "simd/simd.hpp"

// Check if SIMD is enabled
#if NPY_SIMDX
#if NPY_HWY
// SIMD code
#else
// Scalar fallback code
#endif

// Check for float64 support
#if NPY_SIMDX_F64
#if NPY_HWY_F64
// Use float64 SIMD operations
#endif

// Check for FMA support
#if NPY_SIMDX_FMA
#if NPY_HWY_FMA
// Use FMA operations
#endif
```
Expand All @@ -70,9 +70,9 @@ The wrapper provides type constraints to help with SFINAE (Substitution Failure
```cpp
// Base template - always defined, even when SIMD is not enabled (for SFINAE)
template <typename TLane>
constexpr bool kSupportLane = NPY_SIMDX != 0;
constexpr bool kSupportLane = NPY_HWY != 0;
template <>
constexpr bool kSupportLane<double> = NPY_SIMDX_F64 != 0;
constexpr bool kSupportLane<double> = NPY_HWY_F64 != 0;
```

- `kMaxLanes<TLane>`: Maximum number of lanes supported by the SIMD extension for the specified lane type.
Expand Down Expand Up @@ -175,15 +175,15 @@ The SIMD wrapper automatically disables SIMD operations when optimizations are d

2. **Legacy Universal Intrinsics**
- The older universal intrinsics C interface (in `simd.h` and accessible via `NPY_SIMD` macros) is deprecated
- All new SIMD code should use this Highway-based wrapper (accessible via `NPY_SIMDX` macros)
- All new SIMD code should use this Highway-based wrapper (accessible via `NPY_HWY` macros)
- The legacy code is maintained for compatibility but will eventually be removed

3. **Feature Detection Constants vs. Highway Constants**
- NumPy-specific constants (`NPY_SIMDX_F16`, `NPY_SIMDX_F64`, `NPY_SIMDX_FMA`) provide additional safety beyond raw Highway constants
- NumPy-specific constants (`NPY_HWY_F16`, `NPY_HWY_F64`, `NPY_HWY_FMA`) provide additional safety beyond raw Highway constants
- Highway constants (e.g., `HWY_HAVE_FLOAT16`) only check platform capabilities but don't consider NumPy's build configuration
- Our constants combine both checks:
```cpp
#define NPY_SIMDX_F16 (NPY_SIMDX && HWY_HAVE_FLOAT16)
#define NPY_HWY_F16 (NPY_HWY && HWY_HAVE_FLOAT16)
```
- This ensures SIMD features won't be used when:
- Platform supports it but NumPy optimization is disabled via meson option:
Expand Down
22 changes: 11 additions & 11 deletions numpy/_core/src/common/simd/simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
/**
* Since `NPY_SIMD` is only limited to NumPy C universal intrinsics,
* `NPY_SIMDX` is defined to indicate the SIMD availability for Google's Highway
* `NPY_HWY` is defined to indicate the SIMD availability for Google's Highway
* C++ code.
*
* Highway SIMD is only available when optimization is enabled.
Expand All @@ -29,31 +29,31 @@
*
* Therefore, we only enable SIMD when the Highway target is not scalar.
*/
#define NPY_SIMDX (HWY_TARGET != HWY_SCALAR)
#define NPY_HWY (HWY_TARGET != HWY_SCALAR)

8000 // Indicates if the SIMD operations are available for float16.
#define NPY_SIMDX_F16 (NPY_SIMDX && HWY_HAVE_FLOAT16)
#define NPY_HWY_F16 (NPY_HWY && HWY_HAVE_FLOAT16)
// Note: Highway requires SIMD extentions with native float32 support, so we don't need
// to check for it.

// Indicates if the SIMD operations are available for float64.
#define NPY_SIMDX_F64 (NPY_SIMDX && HWY_HAVE_FLOAT64)
#define NPY_HWY_F64 (NPY_HWY && HWY_HAVE_FLOAT64)

// Indicates if the SIMD floating operations are natively supports fma.
#define NPY_SIMDX_FMA (NPY_SIMDX && HWY_NATIVE_FMA)
#define NPY_HWY_FMA (NPY_HWY && HWY_NATIVE_FMA)

#else
#define NPY_SIMDX 0
#define NPY_SIMDX_F16 0
#define NPY_SIMDX_F64 0
#define NPY_SIMDX_FMA 0
#define NPY_HWY 0
#define NPY_HWY_F16 0
#define NPY_HWY_F64 0
#define NPY_HWY_FMA 0
#endif

namespace np {

/// Represents the max SIMD width supported by the platform.
namespace simd {
#if NPY_SIMDX
#if NPY_HWY
/// The highway namespace alias.
/// We can not import all the symbols from the HWY_NAMESPACE because it will
/// conflict with the existing symbols in the numpy namespace.
Expand All @@ -67,7 +67,7 @@ using _Tag = hn::ScalableTag<TLane>;

/// Represents the 128-bit SIMD width.
namespace simd128 {
#if NPY_SIMDX
#if NPY_HWY
namespace hn = hwy::HWY_NAMESPACE;
template <typename TLane>
using _Tag = hn::Full128<TLane>;
Expand Down
12 changes: 6 additions & 6 deletions numpy/_core/src/common/simd/simd.inc.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef NPY_SIMDX
#ifndef NPY_HWY
#error "This is not a standalone header. Include simd.hpp instead."
#define NPY_SIMDX 1 // Prevent editors from graying out the happy branch
#define NPY_HWY 1 // Prevent editors from graying out the happy branch
#endif

// Using anonymous namespace instead of inline to ensure each translation unit
Expand All @@ -17,9 +17,9 @@ namespace {
* @tparam TLane The lane type to check for support.
*/
template <typename TLane>
constexpr bool kSupportLane = NPY_SIMDX != 0;
constexpr bool kSupportLane = NPY_HWY != 0;

#if NPY_SIMDX
#if NPY_HWY
// Define lane type support based on Highway capabilities
template <>
constexpr bool kSupportLane<hwy::float16_t> = HWY_HAVE_FLOAT16 != 0;
Expand Down Expand Up @@ -127,6 +127,6 @@ using hn::Sqrt;
using hn::Sub;
using hn::Xor;

#endif // NPY_SIMDX
#endif // NPY_HWY

} // namespace anonymous
} // namespace
0