Skip to content
class

LpNormalizeBackward

LpNormalizeBackward

Autograd node for Lp normalisation along a single axis.

Computes y=x/max⁡(∥x∥p,ϵ)y = x / \max(\|x\|_p, \epsilon) along axis, dividing the input by its Lp-norm per slice while clamping the denominator from below by eps to avoid division by zero. axis is resolved to a non-negative index inside forward so the saved value is always normalised.

Inherits FuncOp<LpNormalizeBackward, 1> with a single saved-input slot — x. The per-slice norm saved_norm_ is cached for the backward rule.

Math

Let SS denote a slice along the chosen axis (the elements summed over):

∥x∥p=(∑i∈S∣xi∣p)1/p,yi=ximax⁡(∥x∥p, ϵ) \|x\|_p = \Bigl(\sum_{i\in S} |x_i|^p\Bigr)^{1/p}, \qquad y_i = \frac{x_i}{\max(\|x\|_p,\,\epsilon)}

Attributes

schema_v1OpSchema
Registered schema (name="lp_normalize", version=1, amp_policy=ForceFP32, produces_grad=true).
ord_double
Norm order pp. Defaults to 2.0 (Euclidean L2 norm).
axis_int
Resolved non-negative axis along which the norm is computed. Negative axes passed to forward are converted before storage.
eps_double
Minimum allowed denominator, clamping the norm from below to avoid division by zero. Default: 1e-12.
saved_norm_Storage
Per-slice norm value ∥x∥p\|x\|_p retained for backward.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & x, double ord, int axis, double eps)

Run the forward pass.

Computes the per-slice Lp norm along axis, divides x by the clamped norm, caches saved_norm_, and (when grad mode is on) registers this as the grad_fn of the returned output. axis is normalised to a non-negative index before being stored in axis_.

Parameters

xTensorImplPtr
Input tensor of any shape.
orddouble
Norm order pp (e.g. 2.0 for the L2 norm).
axisint
Axis along which to compute the norm. Negative values count from the back.
epsdouble
Minimum allowed denominator.

Returns

TensorImplPtr

Normalised output, same shape as x.

Methods

3
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Compute the gradient with respect to x.

Returns {dx}. The closed-form involves a projection of grad_out along the normalised slice direction; see the .cpp for the explicit formula.

Parameters

grad_outStorage
Incoming gradient, same shape as the forward output.

Returns

std::vector<Storage>

Single-element vector {dx}.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same gradient, recorded, with the norm recomputed from x.

fn

release_own_saved

void release_own_saved()