TorchLean API

NN.API.Core

API Core #

Public convenience API core on top of TorchLean's spec + runtime entrypoints.

This module holds the small foundational surface used by the higher-level API modules:

Most end-user code should not import this file directly; it is re-exported through NN.API.Public and via the top-level NN facade.

PyTorch Mapping #

DType here plays a role similar to torch.dtype selection in Python, but it also encodes a key TorchLean distinction:

@[reducible, inline]

Public name for TorchLean's generic scalar semantics contract.

Read this as:

  • "the model/loss is allowed to do its math over α"
  • not "this backend is executable"

That second role belongs to API.Runtime.Scalar.

Instances For
    def NN.API.Semantics.relu {α : Type} [Zero α] [Max α] (x : α) :
    α

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

    Instances For

      Runtime conversion from host Float constants into a TorchLean scalar type.

      PyTorch analogy:

      • in Python tutorials, users write float literals directly and tensors inherit a runtime dtype;
      • in TorchLean, examples often start from host Float literals and inject them into the chosen runtime scalar backend with Runtime.ofFloat.
      • ofFloat : Floatα

        Convert a host Float literal into this runtime scalar backend.

      Instances
        def NN.API.Runtime.ofFloat {α : Type} [Scalar α] (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.

          Strip at most one occurrence of a --key flag from an argument list.

          Accepted forms:

          • --key value
          • --key=value
          Instances For

            Return true when args contains --key value or --key=value.

            Instances For

              Remove a no-value boolean flag once, returning whether it appeared.

              Instances For

                Drop the leading -- separator commonly used with lean --run.

                Instances For

                  Fail if there are any unconsumed CLI arguments.

                  Instances For

                    Like takeFlagValueOnce, but parse the value as a Nat.

                    Instances For

                      Parse a JSON-style floating-point literal. Accepts the same decimal forms as Lean.Json.

                      Instances For

                        Like takeFlagValueOnce, but parse the value as a Float.

                        Instances For

                          Common training flags that appear across many demos: --epochs and --batch.

                          • epochs :

                            Number of epochs to train for.

                          • batch :

                            Batch size.

                          Instances For
                            def NN.API.CLI.takeEpochBatch (args : List String) (defaultEpochs defaultBatch : ) :

                            Parse optional --epochs and --batch flags.

                            Returns the parsed values (falling back to the provided defaults) and the remaining args.

                            Instances For

                              Parse a --steps flag, but also accept --epochs as a synonym.

                              This is a small ergonomics helper for tutorials that use a step-based loop, while still letting users pass --epochs out of habit from typical PyTorch training code.

                              If both --steps and --epochs are provided, this errors.

                              Instances For
                                def NN.API.CLI.takeSeed (args : List String) (default : := 0) :

                                Parse an optional --seed flag (defaults to the provided value).

                                Instances For

                                  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
                                    @[implicit_reducible]
                                    Instances For
                                      inductive NN.API.DType :

                                      Scalar type choice for demos and small 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]
                                        def NN.API.instDecidableEqDType.decEq (x✝ x✝¹ : DType) :
                                        Decidable (x✝ = x✝¹)
                                        Instances For
                                          @[implicit_reducible]

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

                                          Instances For

                                            Log a human-readable 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

                                                      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 NN.API.DType.withExec (dt : DType) (k : {α : Type} → [Semantics.Scalar α] → [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 small demos that want to construct tensors from Float lists.

                                                        Instances For