Skip to content
module

autograd

23 members
lucid.autograd

lucid.autograd — reverse-mode differentiation and the grad-mode controls.

backward and grad walk the tape the engine records as ops execute. Function together with FunctionCtx lets Python define a new differentiable op by writing its forward and backward explicitly, for cases the composed ops cannot express.

Grad modes: no_grad, enable_grad, inference_mode, and the set_grad_enabled / is_grad_enabled pair. set_grad_enabled(flag) switches the mode when called and, used in a with statement or as a decorator, restores the previous mode on exit like the other three.

Higher-order and verification: jacobian, hessian, vjp, jvp, hvp and vhp for derivatives of derivatives, and gradcheck / gradgradcheck to verify a hand-written backward against finite differences — worth running on any new Function, since a wrong backward produces plausible numbers rather than an error. checkpoint trades recomputation for activation memory, and detect_anomaly traces the op that first produced a NaN.

Classes

Functions

fnis_grad_enabled→ bool

Return whether autograd gradient tracking is currently enabled.

fnis_inference_mode_enabled→ bool

Return whether inference mode is active on the calling thread.

fnbackward→ None

Compute gradients of tensors w.r.t. the leaf variables in their graph.

fngrad→ tuple[Tensor or None, ...]

Compute gradients of outputs w.r.t. inputs, returning them as a tuple.

fngradcheck→ bool True if all gradients agree within tolerance.

Compare analytical gradients from backward() against finite-difference Jacobians.

fngradgradcheck→ bool

Verify second-order gradients via finite differences.

fnis_anomaly_enabled→ bool

Return whether autograd anomaly detection is currently enabled.

fnset_detect_anomaly→ None

Programmatic global toggle for autograd anomaly detection.

fnjacobian→ Tensor, tuple of Tensor or tuple of tuple of Tensor

Compute the Jacobian matrix of func with respect to each input.

fnhessian→ Tensor or tuple of tuple of Tensor

Compute the Hessian matrix of a scalar-valued func.

fnvjp→ tuple of (Tensor or tuple of Tensor, Tensor or tuple of Tensor)

Vector-Jacobian product v⊤Jv^\top J (reverse-mode AD).

fnjvp→ tuple of (Tensor or tuple of Tensor, Tensor or tuple of Tensor)

Jacobian-vector product JvJ v (forward-mode directional derivative).

fnhvp→ tuple of (Tensor, Tensor or tuple of Tensor)

Hessian-vector product HvH v of a scalar-valued func.

fnvhp→ tuple of (Tensor, Tensor or tuple of Tensor)

Vector-Hessian product v⊤Hv^\top H of a scalar-valued func.

fncheckpoint→ Tensor or tuple of Tensor

Run function under gradient checkpointing.