From 696a1537992d63f3a3edaa37db935079e176df28 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 26 Jun 2026 18:24:20 +0000 Subject: [PATCH 1/2] Initial plan From d2ddc6ab85b71a968ea15234bd7cc3473b977d5a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 26 Jun 2026 18:34:53 +0000 Subject: [PATCH 2/2] Fix tanh derivative: use 1-tanh(x)^2 instead of 1/cosh(x)^2 for numerical stability --- enzyme/Enzyme/InstructionDerivatives.td | 8 ++++---- enzyme/test/Enzyme/ReverseMode/tanh19.ll | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/enzyme/Enzyme/InstructionDerivatives.td b/enzyme/Enzyme/InstructionDerivatives.td index c0eeb99dcc75..60e626859da0 100644 --- a/enzyme/Enzyme/InstructionDerivatives.td +++ b/enzyme/Enzyme/InstructionDerivatives.td @@ -352,19 +352,19 @@ def : CallPattern<(Op $x, $y), def : CallPattern<(Op $x), ["tanh"], - [(FDiv (DiffeRet), (FMul(Call<(SameTypesFunc<"cosh">), [ReadNone,NoUnwind]> $x):$c, $c))], + [(FMul (DiffeRet), (FSub (ConstantFP<"1.0"> $x), (FMul(Call<(SameFunc), [ReadNone,NoUnwind]> $x):$c, $c)))], (ForwardFromSummedReverse), [ReadNone, NoUnwind] >; def : CallPattern<(Op $x), ["tanhf"], - [(FDiv (DiffeRet), (FMul(Call<(SameTypesFunc<"coshf">), [ReadNone,NoUnwind]> $x):$c, $c))], + [(FMul (DiffeRet), (FSub (ConstantFP<"1.0"> $x), (FMul(Call<(SameFunc), [ReadNone,NoUnwind]> $x):$c, $c)))], (ForwardFromSummedReverse), [ReadNone, NoUnwind] >; def : CallPattern<(Op $x), ["tanhl"], - [(FDiv (DiffeRet), (FMul(Call<(SameTypesFunc<"coshl">), [ReadNone,NoUnwind]> $x):$c, $c))], + [(FMul (DiffeRet), (FSub (ConstantFP<"1.0"> $x), (FMul(Call<(SameFunc), [ReadNone,NoUnwind]> $x):$c, $c)))], (ForwardFromSummedReverse), [ReadNone, NoUnwind] >; @@ -931,7 +931,7 @@ def : CallPattern<(Op (Op $x, $y):$z), def : IntrPattern<(Op $x), [["tanh", "19", ""]], - [(FDiv (DiffeRet), (FMul(Intrinsic<"cosh"> $x):$c, $c))], + [(FMul (DiffeRet), (FSub (ConstantFP<"1.0"> $x), (FMul(Intrinsic<"tanh"> $x):$c, $c)))], (ForwardFromSummedReverse) >; diff --git a/enzyme/test/Enzyme/ReverseMode/tanh19.ll b/enzyme/test/Enzyme/ReverseMode/tanh19.ll index 5fde75e700c7..2f516cd0c6b0 100644 --- a/enzyme/test/Enzyme/ReverseMode/tanh19.ll +++ b/enzyme/test/Enzyme/ReverseMode/tanh19.ll @@ -21,9 +21,10 @@ declare double @__enzyme_autodiff(ptr, ...) ; CHECK: define internal { double } @diffetester(double %x, double %differeturn) ; CHECK-NEXT: entry: -; CHECK-NEXT: %0 = call fast double @llvm.cosh.f64(double %x) +; CHECK-NEXT: %0 = call fast double @llvm.tanh.f64(double %x) ; CHECK-NEXT: %1 = fmul fast double %0, %0 -; CHECK-NEXT: %2 = fdiv fast double %differeturn, %1 -; CHECK-NEXT: %3 = insertvalue { double } undef, double %2, 0 -; CHECK-NEXT: ret { double } %3 +; CHECK-NEXT: %2 = fsub fast double 1.000000e+00, %1 +; CHECK-NEXT: %3 = fmul fast double %differeturn, %2 +; CHECK-NEXT: %4 = insertvalue { double } undef, double %3, 0 +; CHECK-NEXT: ret { double } %4 ; CHECK-NEXT: }