TorchLean API

NN.API.Text.Options

Text Workflow Configuration #

Display helpers, generation and corpus option records, training-log metadata, and CLI parsers for text workflows.

Causal LM Display Helpers #

def TorchLean.text.tokenWindow (tokenizer : Tokenizer) (length : ) (text : String) (offset paddingTokenId : := 0) :
Tensor [length]

Return a fixed-length token window from a text string.

offset = 0 is the model prompt window; offset = 1 is the usual next-token target window for causal language modeling. Missing tokens are padded with paddingTokenId, matching Data.CausalLM.oneHotSample.

Example:

-- `offset = 0` is the prompt window and `offset = 1` is its next-token target. That pair is the
-- whole of causal language-model supervision.
def prompt : Tensor Nat [8] :=
  text.tokenWindow text.Tokenizer.byte 8 "hello world"

def target : Tensor Nat [8] :=
  text.tokenWindow text.Tokenizer.byte 8 "hello world" (offset := 1)
Instances For
    def TorchLean.text.decodeWindow (tokenizer : Tokenizer) (length : ) (text : String) (offset paddingTokenId : := 0) :

    Decode a fixed token window extracted by tokenWindow.

    Instances For

      Escape a short text fragment for one-line terminal output.

      Display-only: this does not change tokenizer semantics. Quotes and backslashes use their usual escapes, common whitespace controls use \\n, \\r, and \\t, and every other ASCII control character is written as \\xNN. Thus byte-token predictions cannot turn a log into a binary file.

      The argument is called fragment rather than text so it cannot shadow the text namespace inside the body; a local named text makes every text.foo spelling in scope resolve to a field access instead, which is a genuinely confusing error to read.

      Instances For

        Sampling Helpers (Top-k) #

        Shared text-generation flags for GPT-style examples.

        Example:

        -- `topK := 1` is greedy decoding; anything larger samples, and `seed` is what makes that sampling
        -- reproducible from one run to the next.
        def greedy : text.GenerationOptions :=
          { prompt := "Once upon a time"
            newTokenCount := 64
            temperature := 1.0
            topK := 1
            repeatPenalty := 0.0
            repeatWindow := 0
            seed := 0
            asciiOnly := true }
        
        • prompt : String

          Prompt used to seed autoregressive generation.

        • newTokenCount :

          Number of new tokens to append.

        • temperature : Float

          Softmax temperature. Must be finite and positive for sampling; ignored by greedy decoding.

        • topK :

          Top-k cutoff. 0 samples the full vocabulary; 1 gives greedy decoding.

        • repeatPenalty : Float

          Finite nonnegative penalty subtracted for repeated recent tokens. 0 disables it.

        • repeatWindow :

          Number of recent tokens considered by the repeat penalty. 0 disables the window.

        • seed :

          Deterministic RNG seed for sampling.

        • asciiOnly : Bool

          Restrict generated ids to a model-specific ASCII allow-list.

        Instances For

          Parse --ascii-only, accepting either a bare flag or a true/false value.

          Internal on purpose: GenerationOptions.parse is the entry point, and parsing this flag on its own would let a command accept it without recording it in the training log.

          Instances For

            Parse the generation flags shared by GPT-style examples.

            The model command supplies its concrete default prompt and sampling policy. This parser owns only the stable generation flags and returns arguments belonging to the caller.

            Instances For

              Text Workflow Option Records #

              Required text-corpus path plus the explicit small-data option used by local corpus trainers.

              • dataFile : System.FilePath

                UTF-8 or raw-byte corpus path selected by --data-file.

              • allowSmallData : Bool

                Allow local runs below the normal corpus-size floor.

              Instances For

                Parse the required --data-file corpus flag and optional --allow-small-data switch.

                Instances For

                  Optional text-corpus path selected by --data-file, with caller-supplied default.

                  Instances For

                    Parse an optional --data-file flag using the supplied default path.

                    Instances For

                      Optional second corpus pass after the main training run.

                      • finetuneFile? : Option System.FilePath

                        Optional corpus used for a second fine-tuning pass.

                      • finetuneSteps :

                        Number of optimizer steps used on that second corpus when present.

                      Instances For

                        Parse the optional --finetune-file / --finetune-steps pair.

                        The caller supplies the default step count so commands can reuse their main training-step default.

                        Instances For

                          Optional GPT-2 BPE tokenizer bundle plus an optional bounded-text cap.

                          Instances For

                            Parse the optional GPT-2 BPE tokenizer bundle.

                            --bpe-vocab and --bpe-merges must appear together; --max-chars is independent.

                            Instances For

                              Shared terminal-REPL toggle used by interactive text examples.

                              • interactive : Bool

                                Keep the trained model alive and read prompts from stdin.

                              Instances For

                                Parse the shared --interactive flag used by text examples with a terminal prompt loop.

                                Instances For

                                  Shared prompt plus continuation-length options for simple text-generation commands.

                                  • prompt : String

                                    Prompt used for before/after reports and generation.

                                  • newTokenCount :

                                    Number of generated tokens or characters after training.

                                  Instances For

                                    Parse the shared --prompt / --generate flags.

                                    Instances For

                                      Text TrainLog Notes #

                                      TrainLog note fields for generation-capable text commands.

                                      The stable generation surface is prompt, continuation length, temperature/top-k, repetition control, RNG seed, and ASCII-only filtering. Model commands can prepend dataset or architecture notes through extra.

                                      Internal on purpose: these note arrays only make sense inside the Log.write* wrappers below, which pair them with the matching loss comparison.

                                      Instances For

                                        TrainLog note fields for prompt commands that do not expose the full sampling surface.

                                        Instances For

                                          Training logs #

                                          text.Log is the whole logging surface for text commands. It lives in its own namespace so that text. completion shows tokenizers, sampling, and option parsers rather than a pair of long write*TrainLog names.

                                          def TorchLean.text.Log.writeGeneration (destination : Training.LogDestination) (title : String) (trainingSteps : ) (lossBefore lossAfter : Float) (options : GenerationOptions) (generated? : Option String := none) (extraNotes : Array String := #[]) :

                                          Write a before/after loss log for a generation-capable text training command.

                                          Instances For
                                            def TorchLean.text.Log.writePrompt (destination : Training.LogDestination) (title : String) (trainingSteps : ) (lossBefore lossAfter : Float) (options : PromptGenerationOptions) (generated? : Option String := none) (extraNotes : Array String := #[]) :

                                            Write a before/after loss log for a prompt-based text training command.

                                            Instances For

                                              Text Training Option Combinators #

                                              Number of corpus windows used by a finite or cyclic text-training command.

                                              • windowCount :

                                                Number of windows available to the training sampler.

                                              Instances For
                                                def TorchLean.text.WindowOptions.parse (exeName : String) (arguments : List String) (defaultWindows : ) :

                                                Parse a positive --windows value.

                                                Instances For

                                                  Optional model-checkpoint paths for text training and generation.

                                                  Instances For

                                                    Parse --load-checkpoint and --save-checkpoint.

                                                    Instances For