pytorch / pytorch/pytorch

GroupNorm with num_groups=0 raises ZeroDivisionError instead of ValueError

Open
#184,280 8 comments 0 reactions 0 assignees View on GitHub
bot-triaged enhancement low priority module: error checking module: nn module: norms and normalization triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

```md
### Describe the bug

`torch.nn.GroupNorm(0, 4)` raises a raw `ZeroDivisionError` instead of validating `num_groups`.

Since `num_groups` is a user-facing constructor argument, `num_groups <= 0` should be rejected with a clear validation error instead of leaking an internal modulo-by-zero failure.

### Repro

```py
import torch

try:
torch.nn.GroupNorm(0, 4)
except Exception as e:
print(type(e).__name__, str(e))
```

### Actual behavior

```text
ZeroDivisionError integer modulo by zero
```

The exception happens during module construction, before any CPU/CUDA forward pass is reached.

### Expected behavior

PyTorch should reject `num_groups <= 0` with a clear validation error, for example:

```text
ValueError: num_groups must be a positive integer
```

### Why this matters

This is a small API validation / error-reporting issue. A clear `ValueError` would make invalid user input easier to understand and easier to handle in fuzzing or automated testing.

### Environment

```text
Python: 3.12.13
PyTorch: 2.10.0+cu128
CUDA: 12.8
GPU: Tesla T4
```

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @malfet

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.