Cannot use `torch.jit.trace` to trace `LightningModule` in Lightning v1.7

See original GitHub issue

πŸ› Bug

When I use torch.jit.trace to trace a LightningModule, I got RuntimeError: XXX(LightningModule class name) is not attached to a Trainer.

This because in lightning 1.7.0, property trainer will raise an RuntimeError if the module doesn’t attach a Trainer.

https://github.com/Lightning-AI/lightning/blob/12a061f2aaefaa9ed9ccf81ab6f378835b675a7e/src/pytorch_lightning/core/module.py#L179

but in torch.jit, it will justify each attr by hasattr,

https://github.com/pytorch/pytorch/blob/de0e03001d31523ef86c3d7852c87cdad6d96632/torch/_jit_internal.py#L749

and in hasattr docstring, Return whether the object has an attribute with the given name. This is done by calling getattr(obj, name) and catching AttributeError.

To Reproduce

Initialize any LightningModule under Lightning v1.7.0, and trace it by torch.jit.trace without attach trainer to the lightning module.

To Fix

Replace RuntimeError by AttributeError.

This fix is work for me, but I don’t know will this cause other problems.

Environment

  • Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow): LightningModule
  • PyTorch Lightning Version (e.g., 1.5.0): 1.7.0
  • PyTorch Version (e.g., 1.10): 1.10
  • Python version (e.g., 3.9): 3.7.12

cc @carmocca @justusschock @awaelchli @borda @ananthsub @ninginthecloud @jjenniferdai @rohitgr7

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
kyoungrok0517commented, Sep 26, 2022

UPDATE: Solved in 1.7.7

The same happens with me.

2reactions
Stack-Attackcommented, Nov 24, 2022

I am also still seeing this error. On the most recent versions. pytorch-lightning 1.8.3.post0 pytorch 1.13.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

LightningModule - PyTorch Lightning - Read the Docs
Thus, to use Lightning, you just need to organize your code which takes about 30 ... will be passed to the torch.jit.script() or...
Read more >
torch.jit.trace β€” PyTorch 1.13 documentation
Trace a function and return an executable or ScriptFunction that will be optimized using just-in-time compilation. Tracing is ideal for code that operates...
Read more >
TorchScript β€” pytorch-transformers 1.0.0 documentation
Pytorch's two modules JIT and TRACE allow the developer to export their model to be re-used in other programs, such as efficiency-oriented C++...
Read more >
Torchscripted Pytorch Lightning Module Fails to Load
I'm attempting to launch a triton server instance with a torchscripted module. The module was trained with assistance from pytorch lightning ...
Read more >
Converting Variational Models to TorchScript
In general the process is quite similar to standard torch models, where we will trace them using torch.jit.trace . However there are two...
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