Skip to main content

CuNumpy

Simple wrapper for numpy and cupy. Replace import numpy as np with import cunumpy as xp.

Install

pip install cunumpy

Example usage:

export ARRAY_BACKEND=cupy
import cunumpy as xp

xp.set_backend("cupy")

arr = xp.array([1, 2])

print(f"{type(arr) = }")
print(f"{xp.__version__ = }")

# Convert to NumPy
arr_np = xp.to_numpy(arr)

# Convert to active backend
arr_xp = xp.to_cunumpy(arr)

# Inspect backend
print(f"{xp.get_backend(arr) = }")
print(f"{xp.is_gpu(arr) = }")
print(f"{xp.is_cpu(arr) = }")

# Temporarily switch backend
with xp.use_backend("numpy"):
    # This code runs on CPU even if ARRAY_BACKEND=cupy
    arr_cpu = xp.zeros(100)

# Set backend globally
xp.set_backend("cupy")

# Synchronize GPU operations (no-op on CPU)
xp.synchronize()

Output:

type(arr) = <class 'cupy.ndarray'>
xp.__version__ = '0.1.4'
xp.get_backend(arr) = 'cupy'
xp.is_gpu(arr) = True
xp.is_cpu(arr) = False

Pyodide

cuNumPy supports Pyodide with the NumPy backend. In an initialized Pyodide JavaScript runtime, install the package and run a Python-source kernel:

await pyodide.loadPackage("micropip");
await pyodide.runPythonAsync(`
    import micropip
    await micropip.install("cunumpy")

    import cunumpy as xp
    xp.set_backend("numpy")

    def scale(values, factor):
        values[:] *= factor
        return values

    values = xp.array([1.0, 2.0, 3.0])
    result = xp.PyccelKernel(scale)(values, 2.0)
    assert result is values
    print(xp.to_numpy(result))  # [2. 4. 6.]
`);

NumPy is the default backend when ARRAY_BACKEND is unset. CuPy/CUDA and native Pyccel compilation are not supported in Pyodide. Despite its name, PyccelKernel accepts ordinary Python callables and neither imports Pyccel nor compiles code. Applications must supply Python-source kernels with Pyodide-compatible imports.

CI tests the built wheel in Pyodide's WebAssembly runtime under Node.js, including array operations, conversions, mutation/aliasing, backend context restoration, and Python kernels, while rejecting CuPy or Pyccel imports. This does not test browser-specific integration such as page loading or workers. See the Pyodide guide for details and local test commands.

Development tests

pip install -e '.[test]'
pytest tests/portable

The test extra is compiler-free. For compiled-kernel tests, install '.[test-compiled]' and a working native compiler, then run pytest. The dev extra includes these compiled-test dependencies as before.

Build docs

make html
cd ../
open docs/_build/html/index.html

Metadata

Release files for cunumpy 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cunumpy 0.1.5
File Size Uploaded
cunumpy-0.1.5.tar.gz 11.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cunumpy 0.1.5
File Interpreter ABI Platform
cunumpy-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / cunumpy-0.1.5.tar.gz

Download URL cunumpy-0.1.5.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
98b402679c5630220de0d5988919a58b6b56f5a182b564c3868a1a2edfbeb0c7
BLAKE2b-256 checksum
How to use checksums
11e3328a61d8b509a46fa78abe3a1af09ef67e499b093b4f4b68db4631b6d1a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release files / cunumpy-0.1.5-py3-none-any.whl

Download URL cunumpy-0.1.5-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d974329671500398231bf211a1d72c4a032935c5841965e9e2bb38084ef818b
BLAKE2b-256 checksum
How to use checksums
6004e66ba08facd22fbfd82873b3259b8889ea79f23cb6ec5f1b1aec16ca44d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.21

Release history Release notifications | RSS feed

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page