ggml_clamp: Clamp (Graph)

View source: R/operations.R

ggml_clampR Documentation

Clamp (Graph)

Description

Creates a graph node for clamping values to a range: clamp(x, min, max)

Usage

ggml_clamp(ctx, a, min_val, max_val)

Arguments

ctx

GGML context

a

Input tensor

min_val

Minimum value

max_val

Maximum value

Value

Tensor with values clamped to [min_val, max_val]

Inference only

GGML_OP_CLAMP has no backward pass, and the node it builds is a view of its input. A graph containing it therefore cannot be differentiated: the backward pass aborts with "inplace operations are currently not supported" rather than returning an error. This matters inside ggml_layer_custom, whose gradients flow only through operations that implement a backward pass.

For a trainable equivalent, express the clamp with operations that do: min(x, hi) is x - ggml_relu(ctx, x - hi) and max(x, lo) is lo + ggml_relu(ctx, x - lo).

See Also

ggml_relu for the differentiable formulation above.


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.