Dataclasses + JAX
Project description
jax_dataclasses
For compatibility with function transformations in JAX (jit, grad, vmap, etc), arguments and return values must all be registered as pytree containers. Dataclasses, by default, are not.
This library provides a thin wrapper around dataclasses.dataclass, which
automatically enables:
- Pytree registration. This allows dataclasses to be used at API boundaries in JAX. (necessary for function transformations, etc)
- Support for serialization via
flax.serialization. - Static analysis-friendly. Works out of the box with tools like
mypyandjedi.
This library was heavily influenced by some great existing work; see Alternatives for a comparison.
Installation
pip install jax_dataclasses
Core interface
jax_dataclasses is meant to provide a drop-in replacement for
dataclasses.dataclass:
jax_dataclasses.dataclasshas the same interface asdataclasses.dataclass, but also registers the target class as a pytree.jax_dataclasses.static_fieldhas the same interface asdataclasses.field, but will also mark the field as static. In a pytree node, static fields are treated as part of the treedef instead of as a child of the node.
We also provide several aliases:
jax_dataclasses.[field, asdict, astuples, is_dataclass, replace] are all
identical to their counterparts in the standard dataclasses library.
Mutations
All dataclasses are automatically marked as frozen and thus immutable (even when
no frozen= parameter is passed in). To make changes to nested structures
easier, we provide an interface that will (a) make a copy of a pytree and (b)
return a context in which any of that copy's contained dataclasses are
temporarily mutable:
from jax import numpy as jnp
import jax_dataclasses
@jax_dataclasses.dataclass
class Node:
child: jnp.ndarray
obj = Node(child=jnp.zeros(3))
with jax_dataclasses.copy_and_mutate(obj) as obj_updated:
# Make mutations to the dataclass. This is primarily useful for nested
# dataclasses.
#
# Also does input validation: if the treedef, leaf shapes, or dtypes of `obj`
# and `obj_updated` don't # match, an AssertionError will be raised.
# This can be disabled with a `validate=False` argument.
obj_updated.child = jnp.ones(3)
print(obj)
print(obj_updated)
Alternatives
A few other solutions exist for automatically integrating dataclass-style
objects into pytree structures. Great ones include:
chex.dataclass,
flax.struct, and
tjax.dataclass. These all influenced
this library.
The main differentiators of jax_dataclasses are:
-
Static analysis support. Libraries like
dataclassesandattrsrely on tooling-specific custom plugins for static analysis, which don't exist forchexorflax.tjaxhas a custom mypy plugin to enable type checking, but isn't supported by other tools.- Because
@jax_dataclasses.dataclassdecorator has the same API as@dataclasses.dataclass, it can include pytree registration behavior at runtime while being treated as the standard decorator during static analysis. This means that all static checkers, language servers, and autocomplete engines that support the standarddataclasseslibrary should work out of the box withjax_dataclasses.
- Because
-
Nested dataclasses. Making replacements/modifications in deeply nested dataclasses is generally very frustrating. The three alternatives all introduce a
.replace(self, ...)method to dataclasses that's a bit more convenient than the traditionaldataclasses.replace(obj, ...)API for shallow changes, but still becomes really cumbersome to use when dataclasses are nested.jax_dataclasses.copy_and_mutate()is introduced to address this. -
Static field support. Parameters that should not be traced in JAX should be marked as static. This is supported in
flax,tjax, andjax_dataclasses, but notchex. -
Serialization. When working with
flax, being able to serialize dataclasses is really handy. This is supported inflax.struct(naturally) andjax_dataclasses, but notchexortjax.
Misc
This code was originally written for and factored out of jaxfg. Nick Heppert provided valuable feedback.
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 jax_dataclasses-0.0.2.tar.gz.
File metadata
- Download URL: jax_dataclasses-0.0.2.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
878f5105baeeb891594236c572c72bedc886d5b8808e0283ca188fa2b3ae890b
|
|
| MD5 |
f50b6a3884cb038d80e00b342fffc9ae
|
|
| BLAKE2b-256 |
5ec3e523d9b995de6013c89c16458aff910c5ca744bdcbbf6b9597f5fc1a6933
|
File details
Details for the file jax_dataclasses-0.0.2-py3-none-any.whl.
File metadata
- Download URL: jax_dataclasses-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90b3240da2a94617a85c4811d7581536b3b6ec516ab6e18d6e9183abe6cd062d
|
|
| MD5 |
63c48d7bfeda8a472a13f1a872b4dc54
|
|
| BLAKE2b-256 |
4a70ab64c9666bf6d5e9febf34218a2e5d98418fc3125ee806a5ccfce0c89a81
|