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] Checkpointing enhancements: Experimentally support msgpack and separate state from architecture. #49497

Conversation

sven1977
Copy link
Contributor

@sven1977 sven1977 commented Dec 30, 2024

Checkpointing API enhancements:

  • Experimentally support msgpack checkpoints on the new API stack.
  • Separate of state (get_state -> dict) from architecture (get_ctor_args_and_kwargs).
    • This allows for restoring from any checkpoint, also older ones from older python versions.
    • Users need to bring their own (updated, current-ray-version config or other c'tor args and kwargs) and then do the normal .from_checkpoint([old msgpack checkpoint path])
  • Added comprehensive backward compatibility tests for checkpoints. At each ray version (starting from 2.40), we generate a complex multi-agent checkpoint, using the same script, add it to the repo, and then make sure through the CI that all these checkpoints always work going forward, w/o ever touching them again.
  • Added example script for how to continue training with a different config, using all the above new features.

Why are these changes needed?

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: sven1977 <svenmika1977@gmail.com>
@sven1977 sven1977 added rllib RLlib related issues rllib-checkpointing-or-recovery An issue related to checkpointing/recovering RLlib Trainers. rllib-newstack labels Dec 30, 2024
@sven1977 sven1977 changed the title [RLlib] Checkpointing enhancements: Experimentally support msgpack and separate of state from architecture. [RLlib] Checkpointing enhancements: Experimentally support msgpack and separate state from architecture. Dec 30, 2024
Signed-off-by: sven1977 <svenmika1977@gmail.com>
Signed-off-by: sven1977 <svenmika1977@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) December 30, 2024 18:22
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Dec 30, 2024
@sven1977 sven1977 disabled auto-merge December 30, 2024 18:22
…kpointing_enhancements_msgpack_and_separation_of_state_and_architecture
Copy link
Collaborator

@simonsays1980 simonsays1980 left a comment

Choose a reason for hiding this comment

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

LGTM.

@@ -3459,6 +3460,7 @@ def rl_module(
def experimental(
self,
*,
_use_msgpack_checkpoints: Optional[bool] = NotProvided,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

- just for testing purposes, restores the entire algorithm from the latest
checkpoint and checks, whether the state of the restored algo exactly match the
state of the previously saved one.
- then changes the original config used (learning rate and other settings) and
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome example!!

f"p{aid}": PolicySpec(
config=AlgorithmConfig.overrides(
lr=5e-5
* (aid + 1), # agent 1 has double the learning rate as 0.
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we have more than 2 agents?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I think, then this example breaks :( . I'll just force the setting to always be 2 in this particular example script, otherwise produce an error. :)

test_eval_results = test_algo.evaluate()
assert (
test_eval_results[ENV_RUNNER_RESULTS][EPISODE_RETURN_MEAN]
>= args.stop_reward_first_config
Copy link
Collaborator

Choose a reason for hiding this comment

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

Dumb question: Does this inequality always hold? We train for mean rewards, so theoretically there might be cases where this inequality does not hold, aren't there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but statistically, I think the chance is super low. We also evaluate, meaning we use greedy actions, which perform much stronger that the stochastic ones used during training.

)

class LoadP0OnAlgoInitCallback(DefaultCallbacks):
def on_algorithm_init(self, *, algorithm, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright so users should always load certain modules via callback? If so my suggestion would be to provide a callback that does this for users.

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 think this is the most expressive and transparent way, yes.

  • Working on another PR allowing to pass a simple, single on_algorithm_init callback lambda to the config. This way, users don't have to provide these clumsy classes anymore (they still can, but don't have to).
  • This also avoids having these paths in the module specs. Imo, they don't belong in there.

policies_to_train=all_pols,
)
expanded_config.rl_module(
algorithm_config_overrides_per_module={
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

…kpointing_enhancements_msgpack_and_separation_of_state_and_architecture
Signed-off-by: sven1977 <svenmika1977@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) December 31, 2024 13:42
Signed-off-by: sven1977 <svenmika1977@gmail.com>
@github-actions github-actions bot disabled auto-merge December 31, 2024 14:52
@sven1977 sven1977 merged commit 7791d13 into ray-project:master Dec 31, 2024
5 checks passed
srinathk10 pushed a commit that referenced this pull request Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests rllib RLlib related issues rllib-checkpointing-or-recovery An issue related to checkpointing/recovering RLlib Trainers. rllib-newstack
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants