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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] return configuration variable from hydra.main function #407

Closed
noklam opened this issue Feb 11, 2020 · 7 comments
Closed

[Bug] return configuration variable from hydra.main function #407

noklam opened this issue Feb 11, 2020 · 7 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@noklam
Copy link

noklam commented Feb 11, 2020

馃悰 Hydra return config variable

I try to return the config from a function but get a None return instead. I am trying to keep the config as a global variable for other modules to import the config

To reproduce

import hydra
from omegaconf import DictConfig

from runner import Runner


@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
    # print(cfg.pretty())
    return cfg



if __name__ == "__main__":
    cfg = my_app()
    print(cfg)

** Minimal Code/Config snippet to reproduce **

** Stack trace/error message **

None
// Paste the bad output here!

Expected Behavior

System information

Additional context

Add any other context about the problem here.

@noklam noklam added the bug Something isn't working label Feb 11, 2020
@omry omry changed the title [Bug] [Bug] return configuration variable from hydra.main function Feb 11, 2020
@omry
Copy link
Collaborator

omry commented Feb 11, 2020

Hi,
This is not supported.
Your app logic should be inside my_app().

If you just want to generate a config object you can use the compose API, but this will not provide command line integration, automatic working directory, logging support and other things.

@omry omry closed this as completed Feb 11, 2020
@omry omry added the wontfix This will not be worked on label Feb 11, 2020
@noklam
Copy link
Author

noklam commented Feb 11, 2020

So if I have modules that rely on this global config, how would I do that by wrapping all the logic inside the main()? I can't quite figure out how I should use this for more complicated experiments setting, I couldn't just write all the parameters inside a big main() or passing the config as an argument everytime I need it.

@omry
Copy link
Collaborator

omry commented Feb 11, 2020

I recommend that you do pass the config around (or parts of it).
however, you are free to store is as a global and use it without passing it around (it's just not good practice).

@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
  global config
  config = cfg
  # call the rest of your logic from here.    

if __name__ == "__main__":
    my_app()

As I said, I do recommend that instead of doing this you pass the config or a part of it forward. for example:

def train(cfg):
  ...

def test(cfg):
  ...

@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
  if cfg.action == "train":
    train(cfg)
  elif cfg.action == "test":
    test(cfg)
  
if __name__ == "__main__":
  my_app()

@noklam
Copy link
Author

noklam commented Feb 11, 2020

Thank you! That helps.

@krishnachaitanya7
Copy link

krishnachaitanya7 commented Dec 13, 2020

I encountered this thread because some of the debugger frames were getting lost when there is an error in the function encapsulated by the Hydra decorator. Hence I was trying to get the config in the same way as @noklam did.

It turned out it's already solved in this PR #930

To not lose any debugger stack frames, set HYDRA_FULL_ERROR to 1. That should do the trick.

I hope that helps!

@omry
Copy link
Collaborator

omry commented Dec 13, 2020

@krishnachaitanya7, I recommend that you always use the latest stable Hydra version :).

@krishnachaitanya7
Copy link

@omry definitely! Thank you for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants