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] - Add env and agent steps in custom evaluation function for comformity with metrics logger. #45652

Conversation

simonsays1980
Copy link
Collaborator

Why are these changes needed?

Algorithm.evaluate() evaluates a custom evaluation function in the new stack, but then adds env_steps=agent_steps=0 to the NUM_ENV_STEPS_SAMPLED_LIFETIME, NUM_AGENT_STEPS_SAMPLES_LIFETIME b/c all other evaluation methods return env_steps and agent_steps together with results.

This PR modifies the return type of the custom evalaution fucntion to metrics, env_steps, agent_steps in the new api stack (i.e. enable_env_runner_and_connector_v2=True).

Related issue number

Closes #44595

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>
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>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
…fter the 'env.reset' instead before. The docstring in the callback clearly states, it should come before the reset.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
… conform to metrics logger in 'Algorithm.evaluate.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
# 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.

Remove from this PR. I think it belongs to another PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What exactly do you mean? The comment was there before.

) = self.config.custom_evaluation_function(self, self.evaluation_workers)
if not env_steps or not agent_steps:
raise ValueError(
"Custom eval function must return "
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we give the user the exact expected return signature here?

Tuple[ResultDict, int, int]?

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.

Look good to me. Thanks @simonsays1980 for the fix.

Some thoughts on the entire problem (10k feet view):
In the mid-run, we should probably change to a world, in which we don't return anything anymore from any "core" method and instead, just log stuff inside these methods to the metrics logger:

  • algo.training_step() -> Algo should call metrics.reduce() later and not visible to the user
  • Learner.update() -> Already done and hidden from user
  • Learner.additional_update() -> method should probably disappear entirely
  • EnvRunner.sample/get_metrics -> should probably be combined and metrics.reduce call should be automated under the hood (hidden from user).

This way, the user would never have to deal with when to call reduce and would simply log their metrics, plain and simple.

Also, we need to unify our timesteps structures:

env_steps_lifetime: [some count]
agent_steps_lifetime/
    [agent 1]: [some count]
    [agent 2]: [some count]
module_steps_lifetime/
     [module 1]: [some count]

@sven1977 sven1977 marked this pull request as ready for review May 31, 2024 12:14
@simonsays1980
Copy link
Collaborator Author

Look good to me. Thanks @simonsays1980 for the fix.

Some thoughts on the entire problem (10k feet view): In the mid-run, we should probably change to a world, in which we don't return anything anymore from any "core" method and instead, just log stuff inside these methods to the metrics logger:

  • algo.training_step() -> Algo should call metrics.reduce() later and not visible to the user
  • Learner.update() -> Already done and hidden from user
  • Learner.additional_update() -> method should probably disappear entirely
  • EnvRunner.sample/get_metrics -> should probably be combined and metrics.reduce call should be automated under the hood (hidden from user).

This way, the user would never have to deal with when to call reduce and would simply log their metrics, plain and simple.

Also, we need to unify our timesteps structures:

env_steps_lifetime: [some count]
agent_steps_lifetime/
    [agent 1]: [some count]
    [agent 2]: [some count]
module_steps_lifetime/
     [module 1]: [some count]

Hiding the reduce call and taking responsibility out of a user's control would be a great step towards more user-friendliness. The sensitivity of where to call reduce makes this call complicated for the user and not dealing with it anymore makes would make things easier.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) June 24, 2024 12:18
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jun 24, 2024
… is already defined in the 'test_utils'.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…ictionaries.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@github-actions github-actions bot disabled auto-merge June 25, 2024 09:17
@sven1977 sven1977 merged commit 63f98f7 into ray-project:master Jun 25, 2024
6 checks passed
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RLlib] Evaluation logic needs clean-up
2 participants