Repository navigation
Reject NaN lr, momentum and weight_decay in SGD - #199953
G26karthik wants to merge 1 commit into
Conversation
SGD validated these with `x < 0.0`, which is False for NaN, so a NaN value was accepted and turned the parameters into NaN during step(). Use `not 0.0 <= x`, as the other built-in optimizers already do, so NaN raises the same ValueError at construction. Fixes pytorch#199516
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - Ready for human review:
Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199953
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 5a5e00b with merge base 921d395 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
@pytorchbot pre-review accept |
There was a problem hiding this comment.
Automated review of 5a5e00b: nothing blocking; ready for human review.
SGD.init now uses not 0.0 <= x for lr/momentum/weight_decay, so NaN is rejected, matching every sibling optimizer (Adam, RMSprop, Adagrad, etc.). The three new ErrorOptimizerInput entries are genuine regression tests: under the old < 0.0 checks they would surface a TypeError from params=None rather than the expected ValueError. No correctness, BC, or test-adequacy problems found.
This review is AI-generated and advisory. Each new automated review replaces the previous one.
janeyx99
left a comment
There was a problem hiding this comment.
oh i was silly and didn't notice u inverted the operands
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: Comment with id 6048096084 not found Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
## Issue being fixed Fixes pytorch#199516 ## BC-breaking? No ## Summary (human written only) I noticed while comparing how the built-in optimizers validate their hyperparameters that SGD is the only one that accepts NaN for `lr`, `momentum` and `weight_decay`. SGD checks `if lr < 0.0`, which is False for NaN, while Adam, AdamW, RMSprop, Adagrad and the other built-in optimizers use `if not 0.0 <= lr`, which rejects it. The result is a silent NaN training run instead of an error at construction. This PR fixes that by using the same check in SGD. ## Agent details (if applicable) > Prepared with Claude Code (AI assistant) and reviewed by me: > > `SGD.__init__` validated `lr`, `momentum` and `weight_decay` with `x < 0.0`. Ordered comparisons with NaN are `False`, so NaN passed all three checks and made the parameters NaN during `step()`. The checks now use `not 0.0 <= x`, the idiom the other built-in optimizers (Adam, AdamW, RMSprop, Adagrad, ...) already use, so NaN raises the same `ValueError` at construction with the existing messages. Valid and negative values behave exactly as before. The shared idiom is used rather than separate `math.isnan` checks so SGD stays consistent with the rest of `torch.optim`. Adds NaN cases for all three arguments to `optim_error_inputs_func_sgd`. > > Tested on nightly 2.15.0.dev20261001+cpu (Windows, CPU only); both changed files are identical on current main and in that nightly. `test_errors_SGD_cpu_float32` fails without the fix (the NaN `lr` gets past validation and construction raises `TypeError` instead of `ValueError`) and passes with it. All 129 tests matching `SGD` pass (27 skipped, CUDA-only). Lint: the RUFF and PYFMT lintrunner adapters report no issues on the changed files. > > ```bash > python test/test_optim.py -k test_errors_SGD > python test/test_optim.py -k SGD > ``` Pull Request resolved: pytorch#199953 Approved by: https://github.com/janeyx99
Issue being fixed
Fixes #199516
BC-breaking?
No
Summary (human written only)
I noticed while comparing how the built-in optimizers validate their hyperparameters that SGD is the only one that accepts NaN for
lr,momentumandweight_decay. SGD checksif lr < 0.0, which is False for NaN, while Adam, AdamW, RMSprop, Adagrad and the other built-in optimizers useif not 0.0 <= lr, which rejects it. The result is a silent NaN training run instead of an error at construction. This PR fixes that by using the same check in SGD.Agent details (if applicable)