Tools for JAX
This repository implements a variety of tools for the differential programming library JAX.
Dataclasses
The dataclass decorator
defines a frozen dataclass that is also a JAX pytree, so you can pass your own structured objects to
JAX transformations. Fields are dynamic by default: JAX traces them, and new values reuse compiled
code. field(static=True) marks a field as static instead, which makes it part of the compiled
function, so Python can branch on it, and changing it recompiles.
import copy
import jax
import jax.numpy as jnp
from jax import Array
from tjax.dataclasses import dataclass, field
@dataclass
class Line:
slope: Array
intercept: Array
clip: bool = field(default=False, static=True)
def __call__(self, x: Array) -> Array:
y = self.slope * x + self.intercept
return jnp.clip(y, 0.0, 1.0) if self.clip else y
@jax.jit
def loss(line: Line, x: Array, y: Array) -> Array:
return jnp.sum(jnp.square(line(x) - y))
x = jnp.array([0.0, 1.0])
y = jnp.array([1.0, 3.0])
line = Line(jnp.asarray(1.0), jnp.asarray(0.0))
loss(line, x, y) # Compiles.
loss(copy.replace(line, slope=jnp.asarray(2.0)), x, y) # Reuses the compiled function.
loss(copy.replace(line, clip=True), x, y) # Recompiles.
# Differentiating gives a Line of derivatives: slope=-4 and intercept=-6.
jax.grad(loss)(line, x, y)
Alternatives
Several libraries offer dataclass-like pytrees. Compared with them, tjax.dataclass:
- vs
chex.dataclass: chex can't mark fields static, which rules out a huge class of designs: flags, shapes, dtypes, mode strings, callable options, anything that should drive recompilation rather than live in the trace. Tjax marks them inline withfield(static=True). - vs
jax.tree_util.register_dataclass: the primitive forces a second call per class and wants the static and dynamic partition as parallel name lists at the registration site — awkward, duplicative, and easy to desync from the fields. Tjax does it in the decorator withfield(static=True)on the field itself. - vs
flax.struct.dataclass: near-equivalent in capability; the win is not dragging Flax in for one decorator. - vs
equinox.Module: too heavyweight for just wanting a pytree dataclass. TheModulebase and metaclass constrain inheritance, and the idiomatic path pushes you intoeqx.filter_jit/eqx.partitionat every call site instead of declaring the static and dynamic boundary once on the class.
Gradient transformations
The gradient package provides every transformation in optax as a dataclass whose fields are the transformation's arguments. Each transformation is a pytree, so JAX traces its hyperparameters like any other array.
This matters because an optax transformation is a pair of functions that close over their
hyperparameters, which JAX can't see. You can't pass an optax transformation to a jitted function;
you construct it inside the traced function or wrap it with optax.inject_hyperparams. A tjax
transformation instead behaves like data:
- Jit. A transformation can be an argument of a jitted function, and a new value of a numeric hyperparameter, such as the learning rate, reuses the compiled function.
- Differentiation.
jax.graddifferentiates through an update with respect to its hyperparameters, for example to tune a learning rate by gradient descent. - Vectorization.
jax.vmapmaps an update over hyperparameter values, for example to sweep learning rates in one call. - Static options. Structural arguments, such as
nesterovand weight-decay masks, are static fields, so jit compiles a specialized function for each setting. - Types.
GradientTransformation[State, Weights]is generic in its state and weight types, so a type checker can check what you pass toupdate. - Display. A transformation prints with its hyperparameters, for example
Adam(learning_rate=0.01, b1=0.9, ...).
For example, this code compiles one Adam step for two learning rates, then differentiates and maps the loss after one SGD step over the learning rate:
import jax
import jax.numpy as jnp
import optax
import tjax.gradient as tg
params = {"w": jnp.array([1.0, -2.0])}
def loss(params):
return jnp.sum(params["w"] ** 2)
@jax.jit
def loss_after_step(transform, params):
gradient = jax.grad(loss)(params)
updates, _ = transform.update(gradient, transform.init(params), params)
return loss(optax.apply_updates(params, updates))
loss_after_step(tg.Adam(1e-2), params) # Compiles.
loss_after_step(tg.Adam(3e-2), params) # Reuses the compiled function.
# Differentiate with respect to the learning rate.
jax.grad(lambda lr: loss_after_step(tg.SGD(lr), params))(0.1)
# Take one step at each of three learning rates.
jax.vmap(lambda lr: loss_after_step(tg.SGD(lr), params))(jnp.array([0.01, 0.1, 0.4]))
See the gradient documentation for its coverage of optax, the multi-loss transformations it adds, and its base classes.
Pretty printing
print_generic displays
aggregate and vector types, including dataclasses, as a colorized tree, and it works on traced
values inside jitted functions. It formats small arrays as tables, and summarizes arrays that don't
fit:
from tjax import print_generic
print_generic({"weights": jnp.arange(6.0).reshape(2, 3), "large": jnp.ones((200, 200))})
dict
├── large=Jax Array (200, 200) float32
│ ├── mean=1.0
│ └── deviation=0.0
└── weights=Jax Array (2, 3) float32
└── 0.0000 │ 1.0000 │ 2.0000
3.0000 │ 4.0000 │ 5.0000
display_generic returns the same tree for you to format yourself.
Other tools
Tjax also includes:
- Versions of
custom_vjpandcustom_jvpthat support being used on methods:custom_vjp_methodandcustom_jvp_method(See shims.) - Tools for working with cotangents. (See cotangent_tools.)
- JAX tree registration for NetworkX graph types. (See graph.)
- A hashable immutable mapping
frozendictthat is registered as a JAX pytree, so it can be passed through transforms likejitandvmap. (See frozendict.) - Leaky integration
leaky_integrateand Ornstein-Uhlenbeck process iterationdiffused_leaky_integrate. (See leaky_integral.) - A testing function
assert_tree_allclosethat automatically produces testing code. And, a related functiontree_allclose, which compares pytrees with matching structure. (See testing.) - Basic tools like
divide_or_zeroanddivide_where. (See tools.)
Contributing
Contributions are welcome. See the contribution guidelines.
Metadata
Release files for tjax 2.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tjax-2.2.0.tar.gz | 83.0 kB | With provenance | Details for tjax-2.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tjax-2.2.0-py3-none-any.whl | Python 3 | none | any | Details for tjax-2.2.0-py3-none-any.whl |
Total release size: 181.2 kB
Release files / tjax-2.2.0.tar.gz
| Download URL | tjax-2.2.0.tar.gz |
|---|---|
| Size | 83.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5eb898d5bce63f1452aff4bd1d31a0597fdba9ec7060c3ac435213d63bf3b5c8
|
|
BLAKE2b-256 checksum How to use checksums |
86f2867b7d5c6b87516ec7ba2ea0079664a3dca99bc1f0dfa9c127ee5fe00dd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / tjax-2.2.0-py3-none-any.whl
| Download URL | tjax-2.2.0-py3-none-any.whl |
|---|---|
| Size | 98.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90bd1312d3ad67658671c2e83bf8b8d9dc508faaf5d44e9659f7af95772f99be
|
|
BLAKE2b-256 checksum How to use checksums |
a92aaa15047cfa2cbe178315e62e3c58cb91612d54f0ef5a1ebaea0c44d67638
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for tjax 2.2.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for tjax-2.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA 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.