Array API compatibility library for Python

1.14.0 · active · verified Fri Apr 10

array-api-compat is a small wrapper around popular array libraries like NumPy, CuPy, PyTorch, Dask, JAX, ndonnx, and Sparse. Its primary goal is to make these libraries compatible with the Python Array API standard, allowing array-consuming libraries to write array-agnostic code today, even if the underlying array libraries do not yet fully implement the standard. The library is actively maintained by the Consortium for Python Data API Standards. The current version is 1.14.0, with ongoing development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the recommended usage of `array-api-compat` by obtaining an Array API compliant namespace using `array_namespace()`. This namespace (`xp`) can then be used with any supported array type (like NumPy, PyTorch, CuPy, etc.), enabling generic array-agnostic code.

import numpy as np
from array_api_compat import array_namespace

def process_arrays(x, y):
    xp = array_namespace(x, y)
    # Now use xp as the array API compliant namespace
    result = xp.mean(x, axis=0) + 2 * xp.std(y, axis=0)
    return result

# Example usage with NumPy arrays
a = np.array([1.0, 2.0, 3.0])
b = np.array([4.0, 5.0, 6.0])

output = process_arrays(a, b)
print(output)
# Expected output type is the same as input (e.g., numpy.ndarray)
print(type(output))

view raw JSON →