Skip to content

fix(clipping): keep zero-update reverse gradients finite - #1815

Merged
selamw1 merged 2 commits into
google-deepmind:mainfrom
jabrailkhalil:fix/global-norm-zero-cotangents
Oct 19, 2026
Merged

selamw1 merged 2 commits into
google-deepmind:mainfrom
jabrailkhalil:fix/global-norm-zero-cotangents

Conversation

@jabrailkhalil

@jabrailkhalil jabrailkhalil commented Oct 12, 2026 •

Copy link
Copy Markdown

Summary

I fix NaN reverse-mode derivatives of clip_by_global_norm at zero updates with a positive threshold. The forward mapping is locally the identity, but the unused clipping branch differentiates a square root and division at zero.

Mask the squared norm before the unused branch's square root and division. Keep the existing strict threshold comparison, nonzero clipped derivatives, public API and dependencies unchanged.

Closes #1814. This is separate from #1752's zero-threshold forward-value fix; it does not change that boundary contract or introduce the optional performance path discussed in #1452. No performance claim is made.

Testing plan — executed

Windows CPU, Python 3.12.10, JAX/jaxlib 0.11.2:

  • Clipping module: 12 passed, 12 subtests passed. The new zero-update reverse-Jacobian and three-step meta-gradient regressions fail on unchanged main in both eager and JIT mode. The nonzero-Jacobian control passes on main and the fix.
  • All transforms, numerics, tree math and optimizer aliases: 789 passed, 62 skipped, 724 subtests passed. Skips are not counted as executed passes.
  • Sdist and wheel build pass. The installed wheel, tested outside the checkout with JAX/jaxlib 0.5.3, also passes all 12 clipping tests and 12 subtests.
  • Changed-file pre-commit, Ruff, CI-selected Flake8/Pylint checks, changed-line Pyink and git diff --check pass.
  • Additional controls: 96 exact forward comparisons with matching eager/JIT modes across float16, bfloat16, float32 and complex64; finite zero-point reverse Jacobians for real dtypes, complex real coordinates and vmap.

The deterministic three-step inner loop now gives the analytic outer gradient -1.458 instead of NaN; centered float32 finite differences give approximately -1.45793.

These local runs are selected affected-test validation, not the entire bash test.sh or accelerator validation.

On head 5a39453983cc19f3c16f5ad9490868bce35e3c55, the upstream tests workflow also passes all 12 jobs: all five declared Ubuntu CPU test-matrix jobs (including JAX 0.5.3 and nightly), doctests, build/Pyrefly and lint checks.

@selamw1
selamw1 self-requested a review October 13, 2026 00:16

@selamw1 selamw1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM

@selamw1
selamw1 requested a review from rdyro October 13, 2026 00:18

@rdyro rdyro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@selamw1
selamw1 merged commit a706d74 into google-deepmind:main Oct 19, 2026
21 checks passed
copybara-service Bot pushed a commit that referenced this pull request Oct 22, 2026
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.

clip_by_global_norm has NaN reverse-mode derivatives at zero updates with a positive threshold

3 participants