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] Validate episodes before adding them to the buffer. #48083

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Oct 17, 2024

Why are these changes needed?

At the moment the OfflinePreLearner samples recorded episodes or SampleBatches from a ray.data dataset and then adds them to a buffer which corrdinates the time step sampling. It could potentially happen that

  1. The sampled episodes are duplicates (either in the batch or in the buffer)
  2. The sampled episodes are not terminated nor truncated and therefore could and certainly will be fragmented in time order (i.e. we maybe sample first an episode chunk that contains timesteps 11 to 21 before we sample 0 to 11).

In both cases the buffer would raise an error as soon as SingleAgentEpisode.concat is called.

This PR introduces a _validate_episodes method to the OfflinePreLearner to check episodes for duplicates and fragments and returns only unique episodes that are not in the buffer, yet. It disallows uncompleted episodes and thereby ensures that no fragments are added. Users are responsible to record only full episodes.

Furthermore, this PR adds a new configuration parameter to the AlgorithmConfig: input_read_batch_size that controls for the size of the raw data batch pulled from an offline dataset. This can be used in cases of EpisodeType or BatchType row formats that contain usually multiple timesteps and therefore even a single row might contain enough timesteps for train_batch_size_per_learner.

Warning The main chorus for users is: Do not record episodes or sample batches that are not done (batch_mode="truncated_episodes") as this would necessary lead to problems with replay buffers b/c episode chunks to not arrive in order.

Note This might become obsolete in the future, if we simply provide sampled episodes with new unique IDs such that an episode buffer considers chunks of the same episode as standalone ones.

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

…t no duplicates or fragments are added to the replay buffer b/c it cannot handle these. Furthermore, refined tests for 'OfflinePreLearner'.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…mplete_episodes' when recording episodes. This ensures that episodes can be read in again for training.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@simonsays1980 simonsays1980 marked this pull request as ready for review October 17, 2024 12:59
…dium'.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
…ent read batch size in case 'EpisodeType' or 'BatchType' data is stored in offline datasets. Added some docstrings.

Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@sven1977 sven1977 changed the title [RLlib; Offline RL] - Validate episodes before adding them to the buffer. [RLlib; Offline RL] Validate episodes before adding them to the buffer. Oct 21, 2024
@@ -179,6 +188,9 @@ def __call__(self, batch: Dict[str, np.ndarray]) -> Dict[str, List[EpisodeType]]
)
for state in batch["item"]
]
# Ensure that all episodes are done and no duplicates are in the batch.
Copy link
Contributor

Choose a reason for hiding this comment

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

dumb question: Why would we expect duplicates at all here? Any algorithm that would be in danger of running into something like this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great question: This is tricky. I just ran into it because in my own projects I was using small datasets for testing. It could be that your data is so small that pulling a batch contains mjultiple duplicates. Let's say you have a dataset of size 10 and you pull a batch of size 100 (silly maybe, but if you have episodes one row could be multiple timesteps) now 90 rows would be duplicates in the batch.

@@ -16,6 +16,9 @@
# and (if needed) use their values toset up `config` below.
args = parser.parse_args()

args.enable_new_api_stack = True
Copy link
Contributor

Choose a reason for hiding this comment

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

remove these?

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. Thanks @simonsays1980 for the PR!
Just a few nits.

@sven1977 sven1977 enabled auto-merge (squash) October 21, 2024 08:02
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Oct 21, 2024
@sven1977 sven1977 added rllib RLlib related issues rllib-offline-rl Offline RL problems rllib-newstack labels Oct 21, 2024
Signed-off-by: simonsays1980 <simon.zehnder@gmail.com>
@sven1977 sven1977 enabled auto-merge (squash) October 21, 2024 12:17
@sven1977 sven1977 merged commit fbf24a0 into ray-project:master Oct 21, 2024
6 checks passed
Jay-ju pushed a commit to Jay-ju/ray that referenced this pull request Nov 5, 2024
JP-sDEV pushed a commit to JP-sDEV/ray that referenced this pull request Nov 14, 2024
mohitjain2504 pushed a commit to mohitjain2504/ray that referenced this pull request Nov 15, 2024
…r. (ray-project#48083)

Signed-off-by: mohitjain2504 <mohit.jain@dream11.com>
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 rllib RLlib related issues rllib-newstack rllib-offline-rl Offline RL problems
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants