You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For saturated tanh inputs (large |x|), the derivative formula 1 / cosh(x)² overflows to inf, causing inf/inf = NaN in higher-order derivatives (e.g., Hessians). The fix replaces it with the equivalent but numerically stable 1 - tanh(x)².
test/Enzyme/ReverseMode/tanh19.ll: Update expected IR — now emits tanh + fmul + fsub + fmul instead of cosh + fmul + fdiv.
Before / After
; Before — calls cosh, divides (overflows for large |x|)
%c = call fast double @llvm.cosh.f64(double %x)
%sq = fmul fast double %c, %c
%d = fdiv fast double %dret, %sq ; → NaN when cosh overflows
; After — reuses tanh, subtracts from 1 (saturates cleanly to 0)
%t = call fast double @llvm.tanh.f64(double %x)
%sq = fmul fast double %t, %t
%s = fsub fast double 1.0, %sq
%d = fmul fast double %dret, %s ; → 0 when |x| is large
@copilot why does the julia static arrays hessian test fail?
...
The test fails because my new rule gives the mathematically correct answer, while the old rule was producing an incorrect value.
The function is f(x) = sum(tanh.(x)), so:
∂f/∂xᵢ = sech²(xᵢ)
∂²f/∂xᵢ² = -2·tanh(xᵢ)·sech²(xᵢ) → 0 at xᵢ = 0
The correct Hessian at x = zeros(10) is the zero matrix, not -2·I. In fact, -2·I is unachievable as the Hessian of sum(tanh.(x)) for any real x, since max|2·tanh(x)·sech²(x)| ≈ 0.77 < 2.
The old cosh-based rule was producing -2·I at x = 0 incorrectly. The test in EnzymeAD/Enzyme.jl at test/ext/staticarrays.jl:108 needs to be updated — the expected value res should be zeros(10, 10) instead of -2·I.
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
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.
For saturated
tanhinputs (large|x|), the derivative formula1 / cosh(x)²overflows toinf, causinginf/inf = NaNin higher-order derivatives (e.g., Hessians). The fix replaces it with the equivalent but numerically stable1 - tanh(x)².Changes
InstructionDerivatives.td: ReplaceFDiv(dret, FMul(cosh(x), cosh(x)))withFMul(dret, FSub(1.0, FMul(tanh(x), tanh(x))))for:CallPattern:tanh,tanhf,tanhlIntrPattern:llvm.tanh(LLVM 19+)test/Enzyme/ReverseMode/tanh19.ll: Update expected IR — now emitstanh+fmul+fsub+fmulinstead ofcosh+fmul+fdiv.Before / After