Declare linen submodule in dataclass-style?
See original GitHub issueHi all,
First of all, thank you for your work on flax. This is not an issue but a question.
As a pytorch user, sometimes I want to create a submodule, then pass it as an attribute to another module. This is quite common because people want to have flexibility about how to pick submodules and assemble them into the model. For instance:
dummy = Dummy()
bigdummy = BigDummy(dummy = dummy)
init_vars = bigdummy.init(rngkey, x)
the modules are defined as
class Dummy(nn.Module):
def setup(self):
self.dense = nn.Dense(4)
def __call__(self, x):
return self.dense(x)
class BigDummy(nn.Module):
dummy: Dummy
def setup(self):
self.proj = nn.Dense(6)
def __call__(self, x):
return self.proj(self.dummy(x))
However, this fails at bigdummy.init
for attribute access bigdummy.dummy
because it doesn’t run dummy.setup
. I guess this has to do with how flax
overrides __setattr__
. So I’m wondering is there any way that flax
supports this use case?
Thank you in advance!
Environment: flax 0.3
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
flax.linen package
Declares and returns a parameter in this Module. Parameters are read-only variables in the collection named “params”. See flax.core.variables for more ...
Read more >Git - Submodules - Git SCM
Let's start by adding an existing Git repository as a submodule of the repository that we're working on. To add a new submodule...
Read more >Concept behind declaring variables in each submodule ...
I was looking into the ways to pass parent module variable/locals to submodule. The purpose behind it to declare all the variables in...
Read more >Git submodule - Atlassian
A git submodule is a record within a host git repository that points to a specific commit in another external repository. Learn more...
Read more >Working with submodules - The GitHub Blog
You can add rock as a submodule of slingshot . In the slingshot repository: git submodule add https://github.com/<user>/rock rock.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@chris-tng @avital this has been a commonly cited pain-point, and I’ve tried to remedy it with PR #864 - I believe your example should work fine with this change. (For the record I don’t think this is actually so related to #686 - that’s more of an interactive use-case question of inspecting instantiated parameters, etc. whereas this is simply wanting to define modules made from simpler ones at the top-level following the common pytorch pattern.)
Let’s not close this issue just yet – I’d like for whoever works on #686 (probably me or @jheek) to keep this use-case in mind.
I agree this is suboptimal. It may be solvable separate from #686.