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

Refactor observation stacking #1238

Merged
merged 33 commits into from
Feb 6, 2023
Merged

Refactor observation stacking #1238

merged 33 commits into from
Feb 6, 2023

Conversation

qgallouedec
Copy link
Collaborator

@qgallouedec qgallouedec commented Dec 25, 2022

Description

Summary of changes:

  • StackedObservations now supports Dict as an observation space. It handles this by nesting instances of StackedObservation.
  • DictStackedObservation has therefore been deprecated.
  • Add tests for StackedObservations
  • Fix type hints.

The changes I need feedback on:

stack_observation_space deprecation

I deprecated the stack_observation_space method for the following reasons:

  • it is no longer used in sb3
  • it is probably not used outside the internal workings of sb3.
  • it has an unsatisfactory way of working between method and static method (uses self.repeat_axis, but takes observation_space as parameter).

compute_stacking new params and returns

static method compute_stacking no longer takes num_envs and return the stacked_shape instead of a zero-filled stacked obs of shape (num_env, *stacked_shape)

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I've read the CONTRIBUTION guide (required)
  • I have updated the changelog accordingly (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.
  • I have opened an associated PR on the SB3-Contrib repository (if necessary)
  • I have opened an associated PR on the RL-Zoo3 repository (if necessary)
  • I have reformatted the code using make format (required)
  • I have checked the codestyle using make check-codestyle and make lint (required)
  • I have ensured make pytest and make type both pass. (required)
  • I have checked that the documentation builds using make doc (required)

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line

@qgallouedec qgallouedec marked this pull request as ready for review January 2, 2023 12:54
@araffin araffin self-requested a review January 4, 2023 17:54
@araffin
Copy link
Member

araffin commented Jan 23, 2023

will try to review this one this week ;)

@araffin
Copy link
Member

araffin commented Jan 30, 2023

Funnily, this is the 2nd time this piece of code is refactored ^^ first time was #243 (currently doing the review ;))


def stack_observation_space(self, observation_space: spaces.Box) -> spaces.Box:
def stack_observation_space(self, observation_space: Union[spaces.Box, spaces.Dict]) -> Union[spaces.Box, spaces.Dict]:
Copy link
Member

Choose a reason for hiding this comment

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

I thought about removing it but after a quick check, it is a bit used: https://github.com/search?o=desc&q=stable_baselines3+stack_observation_space&s=indexed&type=Code

so, let's remove it only in 2.x

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On the contrary, it seems that most of the matches on this search match def stack_observation_space that come from the copies of sb3. I can't find any code where this function is actually used.

Given an observation space, returns a new observation space with stacked observations
This function is deprecated.

As an alternative, use
Copy link
Member

Choose a reason for hiding this comment

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

.stacked_observation_space is not a better alternative?

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 function is supposed to work by passing an observation space as input. This makes it a bit strange, as it is a bit of a cross between a class method and an instance method, in that it depends on self.n_stack and self.repeat_axis, but not on self.observation_space. Anyway, I think it should be removed, and if there is someone who actually uses it, then the correspodance that works in all cases (potentially with a different observation_space than the one of the instance) is the code that is proposed in the docstring.

observation_space = self.stackedobs.stack_observation_space(wrapped_obs_space)
VecEnvWrapper.__init__(self, venv, observation_space=observation_space)
@property
def n_stack(self):
Copy link
Member

Choose a reason for hiding this comment

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

was it used somewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just in stack_observation_space; it's not used elsewhere in sb3, nor in sb3-contrib or rl-zoo3. It's hard to know if this arg is used by codes that depend on sb3.

Copy link
Member

@araffin araffin left a comment

Choose a reason for hiding this comment

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

I removed the stacked dict obs and simplify some code that was a bit duplicated.

I'm also not sure why you added all the # pytype: disable=attribute-error, I tried removing them and it does work locally.
And pytype doesn't use the type stubs if I recall, in comparison to mypy (so if mypy passes, should be fine).
Apart from that comment, the rest look good to me =)

@qgallouedec
Copy link
Collaborator Author

I'm also not sure why you added all the # pytype: disable=attribute-error, I tried removing them and it does work locally.
And pytype doesn't use the type stubs if I recall, in comparison to mypy (so if mypy passes, should be fine).

Usually, we wonder why a bug appears when we have not changed anything. Here, I wonder why a bug disappears when we have not changed anything 🤔. So be it! LGTM too!

@araffin
Copy link
Member

araffin commented Feb 6, 2023

i think i know what happened, pytype allow multiline skip, and that's what your comment did.

@araffin araffin merged commit 2e4a450 into master Feb 6, 2023
@araffin araffin deleted the new-stack-observation branch February 6, 2023 21:42
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.

2 participants