bug(nn): GroupNorm uses biased variance (ddof=0) in .train() vs unbiased (ddof=1) in .eval()
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
- Domain
- machine-learning
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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NVIDIA/physicsnemo
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
NVIDIA/physicsnemo#2354 ·
Maintainers usually reply within 2 days
-
🐛[BUG]: ONNX export fails for Transolver with plus=FalsePossibly taken @jihonjoa claimed this 5 days ago. Open? - Needs Triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NVIDIA/physicsnemo#2323 ·
Maintainers usually reply within 2 days
-
swe_nonlinear_pino: physicsnemo_fdm_swe_nonlin slices batch dim ([1:-1]) -> empty PDE residual / NaN loss_pde with default batchsize=1Possibly taken @dmitriySem claimed this 27 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NVIDIA/physicsnemo#2214 ·
Maintainers usually reply within 2 days
-
Wasserstein CDF distance uses the first width for every binPossibly taken @sylvesterkaczmarek claimed this 60 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
NVIDIA/physicsnemo#2042 ·
Maintainers usually reply within 2 days
-
🐛[BUG]: Darcy2D Jacobi kernel divides grad(k)·grad(u) by 2dx instead of 4dx², so the data doesn't solve the Darcy equationMay be free again @coreyjadams claimed this 58 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NVIDIA/physicsnemo#2036 · 2 comments · 1 assignee ·
Maintainers usually reply within 2 days
All issues in NVIDIA/physicsnemo
Similar issues
-
essreduce
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
Maintainers usually reply within 1 day
-
[PORT] A2A: map audio file parts to audio content and ignore MIME parameters (TS→Py)Possibly taken A pull request linked to this issue is open or already merged. Openarea-a2a bug port python
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
strands-agents/harness-sdk#6697 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TigreGotico/orthography2ipa#2950 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TigreGotico/text2spoken#929 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100