TorchLean API

NN.Examples.Models.Sequence.Gpt2

GPT-2-Style Causal Language Model Example #

Runnable torchlean gpt2 example. It builds a GPT-2-style causal transformer over byte-level tokens, with optional real text input from tiny-shakespeare or --data-file PATH.

For the simplest "Karpathy-style single text file" path, use torchlean chargpt (character-level tokenizer). This gpt2 command is byte-level and shows the Transformer block wiring and save/reload loop.

python3 scripts/datasets/download_example_data.py --tiny-shakespeare
lake -R -K cuda=true exe torchlean gpt2 --device cuda --tiny-shakespeare --steps 1 --windows 1 \
  --generate 0

CLI subcommand name used in terminal banners and error messages.

Instances For

    Default JSON loss-curve path for this command.

    Instances For

      Complete command help, including the text and training flags parsed after runtime selection.

      Instances For

        Batch size for the byte-level causal Transformer.

        Instances For

          Prompt/target window length for the runnable GPT example.

          Instances For

            Byte vocabulary width used by the one-hot tokenizer.

            Instances For

              Embed a byte id in the complete 256-entry byte vocabulary.

              Instances For

                Number of attention heads in the miniature Transformer block.

                Instances For

                  Per-head embedding width.

                  Instances For

                    Transformer embedding width.

                    Instances For

                      Hidden width of the feed-forward sublayer.

                      Instances For

                        Number of Transformer encoder blocks.

                        Instances For
                          @[reducible, inline]

                          Input shape: batched byte-level one-hot token windows.

                          Instances For
                            @[reducible, inline]

                            Output shape: one vocabulary-logit row for every input token position.

                            Instances For

                              Public GPT-style causal Transformer constructor specialized to the byte-level config.

                              Instances For

                                Command-local controls for GPT training, checkpointing, generation, and the prompt loop.

                                Instances For

                                  Parse the byte-level GPT command's training and generation flags.

                                  Instances For

                                    Build a batch sample from exactly one token window per batch row.

                                    Instances For

                                      Parse GPT-2-specific data flags and return the training corpus plus remaining runtime flags.

                                      Instances For

                                        Byte-token window used for reporting prompt/target text.

                                        Instances For

                                          Print a compact before/after language-model report for the first batch row.

                                          Instances For

                                            Convert byte ids into the typed batched one-hot input tensor used for generation.

                                            Instances For
                                              @[reducible, inline]

                                              Fitted byte-level GPT predictor.

                                              Training, saved-checkpoint inference, and future optimized runners all provide this one closure. Generation only needs a logit-producing function; it does not depend on where the logits came from.

                                              Instances For
                                                def NN.Examples.Models.Sequence.Gpt2.generateSampledFromIds {promptLength : } (predict : Predictor) (promptTokens : TorchLean.Tensor [promptLength]) (steps : ) (temperature : Float) (topK seed repeatWindow : ) (repeatPenalty : Float) (asciiOnly : Bool) :
                                                IO (TorchLean.Tensor [promptLength + steps])

                                                Autoregressively extend byte token ids using a trained byte-level GPT model.

                                                Instances For
                                                  def NN.Examples.Models.Sequence.Gpt2.generateSampled (predict : Predictor) (prompt : String) (steps : ) (temperature : Float) (topK seed repeatWindow : ) (repeatPenalty : Float) (asciiOnly : Bool) :

                                                  Encode a string prompt and autoregressively extend it.

                                                  Instances For

                                                    Build a finite training set from approximately evenly spaced corpus windows.

                                                    Instances For

                                                      Interactive prompt loop for the in-memory Float model.

                                                      Each line is appended to the current byte context, decoded through the trained local model, and then kept as context for the next prompt unless the user clears it.

                                                      Instances For

                                                        Train the byte-level model and decode a prediction report.

                                                        Inputs and reports use host Float, while RunConfig.arithmetic selects the arithmetic used by the trainer. The command fixes that runtime choice to native execution.

                                                        Instances For

                                                          CLI entrypoint for byte-level GPT training, sampling, logging, and checkpointing.

                                                          Instances For