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.

Consider changing the semantics of `Module.setup()` to be called immediate during init rather than when a module is bound

See original GitHub issue

Right now, we tell users that setup acts like __init__, but this isn’t completely true as setup is only called when a Module is bound – that is – when it has variables. Therefore, the following code doesn’t work:

class ResNet(nn.Module):
  def setup(self):
    self.backbone = Backbone()
    self.classifier = Classifier()

ResNet().backbone  # not available

A workaround is to expose the backbone as a method, e.g.

class ResNet(nn.Module):
  def make_backbone():
    return Backbone()

  def setup(self):
    self.backbone = self.make_backbone()
    self.classifier = self.make_classifier()

ResNet().make_backbone()

But this is quite confusing.

The proposal here, in short, is to make setup actually fire immediately during module construction, but defer variable from being actually present until the module is bound. This means that accessing the value of parameters defined during setup would throw an error.

There are some open design questions about this proposal, but I think it would simplify transfer learning use-cases, as well as simplifying the mental model around setup.

@jheek and I have discussed this, and we may consider investigating this soon. I believe the change should mostly not impact user code, though in some cases modules that define variables during setup() and access their value may have to be thought about more carefully.

cc @rolandgvc @andsteing

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
levskayacommented, Oct 28, 2021

Actually, historically we went in the opposite direction of calling setup() lazily. We were forced to do this in order to avoid exponential-time blowup via double-recursion which occurs with any eager initialization scheme in the presence of nested transformations (which currently frequently occur when for instance using JAX’s named_call machinery to label profiling traces). As such, I’m closing this old issue.

@shashank2000 - I actually have no idea what that sentence is even talking about - I’ll have to figure out what the author meant by that and remove or redact that statement. It would be better to open a new issue if you have a specific question about initialization or writing a model.

0reactions
shashank2000commented, Oct 28, 2021

Does this issue have anything to do with the initialization of the “last” submodule like here? (The line right above the header linked to)

I’m not sure how to approach this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

2. Building and Running Modules - Linux Device Drivers, 3rd ...
This book deals in kernel modules rather than programs; so, ... in order to serve future requests, and its initialization function terminates immediately....
Read more >
Module — PyTorch 1.13 documentation
Applies fn recursively to every submodule (as returned by .children() ) as well as self. Typical use includes initializing the parameters of a...
Read more >
5. The import system — Python 3.11.1 documentation
Python code in one module gains access to the code in another module by the process of importing it. The import statement is...
Read more >
Programming ILE Concepts - IBM
After being examined, modules are bound, in the order listed, into the program being created. 2. All of the service programs on the...
Read more >
The Linux Kernel Module Programming Guide
Kernel modules must have at least two functions: a "start" (initialization) function called init_module() which is called when the module is insmoded into ......
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