TorchLean API

NN.Runtime.Optim.GradientUtils

GradientUtils #

Gradient utilities for TorchLean runtime training.

These utilities are defined in terms of the canonical TensorGrad operations where possible. The spec layer already contains the scalar-polymorphic definitions of clipping and simple reductions, keeping runtime optimizer behavior aligned with the spec definitions.

This runtime file provides:

This file is a runtime vocabulary layer over the spec definitions, not a second implementation of gradient clipping. If the math changes, it should change in the spec layer first.

PyTorch analogies:

References:

Norms #

def Optim.l2NormSq {α : Type} [Context α] {s : Spec.Shape} (g : Spec.Tensor α s) :
α

Squared $\ell_2$ norm: $\lVert g\rVert_2^2=\sum_i g_i^2$.

Instances For
    def Optim.l2Norm {α : Type} [Context α] {s : Spec.Shape} (g : Spec.Tensor α s) :
    α

    $\ell_2$ norm: $\lVert g\rVert_2=\sqrt{\sum_i g_i^2}$.

    Instances For

      Clipping #

      def Optim.clipByNorm {α : Type} [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] {s : Spec.Shape} (g : Spec.Tensor α s) (maxNorm : α) :

      Global-norm clipping: if $\lVert g\rVert_2>\mathrm{maxNorm}$, rescale $g$ so that $\lVert g\rVert_2=\mathrm{maxNorm}$.

      Mathematically: $g\leftarrow g\,\mathrm{maxNorm}/\lVert g\rVert_2$ when $\lVert g\rVert_2$ exceeds the threshold.

      Instances For
        def Optim.clipByValue {α : Type} [Context α] {s : Spec.Shape} (g : Spec.Tensor α s) (minVal maxVal : α) :

        Elementwise value clipping: $g_i\leftarrow\operatorname{clamp}(g_i,\mathrm{minVal},\mathrm{maxVal})$.

        Instances For
          def Optim.clipByPercentile {α : Type} [Context α] {s : Spec.Shape} (g : Spec.Tensor α s) (pct : ) [DecidableLT α] :

          Percentile-driven clipping: compute a bound from abs(g) and clamp to $[-b,b]$.

          This is only executable when < on α is decidable (e.g. Float, IEEE32Exec).

          Instances For