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 type hints for original_batches in callbacks. #24214

Merged

Conversation

XuehaiPan
Copy link
Contributor

Why are these changes needed?

Fix type hints for original_batches in on_postprocess_trajectory.

Related issue number

N/A

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

Signed-off-by: Xuehai Pan <XuehaiPan@pku.edu.cn>
@@ -196,7 +196,7 @@ def on_postprocess_trajectory(
policy_id: PolicyID,
policies: Dict[PolicyID, Policy],
postprocessed_batch: SampleBatch,
original_batches: Dict[AgentID, SampleBatch],
original_batches: Dict[AgentID, Tuple[Policy, SampleBatch]],
Copy link
Member

Choose a reason for hiding this comment

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

what, that's surprising ...
how can we mix these 2 things together?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pre_batches = {}
for (eps_id, agent_id), collector in self.agent_collectors.items():
# Build only if there is data and agent is part of given episode.
if collector.agent_steps == 0 or eps_id != episode_id:
continue
pid = self.agent_key_to_policy_id[(eps_id, agent_id)]
policy = self.policy_map[pid]
pre_batch = collector.build(policy.view_requirements)
pre_batches[agent_id] = (policy, pre_batch)

pre_batches is a dict with value type Tuple[Policy, SampleBatch]. Then it is fed to on_postprocess_trajectory as name original_batches.

for agent_id, post_batch in sorted(post_batches.items()):
agent_key = (episode_id, agent_id)
pid = self.agent_key_to_policy_id[agent_key]
policy = self.policy_map[pid]
self.callbacks.on_postprocess_trajectory(
worker=get_global_worker(),
episode=episode,
agent_id=agent_id,
policy_id=pid,
policies=self.policy_map,
postprocessed_batch=post_batch,
original_batches=pre_batches,
)

Copy link
Member

Choose a reason for hiding this comment

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

oh it's Tuple, not Union, I got scared.
Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

:) @gjoliver

I think there is only very few places in RLlib where we mix different types e.g. in a return value (for example in the sampler code _process_observations()) and no, we probably shouldn't do this.

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.

@sven1977 can you help merge?

@sven1977
Copy link
Contributor

Nice fix. Thanks @XuehaiPan , really appreciate all your help on RLlib :)

@sven1977 sven1977 changed the title [RLlib] Fix type hints for original_batches [RLlib] Fix type hints for original_batches. Apr 29, 2022
@sven1977 sven1977 changed the title [RLlib] Fix type hints for original_batches. [RLlib] Fix type hints for original_batches in callbacks. Apr 29, 2022
@sven1977 sven1977 merged commit 3c3dd50 into ray-project:master Apr 29, 2022
@XuehaiPan XuehaiPan deleted the fix-typehint-original_batches branch August 23, 2022 06:34
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.

None yet

3 participants