Skip to content
class

InvBackward

InvBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for the matrix inverse A−1A^{-1}.

The forward result B=A−1B = A^{-1} is captured in saved_output_; no input is saved because the closed-form gradient only requires BB. This trades a single matrix-shaped retain (peak memory) for avoiding a second LU factorisation during backward.

Math

B=A−1,G=∂L∂B, B = A^{-1}, \qquad G = \frac{\partial L}{\partial B}, ∂L∂A=−B⊤ G B⊤. \frac{\partial L}{\partial A} = -B^\top\,G\,B^\top.

Shape

  • A: (..., N, N).
  • B = A^{-1}: same shape as A.
  • grad_out: same shape as A.

References

Giles, "Collected Matrix Derivative Results for Forward and Reverse Mode Algorithmic Differentiation" (Oxford NA-08/01, 2008), §2.2.3. Petersen & Pedersen, The Matrix Cookbook (2012), §9.1.3.

Attributes

schema_v1OpSchema
"inv", one input, AmpPolicy::KeepInput so mixed-precision training does not silently downcast the saved inverse.
saved_output_Storage
Storage of the forward result B=A−1B = A^{-1} retained for the backward.

Notes

  • For very large matrices in deep graphs, holding BB alive until backward increases peak memory; users wishing to trade memory for compute would need to recompute the inverse (not currently supported here).
  • Implementation uses matmul_op + neg_op so second-order gradients flow through automatically.

Instance methods

2
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/∂B\partial L/\partial B matching the forward output shape.

Returns

std::vector<Storage>

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

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same -B^T G B^T, recorded, with B = A^-1 recomputed from A.