Skip to main content

A minimal JAX interpretability library

Project description

jaxray

A minimal JAX interpretability library for activation caching and patching.

Why jaxray?

Current JAX interpretability tooling has significant friction:

  • Penzai requires learning new paradigms (pytrees as models, named axes) and has poor interop with existing Flax/Haiku models
  • XLens is a direct port of TransformerLens with limited model coverage
  • No JAX equivalent to PyTorch's NNsight/pyvene for wrapping arbitrary models

Meanwhile, PyTorch researchers have mature, frictionless options (TransformerLens, NNsight, pyvene).

The Core Problem

JAX's functional purity makes activation caching awkward. Side effects break jax.jit:

cache = {}
def model(params, x):
    x = layer(params, x)
    cache['layer'] = x  # side effect  breaks jax.jit
    return x

The Solution

Return the cache explicitly  it's pure and works with all JAX transforms:

def model(params, x):
    cache = {}
    x = layer(params, x)
    cache['layer'] = x
    return x, cache  # pure  jax.jit works

jaxray provides simple scaffolding around this pattern.

Installation

pip install jaxray

Usage

The @intercept Decorator

Decorate your model function and use _save to mark activation points:

from jaxray import intercept
import jax.numpy as jnp
import jax

@intercept
def mlp(params, x, _save):
    x = jnp.dot(x, params['w1']) + params['b1']
    x = _save('pre_relu', x)

    x = jax.nn.relu(x)
    x = _save('post_relu', x)

    x = jnp.dot(x, params['w2']) + params['b2']
    return x

# Basic forward pass  no caching
output, cache = mlp(params, x)  # cache is empty dict

# Cache specific activations
output, cache = mlp(params, x, cache={'pre_relu', 'post_relu'})
print(cache['pre_relu'].shape)

# Patch  replace an activation with a fixed value
output, cache = mlp(params, x, patch={'post_relu': jnp.zeros(hidden_dim)})

# Patch  transform an activation
output, cache = mlp(params, x, patch={'post_relu': lambda act: act * 0.5})

# Combine caching and patching
output, cache = mlp(params, x,
    cache={'pre_relu'},
    patch={'post_relu': lambda act: act + steering_vector}
)

With JAX Transforms

Use functools.partial to fix cache/patch before applying JAX transforms:

from functools import partial

# jit
mlp_cached = partial(mlp, cache={'pre_relu'})
jitted_mlp = jax.jit(mlp_cached)
output, cache = jitted_mlp(params, x)

# vmap
batched_mlp = jax.vmap(partial(mlp, cache={'pre_relu'}))
output, cache = batched_mlp(params, batch_x)

# grad (works directly)
def loss_fn(params, x):
    output, _ = mlp(params, x)
    return output.sum()

grads = jax.grad(loss_fn)(params, x)

Design Principles

  1. No new paradigms  uses standard JAX/Flax patterns
  2. Minimal API  a decorator and a few helpers, not a framework
  3. Composable  works with jax.jit, jax.grad, jax.vmap
  4. Toy model friendly  zero friction for simple MLPs and transformers

Current Status

Implemented:

  • @intercept decorator with caching and patching
  • Full compatibility with JAX transforms

Planned:

  • patching_sweep helper for activation patching experiments
  • steering_hook, zero_ablate, mean_ablate helpers
  • Pre-built toy models (MLP, Transformer)

Project details


Download files

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

Source Distribution

jaxray-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

jaxray-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file jaxray-0.1.0.tar.gz.

File metadata

  • Download URL: jaxray-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for jaxray-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0cfd9934f94afb3936fdefdd5859a8ec1972fa92a756988ce14af11e616274a
MD5 0e025ef3759193c8be73103ae2258215
BLAKE2b-256 10f5726ceef6ea9ee0d3bd2af10ed6781231c722ab3f18eed66b356f4d9d829e

See more details on using hashes here.

File details

Details for the file jaxray-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jaxray-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for jaxray-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7cd50bca78511ce28266ff42ee76e2382bdd5d1115aa50abc02ce24d47a790b
MD5 ecee2e4b782985476f2cb7433ae0ea0c
BLAKE2b-256 b4d70289f1758f1a350e5532f08730dd2830a4ac95bc5941fc2229909d2d43f6

See more details on using hashes here.

Supported by

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