Dataclasses + JAX
Low maintenance activity. Last release 11 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install jax-dataclassesjax_dataclasses
Overview
jax_dataclasses provides a simple wrapper around dataclasses.dataclass for use in
JAX, which enables automatic support for:
- Pytree registration. This allows dataclasses to be used at API boundaries in JAX.
- Serialization via
flax.serialization.
Distinguishing features include:
- An annotation-based interface for marking static fields.
- Improved ergonomics for "model surgery" in nested structures.
Installation
In Python >=3.7:
pip install jax_dataclasses
We can then import:
import jax_dataclasses as jdc
Core interface
jax_dataclasses is meant to provide a drop-in replacement for
dataclasses.dataclass: jdc.pytree_dataclass has
the same interface as dataclasses.dataclass, but also registers the target
class as a pytree node.
We also provide several aliases:
jdc.[field, asdict, astuples, is_dataclass, replace] are identical to
their counterparts in the standard dataclasses library.
Static fields
To mark a field as static (in this context: constant at compile-time), we can wrap its type with :