Ir para o conteúdo

Lição 2 · Getting Started

How Llama Generates Text: Tokens, Weights and Sampling

Understand tokens, parameters, base and instruct models, context windows and sampling settings, so the later Llama lessons on running and tuning make sense.

You do not need the math of transformers to use Llama, but four ideas show up everywhere: tokens, parameters, the context window and sampling. This lesson explains each one in plain words, and shows the difference between a base model and an instruct model.

Tokens

A model does not read letters or words directly. A tokenizer cuts text into tokens: whole common words, pieces of rarer words, punctuation and spaces. Each token has an ID number. The model reads a list of IDs and predicts the next ID.

For English, a token is often a few characters long. Other languages, code and unusual text can use many more tokens per sentence. The only reliable count comes from the tokenizer of the exact model you use. To learn how tokenizers are built, see byte pair encoding in practice.

Python Stand-in: a toy tokenizer with a tiny vocabulary

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Parameters

Parameters (weights) are the numbers learned during training. A name such as "8B" means about 8 billion parameters. More parameters usually mean more ability, more memory and slower replies. They are not the only factor: training data, the training recipe and the generation of the model matter too. A newer small model can beat an older larger one on some tasks.

Some newer models use a mixture of experts (MoE) design. The model has many parameters in total, but only a subset is used for each token. You still need to store all of them, so memory follows the total count, while compute per token follows mostly the active count. The model family lesson shows an example.

Base and instruct models

Base (pretrained) model Instruct model
Trained to Continue text Follow instructions in a chat format
Good for Fine-tuning, raw completion Assistants, Q&A, tools
Prompt style A document to continue Messages with roles

If you ask a base model "How do I boil an egg?", it may reply with more questions, as if continuing a forum page. An instruct model answers, because it was further trained on conversations. For almost every application in this tutorial you want an Instruct variant.

The context window

The context window is the maximum number of tokens the model can consider at once: the system prompt, the history, your documents and the reply being written. Text outside the window is invisible to the model. A larger window costs more memory and time, and tools often load a smaller window than the model supports unless you ask. You will see this in speed and context.

Sampling

After each step the model has a score for every possible next token. Sampling decides which one to take.

  • Temperature flattens or sharpens the probabilities. Low values are more predictable. High values are more varied and more likely to wander.
  • Top-k keeps only the k most likely tokens.
  • Top-p keeps the smallest set of tokens whose probabilities add up to p.
  • A seed can make random choices repeatable, within the limits of your software and hardware.
Python Stand-in: temperature changes the probabilities

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

As the temperature rises, the unlikely words get a bigger share. For extraction, classification and code, start low. For brainstorming, raise it a little. More detail is in decoding and sampling.

Knowledge cutoff and hallucinations

A model learns from data up to a knowledge cutoff. It does not know what happened after that date unless you put the information in the prompt. It can also invent facts, links and function names that look real. This is called hallucination. Giving the model trusted text (see retrieval) reduces the problem, but it does not remove it.

Do not ask a model about its own version and trust the answer. Check the file name, model card or tool listing instead.

Putting it together

One reply looks like this:

  1. Your messages are turned into one long string with special markers.
  2. The tokenizer turns that string into token IDs.
  3. The model scores every possible next token.
  4. Sampling picks one token, which is added to the input.
  5. Steps 3 and 4 repeat until the model emits an end-of-turn token or hits your length limit.

Each of those parts is something you can inspect and control when you run Llama yourself. It also explains common surprises. A reply that stops mid-sentence usually hit the length limit. A reply that never stops may point to a wrong chat format. A different answer to the same question is often just sampling at work.

A quick checklist for reading any setting

When a tool shows a new option, ask which part of the loop it changes. Does it change what the model sees (context), how a token is chosen (sampling) or when generation ends (length limit and stop tokens)? That question usually explains what you can expect from it.

Counting before you send

Because every limit is measured in tokens, count with the model's own tokenizer when it matters. Remember to include the system message, the history, the chat markers and the space you need for the reply. A prompt that just fits leaves no room to answer.

Why this matters later. Memory, speed, templates and tuning all build on these four ideas.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does '8B' in a model name roughly tell you?

    B refers to billions of parameters. Memory also depends on the number format.
  2. 2. Which model is usually the right start for a chat assistant?

    Instruct models are trained to follow instructions in a conversation format.
  3. 3. What does a lower temperature usually do?

    Low temperature concentrates probability on the most likely tokens.
  4. 4. In a mixture-of-experts model, memory mostly follows which count?

    All weights must be stored, even though only some are used per token.

Summary

  • Models read and write tokens, and only the real tokenizer counts them exactly.
  • Parameter count drives memory and ability, but it is not the only factor.
  • Use instruct models for chat; base models continue text.
  • The context window limits what the model can see.
  • Sampling settings control variety; hallucinations remain possible at any setting.

Next: The Llama Model Family

Seu progresso fica salvo neste navegador. Não precisa de conta.