Skip to content
class

NormBackward

NormBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the tensor p-norm.

Reproduces expand_back at backward time using the saved axis_ / keepdims_ hyperparameters; saves both the input AA (for the L2 numerator) and the reduced norm NN (to avoid recomputation).

Math

p=2:∂L∂A=Aclip(N,10−12,∞)⊙expand(∂L/∂N). p = 2: \quad \frac{\partial L}{\partial A} = \frac{A}{\mathrm{clip}(N, 10^{-12}, \infty)} \odot \mathrm{expand}(\partial L/\partial N). p=1:∂L∂A=sign(A)⊙expand(∂L/∂N). p = 1: \quad \frac{\partial L}{\partial A} = \mathrm{sign}(A) \odot \mathrm{expand}(\partial L/\partial N).

Shape

  • A: arbitrary float shape.
  • N: A's shape with axis_ removed (keepdims=false) or set to 1 (keepdims=true); scalar if axis_ is empty and keepdims=false.
  • Output gradient: same shape as A.

References

Petersen & Pedersen, The Matrix Cookbook (2012), §2.5. Horn & Johnson, Matrix Analysis (2nd ed.), §5.6 (matrix norms).

Attributes

schema_v1OpSchema
"norm", one input, AmpPolicy::KeepInput so the A/N division does not lose precision via AMP downcasting.
ord_double
Norm order used in the forward pass (1.0 or 2.0 supported in backward; other values raise not_implemented at backward time).
axis_std::vector<int>
Axes that were reduced. Empty means a full reduction over all elements. Indices may be negative; they are normalised when re-inserting axes during expand_back.
keepdims_bool
Whether reduced axes were retained as size-1 stubs in the output shape.
saved_inputs_[0]Storage
Storage of the forward input AA (needed for ord=2 numerator and ord=1 sign).
saved_output_Storage
Storage of the reduced norm NN (used as the ord=2 denominator).

Raises

NotImplementedError
If ord_ is neither 1.0 nor 2.0 when apply is invoked.

Methods

3
fn

apply

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

Compute the gradient {∂L/∂A} from the upstream gradient.

Parameters

grad_outStorage
Upstream gradient ∂L/∂N\partial L/\partial N with shape matching out_shape_.

Returns

std::vector<Storage>

Single-entry vector {∂L/∂A} aligned with the one differentiable input slot.

Raises

NotImplementedError
If ord_ is not 1.0 or 2.0.
fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same gradient from tracked ops, for create_graph=True.

fnconst

grad_from

int grad_from(const int & A, const int & N, const int & dN)

dA from the input, the norm and its gradient — eager mode passes detached tensors, graph mode tracked ones.