Repository navigation
Conversation
Higher-order derivatives (e.g., computed with nested forward-mode AD) differentiate the expressions of the rules. Rewrite rules whose derivatives suffer from catastrophic cancellation or intermediate over- and underflow, without introducing branches: - log1mexp: `exp(x) / expm1(x)` (previously NaN for x < -709) - log2mexp: `-exp(x) / (1 - expm1(x))` (also improves first order) - logabstanh: `2 * csch(2x)` - logistic: `abs2(sech(x / 2)) / 4` (also fixes log1pexp, loglogistic, log1mlogistic) - asec, acsc, asecd, acscd: avoid over- and underflow for large |x| - acoth: `inv(1 - x) * inv(1 + x)` Add a minimal nestable dual number type to the tests (ForwardDiff cannot be used because it depends on DiffRules) and test higher-order derivatives against BigFloat references. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Higher-order derivatives computed with nested forward-mode AD (e.g. nested
ForwardDiff.Duals) differentiate the derivative expressions of the rules. So a rule can be accurate at first order and still fail at higher orders. For example, withlog1mexp'(x) = -inv(expm1(-x)), the second derivative evaluates to-abs2(inv(expm1(-x))) * exp(-x), which underflows to-0.0for x ≲ −355 and becomesNaN(0 * Inf) for x < −709, while the true value is ≈ −eˣ.This PR contains the uncontroversial part of the fixes: single-expression rewrites without branches. The rest (rules that need branches,
hypot, and the rules that are only stable together with the newhypotrule) is in the stacked PR #124.log1mexp-0/NaNat 2nd/3rd order for x ≪ 0exp(x) / expm1(x)log2mexp-exp(x) / (1 - expm1(x))logabstanh2 * csch(2x)logistic(and solog1pexp,loglogistic,log1mlogistic)abs2(sech(x / 2)) / 4asec,acsc,asecd,acscdinv(abs(x)) * inv(sqrt(abs(x) - 1) * sqrt(abs(x) + 1))acothinv(1 - x) * inv(1 + x)How the rules were checked: all Base, NaNMath and LogExpFunctions rules at orders 1–3, including mixed partials. Each result is compared with the same computation in 512-bit
BigFloat, in Float32 and Float64, over inputs from ±1e-300 to ±1e300 and near the singularities. A point is flagged when the relative error exceeds64 eps · max(1, κ), where κ is the condition number.Tests: ForwardDiff can't be a test dependency because it would create a dependency cycle. So
test/dual.jladds a minimal nestable dual number whose methods are generated from the rules in DiffRules, with division matching ForwardDiff. The new tests compare 2nd and 3rd derivatives withBigFloatat the points that failed before. 29 of them fail with the rules onmaster.🤖 Generated with Claude Code