Fix NaN in scale_by_adam on float16 zero-gradient steps - #1755
Open
kartik1pandey wants to merge 2 commits into
Open
Fix NaN in scale_by_adam on float16 zero-gradient steps#1755kartik1pandey wants to merge 2 commits into
kartik1pandey wants to merge 2 commits into
Conversation
eps/eps_root were added in v's own dtype, so the default eps=1e-8 silently underflowed to 0.0 in float16 (smallest subnormal ~6e-8), turning the division-by-zero guard into a 0/0 NaN whenever a step's gradient was exactly zero. Fixes google-deepmind#1754.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Four pre-existing pyrefly errors on main, each a type-stub precision gap rather than a real bug (verified by running the affected test suites): a while_loop carry variable typed as a union across all carry slots, ArrayLike including complex in a real-only comparison, a None baseline only read behind an if-guard, and ravel_pytree's unravel_fn return type. Fixes google-deepmind#1756.
Author
|
@googlebot I signed it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes NaN in
scale_by_adamwhen used withfloat16params on a zero-gradient step.Changes
scale_by_adam's update now addseps/eps_rootin at leastfloat32precision (jnp.promote_types(v.dtype, jnp.float32)) before casting the result back to the original dtype.test_adam_no_nan_on_float16_zero_gradregression test (float16 params, 3 steps of all-zero gradients, asserts every leaf stays finite).Why
eps(a plain Python float, default1e-8) is silently rounded to0.0when added to afloat16array, since float16's smallest representable subnormal is~6e-8:On a step with an exactly-zero gradient (masked tokens, a frozen-then-unfrozen layer, etc.), both moment estimates are
0, so thesqrt(v) + epssafety denominator collapses to0, producing0 / 0 = NaN:Since NaN propagates through every subsequent step via
apply_updates, this silently poisons the rest of training. The fix is a no-op forfloat32/float64(verified bit-for-bit identical output before/after); onlyfloat16/bfloat16behavior changes.Fixes #1754.
Scoped to
scale_by_adamonly —scale_by_amsgrad,scale_by_belief, andscale_by_yogishare the same pattern (tracked in the linked issue); happy to follow up separately.Testing
ruff checkis clean on both changed files.