Repository navigation
Conversation
DiffRules' `1 - tanh(x)^2` returns 0 once `tanh(x)` rounds to ±1 (JuliaDiff/DiffRules.jl#102). Define `tanh(::Dual)` by hand: reuse the primal value for `|x| < 1` and use `sech(x)^2` otherwise, as in JuliaDiff/ChainRules.jl#864. 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 #874 +/- ##
==========================================
+ Coverage 91.24% 91.28% +0.04%
==========================================
Files 11 11
Lines 1085 1090 +5
==========================================
+ Hits 990 995 +5
Misses 95 95 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The DiffRules rule for
tanhis1 - tanh(x)^2(JuliaDiff/DiffRules.jl#102). Oncetanh(x)rounds to ±1, it returns 0. In Float64 the relative error is already 1e-8 at x = 10 and reaches 100% for |x| ≥ 20.This PR defines
tanh(::Dual)by hand, next tosin/cos, and skips the DiffRules rule:This is the same split as JuliaDiff/ChainRules.jl#864:
sech(x)^2, which is accurate everywhere.DiffRules can't use this branch itself, because Symbolics evaluates DiffRules' expressions on symbolic arguments. JuliaDiff/DiffRules.jl#122 switches the rule to the branch-free
sech(x)^2instead. Without this PR, ForwardDiff would pay for an extracoshfor allx. This PR works with both the current and the updated DiffRules.Performance
ns per
ForwardDiff.derivative(tanh, x), broadcast over 10⁴ points:The extra cost for |x| ≥ 1 is the
coshthat's needed for an accurate result. At those inputs the old result was inaccurate, or exactly 0.Tests
DualTest.jlcompares with≈fortanh, since the formula differs for |x| < 1.BigFloat(rtol = 4 eps) for Float32 and Float64 up to |x| = 300, for plain and nested duals.🤖 Generated with Claude Code