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] Example script custom_metrics_and_callbacks.py should work for batch_mode=complete_episodes. #22684

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Feb 27, 2022

Why are these changes needed?

To enable a better traceability and to give users a universal guideline that works with different batch_modes when producing custom metrics.
So far the batch_mode="complete_episodes" produces an error in the custom_metrics_and_callbacks.py. The reason for this is:

  • When batch_mode="complete_episodes" the SimpleListCollector will be called to build a MultiAgentBatch
  • That in turn will call the build() method of the PolicyCollector and empty the batches attribute.
  • As the batches from the PolicyCollector are empty now, episode.batch_builder.policy_collectors["default_policy"].batches[-1] does not exist in the assert expression.

For most of the end-users running their algorithms with batch_mode="complete_episodes" trying to implement their own metrics by following this example will possibly not able to trace this error back and to find a solution for their own code.

Related issue number

#22683

Checks

  • 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 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 :(

Copy link
Member

@gjoliver gjoliver left a comment

Choose a reason for hiding this comment

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

can you please rebase master into your branch, a lot of strange numpy random number generator errors.
thanks.

][-1], (
"ERROR: `on_episode_end()` should only be called " "after episode is done!"
)
# Check if there are multiple episodes in a batch, i.e.
Copy link
Member

Choose a reason for hiding this comment

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

thanks a ton for noticing this.
any suggestion on how can we assert this for "complete_episode" mode as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Trainer class

I think in case of complete_episodes we do not have to care about, if episodes are really done because they are always done (complete). But to have a callback that can run with both batch_modes we need a prior safeguard for this. In case of truncate_episodes we have to be careful if the episode is indeed done.

Custom usage

This is different, if a user implements the callback in his own code and calls this callback before an episode is done. I don't know if (1) this case is rare and would happen only between a rollout and a batch completion or (2) if it can happen literally at any point of sampling. If we want to ensure that the below assertion does not error out, we might have a look at the batches attribute of the PolicyCollector as this gets emptied when the MultiAgentBatch is created by the simple_list_collector. I have also not yet figured out, if this attribute (batches) is only emptied, when the episode is done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, even in "complete_episodes" mode, there could be more than one episodes in the final train batch (but not here in this callback).

  • We should probably rename multiple_episodes_in_batch into collect_complete_episodes (bool).
  • I think we should rather use worker.policy_config["batch_mode"] here in this if block.

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 this fix! Let me know, if you want to do a follow-up PR with the 2 above suggested changes. I think this would help clarify these batch generating rules for the different settings even more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @sven1977, yes I can do a follow-up PR. Should I do this within the same branch or better create a new one?

@simonsays1980 simonsays1980 force-pushed the custom-metrics-and-callbacks-complete_episodes branch from a820140 to 9d0f2eb Compare February 27, 2022 22:51
@simonsays1980
Copy link
Collaborator Author

simonsays1980 commented Feb 27, 2022

@gjoliver I rebased. Can we rerun the tests somehow?

Copy link
Member

@gjoliver gjoliver left a comment

Choose a reason for hiding this comment

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

ok, the tests look reasonable.
@sven1977, can you help merge? :)

@sven1977 sven1977 changed the title Custom metrics and callbacks complete episodes [RLlib] Example script custom_metrics_and_callbacks.py should work for batch_mode=complete_episodes. Mar 1, 2022
@sven1977 sven1977 merged commit 568cf28 into ray-project:master Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants