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

Custom pytree strange leakage behavior #9283

Closed
eelregit opened this issue Jan 21, 2022 · 3 comments
Closed

Custom pytree strange leakage behavior #9283

eelregit opened this issue Jan 21, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@eelregit
Copy link
Contributor

I found that a custom pytree (of standard mutable pytree like list/dict)
behave strangely in the following example, where the treedef changes inside a function:

from jax.tree_util import register_pytree_node_class

@register_pytree_node_class
class C:
    def __init__(self, l=[]):
        self.l = l

    def tree_flatten(self):
        return self.l, None

    @classmethod
    def tree_unflatten(cls, aux_data, children):
        return cls(children)
    
def f(c):
    c.l.append(0)
    return c

print(vars(C()))
print(vars(C()), vars(f(C())))

returns

{'l': []}
{'l': [0]} {'l': [0]}

The two C() return different outputs and the second one seem to be polluted by the f(C(0)) call.

@eelregit eelregit added the bug Something isn't working label Jan 21, 2022
@eelregit
Copy link
Contributor Author

Another example is DifferentiableUniverseInitiative/jax_cosmo#81 (comment)
where treedef doesn't change but aux_data leak among instances.

@hawkinsp
Copy link
Collaborator

Isn't the problem here that the default value you are using in your __init__ is mutable? That's a known Python antipattern.

Try something like:

    def __init__(self, l=None):
        self.l = [] if l is None else l

@eelregit
Copy link
Contributor Author

Thanks @hawkinsp! That explains it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants