pytorch - 💡(How to fix) Fix ConstantLR accepts factor=0 and crashes with ZeroDivisionError after total_iters steps [1 pull requests]

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

import torch

optimizer = torch.optim.SGD([torch.randn(1)], lr=0.1) scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer, factor=0.0, total_iters=2)

scheduler.step() # OK scheduler.step() # ZeroDivisionError: float division by zero

Root Cause

In torch/optim/lr_scheduler.py:

  • Line 813: if factor > 1.0 or factor < 0: should be if factor > 1.0 or factor <= 0:
  • Line 854: get_lr() computes group["lr"] * (1.0 / self.factor) which divides by zero when factor=0

Fix Action

Fixed

Code Example

import torch

optimizer = torch.optim.SGD([torch.randn(1)], lr=0.1)
scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer, factor=0.0, total_iters=2)

scheduler.step()  # OK
scheduler.step()  # ZeroDivisionError: float division by zero
RAW_BUFFERClick to expand / collapse

Bug Description

ConstantLR accepts factor=0 without raising an error during initialization. However, after total_iters steps, the get_lr() method computes 1.0 / self.factor, which causes a ZeroDivisionError.

The validation at line 813 of torch/optim/lr_scheduler.py uses factor < 0 (strict inequality), but should use factor <= 0 to also reject zero.

Reproducer

import torch

optimizer = torch.optim.SGD([torch.randn(1)], lr=0.1)
scheduler = torch.optim.lr_scheduler.ConstantLR(optimizer, factor=0.0, total_iters=2)

scheduler.step()  # OK
scheduler.step()  # ZeroDivisionError: float division by zero

Root Cause

In torch/optim/lr_scheduler.py:

  • Line 813: if factor > 1.0 or factor < 0: should be if factor > 1.0 or factor <= 0:
  • Line 854: get_lr() computes group["lr"] * (1.0 / self.factor) which divides by zero when factor=0

Inconsistency

LinearLR in the same file correctly validates with start_factor <= 0 (line 921), rejecting zero. ConstantLR should follow the same pattern.

Expected Behavior

ConstantLR(optimizer, factor=0.0) should raise a ValueError at construction time, not silently accept the value and crash later.

Environment

  • PyTorch version: main branch
  • Python version: 3.x
  • OS: any

cc @vincentqb @jbschlosser @albanD @janeyx99 @crcrpar @malfet @pytorch/optim

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…

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 ConstantLR accepts factor=0 and crashes with ZeroDivisionError after total_iters steps [1 pull requests]