Quantized Neural Network Layers #
This module implements two critical optimizations for neural network verification:
Optimization 1: Split-Sign Matrix Decomposition #
Instead of computing interval arithmetic with min/max at every weight:
[a, b] × [x, y] = [min(ax, ay, bx, by), max(...)]
We precompute W = W⁺ - W⁻ where:
- W⁺ᵢⱼ = max(0, Wᵢⱼ)
- W⁻ᵢⱼ = max(0, -Wᵢⱼ)
Then for x ∈ [l, u]:
- y_lo = W⁺ · l - W⁻ · u
- y_hi = W⁺ · u - W⁻ · l
This reduces interval matrix multiplication to 4 standard matrix-vector products with no branching in the inner loop.
Optimization 2: Common Exponent Integer Arithmetic #
Instead of Dyadic arithmetic (mantissa × 2^exp) per operation, we:
- Align all values to a common exponent
- Perform pure integer (GMP) arithmetic
- Reconstruct Dyadic results at the end
This eliminates per-operation exponent handling.
Main Definitions #
SplitWeights- Pre-decomposed W⁺, W⁻ matricesQuantizedLayer- Layer with aligned integer representationforwardQuantized- Optimized forward pass
Helper Lemmas for Fold Induction #
Pure Integer Matrix-Vector Operations #
Monotonicity of integer dot product: if w[i] ≥ 0 for all i, and lo[i] ≤ hi[i], then w·lo ≤ w·hi.
Integer matrix-vector multiplication
Equations
- LeanCert.ML.Optimized.matVecMulInt M v = Array.map (fun (x : Array ℤ) => LeanCert.ML.Optimized.dotProductInt x v) M
Instances For
Integer array addition
Equations
- LeanCert.ML.Optimized.addIntArrays a b = Array.zipWith (fun (x1 x2 : ℤ) => x1 + x2) a b
Instances For
Integer array subtraction
Equations
- LeanCert.ML.Optimized.subIntArrays a b = Array.zipWith (fun (x1 x2 : ℤ) => x1 - x2) a b
Instances For
Integer max(0, x) applied element-wise
Equations
- LeanCert.ML.Optimized.reluInt a = Array.map (max 0) a
Instances For
Helper lemmas for getElem! indexing #
Split-Sign Weight Decomposition #
Pre-decomposed weight matrices for branch-free interval multiplication. Stores W⁺ = max(0, W) and W⁻ = max(0, -W) separately as integer arrays.
Positive part as integers: W⁺ᵢⱼ × 2^(-exp)
Negative part as integers: W⁻ᵢⱼ × 2^(-exp)
- exp : ℤ
Common exponent
Size invariants
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Quantized Layer #
A layer with all weights aligned to a common exponent for pure integer arithmetic.
This is the key optimization: instead of Dyadic operations (which involve per-operation exponent handling), we shift everything to a common exponent and do pure BigInt arithmetic.
- commonExp : ℤ
Common exponent for the entire layer
W⁺ scaled to integers
W⁻ scaled to integers
Bias scaled to integers
- outDim : ℕ
Output dimension
- inDim : ℕ
Input dimension
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Scale a rational to an integer given a common exponent. Returns floor(x × 2^(-exp)).
Equations
Instances For
Create a quantized layer from a rational Layer
Equations
- One or more equations did not get rendered due to their size.
Instances For
Aligned Input Representation #
Equations
- One or more equations did not get rendered due to their size.
Instances For
Align an IntervalArray to a given exponent for integer arithmetic
Equations
- One or more equations did not get rendered due to their size.
Instances For
The Optimized Forward Pass
This is the main verification kernel. It performs:
- Input alignment to layer's common exponent
- Split-sign matrix multiplication (no branching!)
- Integer addition for bias
- Integer max for ReLU
- Result in aligned integer format
All operations are pure integer (GMP) arithmetic.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Convert AlignedInput back to an IntervalArray
Equations
- One or more equations did not get rendered due to their size.
Instances For
Semantics of Quantized Types #
The real value represented by a quantized integer: n × 2^e
Equations
- LeanCert.ML.Optimized.QuantizedLayer.intVal n e = ↑n * 2 ^ e
Instances For
Membership in AlignedInput: a real vector x is contained if each component satisfies lo[i] × 2^exp ≤ x[i] ≤ hi[i] × 2^exp
Equations
- One or more equations did not get rendered due to their size.
Instances For
Split-Sign Arithmetic Lemmas #
Dot product bounds for nonnegative weights. If p[i] ≥ 0 for all i, and l[i] ≤ x[i] ≤ u[i], then Σ p[i]*l[i] ≤ Σ p[i]*x[i] ≤ Σ p[i]*u[i]
Main split-sign lemma: If W = P - N with P,N ≥ 0, and l ≤ x ≤ u, then P·l - N·u ≤ W·x ≤ P·u - N·l
Soundness Theorem #
Soundness of the Optimized Forward Pass.
This theorem establishes that the interval bounds computed by forwardQuantized
are mathematically valid: the output lower bound is ≤ output upper bound.
The proof uses the split-sign decomposition property:
- If W = W⁺ - W⁻ where W⁺, W⁻ ≥ 0
- And l ≤ x ≤ u
- Then W⁺·l - W⁻·u ≤ W·x ≤ W⁺·u - W⁻·l
Combined with ReLU monotonicity, this gives the soundness of the entire forward pass.
Main soundness theorem: The output interval bounds satisfy lo ≤ hi when there exists a real input x in the input interval.
This version shows that the output interval is non-empty.
Full Network with Quantized Layers #
A neural network with all layers quantized for fast verification
- layers : Array QuantizedLayer
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a quantized network from a list of layers
Equations
- LeanCert.ML.Optimized.QuantizedNet.ofLayers ls prec = { layers := (List.map (fun (x : LeanCert.ML.Layer) => LeanCert.ML.Optimized.QuantizedLayer.ofLayer x prec) ls).toArray }
Instances For
Forward pass through the entire quantized network
Equations
- net.forward input = Array.foldl (fun (acc : LeanCert.ML.Optimized.QuantizedLayer.AlignedInput) (l : LeanCert.ML.Optimized.QuantizedLayer) => l.forwardQuantized acc) input net.layers