Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+22 more

    GPU math in Python: CuPy and nvmath-python (CUDA-X)

    1

    Exact signatures, GPU-verified pitfalls and a static checker for CuPy and nvmath-python, so GPU math code is right before it runs.

    Free

    Security scanned
    GPU math in Python: CuPy and nvmath-python (CUDA-X)

    GPU math in Python: CuPy and nvmath-python (CUDA-X)

    Example session with this skill installed

    Port this SciPy code to the GPU with CuPy. It solves a 1-D Laplacian system with conjugate gradient and then finds the 3 smallest eigenvalues.

    import numpy as np
    import scipy.sparse as sp
    import scipy.sparse.linalg as spla
    
    n = 4096
    main = 2.0 * np.ones(n)
    off = -np.ones(n - 1)
    A = sp.diags([off, main, off], [-1, 0, 1], format="csr")
    b = np.ones(n)
    
    x, info = spla.cg(A, b, tol=1e-8)
    w, v = spla.eigsh(A, k=3, sigma=0, which="LM")   # shift-invert: eigenvalues nearest 0
    
    • Read your context and instructions
    • Compiled the gpu math in

    A line-by-line port (numpy → cupy, scipy.sparse → cupyx.scipy.sparse) looks right but fails. The skill's checker finds both problems before the code runs:

    $ python scripts/check_gpu_code.py port.py
    FAIL: 2 problem(s) found
    - line 11 [error: unknown-keyword] `cupyx.scipy.sparse.linalg.cg` has no parameter `tol`. Signature: `cg(A, b, x0=None, *, rtol=1e-05, atol=0.0, maxiter=None, M=None, callback=None)`. Did you mean: `rtol`, `atol`?
    - line 12 [error: unknown-keyword] `cupyx.scipy.sparse.linalg.eigsh` has no parameter `sigma`. Signature: `eigsh(a, k=6, *, which='LM', v0=None, ncv=None, maxiter=None, tol=0, return_eigenvectors=True)`.
    

    The eigsh card explains the second one: CuPy has no shift-invert mode and no which='SM'; it supports only 'LM', 'LA' and 'SA'. Since the Laplacian is positive definite, the smallest eigenvalues are the smallest algebraic ones, so which="SA" gives the same result. The cg card adds that it returns normally when it fails to converge, so info has to be checked.

    import cupy as cp
    import cupyx.scipy.sparse as csp
    import cupyx.scipy.sparse.linalg as cspla
    
    n = 4096
    main = 2.0 * cp.ones(n)
    off = -cp.ones(n - 1)
    A = csp.diags([off, main, off], [-1, 0, 1], format="csr")
    b = cp.ones(n)
    
    x, info = cspla.cg(A, b, rtol=1e-8, maxiter=5 * n)
    if info != 0:
        raise RuntimeError(f"cg did not converge (info={info})")
    residual = float(cp.linalg.norm(A @ x - b) / cp.linalg.norm(b))
    
    # No shift-invert in CuPy; A is positive definite, so the smallest eigenvalues
    # are the smallest algebraic ones.
    w, v = cspla.eigsh(A, k=3, which="SA")
    print(f"relative residual {residual:.1e}; smallest eigenvalues {cp.asnumpy(w)}")
    
    $ python scripts/check_gpu_code.py port.py
    OK: no problems found
    Checked 9 API reference(s): `cupy`, `cupyx.scipy.sparse`, `cupyx.scipy.sparse.linalg`, `cupy.ones`, `cupyx.scipy.sparse.diags`, `cupyx.scipy.sparse.linalg.cg`, `cupy.linalg.norm`, `cupyx.scipy.sparse.linalg.eigsh`, `cupy.asnumpy`
    

    The checker proves the names, arguments and imports are right for CuPy 14.2.0. It doesn't run the code, so the convergence check and the residual are there to confirm the numerics once it runs on a GPU.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    GPU numerical code in Python fails in ways that look fine on screen: a NumPy name that doesn't exist in CuPy, a keyword from an older SciPy, a submodule that import cupy never loaded, an nvmath inverse FFT that comes back scaled by n. None of it shows up until the code runs on a GPU. This skill replaces guessing with lookup: exact signatures, known pitfalls, GPU-verified examples and a static checker, all generated from the library sources at pinned versions.

    What it does

    • Exact signatures for 275 functions and classes of CuPy 14.2.0 and nvmath-python 1.0.0, the Python front ends of cuBLAS, cuFFT, cuSOLVER, cuSPARSE, cuDSS and cuTENSOR.
    • Pitfalls that look right and fail on the GPU, on 43 curated cards: dense linear algebra, FFTs, sparse solvers, nvmath matmul epilogs and the cuDSS direct solver.
    • 39 examples verified on a Tesla T4, each a complete, runnable snippet whose asserts passed there.
    • A static checker that finds functions that don't exist, wrong or positional-only keywords, missing arguments and submodules that were never imported, and suggests the real name. It needs no GPU.

    How it works

    1. Find the function in the index (one line per function, curated ones marked).
    2. Read its card: signature, the import it really needs, pitfalls, verified example.
    3. Skim the pitfalls for your area.
    4. Write the code, then run scripts/check_gpu_code.py on it and fix every error it reports.

    If the mathlib-mcp MCP server is connected, the skill uses its tools instead of the files.

    Measured, not promised

    This skill was measured on a benchmark of 26 GPU tasks run on a Tesla T4, three samples per task. Claude Haiku 4.5 solved 100% of the tasks with it, against 51% without help, 87% with the full documentation in the prompt and 96% with the same knowledge served as MCP tools; none of its final solutions contained a hallucinated API (36% did without help). Claude Sonnet 5.5 went from 94% to 99%. The checker raised no false alarms on 407 passing attempts and flagged 36 of the 41 that failed with an API error. Full results, transcripts and code: github.com/jarski/mathlib-mcp (MIT).

    Use cases

    • Porting NumPy or SciPy pipelines to the GPU without the trial-and-error round trips.
    • Batched linear solves, eigenvalues, SVD and least squares on stacked matrices.
    • FFT-based filtering and convolution, including nvmath-python's unnormalized inverse transforms.
    • Sparse iterative and direct solvers (cuSPARSE, cuDSS) with the right keywords and matrix formats.
    • Fused matrix multiplication with epilogs (bias, ReLU, GELU) through cuBLASLt.

    Requirements and limits

    Python 3.12+ and uv or pip for the checker; no GPU needed to use the skill. Covers cupy, cupy.linalg, cupy.fft, cupy.cublas, cupyx.scipy.linalg, cupyx.scipy.fft, cupyx.scipy.sparse and the nvmath linalg, fft, sparse and tensor modules. Not covered: custom CUDA kernels, cupyx.jit, cupy.testing. The checker is static: it verifies names, arguments and imports, not shapes, dtypes, memory or numerical results.

    How to use

    Skills on Agensi run inside your AI agent. Copy the prompt, paste it into your agent, and it connects to Agensi and installs this skill for you. If a payment is needed, your agent shows it to you first.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill passes our trust checks below.

    Security scanned

    Passed our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Free forever

    No account required to browse

    Trust & safety

    Security scanned

    Verified clean today

    • Free to download with an account

    Requires

    Python 3.12+
    uv (Python tool runner)
    git

    Listedtoday

    What's inside

    Frequently Asked Questions