From e7e56d9941a9a30c65f15ea82da145ad05fc8a4d Mon Sep 17 00:00:00 2001 From: vchamarthi Date: Fri, 5 Jun 2026 10:41:30 -0500 Subject: [PATCH 1/5] Add requirements.txt, add missing patch, and minor updates --- benchmarks/asv.conf.json | 2 +- benchmarks/benchmarks/__init__.py | 5 +++ benchmarks/benchmarks/_patch_setup.py | 65 +++++++++++++++++++++++++++ benchmarks/requirements.txt | 2 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 benchmarks/benchmarks/_patch_setup.py create mode 100644 benchmarks/requirements.txt diff --git a/benchmarks/asv.conf.json b/benchmarks/asv.conf.json index 1e26be0d..19c9771c 100644 --- a/benchmarks/asv.conf.json +++ b/benchmarks/asv.conf.json @@ -19,6 +19,6 @@ "build_cache_size": 2, "default_benchmark_timeout": 500, "regressions_thresholds": { - ".*": 0.3 + ".*": 0.2 } } diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index 9b89c10b..556c4d9c 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -4,6 +4,8 @@ import psutil +from ._patch_setup import _apply_patches + _MIN_THREADS = 4 # minimum physical cores required for multi-threaded mode @@ -19,3 +21,6 @@ def _thread_count(): _THREADS = os.environ.get("MKL_NUM_THREADS", _thread_count()) os.environ["MKL_NUM_THREADS"] = _THREADS + +_apply_patches() +del _apply_patches diff --git a/benchmarks/benchmarks/_patch_setup.py b/benchmarks/benchmarks/_patch_setup.py new file mode 100644 index 00000000..cc3dfe42 --- /dev/null +++ b/benchmarks/benchmarks/_patch_setup.py @@ -0,0 +1,65 @@ +"""MKL patch setup — executed once per ASV worker process at import time. + +Patches NumPy FFT with the Intel MKL FFT implementation. +Hard-fails with a descriptive RuntimeError if mkl_fft is missing or the +patch does not take effect, so benchmarks never silently run on stock NumPy. +""" + +_PATCH_MAP = [ + ("mkl_fft", "patch_numpy_fft"), +] + + +def _apply_patches(): + import numpy as np + + patched = {} + + for mod_name, patch_fn_name in _PATCH_MAP: + try: + mod = __import__(mod_name) + except ImportError as exc: + raise RuntimeError( + f"[mkl-patch] Cannot import {mod_name}: {exc}\n" + f" Ensure the conda env contains {mod_name} " + f"from the Intel channel.\n" + " Required channels: " + "https://software.repos.intel.com/python/conda" + ) from exc + + patch_fn = getattr(mod, patch_fn_name, None) + if patch_fn is None: + raise RuntimeError( + f"[mkl-patch] {mod_name} has no {patch_fn_name}(). " + f"Upgrade {mod_name} to a version that exposes " + "the stock-numpy patch API." + ) + + try: + patch_fn() + except Exception as exc: + raise RuntimeError( + f"[mkl-patch] {mod_name}.{patch_fn_name}() raised: {exc!r}" + ) from exc + + is_patched_fn = getattr(mod, "is_patched", None) + if callable(is_patched_fn) and not is_patched_fn(): + raise RuntimeError( + f"[mkl-patch] {mod_name}.is_patched() returned False " + "after patching. NumPy may have been imported before " + "patching in a conflicting state." + ) + + patched[mod_name] = mod + + _attr_checks = { + "mkl_fft": lambda: np.fft.fft.__module__, + } + for mod_name in patched: + try: + attr = _attr_checks[mod_name]() + except Exception: + attr = "unknown" + print(f"[mkl-patch] {mod_name}: numpy.fft dispatch -> {attr}") + + print("[mkl-patch] ALL OK -- mkl_fft active") diff --git a/benchmarks/requirements.txt b/benchmarks/requirements.txt new file mode 100644 index 00000000..04566f48 --- /dev/null +++ b/benchmarks/requirements.txt @@ -0,0 +1,2 @@ +psutil +scipy From 34faf270c028fb8830d21ef032289177f53fe225 Mon Sep 17 00:00:00 2001 From: vchamarthi Date: Fri, 5 Jun 2026 11:10:48 -0500 Subject: [PATCH 2/5] Minor copilot fixes --- benchmarks/benchmarks/_patch_setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmarks/benchmarks/_patch_setup.py b/benchmarks/benchmarks/_patch_setup.py index cc3dfe42..54f02ca2 100644 --- a/benchmarks/benchmarks/_patch_setup.py +++ b/benchmarks/benchmarks/_patch_setup.py @@ -11,13 +11,15 @@ def _apply_patches(): + import importlib + import numpy as np patched = {} for mod_name, patch_fn_name in _PATCH_MAP: try: - mod = __import__(mod_name) + mod = importlib.import_module(mod_name) except ImportError as exc: raise RuntimeError( f"[mkl-patch] Cannot import {mod_name}: {exc}\n" From 99a265bd6190dd4df3803b8742814c5f38a9f3c3 Mon Sep 17 00:00:00 2001 From: vchamarthi Date: Wed, 10 Jun 2026 10:10:05 -0500 Subject: [PATCH 3/5] PP suggestions --- benchmarks/benchmarks/_patch_setup.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/benchmarks/benchmarks/_patch_setup.py b/benchmarks/benchmarks/_patch_setup.py index 54f02ca2..aaee2b26 100644 --- a/benchmarks/benchmarks/_patch_setup.py +++ b/benchmarks/benchmarks/_patch_setup.py @@ -5,9 +5,9 @@ patch does not take effect, so benchmarks never silently run on stock NumPy. """ -_PATCH_MAP = [ - ("mkl_fft", "patch_numpy_fft"), -] +PATCH_MAP = { + "mkl_fft": "patch_numpy_fft", +} def _apply_patches(): @@ -17,12 +17,12 @@ def _apply_patches(): patched = {} - for mod_name, patch_fn_name in _PATCH_MAP: + for mod_name, patch_fn_name in PATCH_MAP.items(): try: mod = importlib.import_module(mod_name) except ImportError as exc: raise RuntimeError( - f"[mkl-patch] Cannot import {mod_name}: {exc}\n" + f"Cannot import {mod_name}: {exc}\n" f" Ensure the conda env contains {mod_name} " f"from the Intel channel.\n" " Required channels: " @@ -32,7 +32,7 @@ def _apply_patches(): patch_fn = getattr(mod, patch_fn_name, None) if patch_fn is None: raise RuntimeError( - f"[mkl-patch] {mod_name} has no {patch_fn_name}(). " + f"{mod_name} has no {patch_fn_name}(). " f"Upgrade {mod_name} to a version that exposes " "the stock-numpy patch API." ) @@ -41,13 +41,13 @@ def _apply_patches(): patch_fn() except Exception as exc: raise RuntimeError( - f"[mkl-patch] {mod_name}.{patch_fn_name}() raised: {exc!r}" + f"{mod_name}.{patch_fn_name}() raised: {exc!r}" ) from exc is_patched_fn = getattr(mod, "is_patched", None) if callable(is_patched_fn) and not is_patched_fn(): raise RuntimeError( - f"[mkl-patch] {mod_name}.is_patched() returned False " + f"{mod_name}.is_patched() returned False " "after patching. NumPy may have been imported before " "patching in a conflicting state." ) @@ -59,9 +59,6 @@ def _apply_patches(): } for mod_name in patched: try: - attr = _attr_checks[mod_name]() + _attr_checks[mod_name]() except Exception: - attr = "unknown" - print(f"[mkl-patch] {mod_name}: numpy.fft dispatch -> {attr}") - - print("[mkl-patch] ALL OK -- mkl_fft active") + pass From 754fd540bb5a366cab296c87f261864f6ea5410b Mon Sep 17 00:00:00 2001 From: vchamarthi Date: Mon, 15 Jun 2026 12:30:34 -0500 Subject: [PATCH 4/5] PR suggestions --- benchmarks/README.md | 2 +- benchmarks/benchmarks/__init__.py | 5 --- benchmarks/benchmarks/_patch_setup.py | 64 --------------------------- pyproject.toml | 2 +- 4 files changed, 2 insertions(+), 71 deletions(-) delete mode 100644 benchmarks/benchmarks/_patch_setup.py diff --git a/benchmarks/README.md b/benchmarks/README.md index 6370fdb8..b4ec349c 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -44,7 +44,7 @@ transforms, but the explicit warmup makes the intent visible. Prerequisites: ```bash -pip install asv psutil +pip install -e ".[benchmark]" ``` Run benchmarks against the current environment: diff --git a/benchmarks/benchmarks/__init__.py b/benchmarks/benchmarks/__init__.py index 556c4d9c..9b89c10b 100644 --- a/benchmarks/benchmarks/__init__.py +++ b/benchmarks/benchmarks/__init__.py @@ -4,8 +4,6 @@ import psutil -from ._patch_setup import _apply_patches - _MIN_THREADS = 4 # minimum physical cores required for multi-threaded mode @@ -21,6 +19,3 @@ def _thread_count(): _THREADS = os.environ.get("MKL_NUM_THREADS", _thread_count()) os.environ["MKL_NUM_THREADS"] = _THREADS - -_apply_patches() -del _apply_patches diff --git a/benchmarks/benchmarks/_patch_setup.py b/benchmarks/benchmarks/_patch_setup.py deleted file mode 100644 index aaee2b26..00000000 --- a/benchmarks/benchmarks/_patch_setup.py +++ /dev/null @@ -1,64 +0,0 @@ -"""MKL patch setup — executed once per ASV worker process at import time. - -Patches NumPy FFT with the Intel MKL FFT implementation. -Hard-fails with a descriptive RuntimeError if mkl_fft is missing or the -patch does not take effect, so benchmarks never silently run on stock NumPy. -""" - -PATCH_MAP = { - "mkl_fft": "patch_numpy_fft", -} - - -def _apply_patches(): - import importlib - - import numpy as np - - patched = {} - - for mod_name, patch_fn_name in PATCH_MAP.items(): - try: - mod = importlib.import_module(mod_name) - except ImportError as exc: - raise RuntimeError( - f"Cannot import {mod_name}: {exc}\n" - f" Ensure the conda env contains {mod_name} " - f"from the Intel channel.\n" - " Required channels: " - "https://software.repos.intel.com/python/conda" - ) from exc - - patch_fn = getattr(mod, patch_fn_name, None) - if patch_fn is None: - raise RuntimeError( - f"{mod_name} has no {patch_fn_name}(). " - f"Upgrade {mod_name} to a version that exposes " - "the stock-numpy patch API." - ) - - try: - patch_fn() - except Exception as exc: - raise RuntimeError( - f"{mod_name}.{patch_fn_name}() raised: {exc!r}" - ) from exc - - is_patched_fn = getattr(mod, "is_patched", None) - if callable(is_patched_fn) and not is_patched_fn(): - raise RuntimeError( - f"{mod_name}.is_patched() returned False " - "after patching. NumPy may have been imported before " - "patching in a conflicting state." - ) - - patched[mod_name] = mod - - _attr_checks = { - "mkl_fft": lambda: np.fft.fft.__module__, - } - for mod_name in patched: - try: - _attr_checks[mod_name]() - except Exception: - pass diff --git a/pyproject.toml b/pyproject.toml index ab9b8dc1..6bcf72ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ readme = {file = "README.md", content-type = "text/markdown"} requires-python = ">=3.10,<3.15" [project.optional-dependencies] -benchmark = ["asv>=0.6", "psutil"] +benchmark = ["asv>=0.6", "psutil", "scipy>=1.10"] scipy_interface = ["scipy>=1.10", "mkl-service"] test = ["pytest", "scipy>=1.10", "mkl-service"] From c5811f610caea2ed1b5ce6f184181193c7b26dd6 Mon Sep 17 00:00:00 2001 From: vchamarthi Date: Mon, 15 Jun 2026 13:40:20 -0500 Subject: [PATCH 5/5] fix PR comments --- benchmarks/README.md | 2 +- benchmarks/requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/README.md b/benchmarks/README.md index b4ec349c..ab878247 100644 --- a/benchmarks/README.md +++ b/benchmarks/README.md @@ -44,7 +44,7 @@ transforms, but the explicit warmup makes the intent visible. Prerequisites: ```bash -pip install -e ".[benchmark]" +pip install ".[benchmark]" ``` Run benchmarks against the current environment: diff --git a/benchmarks/requirements.txt b/benchmarks/requirements.txt index 04566f48..020002ad 100644 --- a/benchmarks/requirements.txt +++ b/benchmarks/requirements.txt @@ -1,2 +1,2 @@ psutil -scipy +scipy>=1.10