TorchLean API

NN.API.Public.Facade.Base.ModelZoo

TorchLean Model-Zoo Facade #

Shared flags, logs, banners, paths, and runtime operations for built-in model-zoo examples.

Shared CLI and logging definitions for the built-in model-zoo examples.

These are public because examples should read like TorchLean programs. They stay under ModelZoo; ordinary library users should prefer Trainer, Data, and optim directly.

@[reducible, inline]

Training flags shared by runnable model examples.

This covers the knobs almost every example needs: --steps, --log, CUDA memory watching, and --lr. Model files should reuse this record and add only flags that change that model's actual behavior, such as text generation settings or evaluation probes.

Instances For
    @[reducible, inline]

    Model-training flags plus an RNG/data-order seed.

    Use this when the command needs reproducible initialization, synthetic data, or shuffled row order in addition to the standard training flags.

    Instances For
      @[reducible, inline]

      Common CLI result for training commands that accept --steps, --batch-size, and --log.

      Instances For
        @[reducible, inline]

        Common arguments for a model command that reads one supervised CSV.

        Instances For
          @[reducible, inline]

          Shared corpus-window or training-window count used by finite cyclic examples.

          Instances For
            @[reducible, inline]

            Parse the shared --windows flag.

            The model decides how to use the windows; this helper just keeps the flag spelling and positivity check consistent.

            Instances For
              @[reducible, inline]

              Optional parameter-checkpoint load/save paths shared by runnable model commands.

              Instances For
                @[reducible, inline]

                Parse the shared --load-params / --save-params flags.

                Instances For
                  @[reducible, inline]

                  Diffusion schedule knobs shared by model-zoo diffusion commands.

                  Instances For
                    @[reducible, inline]

                    Parse shared diffusion schedule flags: --T, --beta-start, and --beta-end.

                    Instances For
                      @[reducible, inline]

                      Standard TrainLog metadata for a diffusion schedule.

                      Instances For
                        @[reducible, inline]

                        Optional image artifacts emitted by image-generation or reconstruction commands.

                        Instances For
                          @[reducible, inline]

                          Parse shared image artifact flags for generation/reconstruction commands.

                          This only parses artifact paths and the optional reconstruction timestep. The model still decides which images it can write.

                          Instances For
                            @[reducible, inline]

                            Standard TrainLog metadata for requested image artifacts.

                            Instances For
                              @[reducible, inline]

                              Common paired-NPY dataset flags for scientific supervised examples.

                              This shape is for commands that train on one (x,y) tensor pair and evaluate/report on a held-out pair. The model file supplies the tensor shapes and file defaults.

                              Instances For
                                @[reducible, inline]
                                abbrev TorchLean.ModelZoo.PairedNpyEvalFlags.parse (args : List String) (defaultTrainX defaultTrainY defaultTestX defaultTestY : System.FilePath) (defaultTrainRows defaultTestRows : ) :

                                Parse common train/test paired-NPY flags.

                                Commands supply their default paths and row counts. This parser keeps the repeated --train-rows, --test-rows, --eval-rows, --x, --y, --test-x, and --test-y flags in one place.

                                Instances For
                                  @[reducible, inline]

                                  Standard TrainLog metadata for paired train/test NPY tensors.

                                  Instances For
                                    @[reducible, inline]

                                    Optional CSV artifact path for commands that emit one tabular diagnostic.

                                    Instances For
                                      @[reducible, inline]

                                      Parse the shared optional --plot-csv artifact path.

                                      Instances For
                                        @[reducible, inline]

                                        Generic NPY-backed labeled dataset flags.

                                        Examples provide default paths and data-preparation hints. The repeated flags are --seed, --n-total, --x, and --y.

                                        Instances For
                                          @[reducible, inline]

                                          Parse the standard --seed, --n-total, --x, and --y flags for NPY-backed datasets.

                                          Examples provide dataset-specific defaults; this parser keeps the repeated NPY dataset flags consistent.

                                          Instances For
                                            @[reducible, inline]

                                            Standard TrainLog metadata for an NPY-backed dataset branch.

                                            Instances For
                                              @[reducible, inline]

                                              Built-in image dataset branches shared by image-model commands.

                                              Instances For
                                                @[reducible, inline]

                                                Parse --dataset, --cifar10, and --imagenet64, rejecting ambiguous selectors.

                                                Instances For
                                                  @[reducible, inline]

                                                  Common arguments for a fixed-sample command backed by supervised .npy arrays.

                                                  Instances For

                                                    Parse a supervised NPY dataset, logged training flags, and require that no command-specific arguments remain.

                                                    Dataset-specific commands provide parseData, which owns defaults such as CIFAR or ImageNet paths; this helper keeps the reusable "NPY data + logged TrainLog" path in one place.

                                                    Instances For
                                                      @[reducible, inline]

                                                      Common arguments for a model-training command backed by supervised .npy arrays.

                                                      Instances For
                                                        def TorchLean.ModelZoo.NpyModelTrainFlags.parse (exeName : String) (args : List String) (defaultLogPath : System.FilePath) (defaultSteps : := 1) (defaultLr : Float := 1e-3) (parseData : List StringExcept String (NpyDataFlags × List String)) :

                                                        Parse a supervised NPY dataset and standard model-training flags.

                                                        The remaining arguments are returned so callers can still pass runtime/backend flags through a higher-level runner.

                                                        Instances For
                                                          @[reducible, inline]

                                                          Shared forecasting-window data flags: paths, window count, report offset, and seed.

                                                          Instances For
                                                            @[reducible, inline]

                                                            Standard TrainLog metadata for forecasting-window datasets.

                                                            Instances For
                                                              @[reducible, inline]

                                                              Common arguments for forecasting-window model-training commands.

                                                              Instances For

                                                                Parse forecasting-window data flags plus standard model-training flags.

                                                                The data parser comes from the caller because file defaults often depend on a dataset directory or a preparation script.

                                                                Instances For
                                                                  @[reducible, inline]

                                                                  Standard location for model-example training logs under data/model_zoo.

                                                                  Instances For
                                                                    @[reducible, inline]

                                                                    Validate that a natural-number CLI flag is positive.

                                                                    Instances For
                                                                      @[reducible, inline]
                                                                      abbrev TorchLean.ModelZoo.resolvePositiveNatFlag (exeName flag : String) (value? : Option ) (default : ) :

                                                                      Resolve an optional natural-number CLI flag against a default and require that the result is strictly positive.

                                                                      Example parsers use this helper for the common "optional flag + default + positivity check" case instead of restating the same getD / requirePositiveNatFlag sequence.

                                                                      Instances For
                                                                        def TorchLean.ModelZoo.orThrow {α : Type} (exeName : String) (result : Except String α) :
                                                                        IO α

                                                                        Convert an Except String α into an IO α, raising a tagged userError on failure.

                                                                        This is handy in main functions where we want the process to exit with a readable message.

                                                                        Instances For
                                                                          @[reducible, inline]

                                                                          Run an executable on the concrete Float runtime path.

                                                                          We use this for runnable training commands that produce Float-valued artifacts: CPU/CUDA eager execution, native kernels, and JSON loss curves. Commands that need to expose another scalar backend can use runAnyOrFloat.

                                                                          Instances For
                                                                            @[reducible, inline]

                                                                            Run an executable with the standard TorchLean runtime parser, using the polymorphic scalar path by default and switching to the Float path when requested.

                                                                            This is the common shape for public examples that support all executable scalar backends, but need the Float path for CUDA bridges, decoded reports, or JSON artifacts whose metrics are stored as Float.

                                                                            Instances For
                                                                              @[reducible, inline]

                                                                              Run an executable on either the selected scalar backend or the concrete Float path when the generic branch does not need an explicit Float → α cast helper.

                                                                              Instances For
                                                                                @[reducible, inline]

                                                                                Detect --backend compiled in either --backend=compiled or split-flag form.

                                                                                Instances For
                                                                                  @[reducible, inline]

                                                                                  Reject --cpu and add CUDA/runtime flags expected by GPU-first commands.

                                                                                  This helper only rewrites command-line intent before the standard runtime parser runs. It does not change the lower-level runtime semantics.

                                                                                  Instances For
                                                                                    @[reducible, inline]

                                                                                    Reject the proof-compiled backend for commands that require eager runtime execution.

                                                                                    Instances For
                                                                                      @[reducible, inline]

                                                                                      Reject compiled backend and force the CUDA-first flags expected by eager GPU examples.

                                                                                      Instances For
                                                                                        @[reducible, inline]

                                                                                        Run a Float-only command after normalizing its runtime flags.

                                                                                        GPU-first examples use this to keep the public Runtime.runFloat path while inserting required CUDA/eager flags before the standard parser runs.

                                                                                        Instances For
                                                                                          @[reducible, inline]

                                                                                          Run a Float-only command after forcing CUDA runtime flags.

                                                                                          Instances For
                                                                                            @[reducible, inline]

                                                                                            Run a Float-only command after forcing CUDA eager-runtime flags.

                                                                                            Instances For

                                                                                              Runtime device label used by public example banners and notes.

                                                                                              Instances For

                                                                                                Standard device=... note string used by public example logs.

                                                                                                Instances For

                                                                                                  Standard model-zoo banner shape: executable name, short description, and selected device.

                                                                                                  Instances For
                                                                                                    def TorchLean.ModelZoo.bannerWithDeviceDetails (exeName desc details : String) (opts : Options) :

                                                                                                    Standard two-line model-zoo banner: headline plus one indented detail/note line, both carrying the selected device on the headline.

                                                                                                    Instances For
                                                                                                      @[reducible, inline]

                                                                                                      Choose a CUDA memory-watch cadence for public examples.

                                                                                                      Users can pass --cuda-mem-watch N to choose an exact cadence. When no cadence is supplied, long CUDA runs sample about ten times over the requested training horizon. Short runs and CPU runs stay quiet by default, so the examples do not print allocator telemetry unless it is likely to be useful.

                                                                                                      Instances For
                                                                                                        @[reducible, inline]

                                                                                                        Standard TrainLog note for the effective CUDA memory-watch cadence.

                                                                                                        Instances For
                                                                                                          @[reducible, inline]

                                                                                                          Maybe print a one-line CUDA allocator report.

                                                                                                          The report samples the native allocator at a fixed cadence and warns if the observed free-memory slope would cross zero before the requested training horizon.

                                                                                                          Instances For
                                                                                                            @[reducible, inline]
                                                                                                            abbrev TorchLean.ModelZoo.shouldLogStep (logEvery done : ) :

                                                                                                            Return whether a completed training step should emit a progress report.

                                                                                                            The convention is shared across example trainers: logEvery = 0 disables progress output; otherwise we log at exact multiples of the completed-step count.

                                                                                                            Instances For
                                                                                                              def TorchLean.ModelZoo.check (exeName msg : String) (b : Bool) :

                                                                                                              Fail with a tagged userError if a boolean condition is false.

                                                                                                              Executable workflows use this for named precondition checks such as Common.check exeName "loss finite" (loss == loss).

                                                                                                              Instances For
                                                                                                                def TorchLean.ModelZoo.writeBeforeAfterLossLogPath (path : System.FilePath) (title : String) (steps : ) (loss0 loss1 : Float) (notes : Array String := #[]) :

                                                                                                                Write a standard JSON training artifact for routines that record an initial and final loss.

                                                                                                                The function uses Runtime.Training.TrainLog.beforeAfterLoss and the stable TrainLog JSON format. The output schema is independent of the model, dataset, and runtime backend.

                                                                                                                Instances For
                                                                                                                  def TorchLean.ModelZoo.writeBeforeAfterLossLog (dest : Training.LogDestination) (title : String) (steps : ) (loss0 loss1 : Float) (notes : Array String := #[]) :

                                                                                                                  Write a before/after loss log to an explicit logging destination.

                                                                                                                  LogDestination.disabled is a no-op, mirroring wandb disabled for runs where metrics should stay on stdout only.

                                                                                                                  Instances For
                                                                                                                    def TorchLean.ModelZoo.writeCurveLog (dest : Training.LogDestination) (title : String) (curve : Training.Curve) (seriesName : String := "loss") (notes : Array String := #[]) :

                                                                                                                    Write a one-series scalar curve to an explicit logging destination.

                                                                                                                    Instances For
                                                                                                                      def TorchLean.ModelZoo.writeCurveTrainLog (dest : Training.LogDestination) (title : String) (curve : Training.Curve) (seriesName : String) (color : String := "#4e79a7") (notes : Array String := #[]) :

                                                                                                                      Write a single-curve training log with an explicit series color.

                                                                                                                      Use this when a command already owns a Training.Curve and wants the richer TrainLog artifact shape rather than the default "loss" curve writer.

                                                                                                                      Instances For

                                                                                                                        Write a multi-series metric history as a TrainLog artifact.

                                                                                                                        Use this when a command accumulates a Training.MetricHistory with explicit series names/colors and then want to publish the richer TrainLog form without rebuilding the same toTrainLog + writeTrainLog boundary in every example file.

                                                                                                                        Instances For

                                                                                                                          Write a prepared TrainLog JSON artifact and report the file path.

                                                                                                                          Instances For

                                                                                                                            Write a prepared TrainLog to a destination that may be disabled.

                                                                                                                            Instances For
                                                                                                                              @[reducible, inline]

                                                                                                                              Print the standard first/last loss summary for a scalar training curve.

                                                                                                                              Instances For
                                                                                                                                def TorchLean.ModelZoo.trainFixedCurveFloat {σ τ : Shape} (mkModel : ModelBuilder (SequentialModel σ τ)) (mkModuleDef : (model : SequentialModel σ τ) → ScalarModuleDef (modelParamShapes model) [σ, τ]) (mkOptim : (paramShapes : List Shape) → Runtime.Autograd.TorchLean.Optimizer Float paramShapes) (opts : Options) (sample : SupervisedSample Float σ τ) (steps : ) (cudaMemWatch : := 0) :

                                                                                                                                Fixed-sample run specialized to Float, returning a full per-step curve.

                                                                                                                                Instances For