Skip to content

Creating module attributes with @nn.compact #936

Answered by jheek
matthias-wright asked this question in Q&A
Discussion options

You must be logged in to vote

TL;DR it was never safe to do this but now we actually raise an error.

The practical argument is that like all Jax/Flax APIs we want to avoid internal state so we typically freeze things after construction.

The theoretical argument is that in the case of Module we actually need the instance to be Frozen after setup otherwise we cannot guarantee correct behaviour in all cases. Basically, we need to clone Module instances when using transformations (vmap, jit) or Module.apply. Instead of trying to do a magical deep clone we reconstruct the Module from the construction arguments. This is clean and simple as long as there is no internal state like attribute assignment in methods.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@avital
Comment options

@marcvanzee
Comment options

@matthias-wright
Comment options

Answer selected by matthias-wright
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants