question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declare linen submodule in dataclass-style?

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
levskayacommented, Jan 18, 2021

@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.)

1reaction
avitalcommented, Jan 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found