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

Eagerly bind submodules #3077

Merged
merged 1 commit into from
Jun 14, 2023
Merged

Eagerly bind submodules #3077

merged 1 commit into from
Jun 14, 2023

Conversation

cgarciae
Copy link
Collaborator

@cgarciae cgarciae commented May 4, 2023

What does this PR do?

Alternative to #2901. Fixes #2864.

  • Binds submodules during __post_init__ if a scope is available using _register_submodule.
  • Add a _deep_clone flag to Module.clone such that it correctly clones external submodules from dataclass fields when entering a new context (e.g. init / apply / bind) while preserving shared identities.

@codecov-commenter
Copy link

codecov-commenter commented May 9, 2023

Codecov Report

Merging #3077 (8f122f6) into main (df66c81) will increase coverage by 0.12%.
The diff coverage is 96.55%.

@@            Coverage Diff             @@
##             main    #3077      +/-   ##
==========================================
+ Coverage   81.94%   82.07%   +0.12%     
==========================================
  Files          55       55              
  Lines        6037     6058      +21     
==========================================
+ Hits         4947     4972      +25     
+ Misses       1090     1086       -4     
Impacted Files Coverage Δ
flax/linen/module.py 92.81% <96.55%> (+0.78%) ⬆️

@cgarciae cgarciae force-pushed the eager-bind-submodules branch 3 times, most recently from 0d554d0 to 0a2ae27 Compare May 19, 2023 21:32
for field in dataclasses.fields(self):
if (
field.name not in ('parent', 'name')
and field.name in self.__dict__ # ignore fields that have not been set
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not and hasattr(self, field.name) ?

and field.name in self.__dict__ # ignore fields that have not been set
):
value = getattr(self, field.name)
if _tree_has_modules(value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me again why we're doing _tree_has_modules when _register_submodules implements a recursion over submodules? is that missing something?

@levskaya
Copy link
Collaborator

also the test test_repr_should_not_cause_setup has a too-brittle testing strategy for setup() being called that's breaking with the updated PR

@cgarciae cgarciae force-pushed the eager-bind-submodules branch 3 times, most recently from f9becd3 to 0bd31b6 Compare May 25, 2023 22:03
Copy link
Collaborator

@levskaya levskaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks so much for the slog, lol.

@kvablack
Copy link

Original author of #2864 here, just wanted to pop in and say thanks so much @cgarciae! I've actually been following your series of PRs for the past few months lol, and I can't wait for this to get merged and released so I can finally implement my design that's been blocked by #2864 this whole time. It will simplify huge swaths of code 😌

@copybara-service copybara-service bot merged commit 7b7b8bb into main Jun 14, 2023
@copybara-service copybara-service bot deleted the eager-bind-submodules branch June 14, 2023 02:05
@levskaya
Copy link
Collaborator

@kvablack - so very sorry that this took so long to merge - this is a subtle change that required checking a lot of internal code to make sure that we didn't break anyone. Everything seems good so far. Hopefully I won't be forced to roll back anything.

@cgarciae extra thanks for helping plow through this rather annoyingly intricate problem.

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.

Unexpected ScopeParamNotFoundError when sharing submodules
4 participants