Skip to main content

arraybridge

Unified API for NumPy, CuPy, PyTorch, TensorFlow, JAX, and pyclesperanto

PyPI version Documentation Status Python 3.9+ License: MIT Coverage

Features

  • Unified API: Single interface for 6 array/tensor frameworks
  • Automatic Conversion: DLPack + NumPy fallback with automatic path selection
  • Declarative Decorators: @numpy, @torch, @cupy for memory type declarations
  • Device Management: Thread-local GPU contexts and automatic stream management
  • OOM Recovery: Automatic out-of-memory detection and cache clearing
  • Dtype Preservation: Automatic dtype preservation across conversions
  • Zero Dependencies: Only requires NumPy (framework dependencies are optional)

Quick Start

from arraybridge import convert_memory, detect_memory_type
import numpy as np

# Create NumPy array
data = np.array([[1, 2], [3, 4]])

# Convert to PyTorch (if installed)
torch_data = convert_memory(data, source_type='numpy', target_type='torch', gpu_id=0)

# Detect memory type
mem_type = detect_memory_type(torch_data)  # 'torch'

Declarative Decorators

from arraybridge import numpy, torch, cupy

@torch(input_type='numpy', output_type='torch', oom_recovery=True)
def my_gpu_function(data):
    """Automatically converts input from NumPy to PyTorch."""
    return data * 2

# Use with NumPy input
result = my_gpu_function(np.array([1, 2, 3]))  # Returns PyTorch tensor

Installation

# Base installation (NumPy only)
pip install arraybridge

# With specific frameworks
pip install arraybridge[torch]
pip install arraybridge[cupy]
pip install arraybridge[tensorflow]
pip install arraybridge[jax]
pip install arraybridge[pyclesperanto]

# With all frameworks
pip install arraybridge[all]

Supported Frameworks

Framework CPU GPU DLPack Notes
NumPy Base framework
CuPy CUDA arrays
PyTorch Tensors
TensorFlow Tensors
JAX Arrays
pyclesperanto OpenCL arrays

Why arraybridge?

Before (Manual conversion hell):

import numpy as np
import torch
import cupy as cp

def process_data(data, target='torch'):
    if target == 'torch':
        if isinstance(data, np.ndarray):
            return torch.from_numpy(data).cuda()
        elif isinstance(data, cp.ndarray):
            return torch.as_tensor(data, device='cuda')
    elif target == 'cupy':
        if isinstance(data, np.ndarray):
            return cp.asarray(data)
        elif hasattr(data, '__cuda_array_interface__'):
            return cp.asarray(data)
    # ... 30 more lines of if/elif ...

After (arraybridge):

from arraybridge import convert_memory, detect_memory_type

def process_data(data, target='torch'):
    source = detect_memory_type(data)
    return convert_memory(data, source_type=source, target_type=target, gpu_id=0)

Advanced Features

Thread-Local GPU Streams

from arraybridge import torch

@torch(oom_recovery=True)
def parallel_processing(data):
    # Automatically uses thread-local CUDA stream
    # Enables true parallelization across threads
    return data * 2

OOM Recovery

from arraybridge import cupy

@cupy(oom_recovery=True)
def memory_intensive_operation(data):
    # Automatically catches OOM errors
    # Clears GPU cache and retries
    return data @ data.T

Stack Utilities

from arraybridge import stack_slices, unstack_slices

# Stack 2D slices into 3D array
slices_2d = [np.random.rand(100, 100) for _ in range(50)]
volume_3d = stack_slices(slices_2d, memory_type='torch', gpu_id=0)

# Unstack 3D array into 2D slices
slices_back = unstack_slices(volume_3d, memory_type='torch', gpu_id=0)

Documentation

Full documentation available at arraybridge.readthedocs.io

Performance

arraybridge uses DLPack for zero-copy conversions when possible:

Conversion Method Speed
NumPy → PyTorch torch.from_numpy() Zero-copy
PyTorch → CuPy DLPack Zero-copy
CuPy → JAX DLPack Zero-copy
NumPy → CuPy Copy Fast
PyTorch → NumPy .numpy() Zero-copy (CPU)

License

MIT License - see LICENSE file for details

Contributing

Contributions welcome! Please see CONTRIBUTING.md for guidelines.

Credits

Developed by Tristan Simas as part of the OpenHCS project.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arraybridge-0.2.10.tar.gz (119.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arraybridge-0.2.10-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

Details for the file arraybridge-0.2.10.tar.gz.

File metadata

  • Download URL: arraybridge-0.2.10.tar.gz
  • Upload date:
  • Size: 119.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for arraybridge-0.2.10.tar.gz
Algorithm Hash digest
SHA256 7ed13813b08dd28df7ff6c4d5556de77ea50a0e44011f563e520c49fa77e9750
MD5 5d5f90a309800825e847b8441b133e3f
BLAKE2b-256 2ca918235e08fd612241aab734ef0375317deb399942fe7f233a7372b1a70cfd

See more details on using hashes here.

Provenance

The following attestation bundles were made for arraybridge-0.2.10.tar.gz:

Publisher: publish.yml on OpenHCSDev/ArrayBridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file arraybridge-0.2.10-py3-none-any.whl.

File metadata

  • Download URL: arraybridge-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 32.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for arraybridge-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7d1ddb2d943764a1bd79019b8e29fe5b4d8e3fa082833230935db7b117e4963b
MD5 515cb94884a3fc39ff2b26606e487985
BLAKE2b-256 220bb9bb708dd06ce13b0ce8ba73bbb1af10c21d8380784031893faf93848dda

See more details on using hashes here.

Provenance

The following attestation bundles were made for arraybridge-0.2.10-py3-none-any.whl:

Publisher: publish.yml on OpenHCSDev/ArrayBridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page