Commit 89a6a4704 for llama.cpp

commit 89a6a4704c2097f393ae644b0d8ede617e588639
Author: Triss Lazaj <114891155+trisslazaj@users.noreply.github.com>
Date:   Wed Dec 2 08:22:50 2026 -0500

    metal, vulkan : use Taylor series for metal ELU, EXPM1, XIELU and vulkan ELU (#33226)

    * metal : use Taylor series for elu for |x| <= 0.5

    * metal & vulkan : changed Metal ELU to use template format, and Metal EXPM1 and XIELU, fixed Vulkan ELU

diff --git a/ggml/src/ggml-metal/kernels/common.h b/ggml/src/ggml-metal/kernels/common.h
index 237ccfb26..51d447bfc 100644
--- a/ggml/src/ggml-metal/kernels/common.h
+++ b/ggml/src/ggml-metal/kernels/common.h
@@ -121,23 +121,6 @@ inline T erf_approx(T x) {
     return sign_x * y;
 }

-template<typename T> T elu_approx(T x);
-
-template<> inline float elu_approx<float>(float x) {
-    return (x > 0.f) ? x : (exp(x) - 1);
-}
-
-template<> inline float4 elu_approx<float4>(float4 x) {
-    float4 res;
-
-    res[0] = (x[0] > 0.0f) ? x[0] : (exp(x[0]) - 1.0f);
-    res[1] = (x[1] > 0.0f) ? x[1] : (exp(x[1]) - 1.0f);
-    res[2] = (x[2] > 0.0f) ? x[2] : (exp(x[2]) - 1.0f);
-    res[3] = (x[3] > 0.0f) ? x[3] : (exp(x[3]) - 1.0f);
-
-    return res;
-}
-
 template <typename T> struct value_type;
 template <> struct value_type<float4x4> { typedef float type; };
 template <> struct value_type<half4x4>  { typedef half  type; };
diff --git a/ggml/src/ggml-metal/kernels/unary.metal b/ggml/src/ggml-metal/kernels/unary.metal
index c8a441609..c3e075a30 100644
--- a/ggml/src/ggml-metal/kernels/unary.metal
+++ b/ggml/src/ggml-metal/kernels/unary.metal
@@ -40,6 +40,22 @@ inline T ggml_metal_log1p(T x) {
     return select(r, x, tiny);
 }

+// exp(x) - 1, accurate as x -> 0
+template <typename T>
+inline T ggml_metal_expm1(T x) {
+    T p = T(1.0f / 40320.0f);
+    p = p * x + T(1.0f / 5040.0f);
+    p = p * x + T(1.0f / 720.0f);
+    p = p * x + T(1.0f / 120.0f);
+    p = p * x + T(1.0f / 24.0f);
+    p = p * x + T(1.0f / 6.0f);
+    p = p * x + T(1.0f / 2.0f);
+    p = p * x + T(1.0f);
+    const T s = x * p;
+    const T e = precise::exp(x) - T(1.0f);
+    return select(e, s, fabs(x) <= T(0.5f));
+}
+
 template <typename T0, typename T, typename TC>
 kernel void kernel_unary_impl(
         constant ggml_metal_kargs_unary & args,
@@ -153,7 +169,7 @@ kernel void kernel_unary_impl(
         }

         if (FC_OP == OP_UNARY_NUM_ELU) {
-            dst_ptr[i0] = ggml_metal_unary_cast<T>(elu_approx(x));
+            dst_ptr[i0] = ggml_metal_unary_cast<T>(select(ggml_metal_expm1(x), x, x > TC(0.0f)));
         }

         if (FC_OP == OP_UNARY_NUM_NEG) {
@@ -189,8 +205,7 @@ kernel void kernel_unary_impl(
         }

         if (FC_OP == OP_UNARY_NUM_EXPM1) {
-            // TODO: precise implementation
-            dst_ptr[i0] = ggml_metal_unary_cast<T>(exp(x) - 1);
+            dst_ptr[i0] = ggml_metal_unary_cast<T>(ggml_metal_expm1(x));
         }

         if (FC_OP == OP_UNARY_NUM_FLOOR) {
@@ -214,7 +229,7 @@ kernel void kernel_unary_impl(
             const TC gate    = TC(xi > TC(0.0f));
             const TC clamped = fmin(xi, TC(args.val));
             const TC y_pos   = TC(args.scale) * xi * xi + TC(args.bias) * xi;
-            const TC y_neg   = (exp(clamped) - TC(1.0f) - xi) * TC(args.slope) + TC(args.bias) * xi;
+            const TC y_neg   = (ggml_metal_expm1(clamped) - xi) * TC(args.slope) + TC(args.bias) * xi;
             dst_ptr[i0] = ggml_metal_unary_cast<T>(gate * y_pos + (TC(1.0f) - gate) * y_neg);
         }
     }
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp b/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
index 756c70b1d..425026e72 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
@@ -64,8 +64,10 @@ float op_tanh(float x) {
     return 1.0f - 2.0f / (exp(2.0f*x) + 1.0f);
 }

+float op_expm1(float x);
+
 float op_elu(float x) {
-    return x < 0.0f ? exp(x) - 1.0f : x;
+    return x < 0.0f ? op_expm1(x) : x;
 }

 float op_relu(float x) {
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 7acff64cf..895f3cc7b 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -2284,6 +2284,21 @@ struct test_unary_softplus_neg : public test_unary {
     }
 };

+struct test_unary_elu_small : public test_unary {
+    const float min;
+    test_unary_elu_small(float min) : test_unary(GGML_UNARY_OP_ELU), min(min) {}
+
+    std::string vars() override {
+        return test_unary::vars() + "," + VAR_TO_STR(min);
+    }
+
+    void initialize_tensors(ggml_context * ctx) override {
+        for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
+            init_tensor_uniform(t, min, 0.0f);
+        }
+    }
+};
+
 // GGML_OP_GLU
 struct test_glu : public test_case {
     const ggml_glu_op op;
@@ -12166,6 +12181,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {

     // unary ops
     test_cases.emplace_back(new test_unary_softplus_neg());
+    test_cases.emplace_back(new test_unary_elu_small(-1e-5f));
+    test_cases.emplace_back(new test_unary_elu_small(-1.0f));
     for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) {
         for (int v : {0, 1}) {
             for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {