TorchLean API

NN.Proofs.Autograd.FDeriv.PrimitiveExtrema

Minimum and maximum #

The captured minimum and maximum operations use the same comparison masks as their two-input runtime counterparts. A strict winner receives the full upstream gradient, a strict loser receives zero, and each input receives half at a tie. Capturing the right-hand tensor leaves that selection unchanged.

We first identify the stored backward functions coordinate by coordinate, including their value at ties. We then prove that these functions are the adjoints of the forward derivatives wherever every input coordinate differs from its captured partner. The equality case has a selected backward rule, but no classical derivative is claimed there.

theorem Proofs.Autograd.PrimitiveSpecs.hasDerivAt_min_const_of_ne {x c : } (h : x c) :
HasDerivAt (fun (y : ) => min y c) (if x < c then 1 else if c < x then 0 else 1 / 2) x

With unequal arguments, minimum agrees locally with either the input or the constant.

theorem Proofs.Autograd.PrimitiveSpecs.hasDerivAt_max_const_of_ne {x c : } (h : x c) :
HasDerivAt (fun (y : ) => max y c) (if c < x then 1 else if x < c then 0 else 1 / 2) x

With unequal arguments, maximum has derivative one at a strict winner and zero otherwise.

The actual minimum backward multiplies by its selected comparison mask at each coordinate.

The actual maximum backward keeps the same comparison order as the runtime selection.

A minimum tie retains half the upstream gradient even when the other input is captured.

A maximum tie uses the same half-gradient selection as minimum.

theorem Proofs.Autograd.PrimitiveSpecs.minOp_hasFDerivAt {s : Spec.Shape} (rhs : TorchLean.Tensor s) (x : Vec s.size) (hneq : ∀ (i : Fin s.size), x.ofLp i (tensorToVec rhs).ofLp i) :
HasFDerivAt (fun (y : Vec s.size) => tensorToVec ((Spec.minOp rhs).forward (vecToTensor y))) (coordinateDeriv fun (i : Fin s.size) => if x.ofLp i < (tensorToVec rhs).ofLp i then 1 else if (tensorToVec rhs).ofLp i < x.ofLp i then 0 else 1 / 2) x

Away from all coordinate ties, the minimum mask is the derivative of its tensor forward.

theorem Proofs.Autograd.PrimitiveSpecs.maxOp_hasFDerivAt {s : Spec.Shape} (rhs : TorchLean.Tensor s) (x : Vec s.size) (hneq : ∀ (i : Fin s.size), x.ofLp i (tensorToVec rhs).ofLp i) :
HasFDerivAt (fun (y : Vec s.size) => tensorToVec ((Spec.maxOp rhs).forward (vecToTensor y))) (coordinateDeriv fun (i : Fin s.size) => if (tensorToVec rhs).ofLp i < x.ofLp i then 1 else if x.ofLp i < (tensorToVec rhs).ofLp i then 0 else 1 / 2) x

Away from all coordinate ties, the maximum mask is the derivative of its tensor forward.

The stored minimum backward is the adjoint of the forward derivative away from ties.

The stored maximum backward is the adjoint of the forward derivative away from ties.