Skip to content
class

ClipBackward

ClipBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise clip (clamp): y=clip⁡(x,min,max)y = \operatorname{clip}(x, \text{min}, \text{max}).

The gradient is the in-range indicator: gradient flows through where the input lies strictly between the bounds, and is zero on the saturated boundaries.

Math

Forward

yi=max⁡(min, min⁡(max, xi))y_i = \max\bigl(\text{min},\ \min(\text{max},\ x_i)\bigr)

Backward

\frac{\partial L}{\partial y_i} & \text{min} < x_i < \text{max} \\ 0 & \text{otherwise} \end{cases}$$ **See Also** `clip_op`.

Attributes

min_double
Lower clamp bound captured at forward.
max_double
Upper clamp bound captured at forward.
schema_v1OpSchema
Op name "clip" with AmpPolicy::KeepInput (clip is valid on both integer and floating-point inputs).

Notes

min_ and max_ are persisted so grad_formula can rebuild the in-range mask from the saved input rather than caching the mask itself.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & a, double min_v, double max_v)

Forward dispatch with bounds capture — computes y=clip⁡(x,min,max)y = \operatorname{clip}(x, \text{min}, \text{max}) and wires the backward node manually with both bounds persisted.

Parameters

aconst TensorImplPtr&
Input tensor.
min_vdouble
Lower bound.
max_vdouble
Upper bound (must satisfy min_v <= max_v).

Returns

TensorImplPtr

Clamped output tensor with the same shape, dtype, and device as a.

Methods

2
fn

grad_formula

→Storage
int grad_formula(const int & g)

Computes the masked gradient g⊙1[min<x<max]g \odot \mathbb{1}[\text{min} < x < \text{max}] from the saved input and the persisted bounds.

Parameters

gconst Storage&
Upstream gradient ∂L/∂y\partial L/\partial y.

Returns

Storage

Gradient with respect to x; zero at every saturated slot.

fn

grad_formula_impl

int grad_formula_impl(const int & g, const int & x, const int & out)