Skip to main content

acai

Array Control-flow And Iteration: small, backend-independent control-flow primitives for code written with the Python Array API ecosystem.

Installation

Install acai with:

pip install acai-control

Array backends such as NumPy, CuPy, JAX, and PyTorch are not installed by acai-control. array-api-compat provides namespace inference and backend detection, while Optree provides pytree traversal.

Usage

The acai namespace provides four main functions:

  • cond: Apply one of two functions according to a scalar predicate.
  • fori_loop: Apply a function over an integer range.
  • scan: Scan a function over arrays while carrying state.
  • while_loop: Apply a function repeatedly while a condition is true.

The basic usage is:

import numpy as np
from array_api_compat import array_namespace

from acai import cond, fori_loop, scan, while_loop

x = np.asarray(1)
xp = array_namespace(x)

# 1 * 2 * 3 * 4 * 5
factorial = fori_loop(1, 6, lambda i, value: value * i, x, xp=xp)

# The namespace may also be inferred from an array carry.
power_of_two = while_loop(lambda value: value < 16, lambda value: value * 2, x)

# Select one branch without losing staged control flow on JAX.
magnitude = cond(x >= 0, lambda value: value, lambda value: -value, x, xp=xp)

# Accumulate values and collect every intermediate total.
total, cumulative = scan(
    lambda carry, value: (carry + value, carry + value),
    np.asarray(0),
    np.asarray([1, 2, 3]),
)

Implementation details

The loop functions use ordinary Python loops for NumPy, CuPy, and PyTorch. For JAX, they dispatch to jax.lax.while_loop and jax.lax.fori_loop, so loops remain staged under jax.jit. Likewise, cond uses ordinary Python branching except on JAX, where it dispatches to jax.lax.cond. scan uses eager iteration and stacking except on JAX, where it dispatches directly to jax.lax.scan.

PyTorch

PyTorch arrays are supported through eager Python control flow. PyTorch's native structured control-flow primitives are currently prototype features, so acai does not dispatch to them. Consequently, its control flow is not guaranteed to remain structured under torch.compile.

Dask and ndonnx

Dask arrays and ndonnx symbolic arrays are not currently supported. All primitives raise NotImplementedError when given either namespace.

Python Array API standard

acai is not part of the Python Array API standard and is not affiliated with array-api-compat or array-api-extra.

Metadata

Release files for acai-control 0.1.0

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

Source distribution (sdist)

Source distribution for acai-control 0.1.0
File Size Uploaded
acai_control-0.1.0.tar.gz 15.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for acai-control 0.1.0
File Interpreter ABI Platform
acai_control-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.8 kB

Release files / acai_control-0.1.0.tar.gz

Download URL acai_control-0.1.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dfc0e66130f75f6f8d406041430915c1cfc76e7bf4a09e7f0c4c06177c4de6f2
BLAKE2b-256 checksum
How to use checksums
79336ce2f45a305e8dcf2da151842841032ed66af5dba36f62fce316e3e17a3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.

Transparency log

Release files / acai_control-0.1.0-py3-none-any.whl

Download URL acai_control-0.1.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b61bc1aa7f50944175ea563c178ef52b066f4c3e8306c037d7120fdf6df6421a
BLAKE2b-256 checksum
How to use checksums
07ef9b77fc80fc9bf28978d5649a3c71c0e77efe9e1966e5f608e7c3d5ba86b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

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