TorchLean API

NN.Spec.Core.Utils

Miscellaneous spec utilities #

This file collects small helper definitions used across the spec layer.

The contents are intended to be broadly reusable: small constructors, shape-preserving maps, and conversion and pretty-printing helpers used by examples and by higher-level APIs.

Main definitions include:

User-facing constructors in the tensor API (for example tensorOfList!, tensorF!) live in NN/Tensor/API.lean and are built on top of these utilities.

def Spec.mapTensor {α β : Type} {s : Shape} :
(αβ)Tensor α sTensor β s

Map a scalar function across a tensor, changing the element type.

PyTorch analogy: torch.Tensor.to(dtype=...) is implemented as a scalar cast map under the hood. We keep this explicit at the spec layer because it is a common building block.

Instances For
    def Spec.Tensor.Forall {α : Type} (p : αProp) {s : Shape} :
    Tensor α sProp

    Forall p x means that every scalar entry of x satisfies p.

    The definition is rank-generic: domain conditions such as positivity, finite-value assumptions, quantizer code ranges, and interval membership use the same predicate for scalars, vectors, and higher-rank tensors.

    Instances For
      @[simp]
      theorem Spec.Tensor.forall_scalar {α : Type} {p : αProp} {x : α} :
      Forall p (scalar x) p x
      @[simp]
      theorem Spec.Tensor.forall_dim {α : Type} {p : αProp} {n : } {s : Shape} {values : Fin nTensor α s} :
      Forall p (dim values) ∀ (i : Fin n), Forall p (values i)
      theorem Spec.Tensor.forall_true {α : Type} {s : Shape} (x : Tensor α s) :
      Forall (fun (x : α) => True) x

      The predicate that is always true holds at every tensor coordinate.

      theorem Spec.Tensor.forall_mapTensor {α β : Type} {p : αProp} {q : βProp} {f : αβ} {s : Shape} {x : Tensor α s} (hx : Forall p x) (hf : ∀ (a : α), p aq (f a)) :

      Transport a pointwise property through a shape-preserving scalar map.

      theorem Spec.Tensor.forall_replicate {α : Type} {p : αProp} {s : Shape} {x : α} (hx : p x) :

      Replicating a scalar satisfying p produces a tensor satisfying p everywhere.

      theorem Spec.Tensor.forall_fill {α : Type} {p : αProp} {s : Shape} {x : α} (hx : p x) :
      Forall p (fill x s)

      A constant-filled tensor satisfies any predicate satisfied by the constant.

      Small constructors #

      def Spec.zeros (α : Type) [Zero α] (s : Shape) :
      Tensor α s

      All‑zeros tensor of a given shape.

      Instances For
        def Spec.ones (α : Type) [One α] (s : Shape) :
        Tensor α s

        All‑ones tensor of a given shape.

        Instances For
          def Spec.fullLike {α : Type} {s : Shape} :
          αTensor α sTensor α s

          Fill a tensor with a constant, using the shape of an existing tensor.

          PyTorch analogy: torch.full_like(t, value). We implement it by structural recursion so that the argument tensor is genuinely used (and so this stays friendly to linters).

          Instances For
            def Spec.zerosLike {α : Type} [Zero α] {s : Shape} :
            Tensor α sTensor α s

            All‑zeros tensor with the same shape as a given tensor.

            PyTorch analogy: torch.zeros_like(t).

            Instances For
              def Spec.onesLike {α : Type} [One α] {s : Shape} :
              Tensor α sTensor α s

              All‑ones tensor with the same shape as a given tensor.

              PyTorch analogy: torch.ones_like(t).

              Instances For
                def Spec.zip {α : Type} {s : Shape} :
                Tensor α sTensor α sTensor (α × α) s

                Zip two tensors pointwise into a tensor of pairs.

                Instances For

                  Tensor ↔ list utilities #

                  def Spec.toList {α : Type} {s : Shape} :
                  Tensor α sList α

                  Convert a tensor into a flat list (row‑major by outermost dimension).

                  Instances For
                    def Spec.pretty {α : Type} [ToString α] {s : Shape} :
                    Tensor α sString

                    Pretty‑print a tensor using ToString on scalars.

                    Instances For
                      def Spec.useFin {n : } {α : Type} (values : Fin nα) (h : 0 < n) :
                      α

                      Evaluate a Fin n → α at index 0, given a proof that n > 0.

                      Instances For

                        Expand a vector into a column vector by inserting a trailing dimension of size 1.

                        Instances For

                          Build a vector tensor from a list.

                          PyTorch analogy: torch.tensor(xs) producing a 1D tensor.

                          Instances For

                            Build a matrix tensor from a list of rows (strict validation).

                            This is the "safe by default" constructor used by the user-facing API layer. It refuses empty input and refuses ragged rows, because that usually indicates a bug at the call site (e.g. an accidental missing column in imported weights).

                            PyTorch analogy: torch.tensor(xss) will also error if xss is ragged.

                            Instances For
                              def Spec.maxRowLength {α : Type} (xss : List (List α)) :

                              Compute the maximum row length of a list of rows.

                              This is used to build a padded rectangular tensor from ragged input.

                              Instances For
                                def Spec.matrixFromRowsPadTo {α : Type} [Inhabited α] (nCols : ) (xss : List (List α)) :

                                Build a matrix tensor from a list of rows by padding/truncating to nCols.

                                This is the "permissive" constructor: it never fails, but it will silently pad missing entries with default (and ignore any extra entries beyond nCols).

                                This is useful when importing data that is naturally ragged, or when you intentionally want "pad-right with zeros" semantics (common in NLP style batching).

                                PyTorch analogy: this is closer to a manual pad_sequence + torch.tensor, except we do it directly as a tensor constructor at the spec layer.

                                Instances For

                                  Build a matrix tensor from a list of rows by padding to the maximum row length.

                                  If xss = [], this returns a 0 x 0 tensor. Otherwise, the number of columns is max_row_length xss and shorter rows are padded with default.

                                  Instances For