From 37c3d6816382ad017c922f672088efa0566bd6b6 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 27 Oct 2026 16:33:56 +0100 Subject: [PATCH] Run ssort, ruff and numpydoc as pre-commit hooks --- .pre-commit-config.yaml | 19 + src/cunumpy/_algorithms.py | 30 +- src/cunumpy/_cuda_kernel.py | 634 ++++++++++++++++----------------- src/cunumpy/_dispatch.py | 98 ++--- src/cunumpy/_fake_cupy.py | 20 +- src/cunumpy/_fake_cupy_impl.py | 146 ++++---- src/cunumpy/_fake_cupyx.py | 72 ++-- src/cunumpy/_kernel.py | 116 +++--- src/cunumpy/_metal_kernel.py | 24 +- src/cunumpy/_mirror.py | 48 +-- src/cunumpy/_pyccel_cache.py | 6 +- src/cunumpy/_random_streams.py | 16 +- src/cunumpy/_scipy_backend.py | 50 +-- src/cunumpy/_staging.py | 12 +- src/cunumpy/_streams.py | 12 +- src/cunumpy/_transfers.py | 33 +- src/cunumpy/kernel_testing.py | 20 +- src/cunumpy/xp.py | 112 +++--- testing/smoke_wheel.py | 1 + tests/unit/test_cuda_kernel.py | 88 ++--- tests/unit/test_mpi_serial.py | 6 +- tests/unit/test_staging.py | 12 +- tests/unit/test_transfers.py | 14 +- 23 files changed, 806 insertions(+), 783 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index af67085..6125aeb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,3 +13,22 @@ repos: rev: 0.8.1 hooks: - id: nbstripout # remove jupyter notebook cell output + + - repo: https://github.com/bwhmather/ssort + rev: v0.12.0 + hooks: + - id: ssort # sort statements so definitions come before their use + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.17.3 + hooks: + - id: ruff-check # lint, including import sorting + args: ["--fix"] + - id: ruff-format + + - repo: https://github.com/numpy/numpydoc + rev: v1.11.0 + hooks: + - id: numpydoc-validation # docstring sections, configured in pyproject.toml + files: ^src/cunumpy/ + exclude: _fake_cupy diff --git a/src/cunumpy/_algorithms.py b/src/cunumpy/_algorithms.py index 76355df..15876e7 100644 --- a/src/cunumpy/_algorithms.py +++ b/src/cunumpy/_algorithms.py @@ -374,6 +374,21 @@ def _radix_argsort(keys: np.ndarray) -> np.ndarray: return order.astype(np.int64, copy=False) +def _take_along_axis(xpm: Any, array: Any, order: Any, axis: int) -> Any: + """Return `array` taken at `order` along `axis`, as a new C-contiguous array. + + A component-major ``(ncomp, n)`` NumPy array is gathered one contiguous row at a + time, which is about twice as fast as ``take`` along its last axis; everything + else, and CuPy, goes through ``take``. + """ + if xpm is np and array.ndim == 2 and axis == 1: + gathered = np.empty((array.shape[0], order.shape[0]), dtype=array.dtype) + for row in range(array.shape[0]): + gathered[row] = array[row][order] + return gathered + return xpm.take(array, order, axis=axis) + + @sp.profile("cunumpy.sort_by_key", tags=_PROFILE_TAGS) def sort_by_key(keys: Any, *arrays: Any, axis: int = 0) -> tuple[Any, ...]: """Sort `keys` and reorder every array the same way, in one stable argsort. @@ -449,21 +464,6 @@ def sort_by_key(keys: Any, *arrays: Any, axis: int = 0) -> tuple[Any, ...]: ) -def _take_along_axis(xpm: Any, array: Any, order: Any, axis: int) -> Any: - """Return `array` taken at `order` along `axis`, as a new C-contiguous array. - - A component-major ``(ncomp, n)`` NumPy array is gathered one contiguous row at a - time, which is about twice as fast as ``take`` along its last axis; everything - else, and CuPy, goes through ``take``. - """ - if xpm is np and array.ndim == 2 and axis == 1: - gathered = np.empty((array.shape[0], order.shape[0]), dtype=array.dtype) - for row in range(array.shape[0]): - gathered[row] = array[row][order] - return gathered - return xpm.take(array, order, axis=axis) - - @sp.profile("cunumpy.compact_by_mask", tags=_PROFILE_TAGS) def compact_by_mask(mask: Any, *arrays: Any, axis: int = 0) -> int: """Move the entries where `mask` is True to the front of every array, in place. diff --git a/src/cunumpy/_cuda_kernel.py b/src/cunumpy/_cuda_kernel.py index 3e72f02..e6ae384 100644 --- a/src/cunumpy/_cuda_kernel.py +++ b/src/cunumpy/_cuda_kernel.py @@ -584,98 +584,6 @@ def _template_arg(value: Any) -> str: return ctype_of(value) -def parse_cuda_signature( - source: str, - name: str, - *, - structs: Iterable[CudaStruct] = (), - template_args: Sequence[Any] | None = None, -) -> tuple[CudaParameter, ...]: - """Parse the parameters of a ``__global__`` function. - - C types map to dtypes as on Linux (LP64): ``int`` is int32, ``long`` and - ``long long`` are int64. - - Parameters - ---------- - source : str - CUDA C source code. - name : str - Name of the ``__global__`` function. - structs : iterable of CudaStruct, optional - Struct types that may appear as parameters (by value). A definition - of the struct in `source` must match. - template_args : sequence, optional - Template arguments if `name` is a function template: C types or - dtypes (see :func:`~cunumpy.cuda.ctype_of`) for type parameters, - ints or bools for non-type parameters. - - Returns - ------- - tuple of CudaParameter - The parameters, in order. - - Raises - ------ - ValueError - If there is no such function, `template_args` do not fit, a struct - definition does not match its :class:`~cunumpy.arguments.CudaStruct`, - or a parameter type cannot be checked (e.g. a macro or ``double**``). - - Examples - -------- - >>> source = ''' - ... template - ... __global__ void scale(T* x, T factor, int n) {}''' - >>> [(p.name, p.ctype, p.pointer) for p in xp.cuda.parse_cuda_signature( - ... source, "scale", template_args=[np.float32])] - [('x', 'float', True), ('factor', 'float', False), ('n', 'int', False)] - """ - code = _strip_comments(source) - structs = {s.name: s for s in structs} - for struct in structs.values(): - struct.check_source(code) - - pattern = r"(?:template\s*<(?P