Repository navigation
perf: hoist unsupported-dtype blacklist out of the scipy.fft interface hot path - #405
intel-python-devops wants to merge 2 commits into
Conversation
Comparing a dtype against np.half/np.float128/np.complex256 with `in` on a list coerces each scalar type to a dtype on every call. Storing np.dtype objects in a frozenset turns it into one hash lookup, roughly doubling the per-call saving of the previous commit on small transforms.
antonwolfy
left a comment
There was a problem hiding this comment.
The speedup is real, but the frozenset lookup changes which dtypes are rejected (details inline). Switching to a tuple keeps most of the gain and keeps master's behavior.
Performance. I measured on a Xeon Platinum 8480+ (the host CPU of a PVC machine; mkl_fft runs on the CPU). Python 3.13, NumPy 2.5.5, SciPy 1.18.1, MKL 2026.2.1, conda-forge mkl_fft 2.4.0 with only _float_utils.py swapped. Each variant ran in its own process pinned to one core, over 40 rounds in rotated order. A master-vs-master control set the noise floor, under 1% for these cells.
scipy_fft.fft, complex128 |
master | this PR | tuple |
|---|---|---|---|
| n=8 | 1.93 µs | −16.9% | −11.4% |
| n=64 | 1.98 µs | −16.7% | −10.6% |
| n=1024 | 3.75 µs | −8.5% | −6.4% |
via scipy.fft.set_backend, n=8 / 64 / 1024 |
2.25 / 2.30 / 4.17 µs | −16.1 / −14.8 / −8.3% | −10.3 / −8.8 / −5.1% |
| n=65536 and n=2^20 | unchanged | unchanged |
- No per-call slowdown across about 150 cells: fft, ifft, rfft, irfft, fft2, fftn and hfft, four dtypes, 1 and default MKL threads. Only two cells came out above the noise floor, large
rfftwith default threads at +3.7%, and the master-vs-master control moved just as much on those same cells. - The only real cost is one-time: importing
_float_utilstakes about 7.5 µs longer. That is +0.7% ofimport mkl_fft.interfaces.scipy_fft, within noise. - The tuple costs 0.10–0.16 µs per call more than the set and keeps about 60% of the gain.
Tests. The existing suite passes with and without this change, so it does not see the difference. These two tests, placed after test_scipy_no_support_for in mkl_fft/tests/test_interfaces.py, pass on master and with the tuple and fail with this PR:
@pytest.mark.parametrize("dtype", _get_blacklisted_dtypes())
def test_scipy_no_support_for_dtype_with_fields(dtype):
require_scipy_fft()
# (dtype, fields) compares equal to dtype but does not hash like it
dt = np.dtype((dtype, [("a", dtype)]))
x = np.ones(16, dtype=dtype).view(dt)
assert_raises(NotImplementedError, mfi.scipy_fft.ifft, x)
def test_scipy_fft_unhashable_dtype():
require_scipy_fft()
StringDType = getattr(np.dtypes, "StringDType", None)
if StringDType is None:
pytest.skip("requires NumPy >= 2.0")
# hash() of this dtype raises TypeError, the dtype check must not need it
x = np.array(["1", "2"], dtype=StringDType(na_object=[]))
assert_raises(ValueError, mfi.scipy_fft.fft, x)Please also update the description: behavior is not unchanged for (base, fields) dtypes or for dtypes that cannot be hashed.
Not caused by this PR, seen on master while testing: byte-swapped float16 (np.ones(16, '>f2')) is not rejected either, and scipy_fft.fft returns complex128 for it.
| # dtypes the scipy.fft interface rejects; np.dtype keys make `in` a hash lookup | ||
| _UNSUPPORTED_DTYPES = frozenset( |
There was a problem hiding this comment.
in on a frozenset looks the dtype up by hash(), while the old _x.dtype in black_list compared with ==, and NumPy's dtype hash does not always agree with ==. A (base, fields) dtype compares equal to its base but hashes differently: np.dtype((np.float16, [('a', 'f2')])) == np.float16 is True, yet the set does not find it.
So with this PR such inputs are no longer rejected. On master all of these raise NotImplementedError:
- float16 base:
scipy_fft.fft/rfftreturn complex128. - longdouble base:
scipy_fft.rfftsilently returns complex128, losing the long double precision;fftraisesValueError. - clongdouble base:
fftraisesValueError,rfftraisesTypeError.
I got the same result on NumPy 1.26.4, 2.0.2, 2.1.3, 2.2.6, 2.3.5, 2.4.4 and 2.5.5.
A tuple keeps the precomputed constant and the == semantics. Over about 950 dtypes per NumPy version on those 7 versions it behaves exactly like master, and the full test suite passes with it:
| # dtypes the scipy.fft interface rejects; np.dtype keys make `in` a hash lookup | |
| _UNSUPPORTED_DTYPES = frozenset( | |
| # dtypes the scipy.fft interface rejects; a tuple, not a set, so that | |
| # `in` uses dtype ``==`` and needs no hash(dtype) | |
| _UNSUPPORTED_DTYPES = tuple( |
| if hasattr(np, "complex256"): | ||
| black_list.append(np.complex256) | ||
| if _x.dtype in black_list: | ||
| if _x.dtype in _UNSUPPORTED_DTYPES: |
There was a problem hiding this comment.
The set lookup also needs hash(_x.dtype), which raises for some dtypes that master accepted here. For example, np.array(['1', '2'], dtype=np.dtypes.StringDType(na_object=[])) through scipy_fft.fft now fails with TypeError: unhashable type: 'list' instead of master's ValueError. The tuple suggested above fixes this too.
Note
This pull request is entirely AI-generated. Please review thoroughly.
_supported_array_or_not_implemented(called once by everymkl_fft.interfaces.scipy_fftentry point via_validate_input) used to rebuild ablack_listlist and re-run twohasattr(np, ...)probes on every call, and_x.dtype in black_listthen coerced each scalar type to a dtype per comparison. The unsupported dtypes are now computed once at import time as_UNSUPPORTED_DTYPES, a module-levelfrozensetofnp.dtypeobjects, so the check is a single hash lookup.Behavior is unchanged: master and this branch accept and reject the same dtypes, including byte-swapped, metadata-carrying, structured, object and
StringDTypeinputs.Per-call time in µs (median of 12 interleaved rounds, Python 3.12, NumPy 2.5.3, i7-1185G7):
mkl_fft.interfaces.scipy_fft.fftscipy.fft.fftwithset_backendLarge transforms (n=65536) are unchanged within noise.