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

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.5
File Size Uploaded Provenance Actions
caj-0.0.5.tar.gz 6.4 kB Without provenance Details for caj-0.0.5.tar.gz

Built distribution (wheel)

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

Total release size: 13.8 kB

Release files / caj-0.0.5.tar.gz

Download URL caj-0.0.5.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0660c13a1173a9d518b110e04bf1936faa69b967fa9d0047e2a352912848b354
BLAKE2b-256 checksum
How to use checksums
070521f83f81f304292fc45f0e06c6fc23c119cc04d1efcbcf0ee1dc393a0c63
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.5-py3-none-any.whl

Download URL caj-0.0.5-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67255f58ad9ff4694e57c03fe477f3facb62d774e3e875c110aa922aeb22086c
BLAKE2b-256 checksum
How to use checksums
ea32eb67c2c22f62b1e8c7b8bbac7efb714743c5f491d38bbcbb0fc7252e90a9
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.5

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