-
Notifications
You must be signed in to change notification settings - Fork 133
[Feature] Enable loading Callbacks from hydra configs
#245
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
Open
Xmaster6y
wants to merge
2
commits into
facebookresearch:main
Choose a base branch
from
Xmaster6y:hydra-callbacks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| from .common import CallbackConfig | ||
|
|
||
| __all__ = [ | ||
| "CallbackConfig", | ||
| ] | ||
|
|
||
| callback_config_registry = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| import pathlib | ||
|
|
||
| from abc import abstractmethod | ||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional, Type | ||
|
|
||
| from benchmarl.experiment import Callback | ||
| from benchmarl.utils import _read_yaml_config | ||
|
|
||
|
|
||
| @dataclass | ||
| class CallbackConfig: | ||
| """ | ||
| Dataclass representing a callback configuration. | ||
| This should be overridden by implemented callbacks. | ||
| Implementors should: | ||
|
|
||
| 1. add configuration parameters for their callback | ||
| 2. implement all abstract methods | ||
|
|
||
| """ | ||
|
|
||
| def get_callback(self) -> Callback: | ||
| """ | ||
| Main function to turn the config into the associated callback | ||
|
|
||
| Returns: the Callback | ||
|
|
||
| """ | ||
| return self.associated_class()( | ||
| **self.__dict__, # Passes all the custom config parameters | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _load_from_yaml(name: str) -> Dict[str, Any]: | ||
| yaml_path = ( | ||
| pathlib.Path(__file__).parent.parent | ||
| / "conf" | ||
| / "callbacks" | ||
| / f"{name.lower()}.yaml" | ||
| ) | ||
| return _read_yaml_config(str(yaml_path.resolve())) | ||
|
|
||
| @classmethod | ||
| def get_from_yaml(cls, path: Optional[str] = None): | ||
| """ | ||
| Load the callback configuration from yaml | ||
|
|
||
| Args: | ||
| path (str, optional): The full path of the yaml file to load from. | ||
| If None, it will default to | ||
| ``benchmarl/conf/callbacks/self.associated_class().__name__`` | ||
|
Xmaster6y marked this conversation as resolved.
|
||
|
|
||
| Returns: the loaded CallbackConfig | ||
| """ | ||
|
|
||
| if path is None: | ||
| config = CallbackConfig._load_from_yaml( | ||
| name=cls.associated_class().__name__ | ||
| ) | ||
|
|
||
| else: | ||
| config = _read_yaml_config(path) | ||
| return cls(**config) | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def associated_class() -> Type[Callback]: | ||
| """ | ||
| The callback class associated to the config | ||
| """ | ||
| raise NotImplementedError | ||
|
Xmaster6y marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # | ||
| # This source code is licensed under the license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
| # | ||
|
|
||
| import pytest | ||
|
|
||
| from benchmarl.callbacks import callback_config_registry | ||
| from benchmarl.hydra_config import load_callbacks_from_hydra | ||
|
|
||
| from hydra import compose, initialize | ||
|
|
||
|
|
||
| def test_no_callbacks(): | ||
| with initialize(version_base=None, config_path="../benchmarl/conf"): | ||
| cfg = compose( | ||
| config_name="config", | ||
| overrides=[ | ||
| "algorithm=mappo", | ||
| "task=vmas/balance", | ||
| ], | ||
| ) | ||
| callbacks = load_callbacks_from_hydra(getattr(cfg, "callbacks", None) or {}) | ||
| assert len(callbacks) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("callback_name", callback_config_registry.keys()) | ||
| def test_loading_callbacks(callback_name): | ||
| with initialize(version_base=None, config_path="../benchmarl/conf"): | ||
| cfg = compose( | ||
| config_name="config", | ||
| overrides=[ | ||
| "algorithm=mappo", | ||
| "task=vmas/balance", | ||
| f"callback@callbacks.c1={callback_name}", | ||
| ], | ||
| ) | ||
| callback = load_callbacks_from_hydra(cfg.callbacks)[0] | ||
| assert isinstance( | ||
| callback, callback_config_registry[callback_name].associated_class() | ||
| ) | ||
|
|
||
|
|
||
| def test_disabling_callbacks(): | ||
| with initialize(version_base=None, config_path="../benchmarl/conf"): | ||
| cfg = compose( | ||
| config_name="config", | ||
| overrides=[ | ||
| "algorithm=mappo", | ||
| "task=vmas/balance", | ||
| "+callbacks=null", | ||
| ], | ||
| ) | ||
| callbacks = load_callbacks_from_hydra(getattr(cfg, "callbacks", None) or {}) | ||
| assert len(callbacks) == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.