Generating text with generate_text_simple
generate_and_print_sample takes a starting text, encodes it into token IDs, and asks the LLM to generate a continuation of up to 50 new tokens.
Why Numbers Alone Do Not Show What a Model Writes
Training a language model produces a stream of numbers, and those numbers are useful. They tell you how training is progressing, and they make it easy to compare one stage of training with another. But a number does not show what kind of text the model is actually producing. Two very different sets of outputs can produce estimates that look similar on a chart, so a number on its own leaves a gap.
The helper function generate_and_print_sample closes that gap. It takes a short piece of starting text, asks the model to continue it, and prints the continuation so that a person can read it and judge it. In this article you will follow the function step by step, keeping track of what it does to the model, to the input, and to the output at each stage.
What do you think happens?
When generate_and_print_sample returns, which mode is the model in: evaluation mode or training mode?
Reveal answer
Training mode
The function switches the model to evaluation mode before it generates, and switches it back to training mode before it returns. The surrounding training process is therefore preserved. The rest of this article explains why that switch matters.
The Four Stages of the Function
The function has four stages that matter for understanding its behavior: preparing the input, generating new tokens, turning tokens back into text, and printing. Each stage hands its result to the next stage. This means an error in one stage does not stay in that stage. It shows up later as a strange result in the printed text, which is why it helps to know the order in which the stages run.
The function takes a starting text and encodes it into token IDs. Those IDs are what the model actually reads. The model then asks for a continuation of up to 50 new tokens. Once generation is finished, the resulting token IDs are decoded back into readable text, and that text is printed.
Read the diagram from left to right. Notice that the text changes form twice: it becomes token IDs before generation and becomes readable text again after generation. The model only ever works with the token form in the middle of the pipeline, so the printed sample is always the product of a round trip through encoding and decoding.
Switching the Model Between Modes
A model that is being trained is in training mode. The function does not leave the model in that state while it generates text. Before generating, it switches the model to evaluation mode, and before returning, it switches the model back to training mode. Because of this, the training process that surrounds the call continues as it was before the sample was printed.
From Text to Token IDs and Back
The first stage prepares the input. The starting text is encoded into token IDs, and these IDs are the form the model works with. The output of generation is also a set of token IDs. Only at the end of the process are those IDs turned back into text that a person can read.
This round trip matters for debugging. A sample that looks odd may have been caused by how the input was encoded, by what the model generated, or by how the generated IDs were decoded. Knowing that these conversions exist is the first step toward locating the stage where the result went wrong.
Generating Up to 50 New Tokens
The generation stage asks the model to continue the encoded input with up to 50 new tokens. The continuation is built one token at a time. After each new token is produced, it is added to the sequence, and the extended sequence is what the model sees when it produces the next token. The loop stops once the 50-token limit is reached.
The limit is a ceiling, not a target. Generation stops at up to 50 new tokens, so the printed continuation is bounded in length. When you read a sample, the continuation is the text that the model added after your starting text, and it is that continuation you should judge.
Reading a Sample Beside a Numeric Evaluation
The function is designed to sit beside evaluate_model rather than replace it. The evaluation helper produces a numeric estimate of training progress, which is convenient for comparing one training stage with another. The generated sample answers a different question. A number says how the model is scoring, and the sample says what the model is writing.
| Signal | What it tells you | What it cannot tell you |
|---|---|---|
| Numeric estimate from evaluate_model | How training is progressing and how stages compare | What kind of text the model is actually producing |
| Printed generated sample | A concrete, readable continuation you can judge | A single precise score for comparing many stages |
Two complementary signals during training
Used together, the two signals give a fuller picture. The number tells you the trend, and the sample tells you whether the text behind that trend reads the way you expected.
Tracing a Sample That Looks Wrong
Locating the stage behind a garbled continuation
You call generate_and_print_sample with a short starting text. The printed continuation contains readable words, but the words do not connect to the start of the passage at all. Which stage should you examine first?
- 1Check the input stage
Start where the data starts. The starting text must be encoded into token IDs before generation begins. If the encoding of the starting text were wrong, the model would be continuing a different passage than the one you intended.
- 2Check the generation stage
If the input encoded correctly, the next suspect is the continuation itself. The model adds up to 50 new tokens, one at a time, so an unusual continuation can come from what the model generated rather than from the input.
- 3Check the decoding stage
Generated token IDs must be turned back into text. If the decoding step does not match the encoding, the text can look wrong even when the generated IDs were sensible.
- 4Check the printing stage
Printing is the last stage. Because it only displays the finished text, it is the least likely place for the problem, but it is still part of the trace.
Follow the stages in the order the code runs them: input, generation, decoding, then printing. The first stage whose output does not match what you expect is where the result diverged.
Mistakes When Reading a Sample
Common mistake Treating the printed sample as a replacement for the numeric evaluation
Why it failsThe sample shows what the model writes, while the numeric estimate shows how training is progressing. Each answers a different question.
Do this insteadRead the number for the trend and the sample for the content, and use both together.
Common mistake Assuming the model stays in evaluation mode after the call
Why it failsThe function switches the model back to training mode before it returns, so the surrounding training process is preserved.
Do this insteadAfter the call, expect the model to be in training mode again.
Common mistake Judging the whole output as if the starting text were part of the generation
Why it failsThe model generates a continuation of up to 50 new tokens. The continuation is what was added after the input.
Do this insteadJudge the continuation that follows the starting text, and check the length limit when reading it.
Common mistake Looking only at the printed text when a sample is wrong
Why it failsThe printed text is the last stage. The fault may have started during encoding or generation.
Do this insteadTrace the data backward through printing, decoding, generation, and encoding until you find the stage where the result diverged.
Practice: Finding the Likely Stage
A sample printed by generate_and_print_sample is a fluent English sentence, but it does not continue the starting text at all, and the model appears to be writing about a different topic. Identify the pipeline stage that is most likely responsible, and explain how you would confirm it by tracing the data. Then state which mode the model should be in after the call returns.
Hints
- Ask whether the starting text reached the model correctly before asking whether the continuation was generated poorly.
- The first stage is preparing the input, which encodes the starting text into token IDs.
- After the call returns, recall the mode the function restores before it finishes.
Key Takeaways
generate_and_print_sampletakes a starting text, encodes it into token IDs, asks the model to generate a continuation of up to 50 new tokens, decodes the result, and prints it.- The function switches the model to evaluation mode before generating and returns it to training mode before returning, so the surrounding training process is preserved.
- A printed sample complements the numeric estimate from
evaluate_model: the number shows how training is scoring, and the sample shows what the model is writing. - When a sample looks wrong, trace the data through encoding, generation, decoding, and printing, and identify the first stage where the result diverged.