TorchLean API

NN.API.Scalar

Runtime Scalar Selection #

Runtime commands may choose Lean Float, executable IEEE binary32, complex binary32, or exact real-number semantics. DType records that choice. FromFloat supplies the one additional operation needed by commands that construct values from host Float literals.

Model definitions remain polymorphic over the existing Context α interface. The runtime dtype does not replace that mathematical interface; it selects a concrete scalar type that satisfies it.

Conversion from host Float constants into a selected runtime scalar type.

  • ofFloat : Floatα

    Convert a host Float literal into this runtime scalar backend.

Instances
    def TorchLean.Runtime.ofFloat {α : Type} [FromFloat α] (x : Float) :
    α

    Generic host-float injection for TorchLean scalar backends.

    Instances For
      @[implicit_reducible]

      Float is already the host literal type, so injection is identity.

      @[implicit_reducible]

      Inject host Float literals into the executable IEEE-754 binary32 backend.

      @[implicit_reducible]

      Inject host literals into the dual-number backend used by the runtime autograd engine.

      We interpret a literal as a primal value with zero tangent/adjoint component.

      @[implicit_reducible]

      Inject host literals into TorchLean's parametric complex scalar (imaginary part defaults to 0).

      @[implicit_reducible, defaultInstance 100]

      Allow numeric literals like 0.1 to elaborate to any TorchLean runtime scalar backend.

      Configuration for the float32 dtype option.

      We support both:

      • proof-only float32 semantics (mode = .fp32, noncomputable), and
      • executable IEEE-754 float32 semantics (mode = .ieee754Exec).
      Instances For

        Scalar type choice for runnable executables.

        This is a runtime selection mechanism used by example programs; the core library itself is parametric in the scalar type α.

        PyTorch Mapping #

        This corresponds loosely to choosing dtype= in PyTorch, but with additional "proof-only" variants:

        • .float uses Lean's builtin Float (executable, but its IEEE-754 behavior is trusted),
        • .float32 uses TorchLean's float32 model (either proof-only or executable),
        • .complex uses TorchLean's parametric complex scalar over a float32 backend,
        • .real uses (proof-only; not executable).
        Instances For
          @[implicit_reducible]
          Instances For

            Whether this dtype can be used in an executable (IO/#eval) context.

            Instances For

              Log a short description of the chosen dtype to stdout.

              Instances For

                Parse a float32 mode selector string into a Float32Mode.

                Instances For

                  Parse a dtype selector string into a DType.

                  Instances For

                    Parse and remove dtype flags from CLI arguments, using default when no dtype flags are provided.

                    This is the same parsing logic as parseAndStrip, but it lets higher-level runners choose a different default dtype depending on context (e.g. CUDA eager requires Float).

                    Instances For

                      Parse and remove dtype flags from CLI arguments.

                      Supported flags:

                      • --dtype <value> or --dtype=<value>
                      • --float32-mode <mode> or --float32-mode=<mode>

                      The two flags are mutually exclusive.

                      Instances For
                        def TorchLean.Runtime.DType.withRuntime (dt : DType) (k : {α : Type} → [Context α] → [DecidableEq Spec.Shape] → [ToString α] → [FromFloat α] → IO Unit) :

                        Run k under the scalar type selected by dt.

                        If dt is proof-only, this returns an error rather than trying to execute noncomputable code.

                        Instances For
                          def TorchLean.Runtime.DType.withExec (dt : DType) (k : {α : Type} → [Context α] → [DecidableEq Spec.Shape] → [ToString α] → (Floatα)IO Unit) :

                          Run k under the scalar type selected by dt, passing an explicit cast function Float → α.

                          This is a convenient shape for executables that construct tensors from Float lists.

                          Instances For