Skip to content

Make simple rules numerically stable in higher-order derivatives - #125

Draft
devmotion wants to merge 1 commit into
masterfrom
dw/higher-order-stability-basic
Draft

devmotion wants to merge 1 commit into
masterfrom
dw/higher-order-stability-basic

Conversation

@devmotion

Copy link
Copy Markdown
Member

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, with log1mexp'(x) = -inv(expm1(-x)), the second derivative evaluates to -abs2(inv(expm1(-x))) * exp(-x), which underflows to -0.0 for x ≲ −355 and becomes NaN (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 new hypot rule) is in the stacked PR #124.

Rule Problem New form
log1mexp -0/NaN at 2nd/3rd order for x ≪ 0 exp(x) / expm1(x)
log2mexp up to 248 ε first-order error -exp(x) / (1 - expm1(x))
logabstanh 2nd/3rd order underflow to 0 for |x| ≳ 186 2 * csch(2x)
logistic (and so log1pexp, loglogistic, log1mlogistic) cancellation in 2nd/3rd order near 0 abs2(sech(x / 2)) / 4
asec, acsc, asecd, acscd 2nd/3rd order underflow to 0 for |x| ≳ 1e77 inv(abs(x)) * inv(sqrt(abs(x) - 1) * sqrt(abs(x) + 1))
acoth 2nd/3rd order underflow to 0 for |x| ≳ 1e77 inv(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 exceeds 64 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.jl adds 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 with BigFloat at the points that failed before. 29 of them fail with the rules on master.

🤖 Generated with Claude Code

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

No deployments
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.

1 participant