TorchLean API

NN.Widgets.Core.Tensor

Tensor #

Tensor inspection widgets for the Lean infoview.

This module defines a #tensor_view t command that displays a small tensor as a rich HTML panel in the infoview. It is designed for:

It is not intended to be used inside proofs, and it is kept out of TorchLean’s default build surface (you must explicitly import NN.Widgets or a concrete widget module such as NN.Widgets.Core.Tensor).

Implementation note: We build on ProofWidgets’ #html command (which ships with mathlib’s dependency set) rather than introducing any custom JavaScript or external build step.

Main definitions #

Element renderer for #tensor_view.

The tensor widget is used across the library, so we keep element rendering customizable:

  • the default instance uses ToString,
  • specialized instances can add tooltips (e.g. float32 bits), units, or compact formatting.
Instances
    @[instance_reducible]

    Default element renderer for #tensor_view, using ToString.

    Read only the requested prefix, without converting the full backing buffer.

    Instances For

      Render a 1D tensor as a clipped single-row table.

      Instances For

        Render a 2D tensor as a clipped grid table.

        Instances For

          First maxElems entries of a tensor in row-major order, with a marker when there are more.

          Truncating matters here: a widget that tried to print a full tensor would hang the editor on anything of realistic size.

          Instances For
            def NN.Widgets.tensorHtml {α : Type} [TorchLean.Storage α] [ToString α] [TensorElemView α] {s : Spec.Shape} (t : TorchLean.Tensor α s) (maxRows maxCols : := 16) (maxElems : := 64) :

            Render a tensor as HTML.

            For small vectors/matrices, we render an actual table; otherwise we show a compact pretty string plus a flat preview.

            Instances For

              Runtime Wrappers #

              def NN.Widgets.packedTensorHtml {α : Type} [TorchLean.Storage α] [ToString α] [TensorElemView α] (v : Spec.SomeTensor α) (maxRows maxCols : := 16) (maxElems : := 64) :

              Render a Spec.SomeTensor with the same UI as tensorHtml.

              Instances For

                Stats #

                For small tensors, it is often helpful to inspect numeric ranges without expanding every element. This widget computes simple scalar summaries (min/max/mean/norms).

                Main command:

                Numeric summary panel: shape, size, extrema, mean, and the three usual norms.

                Instances For

                  Render simple scalar summary statistics (min/max/mean/norms) for a tensor as HTML.

                  Instances For

                    Commands #