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] Support writing and reading composite spaces samples. #47046

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Aug 9, 2024

Why are these changes needed?

Writing and reading composite space samples does not work out-of-the-box (e.g. storing them in parquet files) because errors like the following might occur:

pyarrow.lib.ArrowNotImplementedError: Unsupported cast from large_binary to list using function cast_list

This PR proposes some functionality to enable full compatibility with composite spaces. It does so by

  1. Providing a utility function to test for composite spaces.
  2. Converting composite spaces to/from JSONable data types (using gymnasiums to/from_jsonable)
  3. Compressing converted composite and unconverted simple space samples.
  4. Giving the user full access to which columns should be compressed when writing AND reading.

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

…ace samples into JSONable data types, if composite, to write them more easily to and read them more easily from disk. Furthermore, added related functionalities to Offline recording and reading classes, basically 'OfflineData', 'OfflinePreLearner', and 'OfflineEnvRunner'.

Signed-off-by: simonsays1980 <[email protected]>
@simonsays1980 simonsays1980 marked this pull request as ready for review August 9, 2024 09:30
@sven1977 sven1977 changed the title [RLlib; Offline RL] - Support writing and reading composite spaces samples. [RLlib; Offline RL] Support writing and reading composite spaces samples. Aug 9, 2024
],
infos=[
{},
batch[schema[Columns.INFOS]][i]
if schema[Columns.INFOS] in batch
else {},
],
actions=[batch[schema[Columns.ACTIONS]][i]],
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated question on the above:

  • Why is the first info always {}?
  • I think else {}, would yield a tuple, correct? So the resulting final list would be: [{}, ({},)]. Maybe I'm wrong, but can you check 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, because there is only a single INFO column in all the offline data. As we need two in the Episode we need to fill in a default and this is filled in at timestep zero.

YOur second point is valid - this could lead to a tuple, even though the comma should be part of the list ... I check this. Thanks1

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yeah, makes sense. We don't have a NEXT_INFOS.

Hmm, I wonder whether this could be a general problem in some strange use cases :)

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 was also thinking about it, but could not came of with cases that need an info at ts=0.

@@ -77,6 +77,7 @@ def sample(
fn_constructor_kwargs={
"config": self.config,
"learner": self.learner_handles[0],
"spaces": self.spaces["__env__"],
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 merge the other (__env__ constant) PR into this one and then push again. This way, we won't forget to change this here.

observations=[
unpack_if_needed(obs),
unpack_if_needed(batch[schema[Columns.NEXT_OBS]][i]),
convert(unpack_if_needed(obs), observation_space)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if observation_space is None here? Could that happen? Or should we make the arg non-optional?

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 could happen and is in fact okay. As it uses from_jsonable_if_needed the conversion simply does not take place, if the input space is None. We need them only, if we want to convert from JSONable data types to a composite space type.

actions=[
convert(
unpack_if_needed(batch[schema[Columns.ACTIONS]][i]),
action_space,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same for action_space. What if it's None (not provided by caller)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See my comment above ;)

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.

LGTM in general! Just some nits and comments to fix before we can merge. Thanks @simonsays1980 !!

@sven1977 sven1977 enabled auto-merge (squash) August 12, 2024 16:53
@github-actions github-actions bot disabled auto-merge August 12, 2024 16:53
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Aug 12, 2024
@sven1977 sven1977 enabled auto-merge (squash) August 13, 2024 14:22
@sven1977 sven1977 merged commit e066289 into ray-project:master Aug 13, 2024
5 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