LLM inference in C/C++
C++ ggml
ggml : fix ggml_clamp (#27644)
Georgi Gerganov6036c635e2d1
diff --git a/ggml/include/ggml.h b/ggml/include/ggml.hindex 32462d79a7..5f6774a630 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 multiplicationGGML_API struct ggml_tensor * ggml_im2col(diff --git a/ggml/src/ggml-alloc.c b/ggml/src/ggml-alloc.cindex 3bda9abbe0..a71838eafc 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;diff --git a/ggml/src/ggml.c b/ggml/src/ggml.cindex 1a60fec791..e0b615c07e 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_maxstatic 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;}