Skip to content

Conversation

@romainjln
Copy link
Contributor

@romainjln romainjln commented Nov 25, 2022

Description

Refactor examples to use ObservationNorm.init_stats instead of get_stats_random_rollout

Motivation and Context

close #699

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

Types of changes

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

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

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.

@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 Nov 25, 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.

I think that here, if we spawn multiple processes, each env on each process will run its own init_env_steps and hence they will all have a different set of summary stats.

We should compute the stats in the main process like we did before (but using the method you provided), then pass these stats to each env.

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
Do you think things would be simpler with state dict?
We could
(1) create a dummy env, compute stats
(2) get the state dict of this dummy env
(3) load the state dict on every env created subsequently

LMK what you think

raise AttributeError("init_env_steps missing from arguments.")

if (
type(proof_environment.transform) != Compose
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not isinstance?
Why equality and not is not?


if (
type(proof_environment.transform) != Compose
and type(proof_environment.transform) != ObservationNorm
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

)

obs_norm_transforms = []
if type(proof_environment.transform) == Compose:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

obs_norm_transforms.append((0, proof_environment.transform))

stats = []
for (idx, transform) in obs_norm_transforms:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Upon reflection we could simply take the state dict of the transforms and load it no?
Wouldn't that be simpler than this?
If loc and scale are buffers it could simplify things a bit

@codecov
Copy link

codecov bot commented Dec 6, 2022

Codecov Report

Merging #715 (60ad3c4) into main (a677fb1) will decrease coverage by 0.04%.
The diff coverage is 98.74%.

@@            Coverage Diff             @@
##             main     #715      +/-   ##
==========================================
- Coverage   88.71%   88.66%   -0.05%     
==========================================
  Files         120      120              
  Lines       20240    20386     +146     
==========================================
+ Hits        17955    18075     +120     
- Misses       2285     2311      +26     
Flag Coverage Δ
habitat-gpu 25.03% <0.00%> (-0.03%) ⬇️
linux-cpu 85.75% <97.94%> (+0.09%) ⬆️
linux-gpu 86.69% <97.94%> (+0.08%) ⬆️
linux-jumanji 30.24% <0.00%> (-0.03%) ⬇️
linux-outdeps-gpu 72.21% <83.57%> (+0.10%) ⬆️
linux-stable-cpu 85.60% <97.94%> (+0.09%) ⬆️
linux-stable-gpu 86.32% <97.94%> (+0.08%) ⬆️
linux_examples-gpu 43.10% <63.93%> (+0.01%) ⬆️
macos-cpu 85.42% <97.94%> (+0.09%) ⬆️
olddeps-gpu 76.26% <99.28%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
torchrl/trainers/helpers/models.py 93.38% <ø> (ø)
torchrl/envs/transforms/transforms.py 87.19% <87.50%> (+<0.01%) ⬆️
examples/dreamer/dreamer.py 87.80% <92.30%> (+0.14%) ⬆️
examples/dreamer/dreamer_utils.py 78.53% <100.00%> (+0.37%) ⬆️
test/test_helpers.py 93.10% <100.00%> (+1.03%) ⬆️
test/test_transforms.py 96.28% <100.00%> (+0.06%) ⬆️
torchrl/trainers/helpers/envs.py 67.26% <100.00%> (-7.48%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more



def retrieve_observation_norms_state_dict(proof_environment: TransformedEnv):
"""Traverse the transforms of the environment and retrieve the ObservationNorm state dicts.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe Traverses + retrieves?

Also for code objects we can use :obj:ObservationNorm for a cleaner rendering in the doc

num_iter: int = 1000,
key: Union[str, Tuple[str, ...]] = None,
):
"""Calling init_stats on all uninitialised ObservationNorms transform of a TransformedEnv.
Copy link
Collaborator

@vmoens vmoens Dec 7, 2022

Choose a reason for hiding this comment

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

Maybe

Calls :obj:`ObservationNorm.init_stats` on all uninitialized :obj:`ObservationNorm` instances of a :obj:`TransformedEnv`.

):
"""Calling init_stats on all uninitialised ObservationNorms transform of a TransformedEnv.
If an ObservationNorm already has non-null loc or stats, it will be skipped.
Copy link
Collaborator

Choose a reason for hiding this comment

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

If an :obj:`ObservationNorm.init_stats` already has  non-null loc or stats, a call to :obj:`initialize_observation_norm_transforms` will be a no-op.

Similarly, if the transformed environment does not contain any ObservationNorm, a call to this function will have no effect.

@vmoens vmoens added the enhancement New feature or request label Dec 8, 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!

@vmoens vmoens merged commit ce350cc into pytorch:main Dec 8, 2022
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] Use ObservationNorm.init_stats for stats computation in example scripts

3 participants