Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a new unflatten for chex.dataclass that avoids __init__ while keeping the (un)flattened order unchanged. #222

Merged
merged 1 commit into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion chex/_src/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ def _init(self, *args, **kwargs):
return dcls


def _dataclass_unflatten(dcls, keys, values):
dcls_object = dcls.__new__(dcls)
attribute_dict = dict(zip(keys, values))
# Looping over fields instead of keys & values preserves the field order.
for field in dataclasses.fields(dcls):
object.__setattr__(dcls_object, field.name, attribute_dict[field.name])
return dcls_object


def register_dataclass_type_with_jax_tree_util(data_class):
"""Register an existing dataclass so JAX knows how to handle it.

Expand All @@ -242,7 +251,7 @@ def register_dataclass_type_with_jax_tree_util(data_class):
in instance.__dict__.
"""
flatten = lambda d: jax.util.unzip2(sorted(d.__dict__.items()))[::-1]
unflatten = lambda keys, values: data_class(**dict(zip(keys, values)))
unflatten = functools.partial(_dataclass_unflatten, data_class)
try:
jax.tree_util.register_pytree_node(
nodetype=data_class, flatten_func=flatten, unflatten_func=unflatten)
Expand Down