Repository navigation
Conversation
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.
🔗 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.
|
| value, torch.Tensor | ||
| ): | ||
| buffer.materialize( | ||
| shape=value.shape, dtype=value.dtype, device=value.device |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
LGTM! Thanks for the fix and for verifying with the meta device! |
Description
Adds
ObservationNorm._load_from_state_dict, which fills in the lazyloc/scalebuffers from the checkpoint before the normal load. Same approachRNDTransformuses for its lazy stats.Motivation and Context
Loading a checkpoint into an
ObservationNormbuilt withoutloc/scalefailed with a size mismatch, which breaks the usual "rebuild the env, then load" restore.Fixes #4757
Types of changes
Checklist
Tests: new
test_observationnorm_load_state_dict_uninitialized(fails on main, passes here).-k ObservationNormintest_observation_transforms.pypasses (182 passed, 2 CUDA-only skipped), lint is clean.