Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions ggml/include/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions ggml/src/ggml-alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 35 additions & 19 deletions ggml/src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down
Loading