Skip to content

[BugFix] Load ObservationNorm state_dict into an uninitialized transform - #4758

Open
Bndrwe wants to merge 2 commits into
pytorch:mainfrom
Bndrwe:fix-observationnorm-load-uninitialized
Open

Bndrwe wants to merge 2 commits into
pytorch:mainfrom
Bndrwe:fix-observationnorm-load-uninitialized

Conversation

@Bndrwe

@Bndrwe Bndrwe commented Nov 22, 2026

Copy link
Copy Markdown

Description

Adds ObservationNorm._load_from_state_dict, which fills in the lazy loc/scale buffers from the checkpoint before the normal load. Same approach RNDTransform uses for its lazy stats.

Motivation and Context

Loading a checkpoint into an ObservationNorm built without loc/scale failed with a size mismatch, which breaks the usual "rebuild the env, then load" restore.

Fixes #4757

  • 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)

Checklist

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

Tests: new test_observationnorm_load_state_dict_uninitialized (fails on main, passes here). -k ObservationNorm in test_observation_transforms.py passes (182 passed, 2 CUDA-only skipped), lint is clean.

An ObservationNorm created without loc/scale holds lazy UninitializedBuffer
entries. Loading a checkpoint into it (e.g. restoring an env built the same
way as at training time) failed with a size-mismatch RuntimeError because
the lazy buffers were never materialized. Materialize loc and scale from the
incoming state_dict before delegating to nn.Module._load_from_state_dict.
@pytorch-bot

pytorch-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4758

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 9b88a1e with merge base df1db3d (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla 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 22, 2026
value, torch.Tensor
):
buffer.materialize(
shape=value.shape, dtype=value.dtype, device=value.device

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should omit device here (matching init_stats above).

Using device=value.device breaks when loading a CPU checkpoint (map_location="cpu") into a CUDA-placed env (env.to("cuda")), forcing loc/scale onto CPU while the rest of the transform is on GPU. Omitting it lets PyTorch default to the module's device, and super()._load_from_state_dict will handle the cross-device copy.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, thanks. Dropped the device in 9b88a1e, so it matches init_stats and loc/scale follow the module. I don't have a GPU here, so I checked it with a transform moved to meta: loading a CPU state_dict now leaves loc/scale on meta (before, they ended up on cpu). The ObservationNorm tests still pass (182 passed, 2 CUDA-only skipped).

Don't take the device from the checkpoint tensor, so a CPU checkpoint loaded
into a transform on CUDA keeps loc/scale on CUDA (same as init_stats).
@coder-jayp

Copy link
Copy Markdown
Contributor

LGTM! Thanks for the fix and for verifying with the meta device!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ObservationNorm.load_state_dict fails when loc/scale weren't passed at construction

2 participants