Jaxpr expression introspection and injection via tags
Project description
Taxpr
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file taxpr-0.0.3.tar.gz.
File metadata
- Download URL: taxpr-0.0.3.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6587b0ceb1b06e19d60ab41b993fb42ff24d231d1644443cd0d08db02a7ef277
|
|
| MD5 |
a617731a1843ed7edfc52985e68a4bca
|
|
| BLAKE2b-256 |
4e278c695e485544a318ccb83c682ad67b5e99c4bf44bad791442e15e47592f3
|
Provenance
The following attestation bundles were made for taxpr-0.0.3.tar.gz:
Publisher:
release.yml on otoomey/taxpr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
taxpr-0.0.3.tar.gz -
Subject digest:
6587b0ceb1b06e19d60ab41b993fb42ff24d231d1644443cd0d08db02a7ef277 - Sigstore transparency entry: 1018039855
- Sigstore integration time:
-
Permalink:
otoomey/taxpr@7401101133061638334b24ba70e34560d70806a1 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/otoomey
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7401101133061638334b24ba70e34560d70806a1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file taxpr-0.0.3-py3-none-any.whl.
File metadata
- Download URL: taxpr-0.0.3-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d95f5f1deb1a6e494ca4dc816c93b817b3bbe60520c0af9f4dc96b0037e5899e
|
|
| MD5 |
fa3afb0c63d84e82675b228208f23c30
|
|
| BLAKE2b-256 |
94f4cc51e52d67638c4ada5ad3215001762eb58f4273c780861fe234effb869b
|
Provenance
The following attestation bundles were made for taxpr-0.0.3-py3-none-any.whl:
Publisher:
release.yml on otoomey/taxpr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
taxpr-0.0.3-py3-none-any.whl -
Subject digest:
d95f5f1deb1a6e494ca4dc816c93b817b3bbe60520c0af9f4dc96b0037e5899e - Sigstore transparency entry: 1018039858
- Sigstore integration time:
-
Permalink:
otoomey/taxpr@7401101133061638334b24ba70e34560d70806a1 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/otoomey
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7401101133061638334b24ba70e34560d70806a1 -
Trigger Event:
release
-
Statement type: