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:

Parameters of a scalar linear relaxation used for bounding ReLU.

Typical shape (for crossing intervals l <= 0 < u) is an upper line y <= slope * x + bias with slope ∈ [0, 1] and bias >= 0.

  • slope : α

    Linear coefficient (often written α in the CROWN literature).

  • bias : α

    Constant offset (often written β).

Instances For

    Compute a standard (upper) linear relaxation for scalar ReLU on an interval [l, u].

    This matches the classic CROWN/DeepPoly choice:

    • If u <= 0, ReLU is identically 0.
    • If l >= 0, ReLU is the identity.
    • If l < 0 < u, use the upper chord through (l, 0) and (u, u).
    Instances For

      Compute a basic (lower) linear relaxation for scalar ReLU on [l, u].

      For the crossing case l <= 0 < u, we choose either:

      • y >= 0 (slope 0), or
      • y >= x (slope 1), depending on which yields the tighter lower bound for the downstream objective.
      Instances For

        Apply relax_scalar elementwise to vector bounds (lo, hi).

        Instances For

          Apply relax_scalar_lower elementwise to vector bounds (lo, hi).

          Instances For
            def NN.MLTheory.CROWN.ReLU.propagateAffine {α : Type} [Context α] {inDim hidDim : } (relax : Spec.Tensor (ReLURelax α) (Spec.Shape.dim hidDim Spec.Shape.scalar)) (aff : AffineVec α inDim hidDim) :
            AffineVec α inDim hidDim

            Propagate an affine form through ReLU using a fixed per-neuron relaxation.

            This is the "forward" DeepPoly-style propagation: given an affine bound on z, we produce an affine bound on relu(z) by scaling rows and adjusting the constant term.

            Instances For

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

              Instances For

                Elementwise positive part of a matrix: replace negative entries by 0.

                Instances For

                  Elementwise negative part of a matrix: replace positive entries by 0.

                  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) utilities for vector-shaped activations.

                        These are correctness-first helpers: they are intended to be sound for any Context α backend that implements the scalar operations used by Activation.Math.*_spec.

                        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
                          @[reducible, inline]

                          Re-export of the runtime-only monotone-activation IBP helper.

                          We keep this file compact and Mathlib-friendly for proofs, but we do not want to maintain two copies of the same computational rule. Canonical implementation lives in: NN.MLTheory.CROWN.Runtime.Ops.IBP.map_minmax.

                          Semantics (per component): given an interval [l,u], this returns [min(f(l), f(u)), max(f(l), f(u))] (intended for monotone f).

                          Instances For
                            @[reducible, inline]

                            Interval bounds for sigmoid.

                            Instances For
                              @[reducible, inline]

                              Interval bounds for tanh.

                              Instances For

                                Interval bounds for leaky-ReLU.

                                Sound when the negative slope αₗ is nonnegative.

                                Instances For

                                  Interval bounds for ELU.

                                  Sound for alpha > 0.

                                  Instances For
                                    structure NN.MLTheory.CROWN.MLP2 (α : Type) (inDim hidDim outDim : ) :

                                    2-layer MLP payload used by this file.

                                    Semantics: y = W2 * relu(W1 * x + b1) + b2.

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

                                    Instances For
                                      def NN.MLTheory.CROWN.forward {α : Type} [Context α] {inDim hidDim outDim : } (net : MLP2 α inDim hidDim outDim) (x : Spec.Tensor α (Spec.Shape.dim inDim Spec.Shape.scalar)) :

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

                                      Instances For
                                        def NN.MLTheory.CROWN.ofLinearSpecs {α : Type} {inDim hidDim outDim : } (l1 : Spec.LinearSpec α inDim hidDim) (l2 : Spec.LinearSpec α hidDim outDim) :
                                        MLP2 α inDim hidDim outDim

                                        Build an MLP2 from two LinearSpec records.

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

                                          Compute an output interval box via pure IBP.

                                          This is fast and always sound, but typically looser than CROWN/DeepPoly affine bounds.

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

                                            Single-pass affine (CROWN/DeepPoly-style) bounds for the 2-layer ReLU MLP.

                                            This constructs upper and lower affine forms by combining:

                                            • pre-activation intervals from IBP,
                                            • per-neuron ReLU relaxations, and
                                            • sign-splitting of the second-layer weights.

                                            Note: this is the direct MLP-specialized implementation; the graph-level engine in NN.MLTheory.CROWN.Graph is the long-term home for fully-general CROWN.

                                            Instances For
                                              def NN.MLTheory.CROWN.boundAffine {α : Type} [Context α] {inDim hidDim outDim : } (net : MLP2 α inDim hidDim outDim) (xB : Box α (Spec.Shape.dim inDim Spec.Shape.scalar)) :

                                              End-to-end bound API exposed by this file.

                                              This API returns the IBP bound (sound for all backends); bound_affine_crown is kept as a reference implementation for the 2-layer ReLU case.

                                              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_scalar_upper_real (l u x : ) (hlx : l x) (hxu : x u) :

                                                Scalar ReLU relaxation soundness over (upper bound).

                                                If x ∈ [l, u] and rp := ReLU.relax_scalar l u, then: relu(x) <= rp.slope * x + rp.bias.

                                                This is the standard CROWN/DeepPoly upper chord construction (arXiv:1811.00866).

                                                theorem NN.MLTheory.CROWN.Theorems.relu_relax_vector_pointwise_upper_real {n : } (lo hi x : Spec.Tensor (Spec.Shape.dim n Spec.Shape.scalar)) (hIn : { lo := lo, hi := hi }.contains x) (i : Fin n) :
                                                have li := match lo, hIn with | Spec.Tensor.dim flo, hIn => match flo i with | Spec.Tensor.scalar v => v; have ui := match hi, hIn with | Spec.Tensor.dim fhi, hIn => match fhi i with | Spec.Tensor.scalar v => v; have xi := match x, hIn with | Spec.Tensor.dim fx, hIn => match fx i with | Spec.Tensor.scalar v => v; have rp := 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.relax_vector 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 : MLP2 inDim hidDim outDim) (xB : Box (Spec.Shape.dim inDim Spec.Shape.scalar)) (x : Spec.Tensor (Spec.Shape.dim inDim Spec.Shape.scalar)) (hx : xB.contains x) :
                                                (boundIbp net xB).contains (forward net x)

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

                                                theorem NN.MLTheory.CROWN.Theorems.bound_affine_sound {inDim hidDim outDim : } (net : MLP2 inDim hidDim outDim) (xB : Box (Spec.Shape.dim inDim Spec.Shape.scalar)) (x : Spec.Tensor (Spec.Shape.dim inDim Spec.Shape.scalar)) (hx : xB.contains x) :
                                                (boundAffine net xB).contains (forward net x)

                                                Soundness of the affine-bound entrypoint for a 2-layer MLP over .

                                                In this module bound_affine delegates to the IBP implementation, so this theorem is a direct corollary of bound_ibp_sound.

                                                def NN.MLTheory.CROWN.Examples.crownMlp2Bounds {α : Type} [Context α] {inDim hidDim outDim : } (l1 : Spec.LinearSpec α inDim hidDim) (l2 : Spec.LinearSpec α hidDim outDim) (x_center : Spec.Tensor α (Spec.Shape.dim inDim Spec.Shape.scalar)) (eps : α) :

                                                Compute both IBP bounds and affine-CROWN bounds for a 2-layer MLP around an ε-box.

                                                The input set is the axis-aligned box centered at x_center with radius eps in each coordinate.

                                                Instances For

                                                  Extract the scalar component t[i] from a vector-shaped tensor.

                                                  Instances For

                                                    Lower endpoint at index i from a vector box.

                                                    Instances For

                                                      Upper endpoint at index i from a vector box.

                                                      Instances For

                                                        Maximum upper bound among competitors k ≠ c.

                                                        Instances For

                                                          Certified margin lower bound: lowerAt c - maxCompetitorUpper c.

                                                          Instances For

                                                            Decide whether class c is certified by a positive margin.

                                                            Instances For

                                                              Return the argmax index of a concrete output vector (when n > 0), otherwise none.

                                                              This is a utility for pairing certified bounds with a predicted class.

                                                              Instances For