Repository navigation
Conversation
The exponential moving averages of x and x**2 were bias-corrected after loc**2 had already been subtracted, leaving a decay**count * mean**2 term in the variance. Divide first, then subtract, in both _norm and _get_loc_scale; use expm1 for the correction and clamp the variance at 0. Fixes pytorch#4491
VecNormV2 stored moving averages of x and x**2 and computed the variance as E[x**2] - E[x]**2. In float32 this cancels catastrophically when the data offset is large compared to its spread (NaN on main, eps-scaled outputs after the bias-correction fix at offset 1e4). loc and var now hold the bias-corrected weighted mean and variance and are updated from the deviation to the current mean. Batches merged with reduce_batch_dims use a two-pass batch variance. Checkpoints saved in the old format are converted on load. [skip ci]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4516
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 8cf68b4 with merge base 0c6f682 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #4514 (its commit is the first one here); I'll rebase once it lands.
Problem
VecNormV2 stores moving averages of
xandx**2and computes the variance asE[x**2] - E[x]**2. In float32 this cancels out when the data offset is large relative to its spread. With observations at1e4 + N(0, 1)anddecay=0.99:Change
_loc/_varnow hold the bias-corrected, exponentially weighted mean and variance (they used to hold biased moving averages ofxandx**2). The update uses weightw = (1 - decay**n) / (1 - decay**count)(n / countfordecay=1), so the stored stats are already bias-corrected and_norm/loc/scalejust read them.var <- (1 - w) * var + w * (batch_var + (1 - w) * (batch_mean - loc)**2). Only the deviation from the current mean is squared, so the offset never enters a subtraction. For a single samplebatch_var = 0; withreduce_batch_dims=Truethe batch variance is computed two-pass around the batch mean.get_extra_statenow writes aversionentry. A state without it is treated as the old format and converted in place on load (keeps shared-memory stats shared), using the instance'sdecay._vecnorm_loc/_vecnorm_varkeys, which now hold mean/variance. They are re-initialised at reset, so only tensordicts saved mid-trajectory by an older version would be read differently.Tests
test_vecnorm2_large_offset(stateful, stateless,reduce_batch_dimson aSerialEnv): offset 1e4, compares every normalized output and the finalloc/scaleto a float64 reference computed from the observations. Fails on main and on [BugFix] Apply VecNormV2 bias correction before subtracting loc**2 #4514, passes here.test_vecnorm2_load_legacy_state_dict: loads an old-format state built from known samples and checksloc/scale, then round-trips the new format.test/transforms/test_normalization.pyand the VecNorm / extra_state tests in collectors, evaluator, configs, render, rb and auto-reset pass locally.Cost
CPU microbenchmark of
_step(17-dim obs): single-sample path 137 -> 131 us/step,reduce_batch_dimswith batch 64: 163 -> 201 us/step (extra pass for the batch variance).