Matrix-Based Network Operations #
This module connects the optimized IntervalMatrix infrastructure with the
neural network layer definitions, enabling:
- Batch Processing: Forward pass on multiple inputs simultaneously
- Attention Mechanism: Q × K^T computation for Transformer verification
- Efficient Layer Application: Using quantized integer arithmetic
Key Definitions #
toIntervalMatrix- Convert weight matrix to IntervalMatrix formatbatchForward- Apply layer to multiple inputs (batch dimension)attentionScores- Compute Q × K^T for self-attention
Design Notes #
The IntervalMatrix uses Structure-of-Arrays layout for cache efficiency.
For Transformer verification, the key operations are:
- Linear projections (matmul with weight matrices)
- Attention score computation (Q × K^T)
- Softmax bounds (separate module)
Conversion Functions #
Convert a rational weight matrix (List of Lists) to IntMatrix with given exponent. Each rational q is converted to an integer q * 2^(-exp) (rounded).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Convert Layer weights to IntervalMatrix (point intervals). Useful for propagating through linear layers with matrix multiply.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Batch Forward Pass #
A batch of inputs represented as an IntervalMatrix. Each column is one input vector, rows are features. Shape: (input_dim, batch_size)
Equations
- LeanCert.ML.Optimized.InputBatch inputDim batchSize = LeanCert.ML.Optimized.IntervalMatrix inputDim batchSize
Instances For
A batch of outputs represented as an IntervalMatrix. Shape: (output_dim, batch_size)
Equations
- LeanCert.ML.Optimized.OutputBatch outputDim batchSize = LeanCert.ML.Optimized.IntervalMatrix outputDim batchSize
Instances For
Apply weight matrix to a batch of inputs: W × X where W : (output_dim, input_dim) and X : (input_dim, batch_size) Result: (output_dim, batch_size)
Equations
- LeanCert.ML.Optimized.batchLinear W X = W.matmul X
Instances For
Attention Mechanism Support #
Compute attention scores: Q × K^T Q : (seq_len, d_k) - Query matrix K : (seq_len, d_k) - Key matrix Result: (seq_len, seq_len) - Attention score matrix
Equations
Instances For
Structure for Multi-Head Attention parameters
- wQ : IntervalMatrix dModel dModel
Query projection weights (dModel, dModel)
- wK : IntervalMatrix dModel dModel
Key projection weights (dModel, dModel)
- wV : IntervalMatrix dModel dModel
Value projection weights (dModel, dModel)
- wO : IntervalMatrix dModel dModel
Output projection weights (dModel, dModel)
Instances For
Project input to Q, K, V matrices. Input X : (seq_len, d_model) Returns (Q, K, V) each of shape (seq_len, d_model)
Equations
Instances For
ReLU for Matrices #
Element-wise ReLU on an IntMatrix: max(0, x)
Equations
- LeanCert.ML.Optimized.reluIntMatrix M = LeanCert.ML.Optimized.IntMatrix.ofFn fun (i : Fin r) (j : Fin c) => max 0 (M.get ↑i ↑j ⋯ ⋯)
Instances For
Element-wise ReLU on IntervalMatrix. For interval [lo, hi]: ReLU([lo, hi]) = [max(0, lo), max(0, hi)]
Equations
Instances For
Bias Addition for Matrices #
Add bias vector to each column of a matrix. M : (out_dim, batch_size), bias : out_dim vector Result: M[i,j] + bias[i] for all i,j
Equations
- One or more equations did not get rendered due to their size.
Instances For
Full Layer Forward (Matrix version) #
Forward pass through a layer with batch input. Computes ReLU(W × X + b) where X is a batch.
Equations
- LeanCert.ML.Optimized.layerForwardBatch W bias_lo bias_hi X = LeanCert.ML.Optimized.reluIntervalMatrix (LeanCert.ML.Optimized.addBiasMatrix (W.matmul X) bias_lo bias_hi)
Instances For
Soundness (Placeholder) #
Soundness of batch linear transformation
Soundness of attention score computation