Skip to main content

Jaxpr expression introspection and injection via tags

Project description

Taxpr

Run tests PyPI Version

Taxpr is a collection of utilities for performing manipulation of Jaxprs. This is achieved by tag-ing specific arrays at trace time, then extracting and manipulating those tags in the final Jaxpr.

⚠️ This package is still very experimental, so expect broken code and breaking changes.

The provided routines are designed to work seamlessly with jit, vmap, custom_jvp and cousins.

Example

The following example shows how you can use taxpr to emulate functions with side effects without violating Jax's pure function rules.

import itertools as it

import jax
import jax.numpy as jnp
from jax._src.core import eval_jaxpr
import taxpr as tx

_state_counter = it.count()

def get_state(shape, dtype):
    count = next(_state_counter)

    def set_state(value):
        return tx.tag(value, op="set", id=count)

    value = jax.numpy.zeros(shape, dtype=dtype)
    return tx.tag(value, op="get", id=count), set_state


def uncurry(fn, *args, **kwargs):
    jaxpr = jax.make_jaxpr(fn)(args, kwargs)
    states = {}

    # iterate through all tags in the jaxpr
    # this recurses all child Jaxprs too

    for params, shape in tx.iter_tags(jaxpr.jaxpr):
        if params["op"] == "get":
            states[params["id"]] = shape

    initial_states = jax.tree.map(
        lambda x: jax.numpy.full_like(x, 0), states
    )

    def injector(states, token, params):
        if params["op"] == "get":
            state = states[params["id"]]
            return state, states
        elif params["op"] == "set": 
            states[params["id"]] = token
            return token, states
        raise ValueError(f"Unknown tag op: {params['op']}")

    # replace the token with a function that performs the state manipulation
    # here we can pass our own context (`initial_states`)

    jaxpr = tx.inject(jaxpr, injector, initial_states)

    def wrapper(states, *args, **kwargs):
        return eval_jaxpr(jaxpr.jaxpr, jaxpr.consts, states, args, kwargs)

    return wrapper, initial_states

################################################

# Usage

def running_sum(x):
    a, set_state = get_state(x.shape, x.dtype)
    sum = set_state(a + x)
    return sum

rsum, state = uncurry(running_sum, jnp.zeros(0))

_, state = rsum(state, jnp.ones(1))
_, state = rsum(state, jnp.ones(1))
_, state = rsum(state, jnp.ones(1))

assert jnp.allclose(next(iter(state.values())), 3)

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

taxpr-0.0.2.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

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

taxpr-0.0.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file taxpr-0.0.2.tar.gz.

File metadata

  • Download URL: taxpr-0.0.2.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for taxpr-0.0.2.tar.gz
Algorithm Hash digest
SHA256 30203a23b98cc60960c7438fdc02f2089c979f1847476db9384e55cfa1f25c04
MD5 edcbd48af4b5e74f2e83ef21e3a4b33b
BLAKE2b-256 d2dbeecf76a0f8dae5504452d048e5b6718221e09bd245b894081eae7e351436

See more details on using hashes here.

Provenance

The following attestation bundles were made for taxpr-0.0.2.tar.gz:

Publisher: release.yml on otoomey/taxpr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file taxpr-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: taxpr-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for taxpr-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 46a84f591ad4f8c26f7f97d5c26204c27c0b0a2a0ebe0f9977b0aef4491f5f6d
MD5 ccb6085c5c08556569d11bd99c600588
BLAKE2b-256 24f1671f12816c7349705126fb54137e33b7d10cc969b866bf26f2bad8c4f2be

See more details on using hashes here.

Provenance

The following attestation bundles were made for taxpr-0.0.2-py3-none-any.whl:

Publisher: release.yml on otoomey/taxpr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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