liveBook · Manning

Generate_text_simple

As explained in Build a Large Language Model (From Scratch)

A simple autoregressive text-generation routine that repeatedly appends the highest-probability next token to a growing context.

generate_text_simple is a simple autoregressive routine: given token IDs, it predicts one next token at a time, appends each prediction to the context, and repeats until max_new_tokens is reached. It uses greedy decoding, selecting the highest-scoring vocabulary entry at every step. section 4.7

Each iteration crops the input to the model's maximum context, runs the GPT model without gradient tracking, keeps the logits for the final sequence position, converts them to probabilities, selects the argmax token, and concatenates it along the sequence dimension. The returned tensor contains the original and generated token IDs. section 4.7

The text-generation pipeline connects several representations:

  • Encode input text into token IDs and add a batch dimension. section 5.1.1
  • Pass the token sequence through GPT to obtain logits for vocabulary entries at each position. section 5.1.1

Generating six tokens

Starting with "Hello, I am", encoding produces a tensor with shape [1, 4]. With max_new_tokens=6, the routine appends six predictions, producing a sequence of length 10. The generated IDs can then be decoded into text; an untrained model may produce incoherent output. section 5.1.1

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains the complete token loop and tensor flow, but defers probabilistic sampling techniques to later treatment.
Examples
Uses "Hello, I am" and "Every effort moves you" to demonstrate encoding, token generation, output length, and decoding.
What it assumes
Assumes a GPTModel, tokenizer, token IDs, and configured context length, while demonstrating text-to-token conversion utilities.
What you can do after
Apply it via PyTorch to generate a fixed number of tokens and decode the resulting sequence into text.
How it connects
Connects autoregressive generation with tokenization, logits, softmax, greedy decoding, context windows, and GPT models.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.