Skip to content

fix(fastsac): correct streaming variance merge - #215

Merged
wlgys8 merged 1 commit into
mainfrom
fix-fastsac-streaming-variance
Dec 3, 2026
Merged

wlgys8 merged 1 commit into
mainfrom
fix-fastsac-streaming-variance

Conversation

@wlgys8

@wlgys8 wlgys8 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

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 difference delta2 = batch_mean - new_mean in a squared cross term:

delta2.square() * (old_count * batch_size / new_count)

For historical count n, batch count m, and N = n + m, the correct cross term is:

(batch_mean - old_mean)^2 * n * m / N

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 is 25, but the previous merge produces 6.25.

Fix

Use the pre-update mean difference in both public and local statistics:

delta.square() * (old_count / new_count) * batch_size

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:

  • the two-singleton counterexample;
  • unequal batches, including a singleton, against directly computed population statistics;
  • constant features;
  • one zero followed by 2**24 ones, 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:

python -m pytest motrix_rl_builtin/tests/test_fastsac_buffer.py motrix_rl_builtin/tests/test_fastsac_learner.py -m 'not integration and not slow' -q
# 16 passed

prek run --all-files
# all hooks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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

@wlgys8
wlgys8 merged commit 0f55d32 into main Dec 3, 2026
13 checks passed
@wlgys8
wlgys8 deleted the fix-fastsac-streaming-variance branch December 3, 2026 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants