Repository navigation
fix(fastsac): correct streaming variance merge - #215
Merged
Merged
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The streaming observation variance correction has no active review issues.
No open issues 🎉
What changed in this PR
Corrects FastSAC’s streaming observation variance calculation for both public statistics and local accumulators.
Changes:
- Uses the pre-update mean difference in the variance merge, avoiding variance underestimation and integer count multiplication.
- Adds regression coverage for singleton and unequal batches, constant features, and float32 mean rounding.
| File | Description |
|---|---|
motrix_rl_builtin/tests/test_fastsac_buffer.py |
Adds population-statistics and numerical-precision regression tests. |
motrix_rl_builtin/src/motrix_rl_builtin/fastsac/buffer.py |
Corrects the variance cross term in both accumulators. |
Review effort: Balanced
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.
Summary
Correct the population-variance merge in FastSAC's empirical observation normalizer, including its local accumulators.
Problem
The normalizer computes
delta = batch_mean - old_mean, updates the mean, and then uses the post-update differencedelta2 = batch_mean - new_meanin a squared cross term:For historical count
n, batch countm, andN = n + m, the correct cross term is:Since
delta2 = delta * n / N, the previous implementation underestimates this term by an additional factor of(n / N)^2.A minimal example is two singleton batches
[0]and[10]: their population variance is25, but the previous merge produces6.25.Fix
Use the pre-update mean difference in both public and local statistics:
This avoids an integer count product and does not depend on subtracting the rounded updated mean. The latter matters for highly unequal batch sizes, where the updated float32 mean can round to the batch mean even though the variance cross term is nonzero.
No changes to replay sampling, training configuration, or variance clamping are included.
Tests
Regression coverage verifies:
2**24ones, retaining nonzero variance when the float32 mean rounds to one (a stride-zero expanded tensor avoids allocating the repeated samples).Validation in the isolated PR worktree: