Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The `npbench/` suite uses kernels from [npbench](https://github.com/spcl/npbench

| File | Ufuncs | Dtypes | Sizes/Presets |
|------|--------|--------|---------------|
| `micro/bench_micro.py` | 25 unary (`exp`, `log`, `sin`, `cos`, `sqrt`, `cbrt`, etc.) + `arctan2`, `power` | float32, float64 | 10k, 100k, 1M |
| `micro/bench_micro.py` | 25 unary (`exp`, `log`, `sin`, `cos`, `sqrt`, `cbrt`, etc.) + `arctan2`, `power`, `add`, `subtract` | float32, float64 (`add`, `subtract` also complex64, complex128) | 10k, 100k, 1M |
| `micro/bench_inplace.py` | `add`, `subtract` in place: `out=a`, `out=b`, `z += z` | complex64, complex128 | 10k, 100k |
| `npbench/bench_softmax.py` | `exp`, `sum` (`max` runs on stock NumPy) | float32 | M (32x8x256x256), L (64x16x448x448) |
| `npbench/bench_arc_distance.py` | `sin`, `cos`, `arctan2`, `sqrt` | float64 | M (1M), L (10M) |
| `npbench/bench_go_fast.py` | `tanh` | float64 | M (6k x 6k), L (20k x 20k) |
Expand Down
54 changes: 54 additions & 0 deletions benchmarks/benchmarks/micro/bench_inplace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Micro-benchmarks for in-place complex add and subtract.

Up to VML_ASM_THRESHOLD (100000 elements) mkl_umath runs its own C loops
instead of oneMKL VM, with separate loops for an output that is one of the
inputs: out=a, out=b and z += z (both inputs and the output are one array).

Arrays are pre-allocated in setup() and reused across timing calls.
Patching is applied once at package import via benchmarks._patch_setup.
"""

import numpy as np

from .bench_micro import _uniform


class _Inplace:
params = (
["complex64", "complex128"],
[10_000, 100_000],
["out=a", "out=b", "z += z"],
)
param_names = ["dtype", "size", "mode"]
ufunc = ""

def setup(self, dtype, size, mode):
self._func = getattr(np, self.ufunc)
rng = np.random.default_rng(42)
a = _uniform(rng, size, dtype)
b = _uniform(rng, size, dtype)
if mode == "out=a":
self.args, self.out = (a, b), a
elif mode == "out=b":
self.args, self.out = (a, b), b
else:
# repeated z += z doubles z and overflows to inf; zeros keep it
# finite, and add/subtract run equally fast on any finite value
z = np.zeros(size, dtype=dtype)
self.args, self.out = (z, z), z
self._func(*self.args, out=self.out)

def time_inplace(self, dtype, size, mode):
self._func(*self.args, out=self.out)


class BenchAddInplace(_Inplace):
"""In-place add"""

ufunc = "add"


class BenchSubtractInplace(_Inplace):
"""In-place subtract"""

ufunc = "subtract"
47 changes: 47 additions & 0 deletions benchmarks/benchmarks/micro/bench_micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,50 @@ def setup(self, dtype, size):

def time_power(self, dtype, size):
np.power(self.base, self.exp)


def _uniform(rng, size, dtype):
"""Uniform [-1, 1) values; complex parts drawn independently"""
dtype = np.dtype(dtype)
if dtype.kind == "c":
parts = rng.uniform(-1.0, 1.0, 2 * size)
return parts.astype(np.finfo(dtype).dtype).view(dtype)
return rng.uniform(-1.0, 1.0, size).astype(dtype)


class BenchAdd:
"""Binary ufunc add (MKL v?Add above 100k elements)"""

params = (
["float32", "float64", "complex64", "complex128"],
[10_000, 100_000, 1_000_000],
)
param_names = ["dtype", "size"]

def setup(self, dtype, size):
rng = np.random.default_rng(42)
self.a = _uniform(rng, size, dtype)
self.b = _uniform(rng, size, dtype)
np.add(self.a, self.b)

def time_add(self, dtype, size):
np.add(self.a, self.b)


class BenchSubtract:
"""Binary ufunc subtract (MKL v?Sub above 100k elements)"""

params = (
["float32", "float64", "complex64", "complex128"],
[10_000, 100_000, 1_000_000],
)
param_names = ["dtype", "size"]

def setup(self, dtype, size):
rng = np.random.default_rng(42)
self.a = _uniform(rng, size, dtype)
self.b = _uniform(rng, size, dtype)
np.subtract(self.a, self.b)

def time_subtract(self, dtype, size):
np.subtract(self.a, self.b)
Loading