-
-
Notifications
You must be signed in to change notification settings - Fork 714
feat(metrics): add WelfordVariance and WelfordCovariance helpers (PR 1 of #3748) #3750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joemunene-by
wants to merge
10
commits into
pytorch:master
Choose a base branch
from
joemunene-by:feat/welford-running-stats-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f6dc05d
feat(metrics): add WelfordVariance and WelfordCovariance helpers
joemunene-by 1b82442
review: drop device/dtype handling, switch to dataclass, no flatten
joemunene-by e153f57
review: collapse update into merge, inline _zero, expand merge docstring
joemunene-by 89d5b36
review: address Copilot pass — docstring drift, no_grad on merge, inl…
joemunene-by 1878aeb
review: trim docstrings, function-style tests, prefix-step numpy check
joemunene-by 9bb7da8
review: simpler all_gather pattern in module docstring
joemunene-by 5a13b17
review: trim verbose docstring on covariance no-clamp rationale
joemunene-by 3e21e2b
Merge branch 'master' into feat/welford-running-stats-helper
aaishwarymishra 263863f
reduce docstring
aaishwarymishra b8fd9b6
review: add end-to-end metric and distributed reduction test
joemunene-by File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| from dataclasses import dataclass, field | ||
|
|
||
| import torch | ||
|
|
||
|
|
||
| @dataclass | ||
| class WelfordVariance: | ||
| """Running mean and population variance via Welford's online algorithm. | ||
|
|
||
| Fold batches in with :meth:`update`. Read off via :attr:`mean`, | ||
| :attr:`variance`, :attr:`std`. Combine two accumulators with | ||
| :meth:`merge` (Chan parallel formula). | ||
| """ | ||
|
|
||
| # mean: running sample mean. | ||
| # sum_sq_dev_from_mean: Σ (x_i - mean)^2, conventionally called M2. | ||
| n_samples: int = 0 | ||
| mean: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
| sum_sq_dev_from_mean: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
|
|
||
| @torch.no_grad() | ||
| def update(self, batch: torch.Tensor) -> None: | ||
| """Fold ``batch`` into the running state. Empty batches are a no-op. | ||
|
|
||
| Any tensor shape is accepted and treated as ``numel`` scalar samples. | ||
| """ | ||
| if batch.numel() == 0: | ||
| return | ||
| batch = batch.detach() | ||
| batch_mean = batch.mean() | ||
| self.merge( | ||
| WelfordVariance( | ||
| n_samples=batch.numel(), | ||
| mean=batch_mean, | ||
| sum_sq_dev_from_mean=(batch - batch_mean).square().sum(), | ||
| ) | ||
| ) | ||
|
|
||
| @torch.no_grad() | ||
| def merge(self, other: "WelfordVariance") -> None: | ||
| """Combine ``other`` into ``self`` via the Chan parallel formula. | ||
|
|
||
| For two accumulators with sample counts ``n_a, n_b`` and M2 sums | ||
| ``M2_a, M2_b``:: | ||
|
|
||
| M2 = M2_a + M2_b + (mean_b - mean_a)^2 * n_a * n_b / (n_a + n_b) | ||
|
|
||
| The third term corrects for the spread of the two local means | ||
| about the combined mean. | ||
| """ | ||
| if other.n_samples == 0: | ||
| return | ||
| if self.n_samples == 0: | ||
| # Copy so callers cannot mutate ``other`` and silently affect self. | ||
| self.n_samples = other.n_samples | ||
| self.mean = other.mean.detach().clone() | ||
| self.sum_sq_dev_from_mean = other.sum_sq_dev_from_mean.detach().clone() | ||
| return | ||
|
|
||
| n_a, n_b = self.n_samples, other.n_samples | ||
| n_ab = n_a + n_b | ||
| delta = other.mean - self.mean | ||
|
|
||
| self.mean = self.mean + delta * n_b / n_ab | ||
| self.sum_sq_dev_from_mean = ( | ||
| self.sum_sq_dev_from_mean + other.sum_sq_dev_from_mean + delta * delta * n_a * n_b / n_ab | ||
| ) | ||
| self.n_samples = n_ab | ||
|
|
||
| @property | ||
| def variance(self) -> torch.Tensor: | ||
| """Population variance (divisor ``n``). Zero on an empty accumulator.""" | ||
| if self.n_samples == 0: | ||
| return torch.tensor(0.0) | ||
| # Variance is non-negative by definition; clamp guards against float | ||
| # rounding producing a tiny negative value when all samples are equal. | ||
| return torch.clamp(self.sum_sq_dev_from_mean / self.n_samples, min=0.0) | ||
|
|
||
| @property | ||
| def std(self) -> torch.Tensor: | ||
| """Population standard deviation (divisor ``n``).""" | ||
| return self.variance.sqrt() | ||
|
|
||
|
|
||
| @dataclass | ||
| class WelfordCovariance: | ||
| """Running covariance for a pair ``(x, y)`` via Welford + Chan. | ||
|
|
||
| Same online algorithm as :class:`WelfordVariance`, extended with the | ||
| cross-product accumulator ``sum_product_of_devs = Σ (x_i - mean_x)(y_i - mean_y)``. | ||
| Read off via :attr:`variance_x`, :attr:`variance_y`, :attr:`covariance`, | ||
| :meth:`correlation`. | ||
| """ | ||
|
|
||
| n_samples: int = 0 | ||
| mean_x: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
| mean_y: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
| sum_sq_dev_x: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
| sum_sq_dev_y: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
| sum_product_of_devs: torch.Tensor = field(default_factory=lambda: torch.tensor(0.0)) | ||
|
|
||
| @torch.no_grad() | ||
| def update(self, batch_x: torch.Tensor, batch_y: torch.Tensor) -> None: | ||
| """Fold a paired batch into the running state. ``batch_x`` and | ||
| ``batch_y`` must have the same shape.""" | ||
| if batch_x.shape != batch_y.shape: | ||
| raise ValueError( | ||
| f"batch_x and batch_y must have the same shape, got {tuple(batch_x.shape)} and {tuple(batch_y.shape)}." | ||
| ) | ||
| if batch_x.numel() == 0: | ||
| return | ||
|
|
||
| x = batch_x.detach() | ||
| y = batch_y.detach() | ||
| mean_x_b = x.mean() | ||
| mean_y_b = y.mean() | ||
| dx = x - mean_x_b | ||
| dy = y - mean_y_b | ||
| self.merge( | ||
| WelfordCovariance( | ||
| n_samples=x.numel(), | ||
| mean_x=mean_x_b, | ||
| mean_y=mean_y_b, | ||
| sum_sq_dev_x=dx.square().sum(), | ||
| sum_sq_dev_y=dy.square().sum(), | ||
| sum_product_of_devs=(dx * dy).sum(), | ||
| ) | ||
| ) | ||
|
|
||
| @torch.no_grad() | ||
| def merge(self, other: "WelfordCovariance") -> None: | ||
| """Combine ``other`` into ``self``. Same correction term as | ||
| :meth:`WelfordVariance.merge`, applied once per second moment | ||
| (``sum_sq_dev_x``, ``sum_sq_dev_y``, ``sum_product_of_devs``).""" | ||
| if other.n_samples == 0: | ||
| return | ||
| if self.n_samples == 0: | ||
| self.n_samples = other.n_samples | ||
| self.mean_x = other.mean_x.detach().clone() | ||
| self.mean_y = other.mean_y.detach().clone() | ||
| self.sum_sq_dev_x = other.sum_sq_dev_x.detach().clone() | ||
| self.sum_sq_dev_y = other.sum_sq_dev_y.detach().clone() | ||
| self.sum_product_of_devs = other.sum_product_of_devs.detach().clone() | ||
| return | ||
|
|
||
| n_a, n_b = self.n_samples, other.n_samples | ||
| n_ab = n_a + n_b | ||
| delta_x = other.mean_x - self.mean_x | ||
| delta_y = other.mean_y - self.mean_y | ||
|
|
||
| self.mean_x = self.mean_x + delta_x * n_b / n_ab | ||
| self.mean_y = self.mean_y + delta_y * n_b / n_ab | ||
|
|
||
| # Three parallel-formula combinations. Coefficient ``n_a * n_b / n_ab`` | ||
| # is inlined per term so arithmetic stays on the operand dtype/device. | ||
| self.sum_sq_dev_x = self.sum_sq_dev_x + other.sum_sq_dev_x + delta_x * delta_x * n_a * n_b / n_ab | ||
| self.sum_sq_dev_y = self.sum_sq_dev_y + other.sum_sq_dev_y + delta_y * delta_y * n_a * n_b / n_ab | ||
| self.sum_product_of_devs = ( | ||
| self.sum_product_of_devs + other.sum_product_of_devs + delta_x * delta_y * n_a * n_b / n_ab | ||
| ) | ||
| self.n_samples = n_ab | ||
|
|
||
| @property | ||
| def variance_x(self) -> torch.Tensor: | ||
| """Population variance of ``x``.""" | ||
| if self.n_samples == 0: | ||
| return torch.tensor(0.0) | ||
| return torch.clamp(self.sum_sq_dev_x / self.n_samples, min=0.0) | ||
|
|
||
| @property | ||
| def variance_y(self) -> torch.Tensor: | ||
| """Population variance of ``y``.""" | ||
| if self.n_samples == 0: | ||
| return torch.tensor(0.0) | ||
| return torch.clamp(self.sum_sq_dev_y / self.n_samples, min=0.0) | ||
|
|
||
| @property | ||
| def covariance(self) -> torch.Tensor: | ||
| """Population covariance of ``(x, y)``.""" | ||
| if self.n_samples == 0: | ||
| return torch.tensor(0.0) | ||
| return self.sum_product_of_devs / self.n_samples | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You did not use |
||
|
|
||
| def correlation(self, eps: float = 1e-8) -> torch.Tensor: | ||
| """Pearson correlation. ``eps`` floors the denominator so a | ||
| constant-variable input returns ``0`` instead of ``NaN``.""" | ||
| denom = torch.clamp(self.variance_x.sqrt() * self.variance_y.sqrt(), min=eps) | ||
| return self.covariance / denom | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.