TorchLean API

NN.MLTheory.CROWN.Models.Mlp

Mlp #

CROWN/DeepPoly-style propagation for MLPs (vector in/out) using TorchLean tensors.

This file is a compact implementation that sits on top of:

What is implemented:

Scope boundaries in this MLP-focused module:

References:

PyTorch analogues:

Column-wise scaling of a matrix by a vector: scale each column j by v[j].

Instances For

    Extract the slope vector from a tensor of ReLU relaxations.

    Instances For

      Extract the bias vector from a tensor of ReLU relaxations.

      Instances For

        Interval Bound Propagation (IBP) for the vector-shaped ReLU used by TwoLayerMLP.

        This is an executable transfer function. Its soundness theorem instantiates the scalar type with ℝ.

        The linear-layer bound helper IBP.linear lives in NN.MLTheory.CROWN.Core.

        Interval bounds for ReLU on a vector.

        This is the standard elementwise interval evaluation: relu([l,u]) = [relu(l), relu(u)].

        Instances For
          structure NN.MLTheory.CROWN.TwoLayerMLP (α : Type) [TorchLean.Storage α] (inDim hidDim outDim : ℕ) :

          Two-layer MLP payload used by this file.

          Semantics: y = outputWeight * relu(hiddenWeight * x + hiddenBias) + outputBias.

          PyTorch analogue: torch.nn.Sequential(Linear(inDim,hidDim), ReLU(), Linear(hidDim,outDim)).

          Instances For
            def NN.MLTheory.CROWN.forward {α : Type} [TorchLean.Storage α] [Context α] {inDim hidDim outDim : ℕ} (net : TwoLayerMLP α inDim hidDim outDim) (x : TorchLean.Tensor α [inDim]) :

            Forward semantics for TwoLayerMLP (used to state soundness theorems).

            Instances For
              def NN.MLTheory.CROWN.boundIbp {α : Type} [TorchLean.Storage α] [Context α] {inDim hidDim outDim : ℕ} [BoundOps α] (net : TwoLayerMLP α inDim hidDim outDim) (xB : Box α (Spec.Shape.dim inDim Spec.Shape.scalar)) :

              Compute an output interval box via pure IBP.

              This is fast and, when BoundOps α supplies sound outward endpoint operations, typically looser than CROWN/DeepPoly affine bounds.

              Instances For
                def NN.MLTheory.CROWN.affineCrownForms {α : Type} [TorchLean.Storage α] [Context α] {inDim hidDim outDim : ℕ} [BoundOps α] (net : TwoLayerMLP α inDim hidDim outDim) (xB : Box α (Spec.Shape.dim inDim Spec.Shape.scalar)) :
                AffineVec α inDim outDim × AffineVec α inDim outDim

                The lower and upper affine CROWN forms for this two-layer ReLU MLP.

                The returned pair is (lower, upper). boundAffineCrown evaluates these forms on the input box and takes the lower and upper endpoints. Exact backends use the two-layer algebraic formula. Rounded backends share the graph engine's directed coefficient propagation, which has no coefficient transfer for ReLU: the hidden layer is consumed through its IBP box, so the input coefficients are zero and the result is the IBP bound of the output layer. No theorem covers the rounded branch.

                Instances For
                  def NN.MLTheory.CROWN.boundAffineCrown {α : Type} [TorchLean.Storage α] [Context α] {inDim hidDim outDim : ℕ} [BoundOps α] (net : TwoLayerMLP α inDim hidDim outDim) (xB : Box α (Spec.Shape.dim inDim Spec.Shape.scalar)) :

                  Affine (CROWN/DeepPoly-style) bounds for the 2-layer ReLU MLP.

                  Rounded scalar backends use the same directed arithmetic as the general graph CROWN API.

                  Instances For

                    Theorems inspired by CROWN (Zhang et al., 2018, arXiv:1811.00866)

                    We record soundness properties of the relaxations and bound propagation. Proofs below require elementary order reasoning and case splits on signs, plus properties of mat-vec interval arithmetic.

                    theorem NN.MLTheory.CROWN.Theorems.relu_relax_vector_pointwise_upper_real {n : ℕ} (lo hi x : TorchLean.Tensor ℝ [n]) (hIn : { lo := lo, hi := hi }.contains x) (i : Fin n) :
                    have li := lo.getScalar i; have ui := hi.getScalar i; have xi := x.getScalar i; have rp := Runtime.Ops.ReLU.relaxScalar li ui; Activation.Math.reluSpec xi ≤ rp.slope * xi + rp.bias

                    Vectorized ReLU relaxation (pointwise upper bound) over ℝ.

                    If x ∈ [lo, hi] and rp := ReLU.relaxScalar loᵢ hiᵢ, then for every component i we have relu(xᵢ) ≤ rpᵢ.slope * xᵢ + rpᵢ.bias.

                    Soundness of IBP.linear over ℝ.

                    If x ∈ xB and b ∈ bB, then W*x + b lies in the interval box computed by IBP.linear W xB bB.

                    theorem NN.MLTheory.CROWN.Theorems.bound_ibp_sound {inDim hidDim outDim : ℕ} (net : TwoLayerMLP ℝ inDim hidDim outDim) (xB : Box ℝ (Spec.Shape.dim inDim Spec.Shape.scalar)) (x : TorchLean.Tensor ℝ [inDim]) (hx : xB.contains x) :
                    (boundIbp net xB).contains (forward net x)

                    Soundness of pure IBP bounds for a 2-layer MLP over ℝ.