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 runtime-selected trainer.open and trainer.train paths accept the two real modes; trainer.openTyped chooses its scalar directly on CPU. 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 : Float → Float

    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
      def TorchLean.Runtime.ofFloatDirected {α : Type} [FromFloat α] (up : Bool) (x : Float) :
      α

      Convert a binary64 value toward -∞ (up = false) or +∞ (up = true).

      This requires roundForValidation to report the converted scalar's value faithfully in binary64, as it does for the native and configured binary32 instances. Move the source away from x until its conversion lands on the requested side, falling back to that side's infinity after 64 attempts. Custom instances whose validation hook is only the default identity do not supply this guarantee.

      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, instance 100]

        Round the exact binary64 source value once into any configured binary format.

        The binary32 specialization above retains its native conversion path. Validation converts the rounded destination value back to binary64, exposing overflow, underflow, and domain changes. Explicit scalar-valued tensors and state do not use this conversion.

        @[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