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.6

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.6
File Size Uploaded Provenance Actions
caj-0.0.6.tar.gz 6.5 kB Without provenance Details for caj-0.0.6.tar.gz

Built distribution (wheel)

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

Total release size: 13.8 kB

Release files / caj-0.0.6.tar.gz

Download URL caj-0.0.6.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5f3c21d47e8382cb37ee5c208ee9b2d26bda56ba3c5a0439b081b2251b530862
BLAKE2b-256 checksum
How to use checksums
5557acf8edbad4a1c494cae84b0106c8b320e929c5731afe0b4c7349f6d43558
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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.6-py3-none-any.whl

Download URL caj-0.0.6-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03a4ec84db3f131ab430dbbe1ef4c6e1f89b8076b6c8ab95f4e324ba434b6438
BLAKE2b-256 checksum
How to use checksums
00c733d5075812270d13ae4667a0a7c60a36d0dce45845366c7678e1b21c70f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","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.6

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