Commit b12f22d2f for llama.cpp
commit b12f22d2f27412db16f8746280cf65f33bc1a0dd
Author: Georgi Gerganov <ggerganov@gmail.com>
Date: Tue Nov 24 09:30:43 2026 +0200
ggml : use a range-reduced log1p for softplus (#32655)
* metal : use a range-reduced log1p for softplus
Use the atanh series for x in [-0.5, 1] and precise::log(1 + x) otherwise.
This avoids the cancellation in log(1 + exp(x)) for negative inputs and does
not rely on fast-math folding of (1 + e) - 1.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* tests : add a softplus test for negative inputs
Use inputs in [-16.6, -16.0], where log(1 + exp(x)) has up to ~2x
relative error. This catches both the old form and the fast-math-folded
compensated form.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
* ggml : use log1pf in ggml_compute_softplus_f32
Use log1pf(expf(input)) so the host helper keeps the true value for
x < -16.64.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>
* vulkan : use a range-reduced log1p for softplus
Use the atanh series for x in [-0.5, 1] and log(1 + x) otherwise.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>
* opencl : use a range-reduced log1p for softplus
Use the atanh series for x in [-0.5, 1] and log(1 + x) otherwise.
Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>
* vulkan : move log1p to utils.glsl
Move the range-reduced log1p out of unary.comp into utils.glsl so that
other ops such as topk_moe and ssm_scan can include it. The body is
unchanged; only the call site in op_softplus is renamed.
Co-authored-by: IsNot-10 <209144869@qq.com>
Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
---------
Co-authored-by: IsNot-10 <209144869@qq.com>
diff --git a/ggml/src/ggml-impl.h b/ggml/src/ggml-impl.h
index 7df7e1efd..095ff6433 100644
--- a/ggml/src/ggml-impl.h
+++ b/ggml/src/ggml-impl.h
@@ -105,7 +105,7 @@ static inline bool ggml_impl_is_view(const struct ggml_tensor * t) {
}
static inline float ggml_compute_softplus_f32(float input) {
- return (input > 20.0f) ? input : logf(1 + expf(input));
+ return (input > 20.0f) ? input : log1pf(expf(input));
}
// compute correction dims for YaRN RoPE scaling
diff --git a/ggml/src/ggml-metal/kernels/unary.metal b/ggml/src/ggml-metal/kernels/unary.metal
index b361da996..c8a441609 100644
--- a/ggml/src/ggml-metal/kernels/unary.metal
+++ b/ggml/src/ggml-metal/kernels/unary.metal
@@ -21,6 +21,25 @@ inline bfloat4 ggml_metal_unary_cast<bfloat4, float>(float x) {
}
#endif
+template <typename T>
+inline T ggml_metal_log1p(T x) {
+ const T y = x / (T(2.0f) + x);
+ const T y2 = y * y;
+ T p = T(1.0f / 13.0f);
+ p = p * y2 + T(1.0f / 11.0f);
+ p = p * y2 + T(1.0f / 9.0f);
+ p = p * y2 + T(1.0f / 7.0f);
+ p = p * y2 + T(1.0f / 5.0f);
+ p = p * y2 + T(1.0f / 3.0f);
+ p = p * y2 + T(1.0f);
+ const T s = T(2.0f) * y * p;
+ const T l = precise::log(T(1.0f) + x);
+ const auto in_series = (x >= T(-0.5f)) & (x <= T(1.0f));
+ const auto tiny = fabs(x) < T(2.5e-38f);
+ const T r = select(l, s, in_series);
+ return select(r, x, tiny);
+}
+
template <typename T0, typename T, typename TC>
kernel void kernel_unary_impl(
constant ggml_metal_kargs_unary & args,
@@ -166,7 +185,7 @@ kernel void kernel_unary_impl(
}
if (FC_OP == OP_UNARY_NUM_SOFTPLUS) {
- dst_ptr[i0] = ggml_metal_unary_cast<T>(select(log(1 + exp(x)), x, x > 20));
+ dst_ptr[i0] = ggml_metal_unary_cast<T>(select(ggml_metal_log1p(precise::exp(x)), x, x > TC(20.0f)));
}
if (FC_OP == OP_UNARY_NUM_EXPM1) {
diff --git a/ggml/src/ggml-opencl/kernels/softplus.cl b/ggml/src/ggml-opencl/kernels/softplus.cl
index 6f8b74741..8c59c0ae4 100644
--- a/ggml/src/ggml-opencl/kernels/softplus.cl
+++ b/ggml/src/ggml-opencl/kernels/softplus.cl
@@ -4,6 +4,46 @@
// softplus
//------------------------------------------------------------------------------
+inline float op_log1p(float x) {
+ const float y = x / (2.0f + x);
+ const float y2 = y * y;
+ float p = 1.0f / 13.0f;
+ p = p * y2 + 1.0f / 11.0f;
+ p = p * y2 + 1.0f / 9.0f;
+ p = p * y2 + 1.0f / 7.0f;
+ p = p * y2 + 1.0f / 5.0f;
+ p = p * y2 + 1.0f / 3.0f;
+ p = p * y2 + 1.0f;
+ const float s = 2.0f * y * p;
+ const float l = log(1.0f + x);
+ const float r = select(l, s, (x >= -0.5f) & (x <= 1.0f));
+ return select(r, x, fabs(x) < 2.5e-38f);
+}
+
+inline float4 op_log1p4(float4 x) {
+ const float4 y = x / ((float4)(2.0f) + x);
+ const float4 y2 = y * y;
+ float4 p = (float4)(1.0f / 13.0f);
+ p = p * y2 + (float4)(1.0f / 11.0f);
+ p = p * y2 + (float4)(1.0f / 9.0f);
+ p = p * y2 + (float4)(1.0f / 7.0f);
+ p = p * y2 + (float4)(1.0f / 5.0f);
+ p = p * y2 + (float4)(1.0f / 3.0f);
+ p = p * y2 + (float4)(1.0f);
+ const float4 s = (float4)(2.0f) * y * p;
+ const float4 l = log((float4)(1.0f) + x);
+ const float4 r = select(l, s, (x >= (float4)(-0.5f)) & (x <= (float4)(1.0f)));
+ return select(r, x, fabs(x) < (float4)(2.5e-38f));
+}
+
+inline float softplus_f32(float x) {
+ return select(op_log1p(exp(x)), x, x > 20.0f);
+}
+
+inline float4 softplus_f32_4(float4 x) {
+ return select(op_log1p4(exp(x)), x, x > (float4)(20.0f));
+}
+
kernel void kernel_softplus_f32(
global const float * src0,
ulong offset0,
@@ -13,7 +53,7 @@ kernel void kernel_softplus_f32(
src0 = (global float*)((global char*)src0 + offset0);
dst = (global float*)((global char*)dst + offsetd);
- dst[get_global_id(0)] = (src0[get_global_id(0)] > 20.0f) ? src0[get_global_id(0)] : log(1.0f + exp(src0[get_global_id(0)]));
+ dst[get_global_id(0)] = softplus_f32(src0[get_global_id(0)]);
}
kernel void kernel_softplus_f32_4(
@@ -25,7 +65,7 @@ kernel void kernel_softplus_f32_4(
src0 = (global float4*)((global char*)src0 + offset0);
dst = (global float4*)((global char*)dst + offsetd);
- dst[get_global_id(0)] = (src0[get_global_id(0)] > 20.0f) ? src0[get_global_id(0)] : log(1.0f + exp(src0[get_global_id(0)]));
+ dst[get_global_id(0)] = softplus_f32_4(src0[get_global_id(0)]);
}
kernel void kernel_softplus_f16(
@@ -38,7 +78,7 @@ kernel void kernel_softplus_f16(
dst = (global half*)((global char*)dst + offsetd);
const float x = convert_float(src0[get_global_id(0)]);
- dst[get_global_id(0)] = convert_half_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
+ dst[get_global_id(0)] = convert_half_rte(softplus_f32(x));
}
kernel void kernel_softplus_f16_4(
@@ -51,7 +91,7 @@ kernel void kernel_softplus_f16_4(
dst = (global half4*)((global char*)dst + offsetd);
const float4 x = convert_float4(src0[get_global_id(0)]);
- dst[get_global_id(0)] = convert_half4_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
+ dst[get_global_id(0)] = convert_half4_rte(softplus_f32_4(x));
}
kernel void kernel_softplus_f32_nc(
@@ -80,7 +120,7 @@ kernel void kernel_softplus_f32_nc(
global const float * x = (global const float *)(src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
global float * y = (global float *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
- *y = (*x > 20.0f) ? *x : log(1.0f + exp(*x));
+ *y = softplus_f32(*x);
}
}
@@ -111,6 +151,6 @@ kernel void kernel_softplus_f16_nc(
global half * hy = (global half *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
const float x = convert_float(*hx);
- *hy = convert_half_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
+ *hy = convert_half_rte(softplus_f32(x));
}
}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp b/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
index 04a3756bd..756c70b1d 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/unary.comp
@@ -1,6 +1,7 @@
#version 450
#include "types.glsl"
+#include "utils.glsl"
#if defined(UNARY_MUL_FUSION)
#include "generic_binary_head.glsl"
#else
@@ -116,7 +117,7 @@ float op_expm1(float x) {
}
float op_softplus(float x) {
- return (x > 20.0f) ? x : log(1.0f + exp(x));
+ return (x > 20.0f) ? x : log1p(exp(x));
}
float op_gelu_erf(float a) {
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/utils.glsl b/ggml/src/ggml-vulkan/vulkan-shaders/utils.glsl
index 8aac64d75..1f3b74783 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/utils.glsl
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/utils.glsl
@@ -25,6 +25,26 @@ uint fastdiv_small(uint a, uint b) {
return (a < b) ? 0 : (a / b);
}
+// log(1 + x); the atanh series 2*y*(1 + y^2/3 + ... + y^12/13), y = x/(2+x),
+// keeps the precision that log(1 + x) loses for x in [-0.5, 1].
+float log1p(float x) {
+ float y = x / (2.0f + x);
+ float y2 = y * y;
+ float p = 1.0f / 13.0f;
+ p = p * y2 + 1.0f / 11.0f;
+ p = p * y2 + 1.0f / 9.0f;
+ p = p * y2 + 1.0f / 7.0f;
+ p = p * y2 + 1.0f / 5.0f;
+ p = p * y2 + 1.0f / 3.0f;
+ p = p * y2 + 1.0f;
+ float s = 2.0f * y * p;
+ float l = log(1.0f + x);
+ bool in_series = (x >= -0.5f) && (x <= 1.0f);
+ bool tiny = abs(x) < 2.5e-38f;
+ float r = in_series ? s : l;
+ return tiny ? x : r;
+}
+
void get_indices(uint idx, out uint i00, out uint i01, out uint i02, out uint i03, uint ne00, uint ne01, uint ne02, uint ne03) {
i03 = fastdiv_small(idx, (ne02*ne01*ne00));
const uint i03_offset = i03 * ne02*ne01*ne00;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index dc9e4a1ae..2d50c607c 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -2258,6 +2258,20 @@ struct test_unary : public test_case {
};
+struct test_unary_softplus_neg : public test_unary {
+ test_unary_softplus_neg() : test_unary(GGML_UNARY_OP_SOFTPLUS) {}
+
+ std::string vars() override {
+ return test_unary::vars() + ",neg";
+ }
+
+ 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, -16.6f, -16.0f);
+ }
+ }
+};
+
// GGML_OP_GLU
struct test_glu : public test_case {
const ggml_glu_op op;
@@ -11130,7 +11144,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
std::default_random_engine rng(0);
// unary ops
- // TODO: add GGML_UNARY_OP_SOFTPLUS test that is sensitive to using `log1p(exp(x))` vs `log(exp(x) + 1)`
+ test_cases.emplace_back(new test_unary_softplus_neg());
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++) {