TorchLean API

NN.Runtime.Autograd.Torch.Core.Ops

Eager Tensor Operations #

PyTorch-style tensor operations backed by the eager CPU/CUDA tapes. These wrappers record runtime nodes, dispatch CUDA kernels when requested, and preserve the typed TensorRef surface.

Tensor ops (eager tape wrappers) #

The following definitions are the eager front-end for Runtime.Autograd.Tape.* primitives. Each one:

PyTorch comparison: this is the standard eager autograd mechanism (a dynamic tape of ops).

def Runtime.Autograd.Torch.Internal.EagerSession.dispatchCudaOpt {α β : Type} (s : EagerSession α) (opName : String) (cpu : IO β) (cuda : IO (Option β)) :
IO β

Dispatch an eager op with optional CUDA support.

When Options.device = .cuda, any op whose CUDA implementation returns none will throw.

TorchLean's CUDA eager mode has no per-op CPU fallback: either the op is supported by CUDA, or it errors immediately.

Instances For

    Record elementwise addition a + b. PyTorch: torch.add.

    Instances For

      Record elementwise subtraction a - b. PyTorch: torch.sub.

      Instances For

        Record elementwise multiplication a * b. PyTorch: torch.mul.

        Instances For

          Record scaling by a scalar constant. PyTorch: x * c.

          Instances For
            def Runtime.Autograd.Torch.Internal.EagerSession.abs {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (x : TensorRef α sh) :
            IO (TensorRef α sh)

            Record elementwise absolute value. PyTorch: torch.abs.

            Instances For
              def Runtime.Autograd.Torch.Internal.EagerSession.sqrt {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (x : TensorRef α sh) :
              IO (TensorRef α sh)

              Record elementwise square root. PyTorch: torch.sqrt.

              Instances For
                def Runtime.Autograd.Torch.Internal.EagerSession.clamp {α : Type} [CudaBridge.TensorConv α] (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (x : TensorRef α sh) (minVal maxVal : α) :
                IO (TensorRef α sh)

                Record elementwise clamp to [minVal,maxVal]. PyTorch: torch.clamp.

                Instances For
                  def Runtime.Autograd.Torch.Internal.EagerSession.max {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (a b : TensorRef α sh) :
                  IO (TensorRef α sh)

                  Record elementwise maximum. PyTorch: torch.maximum.

                  Instances For
                    def Runtime.Autograd.Torch.Internal.EagerSession.min {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (a b : TensorRef α sh) :
                    IO (TensorRef α sh)

                    Record elementwise minimum. PyTorch: torch.minimum.

                    Instances For
                      def Runtime.Autograd.Torch.Internal.EagerSession.relu {α : Type} (s : EagerSession α) [Mul α] [Zero α] [Max α] [One α] [LT α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {sh : Spec.Shape} (x : TensorRef α sh) :
                      IO (TensorRef α sh)

                      Record elementwise ReLU. PyTorch: torch.relu / torch.nn.functional.relu.

                      Instances For

                        Record elementwise sigmoid. PyTorch: torch.sigmoid.

                        Instances For

                          Record elementwise tanh. PyTorch: torch.tanh.

                          Instances For

                            Record softmax (shape-preserving).

                            PyTorch comparison: torch.softmax(x, dim=...) (dimension convention is chosen by the underlying tape op).

                            Instances For

                              Record stable log-softmax (shape-preserving, last-axis convention).

                              PyTorch comparison: torch.nn.functional.log_softmax(x, dim=-1).

                              Instances For

                                Record elementwise softplus. PyTorch: torch.nn.functional.softplus.

                                Instances For

                                  Record elementwise exponential. PyTorch: torch.exp.

                                  Instances For

                                    Record elementwise log. PyTorch: torch.log.

                                    Instances For

                                      Record elementwise inverse 1/x. PyTorch: torch.reciprocal.

                                      Instances For

                                        Record elementwise log with epsilon guard.

                                        PyTorch comparison: torch.log(torch.clamp(x, min=ε)).

                                        Instances For

                                          Sum-reduce all elements to a scalar. PyTorch: x.sum().

                                          Instances For

                                            Flatten a tensor to a 1D vector. PyTorch: torch.flatten.

                                            Instances For

                                              Reshape a tensor while preserving total number of elements.

                                              PyTorch comparison: torch.reshape / view (when valid).

                                              Instances For

                                                Transpose a 2D matrix. PyTorch: x.t() / x.transpose(0,1).

                                                Instances For

                                                  Swap two adjacent axes at a given depth. PyTorch analogue: x.transpose(dim, dim+1).

                                                  Instances For

                                                    Swap the last two axes of a 3D tensor (a,b,c) → (a,c,b). PyTorch: x.transpose(1,2).

                                                    Instances For

                                                      Broadcast a tensor to a larger shape. PyTorch: implicit broadcasting / expand.

                                                      Instances For

                                                        Sum-reduce along axis. PyTorch: torch.sum(x, dim=axis).

                                                        Instances For

                                                          Mean-reduce along axis. PyTorch: torch.mean(x, dim=axis).

                                                          Instances For

                                                            Gather a scalar from a 1D vector with a Fin n index. PyTorch: x[i].

                                                            Instances For

                                                              Gather a row from a 2D tensor with a Fin rows index. PyTorch: x[i] for 2D tensors.

                                                              Instances For

                                                                Gather a scalar from a 1D vector with a raw Nat index (totalized by the tape op).

                                                                Instances For

                                                                  Dynamic gather scalar using an index stored in NatRef.

                                                                  Instances For

                                                                    Dynamic gather row using an index stored in NatRef (out-of-range gives a zero row).

                                                                    Instances For

                                                                      Gather k scalars using an explicit index tensor. PyTorch analogue: gather / advanced indexing.

                                                                      Instances For

                                                                        Gather k rows using an explicit index tensor. PyTorch: index_select(dim=0, index=...).

                                                                        Instances For

                                                                          Gather k scalars using indices stored in the nat-environment (NatVecRef).

                                                                          Instances For

                                                                            Gather k rows using indices stored in the nat-environment (NatVecRef).

                                                                            Instances For

                                                                              Scatter-add into a vector: return a copy of x with x[i] += v.

                                                                              Instances For

                                                                                Scatter-add into a matrix row: return a copy of x with x[i,:] += v.

                                                                                Instances For

                                                                                  Fully-connected linear layer y = w x + b (matvec).

                                                                                  If opts.fastKernels is enabled, uses a runtime-only fast kernel implementation. PyTorch comparison: torch.nn.functional.linear(x, weight=w, bias=b).

                                                                                  Instances For

                                                                                    Mean-squared-error loss returning a scalar.

                                                                                    If opts.fastKernels is enabled, uses a runtime-only fast kernel implementation. PyTorch comparison: torch.nn.functional.mse_loss(..., reduction=\"mean\").

                                                                                    Instances For
                                                                                      def Runtime.Autograd.Torch.Internal.EagerSession.layerNorm {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {seqLen embedDim : } (h_seq_pos : seqLen > 0) (h_embed_pos : embedDim > 0) (x : TensorRef α (Spec.Shape.dim seqLen (Spec.Shape.dim embedDim Spec.Shape.scalar))) (gamma beta : TensorRef α (Spec.Shape.dim embedDim Spec.Shape.scalar)) :

                                                                                      Layer normalization over embedding dimension. PyTorch: nn.LayerNorm / functional.layer_norm.

                                                                                      Instances For
                                                                                        def Runtime.Autograd.Torch.Internal.EagerSession.batchnormChannelFirst {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {channels height width : } (h_c : channels > 0) (h_h : height > 0) (h_w : width > 0) (x : TensorRef α (Spec.Shape.dim channels (Spec.Shape.dim height (Spec.Shape.dim width Spec.Shape.scalar)))) (gamma beta : TensorRef α (Spec.Shape.dim channels Spec.Shape.scalar)) :

                                                                                        BatchNorm for channel-first images (C,H,W) (no batch axis). PyTorch: nn.BatchNorm2d (conceptually).

                                                                                        Instances For
                                                                                          def Runtime.Autograd.Torch.Internal.EagerSession.multiHeadAttention {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {n numHeads dModel headDim : } (h1 : n 0) (wq wk wv : TensorRef α (Spec.Shape.dim dModel (Spec.Shape.dim (numHeads * headDim) Spec.Shape.scalar))) (wo : TensorRef α (Spec.Shape.dim (numHeads * headDim) (Spec.Shape.dim dModel Spec.Shape.scalar))) (x : TensorRef α (Spec.Shape.dim n (Spec.Shape.dim dModel Spec.Shape.scalar))) (mask : Option (Spec.Tensor Bool (Spec.Shape.dim n (Spec.Shape.dim n Spec.Shape.scalar))) := none) :

                                                                                          Multi-head self-attention (typed, proof-friendly). PyTorch: nn.MultiheadAttention (conceptually).

                                                                                          Instances For
                                                                                            def Runtime.Autograd.Torch.Internal.EagerSession.conv {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {d inC outC : } {kernel stride padding inSpatial : Vector d} {hInC : inC 0} {hKernel : ∀ (i : Fin d), kernel.get i 0} (w : TensorRef α (Spec.Shape.ofList (outC :: inC :: kernel.toList))) (b : TensorRef α (Spec.Shape.dim outC Spec.Shape.scalar)) (x : TensorRef α (Spec.Shape.ofList (inC :: inSpatial.toList))) :
                                                                                            IO (TensorRef α (Spec.Shape.ofList (outC :: (Spec.convOutSpatial inSpatial kernel stride padding).toList)))

                                                                                            N-D convolution for channels-first tensors (inC, spatial...) (no batch axis).

                                                                                            This is the generic counterpart to conv2d. PyTorch comparison: torch.nn.functional.conv{d}d specialized to a single sample.

                                                                                            Instances For
                                                                                              def Runtime.Autograd.Torch.Internal.EagerSession.conv2d {α : Type} (s : EagerSession α) [Context α] [DecidableRel fun (x1 x2 : α) => x1 > x2] [DecidableEq Spec.Shape] {inC outC kH kW stride padding inH inW : } {h1 : inC 0} {h2 : kH 0} {h3 : kW 0} (kernel : TensorRef α (Spec.Shape.dim outC (Spec.Shape.dim inC (Spec.Shape.dim kH (Spec.Shape.dim kW Spec.Shape.scalar))))) (bias : TensorRef α (Spec.Shape.dim outC Spec.Shape.scalar)) (input : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                              IO (TensorRef α (Spec.Shape.dim outC (Spec.Shape.dim ((inH + 2 * padding - kH) / stride + 1) (Spec.Shape.dim ((inW + 2 * padding - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                              2D convolution for channel-first images (C,H,W) (no batch axis). PyTorch: torch.nn.functional.conv2d.

                                                                                              Instances For
                                                                                                def Runtime.Autograd.Torch.Internal.EagerSession.convTranspose {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {d inC outC : } {kernel stride padding inSpatial : Vector d} {hInC : inC 0} {hKernel : ∀ (i : Fin d), kernel.get i 0} (w : TensorRef α (Spec.Shape.ofList (inC :: outC :: kernel.toList))) (b : TensorRef α (Spec.Shape.dim outC Spec.Shape.scalar)) (x : TensorRef α (Spec.Shape.ofList (inC :: inSpatial.toList))) :
                                                                                                IO (TensorRef α (Spec.Shape.ofList (outC :: (Spec.convTransposeOutSpatial inSpatial kernel stride padding).toList)))

                                                                                                N-D transpose convolution for channels-first tensors (inC, spatial...) (no batch axis).

                                                                                                This is the generic counterpart to conv_transpose2d. PyTorch comparison: torch.nn.functional.conv_transpose{d}d specialized to a single sample.

                                                                                                Instances For
                                                                                                  def Runtime.Autograd.Torch.Internal.EagerSession.convTranspose2d {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {inC outC kH kW stride padding inH inW : } {h1 : inC 0} {h2 : kH 0} {h3 : kW 0} (kernel : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim outC (Spec.Shape.dim kH (Spec.Shape.dim kW Spec.Shape.scalar))))) (bias : TensorRef α (Spec.Shape.dim outC Spec.Shape.scalar)) (input : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                  IO (TensorRef α (Spec.Shape.dim outC (Spec.Shape.dim ((inH - 1) * stride - 2 * padding + kH) (Spec.Shape.dim ((inW - 1) * stride - 2 * padding + kW) Spec.Shape.scalar))))

                                                                                                  2D transpose convolution for channel-first images (C,H,W) (no batch axis). PyTorch: torch.nn.functional.conv_transpose2d.

                                                                                                  Instances For

                                                                                                    2D matrix multiplication. PyTorch: torch.matmul for 2D tensors.

                                                                                                    Instances For

                                                                                                      Batched matrix multiplication. PyTorch: torch.bmm.

                                                                                                      Instances For

                                                                                                        Concatenate two vectors along dim 0. PyTorch: torch.cat([a,b], dim=0).

                                                                                                        Instances For

                                                                                                          Concatenate along dim 0 for tensors with leading dimension. PyTorch: torch.cat(..., dim=0).

                                                                                                          Instances For
                                                                                                            def Runtime.Autograd.Torch.Internal.EagerSession.sliceRange0 {α : Type} (s : EagerSession α) [Zero α] [DecidableEq Spec.Shape] {n : } {sh : Spec.Shape} (x : TensorRef α (Spec.Shape.dim n sh)) (start len : ) (h : len + start n) :

                                                                                                            Slice along dim 0: x[start:start+len]. PyTorch: standard slicing.

                                                                                                            Instances For
                                                                                                              def Runtime.Autograd.Torch.Internal.EagerSession.maxPool {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {d C : } {inSpatial kernel stride padding : Vector d} {hKernel : ∀ (i : Fin d), kernel.get i 0} (x : TensorRef α (Spec.Shape.ofList (C :: inSpatial.toList))) :
                                                                                                              IO (TensorRef α (Spec.Shape.ofList (C :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).toList)))

                                                                                                              N-D max pooling for channels-first tensors (C, spatial...) (no batch axis).

                                                                                                              PyTorch comparison: torch.nn.functional.max_pool1d / max_pool2d / max_pool3d depending on the spatial rank d.

                                                                                                              Instances For
                                                                                                                def Runtime.Autograd.Torch.Internal.EagerSession.avgPool {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {d C : } {inSpatial kernel stride padding : Vector d} (hKernel : ∀ (i : Fin d), kernel.get i 0) (x : TensorRef α (Spec.Shape.ofList (C :: inSpatial.toList))) :
                                                                                                                IO (TensorRef α (Spec.Shape.ofList (C :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).toList)))

                                                                                                                N-D average pooling for channels-first tensors (C, spatial...) (no batch axis).

                                                                                                                PyTorch comparison: torch.nn.functional.avg_pool1d / avg_pool2d / avg_pool3d depending on the spatial rank d.

                                                                                                                Instances For
                                                                                                                  def Runtime.Autograd.Torch.Internal.EagerSession.smoothMaxPool {α : Type} [CudaBridge.TensorConv α] (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {d C : } {inSpatial kernel stride padding : Vector d} {hKernel : ∀ (i : Fin d), kernel.get i 0} (x : TensorRef α (Spec.Shape.ofList (C :: inSpatial.toList))) (beta : α) :
                                                                                                                  IO (TensorRef α (Spec.Shape.ofList (C :: (Spec.poolOutSpatialPad inSpatial kernel stride padding).toList)))

                                                                                                                  N-D smooth max pooling (log-sum-exp surrogate) for channels-first tensors (C, spatial...).

                                                                                                                  This is a differentiable approximation to max pooling; PyTorch does not expose it as a single primitive, but it can be emulated with logsumexp over local windows.

                                                                                                                  Instances For
                                                                                                                    def Runtime.Autograd.Torch.Internal.EagerSession.maxPool2d {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride : } {h1 : kH 0} {h2 : kW 0} (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                    IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH - kH) / stride + 1) (Spec.Shape.dim ((inW - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                    2D max-pooling (no batch axis). PyTorch: torch.nn.functional.max_pool2d.

                                                                                                                    Instances For
                                                                                                                      def Runtime.Autograd.Torch.Internal.EagerSession.maxPool2dPad {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride padding : } {h1 : kH 0} {h2 : kW 0} (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                      IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH + 2 * padding - kH) / stride + 1) (Spec.Shape.dim ((inW + 2 * padding - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                      2D max-pooling with padding (no batch axis). PyTorch: max_pool2d(..., padding=...).

                                                                                                                      Instances For
                                                                                                                        @[reducible, inline]
                                                                                                                        abbrev Runtime.Autograd.Torch.Internal.EagerSession.maxPoolPad {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride padding : } {h1 : kH 0} {h2 : kW 0} (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                        IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH + 2 * padding - kH) / stride + 1) (Spec.Shape.dim ((inW + 2 * padding - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                        Alias for max_pool2d_pad (PyTorch-style shorthand).

                                                                                                                        Instances For
                                                                                                                          def Runtime.Autograd.Torch.Internal.EagerSession.smoothMaxPool2d {α : Type} [CudaBridge.TensorConv α] (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride : } {h1 : kH 0} {h2 : kW 0} (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) (beta : α) :
                                                                                                                          IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH - kH) / stride + 1) (Spec.Shape.dim ((inW - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                          Smooth max-pooling (softmax pooling). Not a standard PyTorch primitive; see Torch.LinkedSession.smooth_max_pool2d.

                                                                                                                          Instances For
                                                                                                                            def Runtime.Autograd.Torch.Internal.EagerSession.avgPool2d {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride : } (h1 : kH 0) (h2 : kW 0) (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                            IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH - kH) / stride + 1) (Spec.Shape.dim ((inW - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                            2D average-pooling (no batch axis). PyTorch: torch.nn.functional.avg_pool2d.

                                                                                                                            Instances For
                                                                                                                              def Runtime.Autograd.Torch.Internal.EagerSession.avgPool2dPad {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride padding : } (h1 : kH 0) (h2 : kW 0) (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                              IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH + 2 * padding - kH) / stride + 1) (Spec.Shape.dim ((inW + 2 * padding - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                              2D average-pooling with padding (no batch axis). PyTorch: avg_pool2d(..., padding=...).

                                                                                                                              Instances For
                                                                                                                                @[reducible, inline]
                                                                                                                                abbrev Runtime.Autograd.Torch.Internal.EagerSession.avgPoolPad {α : Type} (s : EagerSession α) [Context α] [DecidableEq Spec.Shape] {kH kW inH inW inC stride padding : } (h1 : kH 0) (h2 : kW 0) (x : TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim inH (Spec.Shape.dim inW Spec.Shape.scalar)))) :
                                                                                                                                IO (TensorRef α (Spec.Shape.dim inC (Spec.Shape.dim ((inH + 2 * padding - kH) / stride + 1) (Spec.Shape.dim ((inW + 2 * padding - kW) / stride + 1) Spec.Shape.scalar))))

                                                                                                                                Alias for avg_pool2d_pad (PyTorch-style shorthand).

                                                                                                                                Instances For