Documentation

LeanCert.ML.Optimized.Matrix

Quantized Matrix Operations #

This module provides the foundation for Transformer verification: Matrix-Matrix multiplication for Intervals.

Key Definitions #

Integer Matrix (Data Layer) #

structure LeanCert.ML.Optimized.IntMatrix (rows cols : ) :

A dense integer matrix stored in row-major format.

Instances For
    def LeanCert.ML.Optimized.instReprIntMatrix.repr {rows✝ cols✝ : } :
    IntMatrix rows✝ cols✝Std.Format
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      @[inline]
      def LeanCert.ML.Optimized.IntMatrix.get {r c : } (M : IntMatrix r c) (i j : ) (hi : i < r) (hj : j < c) :

      Get element at (r, c)

      Equations
      Instances For
        def LeanCert.ML.Optimized.IntMatrix.ofFn {r c : } (f : Fin rFin c) :

        Create a matrix from a function

        Equations
        Instances For
          @[simp]
          theorem LeanCert.ML.Optimized.IntMatrix.ofFn_get {r c : } (f : Fin rFin c) (i : Fin r) (j : Fin c) :
          (ofFn f).get i j = f i j

          Get an element from an ofFn matrix

          Matrix Transpose

          Equations
          Instances For

            Interval Matrix (Verification Layer) #

            Interval Matrix in SoA layout (separate lo/hi matrices)

            Instances For
              def LeanCert.ML.Optimized.IntervalMatrix.getBounds {r c : } (M : IntervalMatrix r c) (i j : ) (hi : i < r) (hj : j < c) :

              Get the interval bounds at (i, j) as a pair

              Equations
              Instances For
                @[inline]

                Interval Arithmetic Product

                Computes [l1, h1] * [l2, h2]. Result lo = min(l1l2, l1h2, h1l2, h1h2) Result hi = max(l1l2, l1h2, h1l2, h1h2)

                Equations
                Instances For
                  @[inline]

                  Interval addition of two pairs

                  Equations
                  Instances For

                    Matrix-Matrix Multiplication

                    C = A × B where A is (n × m) and B is (m × p).

                    This implements the standard O(N³) loop but lifting operations to intervals. Unlike vector-matrix mul, we cannot easily decompose into Pos/Neg weights because both inputs are variable intervals.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      Transpose an interval matrix

                      Equations
                      Instances For

                        Create from function

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Semantics and Correctness #

                          noncomputable def LeanCert.ML.Optimized.intVal (x exp : ) :

                          Value of integer as real given an exponent (2^exp scaling)

                          Equations
                          Instances For
                            theorem LeanCert.ML.Optimized.intVal_mul (x y e : ) :
                            intVal x e * intVal y e = intVal (x * y) (e + e)

                            Multiplication of scaled values: intVal x e * intVal y e = intVal (x*y) (e+e)

                            theorem LeanCert.ML.Optimized.intVal_add (x y e : ) :
                            intVal x e + intVal y e = intVal (x + y) e

                            intVal distributes over addition

                            noncomputable def LeanCert.ML.Optimized.IntMatrix.valAt {r c : } (M : IntMatrix r c) (exp : ) (i : Fin r) (j : Fin c) :

                            Value of integer matrix entry given an exponent

                            Equations
                            Instances For
                              def LeanCert.ML.Optimized.IntervalMatrix.mem {r c : } (M : IntervalMatrix r c) (exp : ) (A_real : Matrix (Fin r) (Fin c) ) :

                              Membership in IntervalMatrix

                              Equations
                              Instances For

                                Foldl to Finset.sum conversion #

                                theorem LeanCert.ML.Optimized.finRange_foldl_add_eq_sum {α : Type u_1} [AddCommMonoid α] {m : } (f : Fin mα) :
                                List.foldl (fun (acc : α) (j : Fin m) => acc + f j) 0 (List.finRange m) = Finset.univ.sum f

                                Key lemma: foldl with addition over List.finRange equals Finset.univ.sum. This converts the computational foldl in matmul to the mathematical Finset.sum.

                                Int to Real cast lemmas for foldl #

                                theorem LeanCert.ML.Optimized.Int_cast_foldl_add {β : Type u_1} (l : List β) (f : β) :
                                (List.foldl (fun (acc : ) (b : β) => acc + f b) 0 l) = List.foldl (fun (acc : ) (b : β) => acc + (f b)) 0 l

                                Int.cast distributes over foldl with addition

                                Helper for sum inequalities #

                                Interval Multiplication Soundness #

                                theorem LeanCert.ML.Optimized.intervalMul_sound (l1 h1 l2 h2 : ) (x y : ) (hx_lo : l1 x) (hx_hi : x h1) (hy_lo : l2 y) (hy_hi : y h2) :
                                match IntervalMatrix.intervalMul l1 h1 l2 h2 with | (lo, hi) => lo x * y x * y hi

                                The interval multiplication is sound: if x ∈ [l1, h1] and y ∈ [l2, h2], then x * y ∈ [intervalMul.lo, intervalMul.hi]

                                theorem LeanCert.ML.Optimized.intervalMul_sound_scaled (l1 h1 l2 h2 : ) (x y : ) (exp : ) (hx_lo : intVal l1 exp x) (hx_hi : x intVal h1 exp) (hy_lo : intVal l2 exp y) (hy_hi : y intVal h2 exp) :
                                match IntervalMatrix.intervalMul l1 h1 l2 h2 with | (lo, hi) => intVal lo (exp + exp) x * y x * y intVal hi (exp + exp)

                                Scaled version of intervalMul_sound using intVal with exponents. If x ∈ [intVal l1 exp, intVal h1 exp] and y ∈ [intVal l2 exp, intVal h2 exp], then x * y ∈ [intVal lo (exp+exp), intVal hi (exp+exp)] where (lo, hi) = intervalMul l1 h1 l2 h2.

                                theorem LeanCert.ML.Optimized.mem_transpose {r c : } (M : IntervalMatrix r c) (exp : ) (A_real : Matrix (Fin r) (Fin c) ) (hM : M.mem exp A_real) :
                                M.transpose.mem exp A_real.transpose

                                Soundness of Matrix Transpose

                                If A_real ∈ A_interval, then A_realᵀ ∈ A_intervalᵀ. The exponent is preserved.

                                theorem LeanCert.ML.Optimized.mem_matmul {n m p : } (A : IntervalMatrix n m) (B : IntervalMatrix m p) (Ar : Matrix (Fin n) (Fin m) ) (Br : Matrix (Fin m) (Fin p) ) (exp : ) (hA : A.mem exp Ar) (hB : B.mem exp Br) :
                                (A.matmul B).mem (exp + exp) (Ar * Br)

                                Main Theorem: Soundness of Matrix Multiplication

                                If A_real ∈ A_interval and B_real ∈ B_interval, then (A_real * B_real) ∈ (A_interval * B_interval).

                                Note: Exponents add. If A has exp e and B has exp e, result has 2e.