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

Make various 'space' functions easier to extend for custom Spaces #2093

Closed
wants to merge 12 commits into from

Conversation

lebrice
Copy link

@lebrice lebrice commented Nov 13, 2020

Adds better support for "custom" Space subclasses, by making it possible to "extend" the various gym functions that take a Space as an argument by registering new handlers that type.

This is achieved by converting the given functions into singledispatch functions:

  • gym.spaces.utils.flatten
  • gym.spaces.utils.flatdim
  • gym.spaces.utils.unflatten
  • gym.spaces.utils.flatten_space
  • gym.vector.utils.numpy_utils.concatenate
  • gym.vector.utils.numpy_utils.create_empty_array
  • gym.vector.utils..shared_memory.create_shared_memory
  • gym.vector.utils..shared_memory.read_from_shared_memory
  • gym.vector.utils..shared_memory.write_to_shared_memory

The ordering of the positional arguments for the following functions was changed, since the type of the first argument (in this case space) is the one used by singledispatch to choose the appropriate callable to use. There is however code in each of these to re-order the arguments in case they weren't passed in the right order, making this change fully backward-compatible.

  • gym.vector.utils.numpy_utils.concatenate
  • gym.vector.utils..shared_memory.read_from_shared_memory
  • gym.vector.utils..shared_memory.write_to_shared_memory

Signed-off-by: Fabrice Normandin fabrice.normandin@gmail.com

@jkterry1
Copy link
Collaborator

jkterry1 commented Jul 27, 2021

So after discussion with @benblack769 this actually seems like a generally good idea. However, this detailed tests need to be added before I can merge this and Pr currently does not have them.

@jkterry1
Copy link
Collaborator

Reviewer: @benblack769

@jkterry1
Copy link
Collaborator

@lebrice tests are failing and there are merge conflicts. Are you still interested in working on this PR?

@lebrice
Copy link
Author

lebrice commented Sep 13, 2021

Hey @jkterry1, Yeah I will update this when I find a moment, should be relatively soon.

@lebrice lebrice force-pushed the easier_custom_spaces branch from aff3e82 to 037adb0 Compare September 13, 2021 22:50
@lebrice
Copy link
Author

lebrice commented Sep 13, 2021

Alright so turns out that @tristandeleu actually already added part of what this PR was meant for in #2328 (at least for the flatten/unflatten/flatdim utils).

This PR also transforms the other utility functions into singledispatch callables, and also fixes a potential bug:

  • Calling these generic utility functions without a Space positional argument would raise an error.

My opinion is that there shouldn't be any other tests needed specifically for this PR, since it's simply a refactoring of the utility functions, each of which already has tests. This PR also adds a test for the positional argument issue I just described.

Let me know what you think.

@tristandeleu
Copy link
Contributor

Alright so turns out that @tristandeleu actually already added part of what this PR was meant for in #2328 (at least for the flatten/unflatten/flatdim utils).

Apologies for the changes, I should have noted them here.

I would also be in favor of eventually changing the order of the arguments in all the functions in gym/vector/utils to get the space as a first argument, and make all the changes internally in VectorEnv. This would indeed break backward-compatibility though, as noted by @lebrice, but these are largely internal functions.

@lebrice
Copy link
Author

lebrice commented Sep 15, 2021

This would indeed break backward-compatibility though, as noted by @lebrice, but these are largely internal functions.

@tristandeleu This is actually not the case! I've added a "wrapper" function around all the introduced generic functions, which makes this totally backward-compatible! As an example, I didn't need to change anything VectorEnvs for this to work! :)

@tristandeleu
Copy link
Contributor

Yes sorry I saw the wrapper function. What I was suggesting was to change the order of the arguments everywhere (and remove the wrapper function). The PR in its current form is absolutely fine, but in the future it would be cleaner (and possibly less confusing) to have all these functions have space as their first argument.

@jkterry1
Copy link
Collaborator

@lebrice is this PR currently in a state where you'd like it to be reviewed for being merged?

@jkterry1
Copy link
Collaborator

@lebrice could you please also fix tests and change argument orders per Tristan's comment?

@lebrice
Copy link
Author

lebrice commented Sep 30, 2021

PR is in a good state and ready for review, however I guess I could first change the order of arguments as you mentioned. I'll also keep it backward-compatible as well and raise a warning or something in case the old ordering is used with positional args.

@jkterry1
Copy link
Collaborator

@lebrice did you change the argument order and can you please fix the merge conflicts?

@lebrice
Copy link
Author

lebrice commented Oct 16, 2021

Not yet, will do. You can close this if you want, or mark it as draft, and I'll mark it as "ready for review" when it's good to go

@vwxyzjn
Copy link
Contributor

vwxyzjn commented Oct 18, 2021

Hey, this looks really good in refactoring the code. One thing that pops to me was the use of _space_as_first_positional_argument. It seems pretty hacky: shouldn’t we just tell the users to put space as the first positional argument?

Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
- Fix bug where singledispatch requires at least one positional
  argument. Added a `pass_space_as_first_positional_argument` wrapper
  which fixes this issue.
- Improve tests for the functions which had the ordering of their
  arguments changed: Tests now cover both the old ordering as well as
  the new one (they previously only used the old ordering).

Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
Signed-off-by: Fabrice Normandin <fabrice.normandin@gmail.com>
@lebrice lebrice force-pushed the easier_custom_spaces branch from 6f87995 to 1578ad6 Compare November 4, 2021 16:46
@jkterry1
Copy link
Collaborator

Closed in favor of #2525

@jkterry1 jkterry1 closed this Dec 21, 2021
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.

4 participants