Skip to content

Conversation

@tcbegley
Copy link
Contributor

@tcbegley tcbegley commented Oct 3, 2022

Description

Specs are optionally provided via the info_spec property of the info_dict_reader (all readers should inherit from BaseInfoDictReader). When the info_dict_reader is set, GymLikeEnv recovers the specs and adds them to observation_spec.

I would appreciate some guidance on how / where best to test these changes.

Motivation and Context

Closes #453

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • New feature (non-breaking change which adds core functionality)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

Specs are optionally provided via the info_spec property of the
info_dict_reader (all readers should inherit from BaseInfoDictReader).
When the info_dict_reader is set, GymLikeEnv recovers the specs and adds
them to observation_spec.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 3, 2022
spec = {}

self._info_spec = {
key: spec.get(key, UnboundedContinuousTensorSpec()) for key in self.keys
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MyPy gives me a warning here because TensorSpec has abstract method index which UnboundedContinuousTensorSpec does not define.

@vmoens vmoens changed the title Add info dict key-spec pairs to observation_spec [Feature] Add info dict key-spec pairs to observation_spec Oct 3, 2022
Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

Wonderful,
do you think we could sketch a couple of tests for this?
You could use HalfCheetah-v4 (https://www.gymlibrary.dev/environments/mujoco/half_cheetah/) which comes with a "x_position" in the info dict

Comment on lines +1133 to +1134
assert "x_position" in env.observation_spec.keys()
assert isinstance(env.observation_spec["x_position"], UnboundedContinuousTensorSpec)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there something else I could be checking here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

you could do assert env.observation_spec["x_position"].is_in(tensordict["x_position"])

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's essentially testing that x_position is what it's supposed to be
Another thing we could do is set up a screwed up tensor spec (e.g. one that has wrong shape or wrong dtype) and check that this assertion raises an error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added some additional tests as you suggested, although it wasn't raising an error at any stage, just returning false for

env.observation_spec["x_position"].is_in(tensordict["x_position"])

Would you have expected an exception at some point?

Copy link
Collaborator

Choose a reason for hiding this comment

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

no but yes if you put an assert if front of it :p

@vmoens vmoens added the enhancement New feature or request label Oct 4, 2022
Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for this contribution!

@vmoens vmoens merged commit ceba0e9 into pytorch:main Oct 5, 2022
@tcbegley tcbegley deleted the info-key-specs branch October 5, 2022 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add info dict key-spec pairs to env.observation_spec if they're provided to a gym environment through set_info_dict_reader

3 participants