TorchLean API

NN.MLTheory.CROWN.Graph.Engine.Base

Shared definitions for the graph CROWN engine.

This file contains the flat vector representation, parameter stores, interval boxes, shape permutation helpers, and tensor casts used by the IBP, derivative, affine, CROWN, and backward objective passes.

Flat vector pack: a tensor paired with its flattened dimension.

This is used for constant payloads and objective coefficient vectors in the flat LiRPA engine.

Instances For

    Parameters for a linear layer y = W*x + b in flattened form.

    m is the output dimension and n is the input dimension.

    Instances For

      Matrix parameters for bias-free matmul: y = W x.

      Instances For
        @[reducible, inline]

        Conv2D parameters with cached spatial dimensions for graph propagation.

        Instances For
          @[reducible, inline]

          Eval-mode BatchNorm2d parameters for an N×C×H×W node.

          Instances For

            Channel index for a flattened N×C×H×W tensor in row-major order.

            Instances For

              Eval BatchNorm scale for one channel.

              Instances For

                Eval BatchNorm bias for one channel after folding running statistics into an affine map.

                Instances For

                  Build the exact diagonal affine form for eval-mode BatchNorm2d over an N×C×H×W tensor.

                  The IR stores the channel parameters in the node payload. The spatial dimensions come from the checked parent shape, so malformed shapes simply do not produce a verifier transfer rule.

                  Instances For

                    Parameters keyed by node id (weights, biases, constants, and seeded input boxes).

                    This is kept compact: it is the graph interpreter used to run IBP/CROWN on a pure Graph without pulling in a heavyweight runtime.

                    Instances For

                      Insert an input interval box for a graph node.

                      Instances For
                        def NN.MLTheory.CROWN.Graph.ParamStore.seedLInfBall {α : Type} [Context α] {s : Spec.Shape} (ps : ParamStore α) (inputId : ) (center : Spec.Tensor α s) (eps : α) :

                        Seed a graph input with a uniform ℓ∞ box around a shaped tensor.

                        Instances For
                          def NN.MLTheory.CROWN.Graph.outputBox? {α : Type} [Context α] (boxes : Array (Option (FlatBox α))) (outId : ) :

                          Read a node's interval box from an IBP-style result array.

                          Instances For
                            @[implicit_reducible]

                            Default inhabitant for FlatBox (a 0-dimensional box at 0).

                            Elementwise product of two FlatBoxes (interval product per component). Requires equal dims.

                            Instances For

                              Derivative range for exp over a value box; exp' = exp is monotone.

                              Instances For

                                Derivative range for the positive-domain log rule used by derivative propagation.

                                Instances For

                                  Chain-rule multiplication for derivative intervals. Returns none on dimension mismatch.

                                  Instances For

                                    Convert a dependent Box of shape .dim n .scalar into a FlatBox with dim := n.

                                    Instances For

                                      Convert a FlatBox to a dependent Box at shape .dim B.dim .scalar.

                                      Instances For
                                        def NN.MLTheory.CROWN.Graph.box_add {α : Type} [Context α] [BoundOps α] (B1 B2 : FlatBox α) :

                                        Add two flat interval boxes coordinatewise; dimension mismatches preserve the left box.

                                        Instances For
                                          def NN.MLTheory.CROWN.Graph.box_sub {α : Type} [Context α] [BoundOps α] (B1 B2 : FlatBox α) :

                                          Interval subtraction on FlatBox endpoints (sound enclosure).

                                          Instances For

                                            Apply ReLU to both endpoints of a FlatBox (monotone activation, so endpoints suffice).

                                            Instances For

                                              Componentwise absolute value bounds. Soundly encloses abs over each interval component.

                                              Instances For

                                                Componentwise sqrt bounds. Uses the spec semantics sqrt(max(x,0)), which is monotone.

                                                Instances For

                                                  Componentwise reciprocal bounds using operators.arithmetic.ibp_reciprocal.

                                                  Instances For
                                                    @[reducible, inline]

                                                    Dynamic tensor value used while reshaping and permuting flattened boxes.

                                                    Instances For

                                                      Shape projection for FlatDVal.

                                                      Instances For

                                                        Tensor projection for FlatDVal, preserving the dependent shape stored beside it.

                                                        Instances For

                                                          Return the position of x in a list, if present.

                                                          Instances For

                                                            Swap adjacent entries d and d+1 in a list of axis ids.

                                                            Instances For

                                                              Decompose an axis permutation into adjacent swaps, rejecting invalid permutations.

                                                              Instances For

                                                                Apply one adjacent-axis swap to a dynamic tensor value.

                                                                Instances For

                                                                  Apply a full axis permutation to a dynamic tensor value when the permutation is valid.

                                                                  Instances For

                                                                    Componentwise max bounds: max(x,y) over interval boxes.

                                                                    Instances For

                                                                      Componentwise min bounds: min(x,y) over interval boxes.

                                                                      Instances For

                                                                        Componentwise square of an interval box: for each component [l,u] produce [min (l^2,u^2), max (l^2,u^2)], with 0 as the minimum when the interval crosses 0.

                                                                        The body is exposed because the proof-facing theorem module unfolds this executable rule when proving dimension preservation and pointwise enclosure.

                                                                        Instances For
                                                                          def NN.MLTheory.CROWN.Graph.intervalMul {α : Type} [Context α] (aLo aHi bLo bHi : α) :
                                                                          α × α

                                                                          Interval multiplication for scalar endpoints: given [aLo,aHi] and [bLo,bHi], return bounds on the product.

                                                                          Instances For

                                                                            Length of the last axis of a shape; scalars are treated as length one.

                                                                            Instances For

                                                                              Runtime witness that an axis is valid for a shape.

                                                                              Instances For

                                                                                Runtime witness that one shape can broadcast to another.

                                                                                Instances For

                                                                                  Reinterpret a flattened tensor as shape s when the element counts agree.

                                                                                  Instances For
                                                                                    def NN.MLTheory.CROWN.Graph.ibpBroadcastTo {α : Type} [Context α] (s₁ s₂ : Spec.Shape) (Xin : FlatBox α) :

                                                                                    IBP rule for broadcasting a flattened input box to a target shape.

                                                                                    Instances For

                                                                                      IBP rule for reducing a shaped box by summing along one axis.

                                                                                      Instances For

                                                                                        IBP rule for reducing a shaped box by averaging along one axis.

                                                                                        Instances For

                                                                                          Softmax IBP (last axis) #

                                                                                          For a 1D vector x with interval bounds l <= x <= u, a standard componentwise enclosure for softmax is:

                                                                                          This uses monotonicity of exp and the fact that all terms in the denominator are nonnegative. The implementation below applies the 1D rule on the last tensor axis and recurses over leading batch dimensions.

                                                                                          References:

                                                                                          Interval bound propagation for softmax, applied on the last axis and lifted over leading dims.

                                                                                          Instances For

                                                                                            LayerNorm IBP (last axis) #

                                                                                            Layer normalization (Ba et al.) computes, per vector, something like:

                                                                                            y = (x - mean(x)) / sqrt(var(x) + eps).

                                                                                            We implement a conservative enclosure by:

                                                                                            1. Bounding mean using sums of endpoints.
                                                                                            2. Bounding variance using a max-deviation upper bound.
                                                                                            3. Bounding the per-component ratio by checking endpoint combinations against a positive denominator interval.

                                                                                            This is intended as a simple checker-side transfer rule. It is conservative and is not an optimized relaxation.

                                                                                            References:

                                                                                            Upper bound on the variance term used by the LayerNorm interval rules.

                                                                                            Given endpoint bounds for a vector and bounds on its mean, each coordinate is at most max |x_i - μ| away from the bounded mean interval. Squaring and summing those coordinate radii gives the conservative variance upper bound reused by IBP, affine propagation, and derivative interval passes.

                                                                                            Instances For

                                                                                              Mean bounds for a vector whose coordinates are bounded by endpoint tensors.

                                                                                              Instances For

                                                                                                Bounds for x - μ when x is bounded coordinatewise and μ is bounded by an interval.

                                                                                                LayerNorm transfer rules repeatedly need this centered interval for the input, first derivative, and second derivative streams. Keeping it here avoids duplicating the same endpoint arithmetic in IBP and derivative propagation.

                                                                                                Instances For
                                                                                                  def NN.MLTheory.CROWN.Graph.layerNormInvStdBounds {α : Type} [Context α] (varHi : α) :
                                                                                                  α × α

                                                                                                  Bounds for the reciprocal LayerNorm denominator from an upper variance bound.

                                                                                                  Instances For

                                                                                                    Interval bound propagation for layernorm, applied on the last axis and lifted over leading dims.

                                                                                                    Instances For

                                                                                                      For tensors known to have shape .dim n .scalar, extract the underlying function.

                                                                                                      Instances For

                                                                                                        Cast a 1D Box along an equality of dimensions.

                                                                                                        Instances For

                                                                                                          Cast a ReLU relaxation vector across a proven-equal hidden dimension.

                                                                                                          Instances For
                                                                                                            def NN.MLTheory.CROWN.Graph.castAffineIn {α : Type} {n n' m : } (h : n = n') (a : AffineVec α n m) :
                                                                                                            AffineVec α n' m

                                                                                                            Cast the input dimension of an affine map across a proven equality.

                                                                                                            Instances For
                                                                                                              def NN.MLTheory.CROWN.Graph.castAffineOut {α : Type} {n m m' : } (h : m = m') (a : AffineVec α n m) :
                                                                                                              AffineVec α n m'

                                                                                                              Cast the output dimension of an affine map across a proven equality.

                                                                                                              Instances For
                                                                                                                @[reducible, inline]

                                                                                                                Cast a dim-scalar tensor across an equality of dimensions.

                                                                                                                We keep this as an abbrev so it unfolds aggressively in simp-based soundness proofs.

                                                                                                                Instances For

                                                                                                                  IBP propagation through explicit linear parameters.

                                                                                                                  Instances For
                                                                                                                    def NN.MLTheory.CROWN.Graph.ibp_linear {α : Type} [Context α] [BoundOps α] (id : ) (ps : ParamStore α) (Xin : FlatBox α) :

                                                                                                                    IBP propagation for a .linear node using ParamStore.linearWB.

                                                                                                                    Instances For
                                                                                                                      def NN.MLTheory.CROWN.Graph.ibp_matmul {α : Type} [Context α] [BoundOps α] (id : ) (ps : ParamStore α) (Xin : FlatBox α) :

                                                                                                                      IBP propagation for a .matmul node (bias-free) using ParamStore.matmulW.

                                                                                                                      Instances For
                                                                                                                        def NN.MLTheory.CROWN.Graph.ibpConv2dNode {α : Type} [Context α] (id : ) (ps : ParamStore α) (Xin : FlatBox α) :

                                                                                                                        IBP transfer for a convolution node whose parameters are stored in ParamStore.conv2dCfg.

                                                                                                                        Instances For