XBArray¶
XBArray is a Python array library that runs the same code on NumPy, PyTorch and JAX. It is built on the Python Array API Standard.
You write a function once against a compute backend object. You pass in
NumpyComputeBackend, PyTorchComputeBackend or JaxComputeBackend, and the
function works on that library's arrays: numpy.ndarray, torch.Tensor or jax.Array.
Each backend gives you:
- The Array API namespace for its library (
zeros,matmul,where,linalg, ...), through array-api-compat. - The helper functions from array-api-extra,
such as
atfor out-of-place index updates. - A few things the standard does not cover: explicit random number generators, conversion to and from NumPy and other backends, device serialization, and mapping a function over nested containers of arrays.
On top of the backends, xbarray.transformations has rotation conversions and point
cloud operations written in the same backend-agnostic style.
A short example¶
from xbarray import ComputeBackend
from xbarray.backends.numpy import NumpyComputeBackend
from xbarray.backends.pytorch import PyTorchComputeBackend
def normalize(backend: ComputeBackend, x):
"""Scale each row of x to unit length."""
norms = backend.linalg.vector_norm(x, axis=-1, keepdims=True)
return x / backend.clip(norms, min=1e-12)
for backend in (NumpyComputeBackend, PyTorchComputeBackend):
x = backend.asarray([[3.0, 4.0], [0.0, 2.0]])
print(backend, normalize(backend, x))
The same normalize works with JaxComputeBackend once JAX is installed.
Start here¶
-
Getting started
Install XBArray, run a first example on each backend, and get a
ComputeBackend. -
Concepts
Backends, generic typing, devices and dtypes, random numbers, and moving arrays between backends.
-
Write backend-agnostic code
The patterns that keep one function working on NumPy, PyTorch and JAX.
-
Rotation conversions
Quaternions, rotation matrices, Euler angles, axis-angle and the 6D representation.
-
Point clouds
Depth unprojection, farthest point sampling, ball query and jitter.
-
API reference
Every module, class and function, generated from the source.
Contributing? See Development for running the tests and benchmarks.
Part of UniEnv¶
XBArray is developed as part of UniEnv, which unifies robot environment and data APIs. The source is at github.com/UniEnvOrg/XBArray under the Apache-2.0 license.
Cite¶
XBArray is developed as part of UniEnv. If you use it in research, please cite UniEnv; the current author list and BibTeX are on unienvorg.github.io.