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.
but in torch.jit, it will justify each attr by hasattr,
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:
- Created a year ago
- Comments:10 (4 by maintainers)
Top Related StackOverflow Question
UPDATE: Solved in
1.7.7I am also still seeing this error. On the most recent versions. pytorch-lightning 1.8.3.post0 pytorch 1.13.0