TorchLean API

NN.Spec.Core.Tensor.Constructors

Tensor constructors (spec layer) #

These are small, total constructors for building TorchLean.Tensor values directly.

They are used heavily inside the spec layer (models/layers) and in proofs, where we want:

For ordinary literals, in-memory conversion, reshape, and scalar casts, import NN.Tensor.

Design choice (why these are "total"):

Constant tensors #

def TorchLean.Tensor.full {α : Type} [Storage α] (shape : Spec.Shape) (value : α) :
Tensor α shape

Fill a tensor of arbitrary shape with one value.

PyTorch analogy: torch.full(shape, value).

Instances For
    @[reducible, inline]
    abbrev TorchLean.Tensor.zeros {α : Type} [Storage α] [Zero α] (shape : Spec.Shape) :
    Tensor α shape

    Construct an all-zero tensor of arbitrary shape. Reducible, so lemmas about full apply.

    Instances For
      @[reducible, inline]
      abbrev TorchLean.Tensor.ones {α : Type} [Storage α] [One α] (shape : Spec.Shape) :
      Tensor α shape

      Construct an all-one tensor of arbitrary shape. Reducible, so lemmas about full apply.

      Instances For
        @[simp]
        theorem TorchLean.Tensor.full_apply {α : Type} [Storage α] (shape : Spec.Shape) (value : α) (coordinate : shape.Coord) :
        Internal.Rep.get (full shape value) coordinate = value

        Every coordinate of a filled tensor contains its fill value.

        @[simp]
        theorem TorchLean.Tensor.item_full_scalar {α : Type} [Storage α] (value : α) :

        Reading a scalar filled tensor returns its fill value.

        @[simp]
        theorem TorchLean.Tensor.item_rep_const {α : Type} [Storage α] (value : α) :
        item (Internal.Rep.const value) = value

        The item of an internally constant scalar tensor is the constant.

        @[simp]
        theorem TorchLean.Tensor.getScalar_rep_const {α : Type} [Storage α] {n : } (value : α) (i : Fin n) :

        Every entry of an internally constant vector is the constant.

        @[simp]
        theorem TorchLean.Tensor.replicate_scalar_apply {α : Type} [Storage α] (value : α) (shape : Spec.Shape) (coordinate : shape.Coord) :
        Internal.Rep.get (Spec.replicate (scalar value)) coordinate = value

        Replicating a scalar tensor observes that scalar at every target coordinate.

        @[simp]
        theorem Spec.get_full {α : Type} [TorchLean.Storage α] (n : ) (s : Shape) (value : α) (i : Fin n) :

        Every outer coordinate of a filled tensor is the corresponding filled subtensor.

        @[simp]
        theorem Spec.get2_full {α : Type} [TorchLean.Storage α] (m n : ) (value : α) (i : Fin m) (j : Fin n) :

        Every coordinate of a filled matrix contains its fill value.

        def TorchLean.Tensor.generate {α : Type} [Storage α] (shape : Spec.Shape) (f : List α) :
        Tensor α shape

        Construct an arbitrary-rank tensor from a coordinate function.

        At each coordinate, f receives one natural-number index per dimension, outermost first. The indices are in bounds by construction.

        For example, Tensor.generate [2, 3] f has shape [2, 3], and its entry at row i and column j is f [i, j].

        Instances For
          def TorchLean.Tensor.ofFn {α : Type} [Storage α] {n : } (values : Fin nα) :

          The buffer is filled directly from the index function, without building one scalar tensor per entry.

          PyTorch analogy: torch.tensor([...]) with shape (n,), but our input is a function, not a list.

          Example:

          -- `torch.tensor([0.0, 1.0, 2.0, 3.0])`, except the entries arrive from a function on `Fin n` and
          -- the length is part of the type.
          def ramp : Tensor Float [4] := Tensor.ofFn fun index => index.val.toFloat
          
          Instances For
            @[simp]
            theorem TorchLean.Tensor.ofFn_apply {α : Type} [Storage α] {n : } (values : Fin nα) (coordinate : Spec.Shape.Coord [n]) :
            Internal.Rep.get (ofFn values) coordinate = values coordinate.1

            Evaluating ofFn at a coordinate returns the value supplied at its index.

            @[simp]
            theorem TorchLean.Tensor.unstack_ofFn {α : Type} [Storage α] {n : } (values : Fin nα) (i : Fin n) :
            (ofFn values).unstack i = scalar (values i)

            Every entry of ofFn is the scalar tensor of the supplied value.

            @[simp]
            theorem TorchLean.Tensor.getScalar_rep_ofFn {α : Type} [Storage α] {n : } (values : Spec.Shape.Coord [n]α) (i : Fin n) :

            Reading a scalar entry of an internally generated vector evaluates the generator.

            @[simp]

            The item of an internally generated scalar tensor is the generator's value.

            @[simp]
            theorem TorchLean.Tensor.unstack_rep_ofFn {α : Type} [Storage α] {n : } {shape : Spec.Shape} (values : (Spec.Shape.dim n shape).Coordα) (i : Fin n) :
            unstack (Internal.Rep.ofFn values) i = Internal.Rep.ofFn fun (coordinate : Internal.Coord shape) => values (i, coordinate)

            Slicing an internally generated tensor fixes the leading coordinate of the generator.

            theorem TorchLean.Tensor.ofFn_eq_dim_scalar {α : Type} [Storage α] {n : } (values : Fin nα) :
            ofFn values = dim fun (i : Fin n) => scalar (values i)

            ofFn is extensionally the stack of its scalar entries.

            @[simp]
            theorem TorchLean.Tensor.getScalar_ofFn {α : Type} [Storage α] {n : } (values : Fin nα) (i : Fin n) :
            (ofFn values).getScalar i = values i

            Reading a coordinate from ofFn returns the value supplied at that coordinate.

            @[simp]
            theorem TorchLean.Tensor.ofFn_getScalar {α : Type} [Storage α] {n : } (t : Tensor α [n]) :
            (ofFn fun (i : Fin n) => t.getScalar i) = t

            Rebuilding a vector from all of its coordinates returns the original vector.

            def TorchLean.Tensor.matrix {α : Type} [Storage α] {m n : } (values : Fin mFin nα) :
            Tensor α [m, n]

            Construct a matrix from its row and column coordinate function.

            PyTorch analogy: torch.tensor([...]).reshape(m, n) (again, function input rather than a list).

            Instances For
              @[simp]
              theorem TorchLean.Tensor.getScalar_full {α : Type} [Storage α] (n : ) (value : α) (i : Fin n) :

              Every coordinate of a filled vector contains the fill value.

              A singleton vector.

              PyTorch analogy: x.unsqueeze(0) for a scalar x.

              Instances For

                Pad a tensor with n leading dimensions of size 1.

                This is the tensor-level companion of Shape.padLeft. The row-major buffer is unchanged, so the padding is a zero-copy reinterpretation of the static shape. Broadcasting uses it to align ranks before expanding singleton axes.

                PyTorch analogy: repeated unsqueeze(0).

                Instances For
                  @[simp]
                  theorem Spec.padLeft_zero {α : Type} [TorchLean.Storage α] {s : Shape} (x : TorchLean.Tensor α s) :

                  Padding with zero axes is the identity.

                  theorem Spec.padLeft_succ {α : Type} [TorchLean.Storage α] {n : } {s : Shape} (x : TorchLean.Tensor α s) :

                  Padding one more axis stacks the padded tensor along a new singleton axis.

                  def TorchLean.Tensor.stackArray {α : Type} [Storage α] {n : } {s : Spec.Shape} (xs : Array (Tensor α s)) (_h : n = xs.size) :

                  Stack an array of equal-shaped tensors along a new leading dimension.

                  The explicit size proof prevents silent truncation or padding. Taking tensors as array elements makes this constructor independent of rank: use scalar tensors for a vector, vectors for a matrix, or arbitrary inner tensors for higher-rank values.

                  Instances For
                    theorem TorchLean.Tensor.forall_full {α : Type} [Storage α] {p : αProp} {s : Spec.Shape} {x : α} (hx : p x) :
                    Forall p (full s x)

                    A filled tensor satisfies every pointwise property satisfied by its value.

                    Build a matrix when every row has the same length; reject ragged input.

                    Instances For