Repository navigation
Conversation
For `Float16`, `Float32` and `Float64`, `NaNMath.sqrt` calls the intrinsic `Base.Intrinsics.sqrt_llvm`, which reverse-mode AD such as Zygote cannot differentiate through. A rule for the intrinsic can't be added to ChainRules: all intrinsics share the type `Core.IntrinsicFunction`, so it would also apply to every other unary intrinsic. A rule on `NaNMath.sqrt` only matches this function. Fixes #109, JuliaDiff/ChainRules.jl#844. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #111 +/- ##
==========================================
+ Coverage 96.73% 96.93% +0.20%
==========================================
Files 1 2 +1
Lines 153 163 +10
==========================================
+ Hits 148 158 +10
Misses 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Multiplying a zero (co)tangent with the derivative `Inf` at `x = 0` or `NaN` at `x < 0` gave `NaN`, e.g. in `ifelse(x > 0, NaNMath.sqrt(x), 0)`; zero (co)tangents now stay zero (cf. JuliaDiff/ChainRules.jl#576). At `x = -0.0`, where `NaNMath.sqrt` returns `-0.0`, the derivative was `-Inf` instead of `Inf`. Tests use the ChainRulesTestUtils defaults for `Float64`, at inputs far enough from 0 that finite differencing stays in the domain, and test the `frule` closer to 0 with tangents scaled to the input. `Float16` and `Float32` are compared with the `Float64` rules, since finite differencing is too inaccurate in lower precision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
commented
Oct 24, 2026
On x86, LLVM folds `x >= 0 ? sqrt_llvm(x) : NaN` into a bare `sqrtsd`, which returns a negative NaN, while const-folded calls return the positive `NaN` literal. With coverage enabled (as in CI) both occur in the tests, so bitwise `===` comparisons of the NaN outputs failed on Julia 1.13. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
why is the derivative different than that of sqrt? |
Member
Author
|
What difference? The 0 at 0? That's a bug of the generic sqrt rule, see JuliaDiff/ChainRules.jl#599 and JuliaDiff/ChainRulesCore.jl#551 |
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.
Fixes #109 and JuliaDiff/ChainRules.jl#844.
For IEEE floats,
NaNMath.sqrtcalls the intrinsicsqrt_llvm, so Zygote fails with "Non-differentiable function Core.Intrinsics.sqrt_llvm". A rule on the intrinsic can't go in ChainRules: all intrinsics share the typeCore.IntrinsicFunction, so it would apply to every unary intrinsic. A rule onNaNMath.sqrtonly matches this function.This PR adds
fruleandrruleforNaNMath.sqrt(::Union{Float16, Float32, Float64})in a ChainRulesCore extension, and bumps the version to 1.2.0. Compared with ChainRules' rule forBase.sqrt:NaNwhen multiplied by the derivativeInfatx = 0orNaNatx < 0, e.g. inifelse(x > 0, NaNMath.sqrt(x), 0)(cf. Current rules for sqrt produce NaN for zero primal and (co)tangents JuliaDiff/ChainRules.jl#576, #599).x = -0.0the derivative isInf, not-Inf.🤖 Generated with Claude Code