From 044b6168951b65b1dd024b9efda77c2587e590ff Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Mon, 24 Aug 2026 09:00:59 +0300 Subject: [PATCH 1/2] ggml : fix ggml_clamp --- ggml/include/ggml.h | 21 +++++++++++------- ggml/src/ggml.c | 54 +++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h index 32462d79a78..5f6774a630c 100644 --- a/ggml/include/ggml.h +++ b/ggml/include/ggml.h @@ -1724,6 +1724,19 @@ extern "C" { struct ggml_tensor * a, int n_past); + GGML_API struct ggml_tensor * ggml_clamp( + struct ggml_context * ctx, + struct ggml_tensor * a, + float min, + float max); + + // in-place, returns view(a) + GGML_API struct ggml_tensor * ggml_clamp_inplace( + struct ggml_context * ctx, + struct ggml_tensor * a, + float min, + float max); + GGML_API struct ggml_tensor * ggml_soft_max( struct ggml_context * ctx, struct ggml_tensor * a); @@ -1990,14 +2003,6 @@ extern "C" { struct ggml_tensor * a, int n_offs); - // clamp - // in-place, returns view(a) - GGML_API struct ggml_tensor * ggml_clamp( - struct ggml_context * ctx, - struct ggml_tensor * a, - float min, - float max); - // im2col // converts data into a format that effectively results in a convolution when combined with matrix multiplication GGML_API struct ggml_tensor * ggml_im2col( diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index 1a60fec791d..e0b615c07ed 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -4042,6 +4042,41 @@ struct ggml_tensor * ggml_diag_mask_zero_inplace( return ggml_diag_mask_zero_impl(ctx, a, n_past, true); } +// ggml_clamp + +static struct ggml_tensor * ggml_clamp_impl( + struct ggml_context * ctx, + struct ggml_tensor * a, + float min, + float max, + bool inplace) { + struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a); + + float params[] = { min, max }; + ggml_set_op_params(result, params, sizeof(params)); + + result->op = GGML_OP_CLAMP; + result->src[0] = a; + + return result; +} + +struct ggml_tensor * ggml_clamp( + struct ggml_context * ctx, + struct ggml_tensor * a, + float min, + float max) { + return ggml_clamp_impl(ctx, a, min, max, false); +} + +struct ggml_tensor * ggml_clamp_inplace( + struct ggml_context * ctx, + struct ggml_tensor * a, + float min, + float max) { + return ggml_clamp_impl(ctx, a, min, max, true); +} + // ggml_soft_max static struct ggml_tensor * ggml_soft_max_impl( @@ -4438,25 +4473,6 @@ struct ggml_tensor * ggml_rope_set_offset( return a; } -// ggml_clamp - -struct ggml_tensor * ggml_clamp( - struct ggml_context * ctx, - struct ggml_tensor * a, - float min, - float max) { - // TODO: when implement backward, fix this: - struct ggml_tensor * result = ggml_view_tensor(ctx, a); - - float params[] = { min, max }; - ggml_set_op_params(result, params, sizeof(params)); - - result->op = GGML_OP_CLAMP; - result->src[0] = a; - - return result; -} - static int64_t ggml_calc_conv_output_size(int64_t ins, int64_t ks, int s, int p, int d) { return (ins + 2 * p - d * (ks - 1) - 1) / s + 1; } From 4d858949de2099eb26fe6d89f7ca1b3c606b9db5 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Mon, 24 Aug 2026 09:48:55 +0300 Subject: [PATCH 2/2] cont : update ggml-alloc --- ggml/src/ggml-alloc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ggml/src/ggml-alloc.c b/ggml/src/ggml-alloc.c index 3bda9abbe03..a71838eafc6 100644 --- a/ggml/src/ggml-alloc.c +++ b/ggml/src/ggml-alloc.c @@ -40,6 +40,7 @@ bool ggml_op_can_inplace(enum ggml_op op) { case GGML_OP_SILU_BACK: case GGML_OP_RMS_NORM: case GGML_OP_RMS_NORM_BACK: + case GGML_OP_CLAMP: case GGML_OP_SOFT_MAX: case GGML_OP_SOFT_MAX_BACK: return true;