facebookresearch / hydra

Hydra is a framework for elegantly configuring complex applications
https://hydra.cc
MIT License
8.82k stars 635 forks source link

[Bug] instantiate fails targeting class defined in `__main__` when using `joblib` in sweep #2488

Open weiyaw opened 1 year ago

weiyaw commented 1 year ago

🐛 Bug

Description

hydra.utils.instantiate fails to instantiate an object when using joblib. For example, python main.py --multirun +x=1,2 works, but not python main.py --multirun hydra/launcher=basic +x=1,2.

Checklist

To reproduce

Minimal Code/Config snippet to reproduce

import hydra
from omegaconf import DictConfig, OmegaConf

class Dummy():
    def __init__(self, x):
        print(x)

@hydra.main(version_base=None)
def main(conf: DictConfig):
    dummy_int = hydra.utils.instantiate(conf, _target_=Dummy)

if __name__ == '__main__':
    main()

then run python main.py --multirun hydra/launcher=joblib +x=1,2

Stack trace/error message

[2022-12-03 18:45:24,789][HYDRA] Joblib.Parallel(n_jobs=-1,backend=loky,prefer=processes,require=None,verbose=0,timeout=None,pre_dispatch=2*n_jobs,batch_size=auto,temp_folder=None,max_nbytes=None,mmap_mode=r) is launching 2 jobs
[2022-12-03 18:45:24,789][HYDRA] Launching jobs, sweep output dir : multirun/2022-12-03/18-45-24
[2022-12-03 18:45:24,789][HYDRA]    #0 : +x=1
[2022-12-03 18:45:24,789][HYDRA]    #1 : +x=2
Error executing job with overrides: ['+x=1']
Traceback (most recent call last):
  File "/Users/kenyonn/Library/CloudStorage/OneDrive-Personal/research/phd/reinforce/sgcv/GD/dummy/main.py", line 16, in <module>
    main()
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/main.py", line 90, in decorated_main
    _run_hydra(
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/utils.py", line 389, in _run_hydra
    _run_app(
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/utils.py", line 460, in _run_app
    run_and_report(
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/utils.py", line 216, in run_and_report
    raise ex
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/utils.py", line 213, in run_and_report
    return func()
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/utils.py", line 461, in <lambda>
    lambda: hydra.multirun(
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/hydra.py", line 162, in multirun
    ret = sweeper.sweep(arguments=task_overrides)
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/_internal/core_plugins/basic_sweeper.py", line 182, in sweep
    _ = r.return_value
  File "/Users/kenyonn/miniconda3/envs/sgcv/lib/python3.10/site-packages/hydra/core/utils.py", line 260, in return_value
    raise self._return_value
hydra.errors.InstantiationException: Error locating target '__main__.Dummy', see chained exception above.

Expected Behavior

Run without complaining, and printing 1 and 2.

System information

Additional context

None

Jasha10 commented 1 year ago

Thanks for the bug report, @weiyaw. I am able to reproduce the failure on my own machine.

Jasha10 commented 1 year ago

A workaround is to put Dummy into another python module (any module except for the __main__ module):

# lib.py
class Dummy:
    def __init__(self, x):
        print(x)
# script.py
import hydra
from omegaconf import DictConfig, OmegaConf
from lib import Dummy

@hydra.main(version_base=None)
def main(conf: DictConfig):
    dummy_int = hydra.utils.instantiate(conf, _target_=Dummy)

if __name__ == "__main__":
    main()
$ python script.py -m hydra/launcher=joblib +x=1,2
[2022-12-05 19:44:28,572][HYDRA] Joblib.Parallel(n_jobs=-1,backend=loky,prefer=processes,require=None,verbose=0,timeout=None,pre_dispatch=2*n_jobs,batch_size=auto,temp_folder=None,max_nbytes=None,mmap_mode=r) is launching 2 jobs
[2022-12-05 19:44:28,572][HYDRA] Launching jobs, sweep output dir : multirun/2022-12-05/19-44-28
[2022-12-05 19:44:28,572][HYDRA]        #0 : +x=1
[2022-12-05 19:44:28,572][HYDRA]        #1 : +x=2
1
2
weiyaw commented 1 year ago

Thanks for the workaround @Jasha10