Skip to content

Unexpected behavior of .save_hyperparameters() for same parameter name #13181

@serena-ruan

Description

@serena-ruan

🐛 Bug

When a pytorch_lightning model is created as a parameter for a class, and they contain parameters with same name, the original class's value overrides the model's value when calling .save_hyperparameters(). Besides, it also saves all parameters for the wrapper class.

To Reproduce

I created a BoringParent class (not actually a parent) that contains a param named same_arg with a default value parent, and inside the init function I created a Boring_saving_hprams object that also contains a param named same_arg and I call save_hyperparameters to save the params for this pytorch_lightning module. However, the value of same_arg is overriden by the default BoringParent class's value.
Please see the colab notebook for details.
https://colab.research.google.com/drive/1De82M7GEzgtc18UTSRHJOV_nNnIJrji6?usp=sharing

Expected behavior

I'm expecting the model should save its own value even when there're param name conflicts. And in this case the class BoringParent is not actually the parent, it's just the object holding this model, we shouldn't iterate over its parameters. I also just want it to save the parameters for this pytorch_lightning modle only, not parameters from the class that holds this model.

Environment

  • CUDA:
    • GPU:
      • Tesla T4
    • available: True
    • version: 11.3
  • Packages:
    • numpy: 1.21.6
    • pyTorch_debug: False
    • pyTorch_version: 1.11.0+cu113
    • pytorch-lightning: 1.6.3
    • tqdm: 4.64.0
  • System:
    • OS: Linux
    • architecture:
      • 64bit
    • processor: x86_64
    • python: 3.7.13
    • version: Proposal for help #1 SMP Sun Apr 24 10:03:06 PDT 2022

Additional context

Inside the save_hyperparameters function, it's updating the init_args here so the later args just override the previous one:
image
And inside collect_init_args it's recursively checking the params passed in, which is not the correct order for my case. I think that we could check whether there's a real inheritance relationship between classes and if not we don't add the param inside the path_args.

Also let me know if you want me to draft a PR. Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglightningmodulepl.LightningModule

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions