TorchLean API

NN.Verification.Builtin.Lowering.Builder

Verifier IR Lowering #

This interpreter records the forward operations of a TorchLean Program in NN.IR.Graph. Tensor constants and layer weights go into a separate ParamStore, keyed by node identifier. The resulting graph can be passed to the interval and CROWN checkers, which first check whether their transfer rules support its operations and parameter payloads.

The builder handles arithmetic, shape operations, common activations, pooling, linear layers, and arbitrary-rank convolution. Composite operations such as multi-head attention produce several IR nodes. Training BatchNorm, tensor-valued gather/scatter indices, and selection on an inner axis return lowering errors.

Softplus and safeLog have dedicated operations that retain the scalar specification's stable branch. SafeLog stores epsilon as a scalar constant parent, so its value, including any dual components, reaches the evaluator unchanged. Its outer logarithm follows the source backend's semantics; certificate transfers separately check the domain needed for a sound enclosure.

LayerNorm stores the supplied epsilon, scale, and bias together in its parameter payload. We keep epsilon even when its scalar equality test reports the default value: for example, Dual equality compares the primal part, while its tangent may still differ. The IR evaluator uses the complete payload. Last-axis IBP and CROWN value bounds use directed arithmetic for the normalization and affine transform. The derivative passes leave these payloads unresolved until their rules account for the supplied parameters and the specification's variance calculation.

Leading-axis select receives a Fin index while constructing the graph. Reading coordinate zero of a state vector therefore becomes a constant one-hot projection followed by a reshape. indexSelect and scatterAdd receive their indices as tensor data and need separate lowering and transfer rules.

PyTorch weight and graph import is implemented under NN.Runtime.PyTorch.Import.

References (informal):

IR builder #

Reference produced while lowering a TorchLean program.

A value is either already materialized as an IR node, or it is still a compile-time tensor constant that can be inserted into the verifier ParamStore if a later operation needs a node parent.

Instances For

    Mutable builder state for translating a TorchLean program into verifier IR.

    Instances For
      @[reducible, inline]

      Builder monad used by TorchLean-to-IR lowering.

      Instances For
        def NN.Verification.Builtin.fail {α : Type} [TorchLean.Storage α] [Context α] {β : Type} (msg : String) :
        BuildM α β

        Raise a lowering error inside BuildM.

        Instances For

          Run a shared IR shape contract and surface its error from lowering.

          Instances For

            Append a freshly constructed IR node to the builder state.

            Instances For

              Return the next node identifier, which is the current node-array size.

              Instances For

                Ensure a Ref is represented by an IR node.

                Compile-time constants are materialized as .const nodes and recorded in the verifier ParamStore; existing graph nodes are returned unchanged.

                Instances For
                  def NN.Verification.Builtin.emitUnary {α : Type} [TorchLean.Storage α] [Context α] {s t : Spec.Shape} (kind : IR.OpKind) (x : Ref α s) (outShape : Spec.Shape := t) :
                  BuildM α (Ref α t)

                  Emit a unary IR operation with one parent node.

                  Instances For
                    def NN.Verification.Builtin.emitBinary {α : Type} [TorchLean.Storage α] [Context α] {s : Spec.Shape} (kind : IR.OpKind) (a b : Ref α s) :
                    BuildM α (Ref α s)

                    Emit a binary IR operation whose operands have the same shape.

                    Instances For
                      def NN.Verification.Builtin.emitMatmul {α : Type} [TorchLean.Storage α] [Context α] {sA sB sOut : Spec.Shape} (a : Ref α sA) (b : Ref α sB) (outShape : Spec.Shape := sOut) :
                      BuildM α (Ref α sOut)

                      Emit a matrix-multiplication IR node.

                      Instances For

                        Emit the designated verifier input node. We keep this at id 0 for bound seeding.

                        Instances For

                          Read a compile-time constant tensor, failing if the value already depends on graph input.

                          Instances For
                            def NN.Verification.Builtin.emitMultiHeadAttention {α : Type} [TorchLean.Storage α] [Context α] {n numHeads dModel headDim : } (wq wk wv : Ref α (Spec.Shape.dim dModel (Spec.Shape.dim (numHeads * headDim) Spec.Shape.scalar))) (wo : Ref α (Spec.Shape.dim (numHeads * headDim) (Spec.Shape.dim dModel Spec.Shape.scalar))) (x : Ref α (Spec.Shape.dim n (Spec.Shape.dim dModel Spec.Shape.scalar))) (mask : Option (TorchLean.Tensor Bool [n, n])) :

                            Lower one sample of multi-head attention into the verifier IR.

                            Instances For
                              def NN.Verification.Builtin.emitLeadingSlice {α : Type} [TorchLean.Storage α] [Context α] {n len : } {s : Spec.Shape} (start : ) (_h : start + len n) (x : Ref α (Spec.Shape.dim n s)) :
                              BuildM α (Ref α (Spec.Shape.dim len s))

                              Exact leading-axis slice expressed through the verifier's affine matrix fragment.

                              Instances For
                                def NN.Verification.Builtin.emitLeadingConcat {α : Type} [TorchLean.Storage α] [Context α] {n m : } {s : Spec.Shape} (a : Ref α (Spec.Shape.dim n s)) (b : Ref α (Spec.Shape.dim m s)) :
                                BuildM α (Ref α (Spec.Shape.dim (n + m) s))

                                Emit a verifier-IR concatenation along the leading axis.

                                Instances For
                                  def NN.Verification.Builtin.emitBatchedMultiHeadAttention {α : Type} [TorchLean.Storage α] [Context α] {batch n numHeads dModel headDim : } (wq wk wv : Ref α (Spec.Shape.dim dModel (Spec.Shape.dim (numHeads * headDim) Spec.Shape.scalar))) (wo : Ref α (Spec.Shape.dim (numHeads * headDim) (Spec.Shape.dim dModel Spec.Shape.scalar))) (x : Ref α (Spec.Shape.dim batch (Spec.Shape.dim n (Spec.Shape.dim dModel Spec.Shape.scalar)))) (mask : Option (TorchLean.Tensor Bool [n, n])) :

                                  Lower batched attention as the leading-axis map of the single-sample verifier graph.

                                  This is intentionally a semantic lowering rather than a claim that the verifier understands a new opaque fused kernel.

                                  Instances For