Skip to content

fix: ZeroDivisionError in get_lr when lr_decay_iters == warmup_iters#691

Open
Bortlesboat wants to merge 1 commit into
karpathy:masterfrom
Bortlesboat:fix/get-lr-zero-division
Open

fix: ZeroDivisionError in get_lr when lr_decay_iters == warmup_iters#691
Bortlesboat wants to merge 1 commit into
karpathy:masterfrom
Bortlesboat:fix/get-lr-zero-division

Conversation

@Bortlesboat

@Bortlesboat Bortlesboat commented Mar 10, 2026

Copy link
Copy Markdown

Bug

get_lr() divides by zero exactly at the warmup/decay boundary when lr_decay_iters == warmup_iters:

warmup_iters = 100
lr_decay_iters = 100
it = 100
# (it - warmup_iters) / (lr_decay_iters - warmup_iters) => 0 / 0

This can occur in short experimental configurations that intentionally disable the cosine-decay interval.

Fix

Clamp the decay interval denominator to at least one:

decay_ratio = (it - warmup_iters) / max(1, lr_decay_iters - warmup_iters)

At the zero-length boundary, the numerator is also zero, so the scheduler returns the peak learning rate. Iterations after lr_decay_iters still return min_lr through the existing earlier branch, and normal positive-length schedules are unchanged.

When lr_decay_iters and warmup_iters are set to the same value (e.g. both
equal to max_iters for a short run), the cosine decay formula divides by
zero. Guard with max(1, ...) so the edge case returns the peak learning
rate gracefully instead of crashing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant