PR: Implement support for *Python Array API Standard*.#1406
Open
KelSolaar wants to merge 1 commit into
Open
Conversation
68f59e9 to
bc223a6
Compare
bc223a6 to
3163508
Compare
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements support for the Python Array API Standard, enabling computations to dispatch onto alternative array backends:
Dispatch is currently opt-in and NumPy-only behaviour is unchanged by default. Once enabled, the backend is selected from the type of the input array. It can be enabled three ways:
1. Environment variable, set before importing Colour:
2. Programmatically, toggle the global state at runtime:
3. Scoped context manager (also usable as a decorator), enable for a block only:
What's added
xp_*operation surface incolour.utilities:array_namespace,is_numpy_namespace,is_non_ndarray,trace_array_namespaceas_ndarray,cast_non_ndarray,xp_as_array/xp_as_float_array/xp_as_int_array,xp_astype,xp_ascontiguousarrayxp_reshape,xp_squeeze,xp_atleast_1d/xp_atleast_2d,xp_broadcast_to,xp_matrix_transpose,xp_resize,xp_pad,xp_insertxp_average,xp_median,xp_nanmean,xp_trapezoid,xp_gradientxp_degrees/xp_radians,xp_sinc,xp_round,xp_nan_to_numxp_lstsq,xp_eig/xp_eigh,xp_create_diagonalxp_linspace,xp_interp,xp_select,xp_isin,xp_setxor1d,xp_uniquexp_isclose,xp_assert_close,xp_assert_equalcontextvars-backed global state (Array API enablement, domain-range scale,ndarraycopy, caching) for thread/async safety.colour.temperature.common), Jakob and Hanika (2019) trilinear interpolation, etc.COLOUR_SCIENCE__DEFAULT_COMPLEX_DTYPE/set_default_complex_dtype.CIE_illuminant_D_series,msds_CIE_illuminant_D_series,msds_blackbody,msds_rayleigh_jeans.xppytest fixture parametrising numpy/jax/torch/torch-mps, withmps_tolerance_absoluteandmps_xfailmarkers for float32 precision, plus a cross-backend benchmark suite (utilities/benchmark.py).advanced.rst.Performance
Per-suite speed-up vs NumPy (best-of-3, HD inputs): speed-up = NumPy ÷ backend over cases succeeding on both, so higher = faster (e.g.
3.0×= 3× faster than NumPy;< 1.0×= slower).numpy (ms)is the summed best-of-3 over the suite's cases.NumPy is the baseline (1.00×). Measured on an Apple M1 Max (10-core, 32 GB), macOS 15.7, Python 3.13, NumPy 2.3, PyTorch 2.9, JAX 0.8; 409 cases across 22 suites.
Preflight
Code Style and Quality
colour,colour.models.Documentation