Skip to main content
caj

Persistent, code-aware caching for JAX functions

CI Codecov Ruff ty uv Publish PyPI PyPI - Python Version

caj is a lightweight persistent cache for JAX-based functions. It stores computation results on disk and reuses them even across Python sessions, avoiding repeated expensive computations.

Unlike caches based only on function arguments, caj also uses JAX's traced computation to identify cached results. Changes to a function's computation—including changes in any other functions it calls—automatically invalidate the corresponding cached results.

import caj
import jax
import jax.numpy as jnp


@caj.cache
def compute(x):
    return jnp.linalg.eigvalsh(x)


x = jnp.eye(1000)

y = compute(x)  # First call computes and caches the result
y = compute(x)  # Following calls just load the cached result

Installation

Install caj from PyPI:

pip install caj

Usage

Simply decorate a JAX-based function with @caj.cache:

import caj


@caj.cache
def f(x): ...

Note: adding @jax.jit is optional. If @jax.jit is used, it should be placed below @caj.cache (as the latter is not itself JIT-compatible). It is recommended to use @jax.jit for performance, since functions that are not JIT-compatible will generally not work with @caj.cache either.

By default, caj stores results in a per-user cache directory, with a shared size limit of 1 GB.

To use a custom directory:

@caj.cache(dir=".cache")
def f(x): ...

The maximum cache size can also be configured:

@caj.cache(dir=".cache", max_bytes=2_000_000_000)
def f(x): ...

Set max_bytes=None for an unlimited cache:

@caj.cache(dir=".cache", max_bytes=None)
def f(x): ...

When a size limit is enabled, least-recently-used entries are removed as necessary to make room for new results.

How it works

caj identifies cached results using a hash of the traced JAX computation, its input values, and the JAX and jaxlib versions. This allows it to reuse results even across Python sessions while automatically invalidating cached results when the computation changes.

Unlike JAX's persistent compilation cache, which stores compiled executables, caj stores the results of computations themselves.

Compatibility with @jax.jit

@caj.cache is compatible with @jax.jit: it can be applied to JIT-compiled functions, and it can even be used inside JIT-compiled code (with caching operations performed via JAX callbacks). Note that using @caj.cache on a function will not apply @jax.jit automatically, even though JIT-compatibility is generally required for functions to work with @caj.cache.

Metadata

Release files for caj 0.0.7

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

Source distribution (sdist)

Source distribution for caj 0.0.7
File Size Uploaded Provenance Actions
caj-0.0.7.tar.gz 6.5 kB Without provenance Details for caj-0.0.7.tar.gz

Built distribution (wheel)

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

Total release size: 13.8 kB

Release files / caj-0.0.7.tar.gz

Download URL caj-0.0.7.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7da6c320591887aa36d0790af3852bf33da0e5ed4ef8d0a87184a6e89746205b
BLAKE2b-256 checksum
How to use checksums
911b90bf24dfeaac14a065aa3ad3bde999428ef5b4079261745e169f1c55ecf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / caj-0.0.7-py3-none-any.whl

Download URL caj-0.0.7-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e54da91e37b75fc6d4eaae6884efe47be7d58d81b2883cd099834f61a22dc2b5
BLAKE2b-256 checksum
How to use checksums
786731fcc62c56575965870a04e2390d0f5f5b836d0b7a28658da9477b17292f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.3 {"installer":{"name":"uv","version":"0.13.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for caj 0.0.7

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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