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

[RLlib] AlgorithmConfig.update_from_dict needs to work for MultiCallbacks. #33796

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions rllib/algorithms/algorithm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,10 @@ def update_from_dict(
# correct methods to properly `.update()` those from given config dict
# (to not lose any sub-keys).
elif key == "callbacks_class":
# Resolve possible classpath.
value = deserialize_type(value, error=True)
# For backward compatibility reasons, only resolve possible
# classpath if value is a str type.
if isinstance(value, str):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just do error=False here instead of the if block?
This way, if value is already a class, nothing will happen.

value = deserialize_type(value, error=True)
self.callbacks(callbacks_class=value)
elif key == "env_config":
self.environment(env_config=value)
Expand Down Expand Up @@ -3142,7 +3144,11 @@ def items(self):
@staticmethod
def _serialize_dict(config):
# Serialize classes to classpaths:
config["callbacks"] = serialize_type(config["callbacks"])
if isinstance(config.get("callbacks"), type):
config["callbacks"] = serialize_type(config["callbacks"])
else:
# TODO(sven): Figure out how to serialize MultiCallbacks
config["callbacks"] = NOT_SERIALIZABLE
config["sample_collector"] = serialize_type(config["sample_collector"])
if isinstance(config["env"], type):
config["env"] = serialize_type(config["env"])
Expand Down
19 changes: 19 additions & 0 deletions rllib/algorithms/tests/test_algorithm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import ray
from ray.rllib.algorithms.algorithm_config import AlgorithmConfig
from ray.rllib.algorithms.callbacks import MultiCallbacks
from ray.rllib.algorithms.ppo import PPO, PPOConfig
from ray.rllib.algorithms.ppo.torch.ppo_torch_rl_module import PPOTorchRLModule
from ray.rllib.core.rl_module.rl_module import SingleAgentRLModuleSpec
Expand Down Expand Up @@ -34,6 +35,24 @@ def test_running_specific_algo_with_generic_config(self):
algo.train()
algo.stop()

def test_update_from_dict_works_for_multi_callbacks(self):
"""Test to make sure callbacks config dict works."""
config_dict = {
"callbacks": MultiCallbacks([])
}
config = (
AlgorithmConfig(algo_class=PPO)
.environment("CartPole-v0")
.training(lr=0.12345, train_batch_size=3000)
)
# This should work.
config.update_from_dict(config_dict)

serialized = config.serialize()

# For now, we don't support serializing MultiCallbacks.
self.assertEqual(serialized["callbacks"], "__not_serializable__")

def test_freezing_of_algo_config(self):
"""Tests, whether freezing an AlgorithmConfig actually works as expected."""
config = (
Expand Down