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; Offline RL] RLUnplugged example on new API stack. #46792

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Jul 25, 2024

Why are these changes needed?

The new Offline RL API uses native ray.data.Datasets to scale to massive datasets with different formats and encodings. To ensure that learning indeed scales to massive data and the API can be used with the most notable Offline RL datasets this PR applies the new Offline RL API to the RLUnplugged data. In the tuned example data from Atari Pong is used with a single TfRecords file from the GCS bucket.

The data is downloaded automatically into a temporary folder. To decode the PNG images this example defines a ConnectorV2 that decodes the images in the learner connector pipeline and stacks frames.

This example show specifically how to scale out on massive data and use the AlgorithmConfig.offline_data's arguments to fine-tune scaling with ray.data. See for more infos ray.data.

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

simonsays1980 and others added 9 commits July 24, 2024 16:25
…class into separate files.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…gorithmConfig' and 'OfflineData' to make the data pipeline better configurable and tuneable. Tested single-and multi-learner sertups with BC.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Co-authored-by: Sven Mika <sven@anyscale.io>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…n class.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…of Atari Pong. Needs to be tuned.

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

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@sven1977 sven1977 marked this pull request as ready for review July 25, 2024 17:55
@sven1977 sven1977 changed the title [RLlib - Offline RL] - RLUnplugged example on new API stack. [RLlib; Offline RL] RLUnplugged example on new API stack. Jul 25, 2024
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
**kwargs,
)

self._multi_agent = multi_agent
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need these options?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, we don't. It was not clear to me, yet, that this does not need to be set when in single-agent mode.


# Make the learner connector.
def _make_learner_connector(observation_space, action_space):
return DecodeObservations()
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's get rid of this by doing in the config below:

config.training(
   ...
   learner_connector: lambda obs_space, act_space: DecodeObservations(),
   ...
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like this more! Thanks!

)
)

# TODO (simon): Change to use the `run_rllib_example` function as soon as tuned.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: TODO? Or still WIP?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

WIP :) It is still not running smoothly and for debugging I am not using tune.


# Make the temporary directory for the downloaded data.
tmp_path = "/tmp/atari"
Path(tmp_path).joinpath(game).mkdir(exist_ok=True, parents=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: should we use the "/" op of pathlib.Path here?

tmp_path = Path("/tmp/atari") / game
tmp_path.mkdir(...)
destination_file_name = tmp_path / "run_...."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, let's do it this is better to read.

# can be chosen by users. To use all data use a list of file paths (see
# `num_shards`) and its usage further below.
run_number = 1
# num_shards = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the be commented out? # num_shards = 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is actually a number that is now hard coded into the path. We have many runs and for each multiple shards in the bucket - each is a file. For the example I use only a single of these files.

def _env_creator(cfg):
return wrap_atari_for_new_api_stack(
gym.make("ALE/Pong-v5", **cfg),
# Perform frame-stacking through ConnectorV2 API.
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, if we say: framestack=4 here then we do NOT perform frame-stacking through ConnectorV2 here.

-> Change comment by removing the ConnectorV2 statement.

Note: At this point, the gain in performance when using the connector is minimal, so can probably be neglected for simplicity (easier for user to do framestacking in env-wrapper).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the hint. WIll remove this comment.



class DecodeObservations(ConnectorV2):
def __init__(
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a (small) docstring here that explains what this connector does.

@@ -0,0 +1,257 @@
"""
schema={
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed here? If this is an explanation of the schema, maybe move this down into the config section (where we explain the schema translation)?

@@ -2499,6 +2499,10 @@ def offline_data(
self.input_read_method_kwargs = input_read_method_kwargs
if input_read_schema is not NotProvided:
self.input_read_schema = input_read_schema
if map_batches_kwargs is not NotProvided:
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes, this was missing :)

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.

Looks great! Just some nits, mostly enhancing/adding comments and explanations.

@sven1977 sven1977 enabled auto-merge (squash) July 31, 2024 12:39
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jul 31, 2024
@@ -0,0 +1,257 @@
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we call this file simply: pong_bc.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we should add a hint that it is pong data from rl_unplugged. What do you think?

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.

Looks good to me now! Thanks @simonsays1980

@sven1977 sven1977 merged commit f34600d into ray-project:master Aug 9, 2024
7 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.

2 participants