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

remove Plugins from Singleton get_state #1312

Merged
merged 9 commits into from
Jan 23, 2021

Conversation

jieru-hu
Copy link
Contributor

@jieru-hu jieru-hu commented Jan 20, 2021

Motivation

We do not need to pickle all of Singleton. change this to only pickle what is needed (JobRuntime and OmegaConf resolvers.)

This will also fix that the plugin integration tests fails if other hydra tools/plugins is isntalled locally as well.

Have you read the Contributing Guidelines on pull requests?

Yes/No

Test Plan

CI
locally

# install other plugin along with  ray launcher
FIX=1 PLUGINS=hydra_colorlog,hydra_ray_launcher  nox -s lint_plugins-3.8 test_plugins-3.8

Related Issues and PRs

(Is this PR part of a group of changes? Link the other relevant PRs and Issues here. Use https://help.github.com/en/articles/closing-issues-using-keywords for help on GitHub syntax)

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 20, 2021
@omry
Copy link
Collaborator

omry commented Jan 20, 2021

What I had in mind is that we:

  1. fix get_state() to not return the the Plugins singleton state.
  2. fix set_state() to call Plugins.instance() to (re)initialize the plugins.

@jieru-hu jieru-hu marked this pull request as draft January 20, 2021 16:54
@jieru-hu jieru-hu requested a review from omry January 20, 2021 19:02
@@ -45,6 +46,7 @@ def launch_jobs(temp_dir: str) -> None:
)
setup_globals()
Singleton.set_state(singleton_state)
Plugins.instance()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this inside set_state()?

@jieru-hu
Copy link
Contributor Author

What I had in mind is that we:

  1. fix get_state() to not return the the Plugins singleton state.
  2. fix set_state() to call Plugins.instance() to (re)initialize the plugins.

I see. This does seem better. I've fixed the PR to be along the same line of thoughts.

I'm not sure if you meant to actually change Singleton class. I've also tried that but ran into a circular imports issue. I didn't go further to make it work, since the issue I'm trying to fix here is still on only ray plugin level.

more details below,

diff --git a/hydra/core/singleton.py b/hydra/core/singleton.py
index d184a4e29..13bd5a9db 100644
--- a/hydra/core/singleton.py
+++ b/hydra/core/singleton.py
@@ -4,6 +4,8 @@ from typing import Any, Dict
 
 from omegaconf.basecontainer import BaseContainer
 
+from hydra.core.plugins import Plugins
+
 
 class Singleton(type):
     _instances: Dict[type, "Singleton"] = {}
@@ -18,6 +20,7 @@ class Singleton(type):
 
     @staticmethod
     def get_state() -> Any:
+        del Singleton.get_state().get("instances")[Plugins]
         return {
             "instances": Singleton._instances,
             "omegaconf_resolvers": deepcopy(BaseContainer._resolvers),
@@ -26,4 +29,5 @@ class Singleton(type):
     @staticmethod
     def set_state(state: Any) -> None:
         Singleton._instances = state["instances"]
+        Plugins.instance()
         BaseContainer._resolvers = deepcopy(state["omegaconf_resolvers"])



Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/__init__.py", line 5, in <module>
    from hydra import utils
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/utils.py", line 12, in <module>
    from hydra._internal.utils import (
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/_internal/utils.py", line 19, in <module>
    from hydra.core.utils import get_valid_filename, split_config_path
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/core/utils.py", line 17, in <module>
    from hydra.core.hydra_config import HydraConfig
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/core/hydra_config.py", line 6, in <module>
    from hydra.conf import HydraConf
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/conf/__init__.py", line 7, in <module>
    from hydra.core.config_store import ConfigStore
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/core/config_store.py", line 9, in <module>
    from hydra.core.singleton import Singleton
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/core/singleton.py", line 7, in <module>
    from hydra.core.plugins import Plugins
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/core/plugins.py", line 14, in <module>
    from hydra._internal.sources_registry import SourcesRegistry
  File "/Users/jieru/workspace/hydra-fork/hydra/hydra/_internal/sources_registry.py", line 4, in <module>
    from hydra.core.singleton import Singleton
ImportError: cannot import name 'Singleton' from partially initialized module 'hydra.core.singleton' (most likely due to a circular import) (/Users/jieru/workspace/hydra-fork/hydra/hydra/core/singleton.py)

@jieru-hu jieru-hu marked this pull request as ready for review January 20, 2021 19:07
@jieru-hu jieru-hu requested a review from omry January 20, 2021 19:07
@jieru-hu jieru-hu marked this pull request as draft January 20, 2021 19:17
@jieru-hu jieru-hu force-pushed the ray_pickle branch 2 times, most recently from dc6db48 to 2ebfea7 Compare January 20, 2021 19:38
@jieru-hu
Copy link
Contributor Author

per offline discussion - move imports to local :)

@jieru-hu jieru-hu marked this pull request as ready for review January 20, 2021 20:48
@jieru-hu jieru-hu changed the title remove pickling Singleton remove Plugins from Singleton get_state Jan 20, 2021
hydra/core/singleton.py Outdated Show resolved Hide resolved
@jieru-hu
Copy link
Contributor Author

You are changing the actual singleton state, don't do that.
Make a copy of the map without the Plugins singleton.
The only reason this did not cause issues is that plugin interaction is currently only happening on startup, but this can change.

I see. Thanks! updated the PR per your comment, also added a unit test.

@jieru-hu
Copy link
Contributor Author

rebase...

Comment on lines 22 to 27
try:
from hydra.core.plugins import Plugins

del instances[Plugins]
except KeyError:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try:

 instances.pop(Plugins, None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!

hydra/core/singleton.py Show resolved Hide resolved
hydra/core/singleton.py Outdated Show resolved Hide resolved
Comment on lines 112 to 114
chdir_hydra_root()
plugin = "hydra_ray_launcher"
os.chdir(Path("plugins") / plugin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably use monkeypatch to change the directory and restore automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know monkeypatch could do that, I thought we usually do fixture?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks!

hydra/core/singleton.py Outdated Show resolved Hide resolved
@jieru-hu jieru-hu requested a review from omry January 22, 2021 23:58
Copy link
Collaborator

@omry omry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge once CI passes.

@jieru-hu jieru-hu merged commit f664c5e into facebookresearch:master Jan 23, 2021
@jieru-hu jieru-hu deleted the ray_pickle branch January 23, 2021 00:38
jieru-hu added a commit to jieru-hu/hydra that referenced this pull request Jan 23, 2021
jieru-hu added a commit to jieru-hu/hydra that referenced this pull request Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants