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] - Get and set states in MultiAgentEpisode and SingleAgentEpisode #45012

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Apr 27, 2024

Why are these changes needed?

This PR adds get_state and from_state to MultiAgentEpisode and SingleAgentEpisode. This is needed for checkpointing EpisodeReplayBuffer objects.

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
…get_state' and 'from_state' to 'SingleAGentEpisode' together with test.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
…ith a test.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@simonsays1980 simonsays1980 self-assigned this Apr 27, 2024
@simonsays1980 simonsays1980 added rllib RLlib related issues rllib-newstack labels Apr 27, 2024
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@@ -44,7 +44,7 @@ def build(self) -> None:
# Note that the KL coeff is not controlled by a Scheduler, but seeks
# to stay close to a given kl_target value in our implementation of
# `self.additional_update_for_module()`.
self.curr_kl_coeffs_per_module: Dict[ModuleID, Scheduler] = LambdaDefaultDict(
self.curr_kl_coeffs_per_module: Dict[ModuleID, TensorType] = LambdaDefaultDict(
Copy link
Contributor

Choose a reason for hiding this comment

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

nice catch

@@ -1704,50 +1704,84 @@ def get_state(self) -> Dict[str, Any]:
return list(
{
"id_": self.id_,
"agent_ids": self.agent_ids,
"agent_to_module_mapping_fn": self.agent_to_module_mapping_fn,
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, let's make the state a dict. States should always be Dict[str, Any]. I think this is a leftover from the early DreamerV3 days :)

}.items()
)

@staticmethod
def from_state(state) -> None:
def from_state(state) -> "MultiAgentEpisode":
Copy link
Contributor

Choose a reason for hiding this comment

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

same here: typehint: state: Dict[str, Any]

"""Creates a multi-agent episode from a state dictionary.

See `MultiAgentEpisode.get_state()` for creating a state for
a `MultiAgentEpisode` pickable state. For recreating a
`MultiAgentEpisode` from a state, this state has to be complete,
i.e. all data must have been stored in the state.

Args:
state: A list of tuples containing all data required to recreate
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, let's make this a dict.

@@ -1643,6 +1643,75 @@ def agent_steps(self) -> int:
"""
return self.env_steps()

def get_state(self) -> list:
Copy link
Contributor

Choose a reason for hiding this comment

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

same here: dict

@@ -342,6 +342,24 @@ def test_sample_with_modules_to_sample(self):
# Assert that all n-steps are 1.0 as passed into `sample`.
self.assertTrue(np.all(n_steps - 1.0 < tolerance))

# def test_get_state_and_set_state(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Remove?

Copy link
Contributor

@sven1977 sven1977 left a comment

Choose a reason for hiding this comment

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

Looks good. Just needs the changes from list to dict, then can be merged. :)
Thanks @simonsays1980 !

@sven1977 sven1977 marked this pull request as ready for review April 29, 2024 09:10
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@sven1977 sven1977 merged commit a45bfe3 into ray-project:master Apr 30, 2024
5 checks passed
harborn pushed a commit to harborn/ray that referenced this pull request May 8, 2024
ryanaoleary pushed a commit to ryanaoleary/ray that referenced this pull request Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rllib RLlib related issues rllib-newstack
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants