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] Fix calling of callback on_episode_created to conform to docstring (after reset). #45651

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented May 31, 2024

Why are these changes needed?

The docstring of the on_episode_created callback states clearly that this callback should be called before the env.reset.

Related issue number

Closes #45544

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 <[email protected]>
…fter the 'env.reset' instead before. The docstring in the callback clearly states, it should come before the reset.

Signed-off-by: Simon Zehnder <[email protected]>
# Create a new multi-agent episode.
_episode = self._new_episode()
self._make_on_episode_callback("on_episode_created", _episode)
_shared_data = {
"agent_to_module_mapping_fn": self.config.policy_mapping_fn,
}

# Reset the environment.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @simonsays1980 , thanks for looking into this problem. I think this issue in general here is unfortunately more complex that what meets the eye right now :( . Let me explain:

  • On single-agent, users are currently not even allowed to override the on_episode_created callback :D . This is because in single-agent, we use gym's vector env, which resets envs automatically after a terminal is hit, which makes it impossible to call the on_episode_created callback before this auto-reset happens. See here and here.
  • For multi-agent (where currently we don't use gym.vector) this does actually work and I therefore would suggest, we only fix this for now on the multi-agent env runner.
  • In MultiAgentEnvRunner, however, we should then also fix it for _sample_episodes().
  • We should update the docstring in callbacks.py to reflect that this callback is NOT currently valid for new API stack + single-agent.
  • We should remove the on_episode_created callback call entirely from single-agent env runner.

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.

Thanks for raising this issue @simonsays1980 , an important one.

I wrote my thoughts and the current problems with this particular callback below and suggested some changes. Then we can merge this. :)

Signed-off-by: Simon Zehnder <[email protected]>
@sven1977 sven1977 marked this pull request as ready for review May 31, 2024 12:15
@sven1977 sven1977 changed the title [RLlib] - Fix calling of callback on_episode_created to conform to docstring (after reset). [RLlib] Fix calling of callback on_episode_created to conform to docstring (after reset). May 31, 2024
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.

We still need to:

  • Add the on_episode_created callback call to MultiAgentEpisode._sample_episodes().

@simonsays1980 simonsays1980 self-assigned this Jun 5, 2024
@simonsays1980 simonsays1980 added bug Something that is supposed to be working; but isn't rllib RLlib related issues rllib-newstack labels Jun 5, 2024
@sven1977 sven1977 enabled auto-merge (squash) June 5, 2024 10:48
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jun 5, 2024
@sven1977 sven1977 merged commit 61bc5d4 into ray-project:master Jun 5, 2024
7 checks passed
richardsliu pushed a commit to richardsliu/ray that referenced this pull request Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that is supposed to be working; but isn't go add ONLY when ready to merge, run all tests rllib RLlib related issues rllib-newstack
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RLlib] Incorrect Callback Order
2 participants