TorchLean API

NN.Runtime.Autograd.Compiled.GraphM.ShapeIndex

GraphM Shape And Indexing Ops #

Reshape, transpose, broadcast, reduction, gather, and scatter builders for proof-compiled graphs.

Flatten a tensor to a 1D vector (preserving total size).

PyTorch comparison: torch.flatten(x) (for a single tensor value).

Instances For
    def Runtime.Autograd.Compiled.GraphM.reshape {α Δ : Type} [Inhabited α] [Zero α] [DecidableEq Spec.Shape] {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (x : Var s₁) (h : s₁.size = s₂.size) :
    MWith α Δ Γ (Var s₂)

    Reshape a tensor, given a proof that the total sizes match.

    PyTorch comparison: torch.reshape(x, new_shape).

    Instances For

      Transpose a 2D matrix. PyTorch comparison: x.transpose(0, 1) / x.T for matrices.

      Instances For

        Transpose a rank-3 tensor by moving the first axis to the last ((a,b,c) → (b,c,a)).

        PyTorch comparison: x.permute(1, 2, 0).

        Instances For

          Transpose a rank-3 tensor by moving the last axis to the first ((a,b,c) → (c,a,b)).

          PyTorch comparison: x.permute(2, 0, 1).

          Instances For

            Swap the last two axes of a rank-3 tensor ((a,b,c) → (a,c,b)).

            PyTorch comparison: x.transpose(1, 2) for a 3D tensor.

            Instances For

              Swap two adjacent axes at a given nesting depth.

              This is the compiled-graph analogue of the eager Tape.swapAdjacentAtDepth. PyTorch comparison: a permute that swaps two neighboring dimensions.

              Instances For
                def Runtime.Autograd.Compiled.GraphM.broadcastTo {α Δ : Type} [Inhabited α] [Add α] [Zero α] [DecidableEq Spec.Shape] {Γ : List Spec.Shape} {s₁ s₂ : Spec.Shape} (cb : s₁.CanBroadcastTo s₂) (x : Var s₁) :
                MWith α Δ Γ (Var s₂)

                Broadcast x : s₁ to a larger shape s₂ (given a CanBroadcastTo witness).

                PyTorch comparison: x.expand(...) / broadcasting semantics in elementwise ops.

                Instances For
                  def Runtime.Autograd.Compiled.GraphM.reduceSum {α Δ : Type} [Add α] [Zero α] [Inhabited α] [DecidableEq Spec.Shape] {Γ : List Spec.Shape} {s : Spec.Shape} (axis : ) [valid : Spec.Shape.valid_axis_inst axis s] [wf : s.WellFormed] (x : Var s) :
                  MWith α Δ Γ (Var (Spec.Tensor.shapeAfterSum s axis))

                  Reduce-sum along a given axis.

                  PyTorch comparison: torch.sum(x, dim=axis).

                  Instances For

                    Reduce-mean along a given axis.

                    PyTorch comparison: torch.mean(x, dim=axis).

                    Instances For

                      Gather a single scalar from a vector at a known-in-bounds index.

                      PyTorch comparison: x[i] for a 1D tensor.

                      Instances For

                        Gather a row from a matrix at a known-in-bounds row index.

                        PyTorch comparison: x[i, :] for a 2D tensor.

                        Instances For

                          Gather a scalar from a vector at a runtime Nat index.

                          If i is out of bounds we return 0 and propagate no gradient (matching the forward choice).

                          Instances For

                            Gather a vector of length k from a length-n vector using an index tensor of Nats.

                            Out-of-bounds indices yield 0 at the corresponding output position.

                            PyTorch comparison: torch.gather for 1D inputs, with explicit bounds handling.

                            Instances For

                              Gather k rows from a (rows×cols) matrix using an index vector of Nats.

                              Out-of-bounds indices yield a zero row.

                              PyTorch comparison: torch.index_select(x, dim=0, index=idx) with explicit bounds handling.

                              Instances For

                                Scatter-add into a vector at a single in-bounds index.

                                scatter_add_vec x v i adds the scalar v into x[i].

                                PyTorch comparison: x.index_add_(dim=0, index=[i], source=[v]) (conceptually).

                                Instances For

                                  Scatter-add into a matrix at a single in-bounds row index.

                                  scatter_add_row x v i adds the row vector v into x[i, :].

                                  PyTorch comparison: x.index_add_(dim=0, index=[i], source=v.unsqueeze(0)) (conceptually).

                                  Instances For