π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
fix: make Linux wheels CPU portable
Christopher Jacoby8dd91c687ae9
diff --git a/.github/workflows/all.yml b/.github/workflows/all.ymlindex b3136d9..e9c5bdf 100644--- a/.github/workflows/all.yml+++ b/.github/workflows/all.yml@@ -160,7 +160,7 @@ jobs:CCACHE_IGNOREOPTIONS: "-fno-strict-overflow -fwrapv -W* -arch x86_64 arm64 -dynamic -fno-common -g -I/usr/local/opt/*"CCACHE_LOGFILE: ${{ github.workspace }}/.ccache_logCCACHE_DEBUG: "1"- USE_PORTABLE_SIMD: "1" # Use portable SIMD to avoid "Illegal instruction" on different runner CPUs+ USE_MARCH_NATIVE: "0" # Keep cached objects compatible across runner CPUsDISABLE_LTO: "1" # Speeds up un-cacheable link step which doesn't really increase performance in tests anywaysCC: ${{ matrix.cc }}CXX: ${{ matrix.cxx }}@@ -357,7 +357,7 @@ jobs:CCACHE_IGNOREOPTIONS: "-fno-strict-overflow -fwrapv -W* -arch x86_64 arm64 -dynamic -fno-common -g -I/usr/local/opt/*"CCACHE_LOGFILE: ${{ github.workspace }}/.ccache_logUSE_ASAN: "1"- USE_PORTABLE_SIMD: "1" # Use portable SIMD to avoid "Illegal instruction" on different runner CPUs+ USE_MARCH_NATIVE: "0" # Keep cached objects compatible across runner CPUsDISABLE_LTO: "1" # Speeds up un-cacheable link step which doesn't really increase performance in tests anywaysCC: ${{ matrix.cc }}CXX: ${{ matrix.cxx }}@@ -471,7 +471,7 @@ jobs:CCACHE_NOINODECACHE: 1CCACHE_IGNOREOPTIONS: "-fno-strict-overflow -fwrapv -W* -arch x86_64 arm64 -dynamic -fno-common -g -I/usr/local/opt/*"CCACHE_LOGFILE: ${{ github.workspace }}/.ccache_log- USE_PORTABLE_SIMD: "1" # Use portable SIMD to avoid "Illegal instruction" on different runner CPUs+ USE_MARCH_NATIVE: "0" # Keep cached objects compatible across runner CPUsDISABLE_LTO: "1" # Speeds up un-cacheable link step which doesn't really increase performance in tests anywaysCC: ${{ matrix.cc }}CXX: ${{ matrix.cxx }}@@ -726,7 +726,7 @@ jobs:CCACHE_IGNOREOPTIONS: "-fno-strict-overflow -fwrapv -W* -arch x86_64 arm64 -dynamic -fno-common -g -I/usr/local/opt/*"CCACHE_LOGFILE: ${{ github.workspace }}/.ccache_logCCACHE_DEBUG: "1"- USE_PORTABLE_SIMD: "1" # Use portable SIMD to avoid "Illegal instruction" on different runner CPUs+ USE_MARCH_NATIVE: "0" # Keep cached objects compatible across runner CPUs# Use the minimum macOS deployment target supported by our version of PyBind:MACOSX_DEPLOYMENT_TARGET: "10.14"# This build caching is only to speed up tests on CI, so we only care about x86_64 (macos-15-intel runners).@@ -831,7 +831,7 @@ jobs:env:DEBUG: "0"USE_ASAN: "1"- USE_PORTABLE_SIMD: "1" # Use portable SIMD to avoid "Illegal instruction" on different runner CPUs+ USE_MARCH_NATIVE: "0" # Keep cached objects compatible across runner CPUsCCACHE_DIR: ${{ github.workspace }}/.ccacheCCACHE_BASEDIR: ${{ github.workspace }}CCACHE_DEBUGDIR: ${{ github.workspace }}/.ccache_debug@@ -891,26 +891,26 @@ jobs:- { os: windows-latest, build: cp314-win_amd64 }- { os: windows-latest, build: cp315-win_amd64 }# - { os: windows-latest, build: cp312-win32 }- - { os: "ubuntu-24.04", build: cp310-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp310-manylinux_x86_64, python: "3.10" }- { os: "ubuntu-24.04-arm", build: cp310-manylinux_aarch64 }- { os: "ubuntu-24.04", build: cp310-musllinux_x86_64 }- { os: "ubuntu-24.04-arm", build: cp310-musllinux_aarch64 }- - { os: "ubuntu-24.04", build: cp311-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp311-manylinux_x86_64, python: "3.11" }- { os: "ubuntu-24.04-arm", build: cp311-manylinux_aarch64 }# - { os: 'ubuntu-24.04', build: cp311-musllinux_x86_64 }# - { os: 'ubuntu-24.04', build: cp311-musllinux_aarch64 }- - { os: "ubuntu-24.04", build: cp312-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp312-manylinux_x86_64, python: "3.12" }- { os: "ubuntu-24.04-arm", build: cp312-manylinux_aarch64 }- { os: "ubuntu-24.04", build: cp312-musllinux_x86_64 }- { os: "ubuntu-24.04-arm", build: cp312-musllinux_aarch64 }- - { os: "ubuntu-24.04", build: cp313-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp313-manylinux_x86_64, python: "3.13" }- { os: "ubuntu-24.04-arm", build: cp313-manylinux_aarch64 }# - { os: 'ubuntu-24.04', build: cp313-musllinux_x86_64 }# - { os: 'ubuntu-24.04', build: cp313-musllinux_aarch64 }- - { os: "ubuntu-24.04", build: cp314-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp314-manylinux_x86_64, python: "3.14" }- { os: "ubuntu-24.04-arm", build: cp314-manylinux_aarch64 }- { os: "ubuntu-24.04-arm", build: cp314t-manylinux_aarch64 }- - { os: "ubuntu-24.04", build: cp315-manylinux_x86_64 }+ - { os: "ubuntu-24.04", build: cp315-manylinux_x86_64, python: "3.15" }- { os: "ubuntu-24.04-arm", build: cp315-manylinux_aarch64 }- { os: "ubuntu-24.04-arm", build: cp315t-manylinux_aarch64 }name: Build wheel for ${{ matrix.build }}@@ -933,9 +933,25 @@ jobs:CIBW_TEST_COMMAND: "python -c \"import pedalboard; print(pedalboard.__version__)\""CIBW_TEST_REQUIRES: ""# on macOS and with Python 3.10: building NumPy from source fails without these options:- CIBW_ENVIRONMENT: NPY_BLAS_ORDER="" NPY_LAPACK_ORDER="" CIBW_BUILD="${{ matrix.build }}"+ CIBW_ENVIRONMENT: NPY_BLAS_ORDER="" NPY_LAPACK_ORDER="" CIBW_BUILD="${{ matrix.build }}" USE_MARCH_NATIVE=0# Use the minimum macOS deployment target supported by our version of PyBind:MACOSX_DEPLOYMENT_TARGET: "10.14"+ - name: Set up the wheel's Python version+ if: contains(matrix.build, 'manylinux_x86_64')+ uses: actions/setup-python@v5+ with:+ python-version: ${{ matrix.python }}+ allow-prereleases: true+ - name: Install the x86_64 CPU emulator+ if: contains(matrix.build, 'manylinux_x86_64')+ run: |+ sudo apt-get update+ sudo apt-get install --yes qemu-user+ - name: Test wheel on supported x86_64 CPUs+ if: contains(matrix.build, 'manylinux_x86_64')+ run: |+ python -m pip install ./wheelhouse/*.whl+ python scripts/test_linux_wheel_cpu_compatibility.py- name: Ensure wheels have required filesif: runner.os != 'Windows'run: |diff --git a/CMakeLists.txt b/CMakeLists.txtindex 89de0fa..d3150d5 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -204,13 +204,12 @@ elseif(UNIX AND NOT APPLE)if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|aarch64")add_compile_definitions(HAVE_NEON=1)else()- # Use -march=native for local builds to optimize for the current CPU,- # but use a portable baseline for CI builds to avoid "Illegal instruction" errors- # when ccache restores objects built on different runner hardware.- if(DEFINED ENV{USE_PORTABLE_SIMD})- add_compile_options(-mavx)- else()+ # Keep distributable builds compatible with AVX-capable x86_64 CPUs.+ # Local builds may explicitly target the build machine for extra optimization.+ if("$ENV{USE_MARCH_NATIVE}" STREQUAL "1")add_compile_options(-march=native)+ else()+ add_compile_options(-mavx)endif()add_compile_definitions(HAVE_AVX)endif()diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 41891ce..a9324ae 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -31,6 +31,9 @@ python3 setup.py build developThen, you can `import pedalboard` from Python (or run the tests with `tox`) to test out your local changes.+Linux x86_64 builds target the portable AVX baseline by default. To optimize a local+build for the current machine instead, set `USE_MARCH_NATIVE=1` while building.+> If you're on macOS or Linux, you can try to compile a debug build _faster_ by using [Ccache](https://ccache.dev/):> ## macOS> ```shelldiff --git a/README.md b/README.mdindex a449423..4206a88 100644--- a/README.md+++ b/README.md@@ -62,6 +62,7 @@ If you are new to Python, follow [INSTALLATION.md](https://github.com/spotify/pe- Tested heavily in production use cases at Spotify- Tested automatically on GitHub with VSTs- Platform `manylinux` and `musllinux` wheels built for `x86_64` (Intel/AMD) and `aarch64` (ARM/Apple Silicon)+ - `x86_64` wheels require AVX support- Most Linux VSTs require a relatively modern Linux installation (with glibc > 2.27)- macOS- Tested manually with VSTs and Audio Unitsdiff --git a/scripts/test_linux_wheel_cpu_compatibility.py b/scripts/test_linux_wheel_cpu_compatibility.pynew file mode 100644index 0000000..3c572cc--- /dev/null+++ b/scripts/test_linux_wheel_cpu_compatibility.py@@ -0,0 +1,54 @@+#!/usr/bin/env python3+"""Smoke-test an installed Linux wheel on supported x86_64 CPUs."""++import argparse+import subprocess+import sys+import tempfile+++SUPPORTED_CPU_MODELS = ("IvyBridge", "EPYC-Milan")+SMOKE_TEST_TIMEOUT_SECONDS = 60+++def smoke_test_wheel(cpu_model: str, python: str = sys.executable) -> None:+ print(f"Testing installed wheel on {cpu_model}...")+ with tempfile.TemporaryDirectory() as working_directory:+ result = subprocess.run(+ [+ "qemu-x86_64",+ "-cpu",+ cpu_model,+ python,+ "-c",+ (+ "import numpy as np; "+ "from pedalboard import Gain; "+ "output = Gain(gain_db=-6)(np.zeros((1, 1024), dtype=np.float32), 48000); "+ "assert output.shape == (1, 1024)"+ ),+ ],+ capture_output=True,+ cwd=working_directory,+ text=True,+ timeout=SMOKE_TEST_TIMEOUT_SECONDS,+ )+ if result.returncode != 0:+ raise RuntimeError(+ f"pedalboard wheel failed its smoke test on {cpu_model} "+ f"(exit code {result.returncode})\n{result.stdout}{result.stderr}"+ )+ print(f"Installed wheel passed on {cpu_model}.")+++def main() -> None:+ parser = argparse.ArgumentParser()+ parser.add_argument("cpu_models", nargs="*", default=SUPPORTED_CPU_MODELS)+ args = parser.parse_args()++ for cpu_model in args.cpu_models:+ smoke_test_wheel(cpu_model)+++if __name__ == "__main__":+ main()diff --git a/setup.py b/setup.pyindex 3998129..7e735d4 100644--- a/setup.py+++ b/setup.py@@ -175,13 +175,12 @@ elif platform.system() == "Linux":else:# And on x86, ignore the ARM-specific SIMD code (and KCVI; not GCC or Clang compatible).fftw_paths = ignore_files_matching(fftw_paths, "neon")- # Use -march=native for local builds to optimize for the current CPU,- # but use a portable baseline for CI builds to avoid "Illegal instruction" errors- # when ccache restores objects built on different runner hardware.- if os.getenv("USE_PORTABLE_SIMD"):- ALL_CFLAGS.append("-mavx")- else:+ # Keep distributable builds compatible with AVX-capable x86_64 CPUs.+ # Local builds may explicitly target the build machine for extra optimization.+ if os.getenv("USE_MARCH_NATIVE") == "1":ALL_CFLAGS.append("-march=native")+ else:+ ALL_CFLAGS.append("-mavx")# Enable SIMD instructions:ALL_CFLAGS.extend([