pytorch - 💡(How to fix) Fix GroupNorm with num_groups=0 raises ZeroDivisionError instead of ValueError

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

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:

Root Cause

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.

Code Example

### 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

---

### Actual behavior

---

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:

---

### 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
RAW_BUFFERClick to expand / collapse
### 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

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:

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

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

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

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

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

ValueError: num_groups must be a positive integer

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix GroupNorm with num_groups=0 raises ZeroDivisionError instead of ValueError