Quantized Matrix Operations #
This module provides the foundation for Transformer verification: Matrix-Matrix multiplication for Intervals.
Key Definitions #
IntMatrix- Flattened array of integers (row-major)IntervalMatrix- Structure-of-Arrays interval matrixmatmul- Verified interval matrix multiplication
Integer Matrix (Data Layer) #
Equations
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a matrix from a function
Equations
- LeanCert.ML.Optimized.IntMatrix.ofFn f = if hc : c = 0 then { data := #[], size_eq := ⋯ } else { data := Array.ofFn (LeanCert.ML.Optimized.IntMatrix.flatToElem✝ f hc), size_eq := ⋯ }
Instances For
Zero matrix
Equations
- LeanCert.ML.Optimized.IntMatrix.zero r c = LeanCert.ML.Optimized.IntMatrix.ofFn fun (x : Fin r) (x_1 : Fin c) => 0
Instances For
Interval Matrix (Verification Layer) #
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
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
Instances For
Zero interval matrix (all zeros)
Equations
- LeanCert.ML.Optimized.IntervalMatrix.zero r c = { lo := LeanCert.ML.Optimized.IntMatrix.zero r c, hi := LeanCert.ML.Optimized.IntMatrix.zero r c }
Instances For
Create from function
Equations
- One or more equations did not get rendered due to their size.
Instances For
Semantics and Correctness #
Value of integer as real given an exponent (2^exp scaling)
Equations
- LeanCert.ML.Optimized.intVal x exp = ↑x * 2 ^ exp
Instances For
Foldl to Finset.sum conversion #
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 #
Int.cast distributes over foldl with addition
Helper for sum inequalities #
Interval Multiplication Soundness #
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.
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.