Skip to main content

PyPI - Version EffVer Versioning SPEC-0 Ruff ty PyPI - Python Version

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 with field(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 with field(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. The Module base and metaclass constrain inheritance, and the idiomatic path pushes you into eqx.filter_jit / eqx.partition at 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.grad differentiates through an update with respect to its hyperparameters, for example to tune a learning rate by gradient descent.
  • Vectorization. jax.vmap maps an update over hyperparameter values, for example to sweep learning rates in one call.
  • Static options. Structural arguments, such as nesterov and 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 to update.
  • 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_vjp and custom_jvp that support being used on methods: custom_vjp_method and custom_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 frozendict that is registered as a JAX pytree, so it can be passed through transforms like jit and vmap. (See frozendict.)
  • Filters that summarize a signal's history in a fixed-size state, such as leaky integrals and Erlang chains, in tjax.filtering. (See the filtering documentation.)
  • A testing function assert_tree_allclose that automatically produces testing code. And, a related function tree_allclose, which compares pytrees with matching structure. (See testing.)
  • Basic tools like divide_or_zero and divide_where. (See tools.)

Contributing

Contributions are welcome. See the contribution guidelines.

Metadata

Release files for tjax 2.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tjax 2.3.2
File Size Uploaded Provenance Actions
tjax-2.3.2.tar.gz 90.2 kB With provenance Details for tjax-2.3.2.tar.gz

Built distribution (wheel)

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

Total release size: 200.0 kB

Release files / tjax-2.3.2.tar.gz

Download URL tjax-2.3.2.tar.gz
Size 90.2 kB
Tags Source
SHA-256 checksum
How to use checksums
629a89799a318fa58672fd58681a85c0305adf3de34cfe8325dba62b11f65c85
BLAKE2b-256 checksum
How to use checksums
1446fe91f7900a5184e799a51a307ab710023d63ca2d7ad0112fd782bcf55b9b
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 17, 2026.

Transparency log

Release files / tjax-2.3.2-py3-none-any.whl

Download URL tjax-2.3.2-py3-none-any.whl
Size 109.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2f2e8be5f7319b9a4c2e4c524e4a84054a4c5e22bbbace6df3cd0c381d6757f
BLAKE2b-256 checksum
How to use checksums
0935a62e10c7c789f1792368d28cbfbe81eafc10402fd61ec331264ed1a03515
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 17, 2026.

Transparency log

Security for tjax 2.3.2

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.

Select from 2 files

Attestation for tjax-2.3.2.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 17, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

2.3.2 This release

2 release files Full provenance

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