TorchLean API

NN.GraphSpec.DAG.Primitives.Nonlinear

DAG Nonlinear and Attention Primitives #

Elementwise nonlinearities, softmax, and multi-head self-attention for typed DAG models.

Coordinatewise sigmoid.

Instances For
    @[simp]

    Pure evaluation of coordinatewise sigmoid.

    Coordinatewise SiLU, also called swish: x * sigmoid x.

    Instances For
      @[simp]

      Pure evaluation of coordinatewise SiLU.

      Coordinatewise Gaussian error linear unit using TorchLean's tanh approximation.

      Instances For
        @[simp]

        Pure evaluation of coordinatewise GELU.

        def NN.GraphSpec.DAG.PrimOp.attention (leading : Spec.Shape) (n numHeads dModel headDim : ℕ) (hN : 0 < n) :
        PrimOp [[dModel, numHeads * headDim], [dModel, numHeads * headDim], [dModel, numHeads * headDim], [numHeads * headDim, dModel], leading.concat [n, dModel]] (leading.concat [n, dModel])

        Multi-head self-attention over an arbitrary leading shape.

        The pure semantics applies Spec.MultiHeadAttention.forward independently at every leading index. The executable path uses the prefix-polymorphic TorchLean operation and may flatten those axes for backend execution without changing the mathematical operation.

        Instances For
          @[simp]
          theorem NN.GraphSpec.DAG.PrimOp.attention_specFwd {leading : Spec.Shape} {n numHeads dModel headDim : ℕ} (hN : 0 < n) {α : Type} [TorchLean.Storage α] [Context α] (wq wk wv : TorchLean.Tensor α [dModel, numHeads * headDim]) (wo : TorchLean.Tensor α [numHeads * headDim, dModel]) (input : TorchLean.Tensor α (leading.concat [n, dModel])) :
          (attention leading n numHeads dModel headDim hN).specFwd (TorchLean.TensorPack.cons wq (TorchLean.TensorPack.cons wk (TorchLean.TensorPack.cons wv (TorchLean.TensorPack.cons wo (TorchLean.TensorPack.cons input TorchLean.TensorPack.nil))))) = TorchLean.Tensor.mapLeading leading (fun (inputs : TorchLean.Tensor α [n, dModel]) => Spec.MultiHeadAttention.forward n ⋯ { queryWeight := wq, keyWeight := wk, valueWeight := wv, outputWeight := wo } inputs none) input

          Pure evaluation of prefix-polymorphic multi-head attention.

          Coordinatewise exponential.

          Instances For

            Coordinatewise multiplicative inverse.

            Instances For
              @[simp]

              The pure meaning of the DAG inverse node is coordinatewise reciprocal.

              Coordinatewise hyperbolic tangent.

              Instances For

                Stable softmax along any in-bounds tensor dimension.

                Instances For