Repository navigation
Add and improve LogExpFunctions rules - #121
Merged
Merged
Conversation
Requires LogExpFunctions 1 (logabstanh was added in 1.0.0). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
The Tracker downstream failure is pre-existing (also on master) and unrelated to this PR: Tracker's |
`2 / sinh(2x)` needs one transcendental call instead of two and is more accurate than `inv(cosh(x) * sinh(x))`. With LogExpFunctions >= 1, all unary rules have Float32 and Float64 methods, so the `hasmethod` guard in the tests is no longer needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Port the rules from JuliaStats/LogExpFunctions.jl#138: more accurate logistic, log1psq, log1mexp, logexpm1, logaddexp and logsubexp rules, and new rules for loglogistic, log1mlogistic, logitexp and logit1mexp. Fixes NaN derivatives of logaddexp(±Inf, ±Inf) (JuliaStats/LogExpFunctions.jl#61). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
added a commit
to JuliaDiff/ForwardDiff.jl
that referenced
this pull request
Nov 10, 2026
`LogExpFunctions.logitexp` and `logit1mexp` are only defined for `x < 0`. JuliaDiff/DiffRules.jl#121 adds rules for them, so the tests that loop over all DiffRules rules evaluate them at `rand()` and throw a `DomainError`. Treat them like `log1mexp` and `log2mexp`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
devmotion
added a commit
to JuliaDiff/ReverseDiff.jl
that referenced
this pull request
Nov 10, 2026
`LogExpFunctions.logitexp` and `logit1mexp` are only defined for `x < 0`. JuliaDiff/DiffRules.jl#121 adds rules for them, so the tests that loop over all DiffRules rules evaluate them at `rand()` and throw a `DomainError`. Treat them like `log1mexp` and `log2mexp`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 10, 2026
devmotion
added a commit
to JuliaDiff/ReverseDiff.jl
that referenced
this pull request
Nov 10, 2026
…333) `LogExpFunctions.logitexp` and `logit1mexp` are only defined for `x < 0`. JuliaDiff/DiffRules.jl#121 adds rules for them, so the tests that loop over all DiffRules rules evaluate them at `rand()` and throw a `DomainError`. Treat them like `log1mexp` and `log2mexp`. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Adds rules for
LogExpFunctions.logcosh,logabssinh,logabstanh,loglogistic,log1mlogistic,logitexpandlogit1mexp, and makes the existing rules forlogistic,log1psq,log1mexp,logexpm1,logaddexpandlogsubexpmore accurate. Fixes JuliaStats/LogExpFunctions.jl#61 (NaNderivatives oflogaddexp(±Inf, ±Inf)in ForwardDiff).New rules
logcosh(x)tanh(x)logabssinh(x)coth(x)logabstanh(x)2 / sinh(2x)loglogistic(x)logistic(-x)log1mlogistic(x)-logistic(x)logitexp(x)-inv(expm1(x))logit1mexp(x)inv(expm1(x))The rules for
logcoshandlogabssinhmatch the ChainRules rules in LogExpFunctions'ChainRulesCoreExt. Forlogabstanh, ChainRules usesinv(cosh(x) * sinh(x)); the equivalent2 / sinh(2x)needs one transcendental call instead of two and is more accurate (max error vs. a BigFloat reference: 2.1 vs. 3.3 ulp forFloat64, 1.8 vs. 3.0 ulp forFloat32). Both forms agree at ±0, ±Inf and NaN.Without these rules, ForwardDiff differentiates through the generic implementations, so
Duals miss the specializedFloat32/Float64kernels (e.g. the polynomial kernel oflogcosh). With the rules, the primal value of aDualis identical (===) to theFloat64result, and the derivative comes from the closed form. See also JuliaStats/LogExpFunctions.jl#136, which fixed the accuracy of the genericlogcoshpath forDuals.Improved rules
The rules are ported from JuliaStats/LogExpFunctions.jl#138, which makes the same changes to the ChainRules rules.
logistic(x)z * (1 - z)withz = logistic(x)e / (1 + e)^2withe = exp(-abs(x))log1psq(x)2x / (1 + x^2)abs(x) < 1, else2 / (x + inv(x))log1mexp(x)-exp(x - log1mexp(x))-inv(expm1(-x))logexpm1(x)exp(x - logexpm1(x))-inv(expm1(-x))logaddexp(x, y)exp(x - logaddexp(x, y)),exp(y - logaddexp(x, y))logistic(x - y),logistic(y - x)logsubexp(x, y)±exp(x - z),∓exp(y - z)withz = logsubexp(x, y)-inv(expm1(y - x)),-inv(expm1(x - y))Max error vs. a BigFloat reference (
Float64, |x| from 1e-300 to 700), before → after:logistic8.5e15 → 1.5 ulp (cancellation in1 - z),log1psq9e15 → 1.0 ulp (overflow ofx^2),log1mexpandlogexpm1502 → 1.0 ulp.logaddexpandlogsubexpare limited by the rounding ofx - y; all other rules are accurate to ≤ 1.5 ulp (Float32andFloat64). The new rules are also faster: per call,logaddexp41 → 7.5 ns,logsubexp25 → 10 ns,log1mexp/logexpm1~20 → 5 ns (log1psqpays ~0.4 ns for the branch).Behaviour changes
logaddexp(x, y)withx == y == ±Inf: partial derivatives(1/2, 1/2)instead of(NaN, NaN). As inLogExpFunctions.logaddexp, theNaNdifferencex - yis replaced with zero. This fixesForwardDiff.derivative(t -> logaddexp(-Inf * t, -Inf * t), 1.0)(now-Inf) andlogaddexp(Dual(-Inf, 0.0), Dual(-Inf, 0.0))(nowDual(-Inf, 0.0)) without NaN-safe mode.logsubexp(x, x): partial derivatives(Inf, -Inf)instead of(-Inf, Inf), the limits asxapproachesyfrom above (wherelogsubexpis differentiable). This includesx == y == -Inf(as inLogExpFunctions.logsubexp);x == y == Infstill givesNaN.x - ywith zero is restricted to infinite arguments: for finitex == y,x - yis already zero, and replacing it would drop higher-order derivatives in nested AD with number types whose==only compares primal values.Changes
src/rules.jl.0.3.2, 1to1:logcoshwas added in 0.3.6,logabssinhin 0.3.24,logabstanhin 1.0.0, andloglogistic,log1mlogistic,logitexpandlogit1mexpin 0.3.28.logabssinhandlogabstanhhave a log singularity at 0, andlogitexpandlogit1mexpone at 0 with domainx < 0, so their finite-difference check points are shifted away from 0.Float32andFloat64methods, so thehasmethodguard around the finite-difference check is removed.logistic,log1psq,log1mexpandlogexpm1are added to the accuracy tests from Avoid catastrophic cancellation in the derivatives oftanhand inverse trigonometric/hyperbolic functions #122 (rule evaluated inFloat32/Float64vs. in 256-bitBigFloat), at arguments where the previous rules fail: large|x|forlogisticandlog1psq, and small|x|forlog1mexpandlogexpm1.Inf/NaNarguments oflogaddexp,logsubexp,log1mexpandlogexpm1, and for the accuracy oflogaddexpandlogsubexp(arguments of large magnitude and nearly equal arguments). These fail with the previous rules.Note for #120:
test/baseline.jlwill need entries for the seven new rules.🤖 Generated with Claude Code