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.

Remove unwrapping logic in strategies in favor of a direct reference to the original module

See original GitHub issue

Proposed refactor

I propose to remove the fragile unwrapping logic in strategies in favor of a keeping a direct reference to the original module.

Motivation

We currently have an unwrapping logic in our strategies that looks something like this:

def unwrap_lightning_module(wrapped_model: nn.Module) -> "pl.LightningModule":
    model = wrapped_model
    if isinstance(model, (DistributedDataParallel, DataParallel)):
        model = unwrap_lightning_module(model.module)
    if isinstance(model, (_LightningModuleWrapperBase, _LightningPrecisionModuleWrapperBase)):
        model = unwrap_lightning_module(model.module)
    ...

On the strategy, we only store a reference self.model to the wrapped model, and we additionally have a property

@property
def lightning_module(self):
    return unwrap_lightning_module(self.model)

That can return the original LightningModule. The basic unwrapping logic has been around since PL had support for DP and DDP. It makes little sense to have a function like this, because the user always gives us the pure LightningModule as input to Trainer.fit. For some reason we kept it and kept extending it, leading to a copy-paste every time a new strategy gets introduced. It also raises a few questions for users who want to build their own strategy. Overall, it’s an unintuitive solution that seems overly complicated when one could just keep the original reference saved in addition to the wrapped model.

In LightningLite, we recently refactored our _LiteModel wrapper to do exactly this, keeping both the original and wrapped nn Module in order to provide pass-through access to attributes (#12597).

Pitch

  1. Remove all unwrap_lightning_module functions
  2. Modify Strategy.connect() to just save a reference to the given model.
  3. Make Strategy.lightning_module directly return this reference instead of calling the unwrap method(s)

There are no breaking changes. The result should be equivalent, but simpler, easier to test and thus less fragile.

Additional context

We wanted to do this for a while, came on my radar again after seeing a contributor struggle with it in #13501.


If you enjoy Lightning, check out our other projects! ⚡

  • Metrics: Machine learning metrics for distributed, scalable PyTorch applications.

  • Lite: enables pure PyTorch users to scale their existing code on any kind of device while retaining full control over their own loops and optimization logic.

  • Flash: The fastest way to get a Lightning baseline! A collection of tasks for fast prototyping, baselining, fine-tuning, and solving problems with deep learning.

  • Bolts: Pretrained SOTA Deep Learning models, callbacks, and more for research and production with PyTorch Lightning and PyTorch.

  • Lightning Transformers: Flexible interface for high-performance research using SOTA Transformers leveraging Pytorch Lightning, Transformers, and Hydra.

cc @justusschock @awaelchli @rohitgr7 @akihironitta

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jstjohncommented, Jul 2, 2022

I like simple 😃 should I close my PR and let you do this?

1reaction
carmoccacommented, Jul 14, 2022

Great proposal! This should also simplify typing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove unwrapping logic in strategies in favor of a direct reference ...
I propose to remove the fragile unwrapping logic in strategies in favor of a keeping a direct reference to the original module.
Read more >
Upgrading to Spring Framework 5.x - GitHub
This page provides guidance on upgrading to Spring Framework 5.0, 5.1, 5.2, and 5.3. See also the Spring-Framework-5-FAQ and What's New in ...
Read more >
Changelog — PyTorch Lightning 1.8.5 documentation
Replaced the unwrapping logic in strategies with direct access to ... the module wrapper now gets passed through to the original module reference...
Read more >
Reactor 3 Reference Guide
Reactor is a fully non-blocking reactive programming foundation for the JVM, with efficient demand management (in the form of managing “ ...
Read more >
Spring Data JDBC - Reference Documentation
Spring Data JDBC includes direct support for the following databases: ... However, Spring Data can then no longer determine a unique module ......
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