drinx.DataClass#
- class drinx.DataClass[source]#
Bases:
objectBase class alternative to the
@drinx.dataclassdecorator.Subclassing
DataClassautomatically applies the@drinx.dataclasstransform, registering the subclass as a frozen dataclass and a JAX pytree node. Fields annotated withstatic_field()(orfield(static=True)) are placed in the pytree auxiliary data (not traced by JAX); all other fields become pytree leaves.Usage:
class MyModel(DataClass): weights: jax.Array learning_rate: float = static_field(default=1e-3)
Dataclass keyword arguments (
init,repr,eq, etc.) can be forwarded via the class definition:class MyModel(DataClass, order=True, kw_only=True): ...
Also provides
aset()for functional nested updates andupdated_copy()as a convenience wrapper arounddataclasses.replace().- __init__()#
Methods
__init__()aset(attr_name, val[, create_new_ok, ...])Sets an attribute of this class.
aset_inplace(attr_name, val[, ...])Sets an attribute of this dataclass in place by bypassing the frozen restriction via
object.__setattr__.updated_copy(**kwargs)Returns an updated copy of the tree with modified top-level attributes.
Attributes
Returns an
_AtProxyfor JAX-style.at[].set()fluent updates.- aset(attr_name, val, create_new_ok=False, allow_private=False, bypass_callbacks=False)[source]#
Sets an attribute of this class. In contrast to the classical .at[].set(), this method updates the class attribute directly and does not only operate on jax pytree leaf nodes. Instead, replaces the full attribute with the new value.
The attribute can either be the attribute name of this class, or for nested classes it can also be the attribute name of a class, which itself is an attribute of this class. The syntax for this operation could look like this: “a->b->[0]->[‘name’]”. Here, the current class has an attribute a, which has an attribute b, which is a list, which we index at index 0, which is an element of type dictionary, which we index using the dictionary key ‘name’.
Note that dictionary keys cannot contain square brackets or single quotes (even if they are escaped).
- Parameters:
attr_name (
str) – Name of attribute to setval (
Any) – Value to set the attribute tocreate_new_ok (
bool) – If false (default), throw an error if the attribute does not exist. If true, creates a new attribute if the attribute name does not exist yet.bypass_callbacks (
bool) – If True, skipon_setattrcallbacks for all attribute operations in the path. If False (default), each attribute write runs the callbacks registered on that field, mirroring the behaviour of__setattr__during__init__.
- Returns:
Updated instance with new attribute value
- Return type:
Self
- aset_inplace(attr_name, val, create_new_ok=False, bypass_callbacks=False)[source]#
Sets an attribute of this dataclass in place by bypassing the frozen restriction via
object.__setattr__.Warning
This method is NOT functional and is potentially very unsafe. It mutates
selfdirectly, violating the immutability contract that JAX pytree registration relies on. Using it outside of__post_init__— or on an object that is already part of a JAX computation — can lead to silent correctness bugs, broken JAX caches, and undefined behaviour. Preferaset()for all normal use.The primary intended use case is setting derived or cached fields inside
__post_init__, before the instance is passed to JAX.Supports the same path syntax as
aset():"a->b->[0]->['key']".- Parameters:
attr_name (
str) – Path string (seeaset()for syntax).val (
Any) – Value to assign at the target location.create_new_ok (
bool) – If False (default), raise if the target attribute or dictionary key does not already exist. If True, allow setting new attributes or creating new dictionary keys.bypass_callbacks (
bool) – If True, skipon_setattrcallbacks. If False, fire the callbacks registered on the target field before writing, mirroring the behaviour ofaset()(default).
- Return type:
None
- property at: _AtProxy[Self]#
Returns an
_AtProxyfor JAX-style.at[].set()fluent updates.Example:
tree = tree.at["weights"].set(new_weights) tree = tree.at["layer"][0].set(new_layer) mask = jax.tree_map(lambda x: x > 0, tree) tree = tree.at[mask].set(0.0)