Commit 55b2238be for llama.cpp

commit 55b2238be803a11835b20f4409e1af809895951d
Author: gianni-cor <gianfranco.cordella@tether.io>
Date:   Thu Nov 26 05:38:00 2026 +0100

    ggml : add TANH backward pass (#32136)

    Build the gradient dy * (1 - y^2) from the forward output with existing
    ops, no new op is needed. Add TANH to the gradient-checked unary ops.

    Signed-off-by: gianni-cor <gianfranco.cordella@tether.io>

diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index c616fa39b..126167098 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -7271,6 +7271,13 @@ static void ggml_compute_backward(
                         ggml_add_or_set(ctx, cgraph, isrc0, ggml_mul(ctx, grad, ggml_sigmoid(ctx, src0)));
                     }
                 } break;
+                case GGML_UNARY_OP_TANH: {
+                    if (src0_needs_grads) {
+                        struct ggml_tensor * tanh_sq  = ggml_sqr(ctx, tensor);
+                        struct ggml_tensor * one_m_sq = ggml_scale_bias(ctx, tanh_sq, -1.0f, 1.0f);
+                        ggml_add_or_set(ctx, cgraph, isrc0, ggml_mul(ctx, grad, one_m_sq));
+                    }
+                } break;
                 default: {
                     fprintf(stderr, "%s: unsupported unary op for backward pass: %s\n",
                         __func__, ggml_unary_op_name(ggml_get_unary_op(tensor)));
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index a2a7c9a95..2379dd823 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -2176,7 +2176,7 @@ struct test_unary : public test_case {
     ggml_tensor * build_graph(ggml_context * ctx) override {
         const bool grad_supported = op == GGML_UNARY_OP_ABS || op == GGML_UNARY_OP_SGN || op == GGML_UNARY_OP_NEG ||
             op == GGML_UNARY_OP_STEP || op == GGML_UNARY_OP_RELU || op == GGML_UNARY_OP_SILU ||
-            op == GGML_UNARY_OP_EXPM1 || op == GGML_UNARY_OP_SOFTPLUS || op == GGML_UNARY_OP_SIGMOID;
+            op == GGML_UNARY_OP_EXPM1 || op == GGML_UNARY_OP_SOFTPLUS || op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH;

         ggml_tensor * a;
         if (v & 1) {
@@ -2218,8 +2218,8 @@ struct test_unary : public test_case {
             max =  10.f;
         }

-        // saturated sigmoid inputs give zero analytic and numeric gradients, which the grad check cannot compare
-        if (mode == MODE_GRAD && op == GGML_UNARY_OP_SIGMOID) {
+        // saturated inputs give zero analytic and numeric gradients, which the grad check cannot compare
+        if (mode == MODE_GRAD && (op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH)) {
             min = -2.f;
             max =  2.f;
         }
@@ -2231,16 +2231,19 @@ struct test_unary : public test_case {
     }

     float grad_eps() override {
+        if (op == GGML_UNARY_OP_TANH) {
+            return 0.15f;
+        }
         return op == GGML_UNARY_OP_SIGMOID ? 0.25f : 15.0f;
     }

     bool grad_precise() override {
-        return op == GGML_UNARY_OP_SIGMOID;
+        return op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH;
     }

-    // f32 rounding of a large summed loss exceeds the tolerance, so only the small sigmoid case is grad checked
+    // f32 rounding of a large summed loss exceeds the tolerance, so only the small cases are grad checked
     int64_t grad_nmax() override {
-        return op == GGML_UNARY_OP_SIGMOID ? 64 : test_case::grad_nmax();
+        return (op == GGML_UNARY_OP_SIGMOID || op == GGML_UNARY_OP_TANH) ? 64 : test_case::grad_nmax();
     }

     std::vector<float> grad_expect() override {
@@ -11172,6 +11175,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
         }
     }
     test_cases.emplace_back(new test_unary(GGML_UNARY_OP_SIGMOID, GGML_TYPE_F32, { 16, 4, 1, 1 }));
+    test_cases.emplace_back(new test_unary(GGML_UNARY_OP_TANH, GGML_TYPE_F32, { 16, 4, 1, 1 }));

     // fused relu + sqr (squared ReLU)
     for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32}) {