-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use torch.jit.trace
to trace LightningModule
in Lightning v1.7
#14036
Comments
torch.jit.trace
to trace LightningModule in Lightning v1.7torch.jit.trace
to trace LightningModule
in Lightning v1.7
Hi! Unfortunately, this is caused by a bug in PyTorch where properties are not correctly ignored: pytorch/pytorch#67146 As a workaround, you can use |
Thank you for your reply, |
Hi @carmocca, I am getting the same error when using FYI, I am using pytorch-lightning version 1.7.3 |
@Animesh081005 can you open an issue with a reproducible script? |
UPDATE: Solved in
|
I am still having this issue. I am using Lightning Flash ObjectDetector with YOLOv5 backbone and neither script or trace the model works. The error says
|
I am also still seeing this error. On the most recent versions. |
Can you share the full error stacktrace? |
I am also having same issue with Error:
|
You will need to torchscript the model first |
hi @carmocca Duplicated in #16157 |
This issue is still not solved. Is a fix to be expected soon? |
@laclouis5 No, and most likely never since PyTorch no longer works on TorchScript since the release of |
can we just create a dummy object and attach it to the lightningmodule? @carmocca |
My workaround is give it a dummy trainer |
Hi, have you solved this problem? I encounter the same problem when I tried to load a lightning model. |
Hi, have you solve this problem? I encountered the same problem when I saved the trained lightning model. |
add a dummy trainer addressed my issue. According to this post #17517 (comment) |
🐛 Bug
When I use
torch.jit.trace
to trace aLightningModule
,I got
RuntimeError: XXX(LightningModule class name) is not attached to a Trainer.
This because in lightning 1.7.0, property
trainer
will raise anRuntimeError
if the module doesn't attach aTrainer
.https://github.com/Lightning-AI/lightning/blob/12a061f2aaefaa9ed9ccf81ab6f378835b675a7e/src/pytorch_lightning/core/module.py#L179
but in
torch.jit
, it will justify each attr byhasattr
,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 bytorch.jit.trace
without attach trainer to the lightning module.To Fix
Replace
RuntimeError
byAttributeError
.This fix is work for me, but I don't know will this cause other problems.
Environment
cc @carmocca @justusschock @awaelchli @Borda @ananthsub @ninginthecloud @jjenniferdai @rohitgr7
The text was updated successfully, but these errors were encountered: