-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
[RLlib] Adjust callback validation to account for MultiCallback
.
#50920
Open
simonsays1980
wants to merge
3
commits into
ray-project:master
Choose a base branch
from
simonsays1980:fix-validation-for-multi-callbacks
base: master
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.
+164
−3
Open
Changes from all commits
Commits
Show all changes
3 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 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 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
Empty file.
This file contains 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,147 @@ | ||
import unittest | ||
import ray | ||
from ray.rllib.algorithms import PPOConfig | ||
from ray.rllib.callbacks.callbacks import RLlibCallback | ||
|
||
|
||
class TestMultiCallback(unittest.TestCase): | ||
"""A tests suite to test the `MultiCallback`.""" | ||
|
||
@classmethod | ||
def setUp(cls) -> None: | ||
ray.init() | ||
|
||
@classmethod | ||
def tearDown(cls) -> None: | ||
ray.shutdown() | ||
|
||
def test_multicallback_with_custom_callback_function(self): | ||
"""Tests if callbacks in `MultiCallback` get executed. | ||
|
||
This also tests, if multiple callbacks from different sources, i.e. | ||
`callback_class` and `on_episode_step` run correctly. | ||
""" | ||
# Define two standard `RLlibCallback`. | ||
class TestRLlibCallback1(RLlibCallback): | ||
def on_episode_step( | ||
self, | ||
*, | ||
episode, | ||
env_runner=None, | ||
metrics_logger=None, | ||
env=None, | ||
env_index, | ||
rl_module=None, | ||
worker=None, | ||
base_env=None, | ||
policies=None, | ||
**kwargs | ||
): | ||
|
||
metrics_logger.log_value( | ||
"callback_1", 1, reduce="mean", clear_on_reduce=True | ||
) | ||
|
||
class TestRLlibCallback2(RLlibCallback): | ||
def on_episode_step( | ||
self, | ||
*, | ||
episode, | ||
env_runner=None, | ||
metrics_logger=None, | ||
env=None, | ||
env_index, | ||
rl_module=None, | ||
worker=None, | ||
base_env=None, | ||
policies=None, | ||
**kwargs | ||
): | ||
|
||
metrics_logger.log_value( | ||
"callback_2", 2, reduce="mean", clear_on_reduce=True | ||
) | ||
|
||
# Define a custom callback function. | ||
def custom_on_episode_step_callback( | ||
episode, | ||
env_runner=None, | ||
metrics_logger=None, | ||
env=None, | ||
env_index=None, | ||
rl_module=None, | ||
worker=None, | ||
base_env=None, | ||
policies=None, | ||
**kwargs | ||
): | ||
|
||
metrics_logger.log_value( | ||
"custom_callback", 3, reduce="mean", clear_on_reduce=True | ||
) | ||
|
||
# Configure the algorithm. | ||
config = ( | ||
PPOConfig() | ||
.environment("CartPole-v1") | ||
.api_stack( | ||
enable_env_runner_and_connector_v2=True, | ||
enable_rl_module_and_learner=True, | ||
) | ||
# Use the callbacks and callback function. | ||
.callbacks( | ||
callbacks_class=[TestRLlibCallback1, TestRLlibCallback2], | ||
on_episode_step=custom_on_episode_step_callback, | ||
) | ||
) | ||
|
||
# Build the algorithm. At this stage, callbacks get already validated. | ||
algo = config.build() | ||
|
||
# Run 10 training iteration and check, if the metrics defined in the | ||
# callbacks made it into the results. Furthermore, check, if the values are correct. | ||
for _ in range(10): | ||
results = algo.train() | ||
self.assertIn("callback_1", results["env_runners"]) | ||
self.assertIn("callback_2", results["env_runners"]) | ||
self.assertIn("custom_callback", results["env_runners"]) | ||
self.assertAlmostEquals(results["env_runners"]["callback_1"], 1) | ||
self.assertAlmostEquals(results["env_runners"]["callback_2"], 2) | ||
self.assertAlmostEquals(results["env_runners"]["custom_callback"], 3) | ||
|
||
algo.stop() | ||
|
||
def test_multicallback_validation_error(self): | ||
"""Check, if the validation safeguard catches wrong `MultiCallback`s.""" | ||
with self.assertRaises(ValueError): | ||
( | ||
PPOConfig() | ||
.environment("CartPole-v1") | ||
.api_stack( | ||
enable_env_runner_and_connector_v2=True, | ||
enable_rl_module_and_learner=True, | ||
) | ||
# This is wrong b/c it needs callables. | ||
.callbacks(callbacks_class=["TestRLlibCallback1", "TestRLlibCallback2"]) | ||
) | ||
|
||
def test_single_callback_validation_error(self): | ||
"""Tests if the validation safeguard catches wrong `RLlibCallback`s.""" | ||
with self.assertRaises(ValueError): | ||
( | ||
PPOConfig() | ||
.environment("CartPole-v1") | ||
.api_stack( | ||
enable_env_runner_and_connector_v2=True, | ||
enable_rl_module_and_learner=True, | ||
) | ||
# This is wrong b/c it needs callables. | ||
.callbacks(callbacks_class="TestRLlibCallback") | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import pytest | ||
import sys | ||
|
||
sys.exit(pytest.main(["-v", __file__])) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! Let's add this to BUILD ...