From 5c3d0444650599afb4e39ec3bcc91860c8a4b6f1 Mon Sep 17 00:00:00 2001 From: Teddy Tennant Date: Thu, 27 Aug 2026 09:51:17 -0400 Subject: [PATCH] fix asinh gradient at zero (#17758) --- test/backend/test_ops.py | 1 + tinygrad/mixin/elementwise.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/backend/test_ops.py b/test/backend/test_ops.py index 303a7cc37d..d0263b9f5b 100644 --- a/test/backend/test_ops.py +++ b/test/backend/test_ops.py @@ -1813,6 +1813,7 @@ class TestOps(unittest.TestCase): helper_test_op([(45,65)], lambda x: x.asinh(), grad_atol=1e-6, low=-300, high=-297) helper_test_op([(45,65)], lambda x: x.asinh(), grad_atol=1e-6, low=300, high=303) helper_test_op([(45,65)], lambda x: x.asinh(), grad_atol=1e-6, low=-1e10, high=-1e9) + helper_test_op(None, lambda x: x.asinh(), grad_atol=1e-6, vals=[[-1.0, 0.0, 1.0]]) def test_acosh(self): helper_test_op([(45,65)], lambda x: x.acosh(), grad_atol=1e-6) helper_test_op([(45,65)], lambda x: x.acosh(), grad_atol=1e-3, grad_rtol=1e-2, low=-300, high=-297) diff --git a/tinygrad/mixin/elementwise.py b/tinygrad/mixin/elementwise.py index dec78a65e0..724b022d6e 100644 --- a/tinygrad/mixin/elementwise.py +++ b/tinygrad/mixin/elementwise.py @@ -870,7 +870,7 @@ class ElementwiseMixin(CreationMixin): print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).asinh().numpy()) ``` """ - return self.sign() * (self.abs() + (self.square() + 1).sqrt()).log() + return (sg:=(self<0).where(-1.0, 1.0)) * (self*sg + (self.square() + 1).sqrt()).log() def acosh(self) -> Self: """