TorchLean API

NN.API.Core.Basic

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 the top-level NN import.

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 examples, 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.

          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 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]
                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 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

                              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 executables that construct tensors from Float lists.

                                Instances For