TorchLean API

NN.Runtime.Autograd.Engine.LibTorch.Ops.NormSoftmax

CUDA Tape Operations: Normalization and Row Softmax #

Record weighted logits cross-entropy and its two native numerical pullbacks.

Instances For

    Record mean teacher-logit cross-entropy. The existing tape owns the student pullback; ATen evaluates its numerical formula without native graph recording.

    Instances For

      Normalization #

      @[inline]
      def Runtime.Autograd.LibTorch.Tape.layerNorm {seqLen embedDim : ℕ} (h_seq_pos : seqLen > 0) (h_embed_pos : embedDim > 0) (t : Tape SomeBuffer) (xId gammaId betaId : ℕ) (epsilon : Float := TorchLean.normalizationEpsilon) :

      LayerNorm over the last dimension for (seqLen, embedDim) buffers.

      The tape records one normalization operation and keeps TorchLean's usual VJP. The native primitive evaluates the forward formula and that VJP with LibTorch tensor operations; intermediate tensors remain internal to the primitive.

      epsilon is added to the variance before taking the square root. Backward reuses the normalized input and inverse standard deviation saved by forward, so both passes use the caller's value.

      Instances For
        @[inline]
        def Runtime.Autograd.LibTorch.Tape.batchNorm {channels : ℕ} {spatial : Spec.Shape} (hWellFormed : (Spec.Shape.dim channels spatial).wellFormed) (t : Tape SomeBuffer) (xId gammaId betaId : ℕ) (epsilon : Float := TorchLean.normalizationEpsilon) :

        Batch normalization over every axis after the channel axis.

        The spatial shape is folded to one contiguous dimension for the CUDA reduction. This is a view of the storage layout, not a rank-specific implementation.

        Instances For

          Softmax (last axis, row folding) #

          All leading dimensions are folded into one row axis; normalization acts independently on the last axis at any rank. Empty tensors retain their shape. A scalar softmax is one, and a scalar log-softmax is zero; both have zero cotangent.

          @[inline]
          def Runtime.Autograd.LibTorch.Tape.Internal.rowOpLast {s : Spec.Shape} (t : Tape SomeBuffer) (opName : String) (xId : ℕ) (scalarForward : Buffer → Buffer) (forward : Buffer → UInt32 → UInt32 → Buffer.WithWorkspace) (backward : Buffer → Buffer → UInt32 → UInt32 → Buffer) :

          Record a row operation whose VJP uses the saved output.

          The scalar forward function runs only after input validation. Empty tensors are copied; for non-scalar, nonempty tensors, row-dimension validation precedes input lookup.

          Instances For
            @[inline]

            Record a last-axis softmax on the tape, returning the extended tape and the new node id.

            Instances For
              @[inline]

              The top-k mask along the last axis (Spec.MoE.topKMaskSpec), as a node that receives no gradient. A scalar is a row of one entry, kept when 0 < k.

              Instances For
                @[inline]

                Stable log-softmax along the last axis, implemented directly on CUDA buffers.

                Instances For