Skip to content

Add __getattribute__ with lazy setup trigger. #2028

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

Merged
merged 1 commit into from
Apr 6, 2022

Conversation

levskaya
Copy link
Collaborator

@levskaya levskaya commented Apr 6, 2022

Currently there's a dumb difference in behavior when accessing submodules defined in setup() vs submodules passed in as dataclass attributes - the former will work in more situations as getattr triggers setup but the latter won't as getattribute does not currently trigger setup. This PR fixes that discrepancy.

@levskaya levskaya requested a review from jheek April 6, 2022 00:15
@codecov-commenter
Copy link

Codecov Report

Merging #2028 (aca3e29) into main (0c647cb) will increase coverage by 0.10%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #2028      +/-   ##
==========================================
+ Coverage   74.86%   74.97%   +0.10%     
==========================================
  Files          59       59              
  Lines        5037     5042       +5     
==========================================
+ Hits         3771     3780       +9     
+ Misses       1266     1262       -4     
Impacted Files Coverage Δ
flax/linen/module.py 94.85% <100.00%> (+0.85%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c647cb...aca3e29. Read the comment docs.

@levskaya
Copy link
Collaborator Author

levskaya commented Apr 6, 2022

One of the main applications of this that users ask for is being able to apply deeply nested submodules like so:

y = nn.apply(lambda top_module, x: top_module.a.b.c.some_method(x), foo)(variables, x)

where a,b,c might be dataclass attributes OR setup defined submodules. This is a lot nicer than having to hack around the variable tree.

@copybara-service copybara-service bot merged commit dba3fa5 into google:main Apr 6, 2022
@marcvanzee
Copy link
Collaborator

marcvanzee commented Apr 11, 2022

@levskaya this is a nice change and it would be useful to add this to the CHANGELOG.md. Would you mind doing this?

@levskaya
Copy link
Collaborator Author

@marcvanzee - I actually had to roll this back in #2034 because it was breaking some user code. It might take me longer to land this.

@marcvanzee
Copy link
Collaborator

marcvanzee commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants