TorchLean API

NN.API.Arithmetic

Runtime Arithmetic #

Runtime commands may choose native binary32, reference IEEE binary32, or complex arithmetic. Arithmetic records that semantic choice. FromFloat supplies conversion from Lean binary64 Float literals and scalar-specific rounding for parameter validation.

The generic runtime dispatcher supports all three modes. The public supervised trainer accepts the two real modes. Explicit complex training uses autograd.complex.grad for a real objective and nn.sgdStep on complex state; its predictions and Checkpoint.State retain both components.

Model definitions remain polymorphic over the existing Context α interface. The arithmetic choice does not replace that mathematical interface; it selects a concrete executable element representation that satisfies it. Proof-only models such as and rounded-real binary32 are selected directly in theorems rather than through a command-line flag.

Conversion from Lean Float constants into a selected runtime arithmetic representation.

  • ofFloat : Floatα

    Convert a Lean binary64 Float literal into this runtime element representation.

  • roundForValidation : FloatFloat

    Represent the rounding of ofFloat in binary64 for parameter-domain checks.

    The default validates only the supplied binary64 value, preserving custom scalar instances. Finite-precision instances override this to reject values that leave the required domain after conversion. This capability needs no arithmetic or order operations from Context.

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

    Convert a Lean Float literal into a TorchLean runtime arithmetic representation.

    Instances For
      @[instance_reducible]

      Float values inject into the same type by identity.

      @[instance_reducible]

      Round binary64 literals to Lean's native binary32 representation.

      @[instance_reducible]

      Inject binary64 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.

      @[instance_reducible]

      Inject binary64 literals into TorchLean's complex representation with zero imaginary part.

      Arithmetic semantics for runnable executables.

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

      Unlike a PyTorch per-tensor dtype, this choice fixes one arithmetic semantics for the complete run:

      • .native uses Lean's native Float32 operations on CPU and binary32 CUDA storage on GPU,
      • .ieee uses FloatLib's configured software IEEE-754 binary32 arithmetic,
      • .complex uses TorchLean's complex representation with binary32 real and imaginary components.
      Instances For

        Stable spelling used by command-line flags.

        Instances For

          Log a short description of the selected arithmetic semantics.

          Instances For

            Parse the value of --arithmetic.

            Instances For

              Parse and remove --arithmetic, using default when the flag is absent.

              Instances For
                def TorchLean.Runtime.Arithmetic.withRuntime (arithmetic : Arithmetic) (continuation : {α : Type} → [Storage α] → [Context α] → [ToString α] → [FromFloat α] → IO Unit) :

                Run continuation under the type selected by arithmetic.

                Instances For