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

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

    Instances For
      @[reducible, inline]

      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 executable transfer functions. Their soundness theorems instantiate the scalar type with and use the corresponding monotonicity or activation-specific proof.

            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, including the zero kink on crossing intervals.

                    Instances For
                      structure NN.MLTheory.CROWN.TwoLayerMLP (α : Type) (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} [Context α] {inDim hidDim outDim : } (net : TwoLayerMLP α inDim hidDim outDim) (x : Spec.Tensor α (Spec.Shape.dim inDim Spec.Shape.scalar)) :

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

                        Instances For
                          def NN.MLTheory.CROWN.ofLinearSpecs {α : Type} {inDim hidDim outDim : } (hiddenLayer : Spec.LinearSpec α inDim hidDim) (outputLayer : Spec.LinearSpec α hidDim outDim) :
                          TwoLayerMLP α inDim hidDim outDim

                          Build a TwoLayerMLP from two LinearSpec records.

                          Instances For
                            def NN.MLTheory.CROWN.boundIbp {α : Type} [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} [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.

                              Instances For
                                def NN.MLTheory.CROWN.boundAffineCrown {α : Type} [Context α] {inDim hidDim outDim : } [BoundOps α] (net : TwoLayerMLP α 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 path is only the direct two-layer MLP version. The graph-level code is still the general CROWN API.

                                Instances For
                                  def NN.MLTheory.CROWN.boundAffine {α : Type} [Context α] {inDim hidDim outDim : } [BoundOps α] (net : TwoLayerMLP α 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. Its enclosure guarantee depends on the selected BoundOps implementation; boundAffineCrown is the direct two-layer ReLU affine implementation.

                                  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.

                                    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 := 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.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 : TwoLayerMLP 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 : TwoLayerMLP 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 wrapper 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.crownTwoLayerMlpBounds {α : Type} [Context α] {inDim hidDim outDim : } [BoundOps α] (hiddenLayer : Spec.LinearSpec α inDim hidDim) (outputLayer : 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 two-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