TorchLean API

NN.API.Public.Facade.Base.Tensor

TorchLean Tensor Facade #

Ops, random, shape, tensor, and semantic names exposed by the NN umbrella.

Executable program operations for verification and compiler-facing examples.

Most model code should use nn.* and Trainer.*. Use Ops.* when writing an explicit TorchLean executable program directly, for example before compiling a hand-built fragment to NN.IR.Graph.

@[reducible, inline]
Instances For
    @[reducible, inline]

    Total number of scalar elements (a.k.a. “numel”).

    Instances For
      @[reducible, inline]
      Instances For
        @[reducible, inline]

        Vector shape n.

        Instances For
          @[reducible, inline]
          abbrev TorchLean.Shape.mat (rows cols : ) :

          Matrix shape rows × cols.

          Instances For
            @[reducible, inline]

            Image shape C × H × W (channel-first).

            Instances For
              @[reducible, inline]

              Batched image shape N × C × H × W (PyTorch default for images).

              Instances For
                @[reducible, inline]
                abbrev TorchLean.Shape.nchw (n c h w : ) :

                Batched image shape N × C × H × W (PyTorch default for images).

                Instances For
                  @[reducible, inline]
                  abbrev TorchLean.Shape.imageSize (channels height width : ) :

                  Number of scalar features in a CHW image shape.

                  Instances For
                    @[reducible, inline]

                    Convert a runtime list of dimensions like [2, 3, 4] into a nested Shape.

                    Why this exists:

                    • In the spec layer we usually carry the shape in the type (great for safety).
                    • In “API land” we often start from lists (CLI args, JSON, compact examples, …).

                    shapeOfDims is the bridge between those representations.

                    Instances For
                      @[reducible, inline]

                      Convert to a list of dimensions (outermost first).

                      Instances For
                        @[reducible, inline]

                        Shape-indexed tensor datatype for the spec layer.

                        This is a functional representation:

                        • a scalar tensor is just an α,
                        • an n-dimensional tensor is a function Fin n → Tensor α s.

                        The spec layer does not commit to a concrete memory layout.

                        Instances For
                          @[reducible, inline]

                          Create a 1-D tensor from a Lean List.

                          Notes:

                          • The shape is computed from xs.length, so the type remembers the length.
                          • This is total and does not perform any runtime checks.

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

                          Instances For
                            @[reducible, inline]

                            Build a vector tensor from a list.

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

                            Instances For
                              @[reducible, inline]

                              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
                                @[reducible, inline]
                                abbrev TorchLean.Tensor.ofList {α : Type} (dims : List ) (xs : List α) :

                                N-D tensor from a runtime dims list and a flat xs, with a runtime length check.

                                This is the “dynamic / user-friendly” version: it fails with a descriptive message if the number of provided scalars doesn’t match the implied numel.

                                Instances For
                                  @[reducible, inline]

                                  1-D tensor from Float literals, cast into the executable IEEE-754 FP32 backend.

                                  Instances For
                                    @[reducible, inline]
                                    abbrev TorchLean.Tensor.fill {α : Type} (value : α) (s : Shape) :
                                    T α s

                                    Fill a tensor of shape s with a constant value.

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

                                    Instances For
                                      @[reducible, inline]
                                      abbrev TorchLean.Tensor.map {α β : Type} {s : Shape} (f : αβ) (x : T α s) :
                                      T β 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
                                        @[reducible, inline]
                                        abbrev TorchLean.Tensor.vecGet {α : Type} {n : } (x : T α (Spec.Shape.dim n Spec.Shape.scalar)) (i : Fin n) :
                                        α

                                        Extract the i-th entry from a vector tensor.

                                        Instances For
                                          @[reducible, inline]

                                          Print a tensor with a dtype tag, or throw an IO.userError if the dtype refuses to print.

                                          Instances For
                                            @[reducible, inline]
                                            abbrev TorchLean.Tensor.pretty {α : Type} [ToString α] {s : Shape} (t : T α s) :

                                            Pretty‑print a tensor using ToString on scalars.

                                            Instances For
                                              @[reducible, inline]

                                              Extract the scalar value from a scalar tensor.

                                              Instances For
                                                @[reducible, inline]
                                                abbrev TorchLean.Tensor.castFloat {α : Type} (cast : Floatα) {s : Shape} (t : T Float s) :
                                                T α s

                                                Cast a tensor elementwise while preserving its type-level shape.

                                                Instances For
                                                  def TorchLean.Tensor.repeatBatch {α : Type} {s : Shape} (batch : ) (x : T α s) :
                                                  T α (Spec.Shape.dim batch s)

                                                  Repeat one tensor across a fixed batch axis.

                                                  Use this for classifier demos whose checked model consumes a whole batch, while the example wants to inspect one ordinary input.

                                                  Instances For

                                                    Convert a runtime tensor back to a Float tensor inside IO.

                                                    This is the public bridge used by trainer prediction handles: examples can train under executable IEEE32 or other runtime scalar backends, but still receive ordinary Float tensors for inspection, printing, and follow-up scripting.

                                                    Instances For
                                                      @[reducible, inline]
                                                      abbrev TorchLean.Semantics.relu {α : Type} [Zero α] [Max α] (x : α) :
                                                      α

                                                      Host-side scalar ReLU for task definitions and synthetic targets.

                                                      Instances For

                                                        Public Namespaces #

                                                        These namespaces are the user-facing spelling for model construction, tensor utilities, training, runtime selection, and verification-adjacent examples. The definitions below forward to the checked implementation; the semantics are not copied or forked.