Documentation

LeanCert.ML.Optimized.MatrixNetwork

Matrix-Based Network Operations #

This module connects the optimized IntervalMatrix infrastructure with the neural network layer definitions, enabling:

  1. Batch Processing: Forward pass on multiple inputs simultaneously
  2. Attention Mechanism: Q × K^T computation for Transformer verification
  3. Efficient Layer Application: Using quantized integer arithmetic

Key Definitions #

Design Notes #

The IntervalMatrix uses Structure-of-Arrays layout for cache efficiency. For Transformer verification, the key operations are:

Conversion Functions #

def LeanCert.ML.Optimized.rationalToIntMatrix (rows cols : ) (weights : List (List )) (exp : ) :
IntMatrix rows cols

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 #

      @[reducible, inline]
      abbrev LeanCert.ML.Optimized.InputBatch (inputDim batchSize : ) :

      A batch of inputs represented as an IntervalMatrix. Each column is one input vector, rows are features. Shape: (input_dim, batch_size)

      Equations
      Instances For
        @[reducible, inline]
        abbrev LeanCert.ML.Optimized.OutputBatch (outputDim batchSize : ) :

        A batch of outputs represented as an IntervalMatrix. Shape: (output_dim, batch_size)

        Equations
        Instances For
          def LeanCert.ML.Optimized.batchLinear {outDim inDim batchSize : } (W : IntervalMatrix outDim inDim) (X : InputBatch inDim batchSize) :
          OutputBatch outDim batchSize

          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
          Instances For

            Attention Mechanism Support #

            def LeanCert.ML.Optimized.attentionScores {seqLen dK : } (Q K : IntervalMatrix seqLen dK) :
            IntervalMatrix seqLen seqLen

            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 LeanCert.ML.Optimized.AttentionParams (dModel numHeads : ) :

              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
                def LeanCert.ML.Optimized.projectQKV {numHeads seqLen dModel : } (X : IntervalMatrix seqLen dModel) (params : AttentionParams dModel numHeads) :
                IntervalMatrix seqLen dModel × IntervalMatrix seqLen dModel × IntervalMatrix seqLen dModel

                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
                  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 #

                      def LeanCert.ML.Optimized.addBiasMatrix {outDim batchSize : } (M : IntervalMatrix outDim batchSize) (bias_lo bias_hi : Fin outDim) :
                      IntervalMatrix outDim batchSize

                      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) #

                        def LeanCert.ML.Optimized.layerForwardBatch {inDim outDim batchSize : } (W : IntervalMatrix outDim inDim) (bias_lo bias_hi : Fin outDim) (X : InputBatch inDim batchSize) :
                        OutputBatch outDim batchSize

                        Forward pass through a layer with batch input. Computes ReLU(W × X + b) where X is a batch.

                        Equations
                        Instances For

                          Soundness (Placeholder) #

                          theorem LeanCert.ML.Optimized.mem_batchLinear {outDim inDim batchSize : } (W : IntervalMatrix outDim inDim) (X : InputBatch inDim batchSize) (W_real : Matrix (Fin outDim) (Fin inDim) ) (X_real : Matrix (Fin inDim) (Fin batchSize) ) (exp : ) (hW : W.mem exp W_real) (hX : IntervalMatrix.mem X exp X_real) :
                          IntervalMatrix.mem (batchLinear W X) (exp + exp) (W_real * X_real)

                          Soundness of batch linear transformation

                          theorem LeanCert.ML.Optimized.mem_attentionScores {seqLen dK : } (Q K : IntervalMatrix seqLen dK) (Q_real K_real : Matrix (Fin seqLen) (Fin dK) ) (exp : ) (hQ : Q.mem exp Q_real) (hK : K.mem exp K_real) :
                          (attentionScores Q K).mem (exp + exp) (Q_real * K_real.transpose)

                          Soundness of attention score computation