π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Test the Linux wheel AVX boundary
Christopher Jacobye2f802641790
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex a9324ae..974cd89 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -33,6 +33,8 @@ Then, you can `import pedalboard` from Python (or run the tests with `tox`) to tLinux x86_64 builds target the portable AVX baseline by default. To optimize a localbuild for the current machine instead, set `USE_MARCH_NATIVE=1` while building.+The previous `USE_PORTABLE_SIMD` variable is no longer used; builds that set it remain+portable because AVX is now the default.> If you're on macOS or Linux, you can try to compile a debug build _faster_ by using [Ccache](https://ccache.dev/):> ## macOSdiff --git a/scripts/test_linux_wheel_cpu_compatibility.py b/scripts/test_linux_wheel_cpu_compatibility.pyindex 3c572cc..52c8893 100644--- a/scripts/test_linux_wheel_cpu_compatibility.py+++ b/scripts/test_linux_wheel_cpu_compatibility.py@@ -1,49 +1,80 @@#!/usr/bin/env python3-"""Smoke-test an installed Linux wheel on supported x86_64 CPUs."""+"""Smoke-test an installed Linux wheel across its x86_64 CPU compatibility boundary."""import argparse+import signalimport subprocessimport sysimport tempfileSUPPORTED_CPU_MODELS = ("IvyBridge", "EPYC-Milan")+UNSUPPORTED_CPU_MODELS = ("Nehalem",)+DEFAULT_CPU_MODELS = SUPPORTED_CPU_MODELS + UNSUPPORTED_CPU_MODELSSMOKE_TEST_TIMEOUT_SECONDS = 60+RUNTIME_PROBE = "print('Python runtime started successfully')"+WHEEL_SMOKE_TEST = (+ "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)"+)-def smoke_test_wheel(cpu_model: str, python: str = sys.executable) -> None:- print(f"Testing installed wheel on {cpu_model}...")+def run_on_cpu(+ cpu_model: str, python_code: str, python: str = sys.executable+) -> subprocess.CompletedProcess[str]:with tempfile.TemporaryDirectory() as working_directory:- result = subprocess.run(+ return 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)"- ),+ python_code,],capture_output=True,cwd=working_directory,text=True,timeout=SMOKE_TEST_TIMEOUT_SECONDS,)+++def format_failure(result: subprocess.CompletedProcess[str]) -> str:+ return f"exit code {result.returncode}\n{result.stdout}{result.stderr}"+++def smoke_test_wheel(cpu_model: str, python: str = sys.executable) -> None:+ print(f"Testing installed wheel on {cpu_model}...")+ if cpu_model in UNSUPPORTED_CPU_MODELS:+ runtime_probe = run_on_cpu(cpu_model, RUNTIME_PROBE, python)+ if runtime_probe.returncode != 0:+ raise RuntimeError(+ f"Python runtime failed to start on {cpu_model} ({format_failure(runtime_probe)})"+ )++ result = run_on_cpu(cpu_model, WHEEL_SMOKE_TEST, python)+ if cpu_model in UNSUPPORTED_CPU_MODELS:+ expected_returncode = -signal.SIGILL+ if result.returncode != expected_returncode:+ raise RuntimeError(+ f"pedalboard wheel unexpectedly ran on non-AVX CPU {cpu_model}; "+ f"expected SIGILL ({expected_returncode}), got {format_failure(result)}"+ )+ print(f"Installed wheel correctly requires AVX on {cpu_model}.")+ return+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}"+ f"pedalboard wheel failed its smoke test on {cpu_model} ({format_failure(result)})")print(f"Installed wheel passed on {cpu_model}.")def main() -> None:parser = argparse.ArgumentParser()- parser.add_argument("cpu_models", nargs="*", default=SUPPORTED_CPU_MODELS)+ parser.add_argument("cpu_models", nargs="*", default=DEFAULT_CPU_MODELS)args = parser.parse_args()for cpu_model in args.cpu_models:diff --git a/tests/test_linux_wheel_cpu_compatibility.py b/tests/test_linux_wheel_cpu_compatibility.pynew file mode 100644index 0000000..3b29e19--- /dev/null+++ b/tests/test_linux_wheel_cpu_compatibility.py@@ -0,0 +1,93 @@+import os+from pathlib import Path+import subprocess+import sys++import pytest+++pytestmark = pytest.mark.skipif(+ sys.platform == "win32", reason="The wheel compatibility harness runs only on Linux"+)+++HARNESS = Path(__file__).parents[1] / "scripts" / "test_linux_wheel_cpu_compatibility.py"+NON_AVX_CPU = "Nehalem"+++def run_harness(+ tmp_path: Path,+ emulator_body: str,+ cpu_models: tuple[str, ...] = (NON_AVX_CPU,),+) -> tuple[subprocess.CompletedProcess, list[str]]:+ invocation_log = tmp_path / "qemu-invocations"+ emulator = tmp_path / "qemu-x86_64"+ emulator.write_text(+ "#!/bin/sh\n"+ 'cpu_model="$2"\n'+ 'for argument do code="$argument"; done\n'+ f'printf "%s|%s\\n" "$cpu_model" "$code" >> "{invocation_log}"\n'+ f"{emulator_body}\n"+ )+ emulator.chmod(0o755)++ env = os.environ.copy()+ env["PATH"] = f"{tmp_path}{os.pathsep}{env['PATH']}"+ result = subprocess.run(+ [sys.executable, HARNESS, *cpu_models],+ capture_output=True,+ env=env,+ text=True,+ )+ invocations = invocation_log.read_text().splitlines()+ return result, invocations+++def test_non_avx_cpu_requires_wheel_import_to_terminate_with_sigill(tmp_path: Path) -> None:+ result, invocations = run_harness(+ tmp_path,+ 'case "$code" in *pedalboard*) kill -ILL $$ ;; *) exit 0 ;; esac',+ )++ assert result.returncode == 0, result.stderr+ assert len(invocations) == 2+ assert "pedalboard" not in invocations[0]+ assert "pedalboard" in invocations[1]+++def test_default_cpu_models_include_the_non_avx_boundary_check(tmp_path: Path) -> None:+ result, invocations = run_harness(+ tmp_path,+ 'case "$cpu_model:$code" in Nehalem:*pedalboard*) kill -ILL $$ ;; *) exit 0 ;; esac',+ cpu_models=(),+ )++ assert result.returncode == 0, result.stderr+ assert any(invocation.startswith("IvyBridge|") for invocation in invocations)+ assert any(invocation.startswith("EPYC-Milan|") for invocation in invocations)+ non_avx_invocations = [+ invocation for invocation in invocations if invocation.startswith(f"{NON_AVX_CPU}|")+ ]+ assert len(non_avx_invocations) == 2+ assert "pedalboard" not in non_avx_invocations[0]+ assert "pedalboard" in non_avx_invocations[1]+++def test_non_avx_cpu_rejects_a_broken_emulator_or_python_runtime(tmp_path: Path) -> None:+ result, invocations = run_harness(tmp_path, "kill -ILL $$")++ assert result.returncode != 0+ assert len(invocations) == 1+ assert "pedalboard" not in invocations[0]+++def test_non_avx_cpu_rejects_a_non_sigill_wheel_failure(tmp_path: Path) -> None:+ result, invocations = run_harness(+ tmp_path,+ 'case "$code" in *pedalboard*) exit 1 ;; *) exit 0 ;; esac',+ )++ assert result.returncode != 0+ assert len(invocations) == 2+ assert "pedalboard" not in invocations[0]+ assert "pedalboard" in invocations[1]