bug(nn): GroupNorm uses biased variance (ddof=0) in .train() vs unbiased (ddof=1) in .eval()

Open Beginner friendly
#2,302 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Read physicsnemo/nn/module/group_norm.py, focusing on the 2D and 3D evaluation branches where variance is calculated. Compare them with the training path and check for existing GroupNorm tests; add or run a train/eval equivalence test using identical inputs. Done means both evaluation paths use population variance and produce the same output as training.

Written by the indexing model from the issue text.

Description

Bug Summary

In physicsnemo/nn/module/group_norm.py (main, v2.2.0, v2.1.1), GroupNorm.forward uses torch.nn.functional.group_norm (biased population variance, ddof=0) when self.training is True, but falls back to x.var(..., keepdim=True) (which defaults to Bessel's unbiased correction correction=1 / ddof=1) in the else (.eval()) branch—affecting both the 3D (line 146 on main) and 2D (line 341 on main, line 257 on v2.1.1/v2.2.0) eval paths:

if self.training:
    x = torch.nn.functional.group_norm(
        x, num_groups=self.num_groups, weight=weight, bias=bias, eps=self.eps
    )
else:
    x = rearrange(x, "b (g c) h w -> b g c h w", g=self.num_groups)
    mean = x.mean(dim=[2, 3, 4], keepdim=True)
    var = x.var(dim=[2, 3, 4], keepdim=True)  # defaults to unbiased=True (ddof=1)!

Because GroupNorm has no running statistics or dropout, .train() and .eval() should produce identical outputs on identical inputs. Currently, switching modes causes a silent numerical shift (e.g. max |y_train - y_eval| = 0.0264 on a 4×4 spatial map in SongUNet / DhariwalUNet / StormCastUNet).

Minimal Reproducer
import torch
from physicsnemo.nn.module.group_norm import GroupNorm

torch.manual_seed(0)
gn = GroupNorm(num_channels=32, num_groups=8, eps=1e-5)
x = torch.randn(1, 32, 4, 4)

gn.train()
y_train = gn(x)

gn.eval()
y_eval = gn(x)

print("max |y_train - y_eval| =", (y_train - y_eval).abs().max().item())  # ~0.026388
Proposed Fix

Pass unbiased=False (or correction=0) to x.var(...) in both the 2D and 3D .eval() branches of physicsnemo/nn/module/group_norm.py. Happy to open a DCO-signed PR with a train/eval equivalence unit test!

Dominant language
Python
Stars
3.5k
Forks
853
Avg merge
2d 2h
Merged PRs (30d)
64

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/physicsnemo

All issues in NVIDIA/physicsnemo

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.