Skip to content
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

List of Callbacks Raises ValueError("Expected a parent") #17485

Closed
amirkhas opened this issue Apr 26, 2023 · 10 comments · Fixed by #17548
Closed

List of Callbacks Raises ValueError("Expected a parent") #17485

amirkhas opened this issue Apr 26, 2023 · 10 comments · Fixed by #17548
Labels
bug Something isn't working callback ver: 2.0.x

Comments

@amirkhas
Copy link

amirkhas commented Apr 26, 2023

Bug description

If the callbacks list, passing to the trainer, has more than one element, it raises the error ValueError("Expected a parent").
If you pass any of those elements alone the code runs fine.

p.s. I'm already importing like this: import lightning.pytorch as pl which is version >=2. specific.

What version are you seeing the problem on?

2.0+

How to reproduce the bug

No response

Error messages and logs

# Error messages and logs here please

ValueError: Expected a parent

Environment

Current environment
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0):
#- Lightning App Version (e.g., 0.5.2):
#- PyTorch Version (e.g., 2.0):
#- Python version (e.g., 3.9):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):
#- Running environment of LightningApp (e.g. local, cloud):

More info

No response

cc @awaelchli

@amirkhas amirkhas added bug Something isn't working needs triage Waiting to be triaged by maintainers labels Apr 26, 2023
@carmocca
Copy link
Contributor

carmocca commented Apr 26, 2023

Can you share your code?

I've seen this happen when lightning.pytorch and pytorch_lightning imports are mixed together. You need to use only one of them. This includes the callback parent

import lightning.pytorch as lp
import pytorch_lightning as pl

class MyCallback(pl.Callback):
    ...

trainer = lp.Trainer(callbacks=MyCallback())  # bad!

@carmocca carmocca added waiting on author Waiting on user action, correction, or update callback and removed needs triage Waiting to be triaged by maintainers labels Apr 26, 2023
@turian
Copy link
Contributor

turian commented Apr 28, 2023

@carmocca I had this bug and your suggestion fixed it

@awaelchli
Copy link
Contributor

@carmocca Do you think we could inspect sys.modules and detect whether there are both pytorch_lightning and lightning loaded, and warn the user? Not sure whether that would help.

@turian
Copy link
Contributor

turian commented Apr 28, 2023

@awaelchli I mean, better warnings are always great.

@mimbres
Copy link

mimbres commented Apr 30, 2023

#17485 (comment)

Can you share your code?

I've seen this happen when lightning.pytorch and pytorch_lightning imports are mixed together. You need to use only one of them. This includes the callback parent

import lightning.pytorch as lp
import pytorch_lightning as pl

class MyCallback(pl.Callback):
    ...

trainer = lp.Trainer(callbacks=MyCallback())  # bad!

this also solved a similar bug for learning rate monitor callbacks

@carmocca
Copy link
Contributor

carmocca commented May 3, 2023

@carmocca Do you think we could inspect sys.modules and detect whether there are both pytorch_lightning and lightning loaded, and warn the user? Not sure whether that would help.

I feel like that's too strong of a check. Imagine you are in a team of data scientist who share utility python packages and two packages use lightning, each with a type of import. This check would break their code just on import even if they don't mix the classes. Fixing it might also be out of the control of the person who just tried to import it.

I think it would be simpler that we add a specific check for this in is_overridden and _maybe_unwrap_optimized

@Borda Borda changed the title List of Callbacks Raises ValueError("Expected a parent") List of Callbacks Raises ValueError("Expected a parent") May 3, 2023
@amirkhas
Copy link
Author

amirkhas commented May 3, 2023

Can you share your code?

I've seen this happen when lightning.pytorch and pytorch_lightning imports are mixed together. You need to use only one of them. This includes the callback parent

import lightning.pytorch as lp
import pytorch_lightning as pl

class MyCallback(pl.Callback):
    ...

trainer = lp.Trainer(callbacks=MyCallback())  # bad!

That solved my error. Thanks!

@amirkhas amirkhas closed this as completed May 3, 2023
@awaelchli awaelchli removed the waiting on author Waiting on user action, correction, or update label May 4, 2023
@davidgilbertson
Copy link
Contributor

I'm seeing this error with the Optuna PyTorchLightningPruningCallback. They extend pytorch_lightning.callbacks.callback.Callback and I can't control that, but I'm using the new lightning package and don't want to change that.

My rather hacky workaround is this:

import lightning
import optuna

class PatchedCallback(
    optuna.integration.PyTorchLightningPruningCallback, lightning.Callback
):
    pass

I think since there's going to be two lightnings floating around for years, instance checks should be updated to be if isinstance(obj, (old_pl.Callback, new_pl.Callback)), before eventually being reverted after a suitable deprecation period.

Also, will the fix #17548 just mean that I get an error if I use a package that is still on old Lightning? Or is it assuming direct use of the class and it doesn't handle the inheritance case?

@lez-s
Copy link

lez-s commented Jul 24, 2023

could you please fix the error msg for this? The error message is useless. in my case, it raised TypeError: __init__() got an unexpected keyword argument ‘gpus’ after the ValueError("Expected a parent"), but I didn't even pass gpus args. And it took so long to find this thread.

Haneol-Kijm added a commit to boostcampaitech7/level1-imageclassification-cv-21 that referenced this issue Sep 15, 2024
Haneol-Kijm added a commit to boostcampaitech7/level1-imageclassification-cv-21 that referenced this issue Sep 15, 2024
* ♻️ Refactor overall redundant codes

* 🎨 Add config factory functionality

* ✏️ Remove minor comments

* 🐛 Fixed val_loss error and add error raising code when gpu is not detected

* 🎨 Fix param_space to have more elegant representation

* 🎨 Improve the pbt config

* ♻️ Rename pytorch_lightning package as lightning

* 📝 Update the precise guide

* 🐛 Fix bug with moduel import

* 🙈 Add .gitignore file

* ✏️ Fix argparese argument to have dash

* ♻️ Fixed the order of import

* 🎨 Fix the old function of LightningModule

* ♻️ Refactor the code and make loading checkpoint clearer

* ♻️ Change the order of arguments for clarity

* 🐛 Add a default model case

* ✏️ Fix minor typo

* ✏️ Fix typo

* 🙈 Update to ignore lightning_logs

* 🐛 Make dataset to read both train csv and test csv

* 🚑 Make a quick fix for saving prediction csv file

* 🎨 Add PredictionCallback

* ✏️ Fix typo

* ✏️ Fix typo

* 🐛 Try to fix callback error

* 🐛 Fix Expected a parent bug, see [the solution](Lightning-AI/pytorch-lightning#17485 (comment))

* 🐛 Move prediction feature from trainer to callback

* 🙈 Update git not to read output csv file

* ⬆️ Rewrite the requirements.yml
@nathanpainchaud
Copy link
Contributor

I'm posting this here for visibility, since this seems to be the issue that gets picked up by search engines when looking for ValueError("Expected a parent") errors with Lightning.

The check for mixed imports implemented by @carmocca fails to detect and warn users in at least some configurations of old/new imports. For a minimal example that still produces the ValueError("Expected a parent"), see this issue I've raised with PyTorch Geometric.

To fix the issue on their end I've proposed that they update their way of importing Lightning to use the new style. However, the uninformative error message made it difficult to troubleshoot the root cause of the error. I thought it was relevant to bring this up to the attention of the Lightning devs, to see if you could maybe improve the detection of mixed import styles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working callback ver: 2.0.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants