AI Hints
Home›Fine-Tuning LLMs Tutorial›Decoding, Instruct Models and Chat Templates
Fine-Tuning LLMs · Lesson 16 of 120

Decoding Settings

Greedy, sampling, temperature.

⏱ 2 min read▶ Video at 25:07🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

At every step, the model gives probabilities for every possible next token. But which one should it actually pick? That choice is called decoding, and a few settings control it.

  • Probabilities for every token
  • Which one to pick?
  • That's called decoding

The main settings

Greedy decoding always picks the most likely token. It's steady, but can be boring, and repetitive. Sampling picks randomly, using the probabilities, so likely tokens win more often. Temperature changes how random sampling is. Low is careful. High is wild. And top p only samples from the most likely tokens, that together make up, for example, ninety percent.

SettingWhat it does
do_sample=Falsegreedy: always the top token
do_sample=Truepick randomly by probability
temperaturelow = careful, high = wild
top_p=0.9only the top 90% of probability

Temperature in action

We sample from the same start, three times, with three temperatures. Set seed makes the random choices repeatable. We only decode the new tokens, by slicing off the input. Run it. At zero point two, the text is sensible. At one, it's more surprising. At one point eight, it falls apart.

app.pypython
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed

name = "HuggingFaceTB/SmolLM2-135M"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name)
inputs = tok("My favourite kind of book is", return_tensors="pt")

for temp in [0.2, 1.0, 1.8]:
    set_seed(1)
    out = model.generate(**inputs, max_new_tokens=10, do_sample=True, temperature=temp)
    new = out[0][inputs["input_ids"].shape[1]:]
    print(f"temperature {temp}: {tok.decode(new, skip_special_tokens=True)!r}")
Output
temperature 0.2: ' the one that I read when I was a kid'
temperature 1.0: ' the kind with good grammar, the kind where people'
temperature 1.8: ' if an actor would perform\ntwo things at a'

Which settings for which job?

For tasks with one right answer, like pulling data out of a message, use greedy decoding. You want the same answer every time. For creative writing, use sampling, with a temperature around zero point seven. In this course, our task has right answers, so we'll mostly use greedy.

keyvalue
One right answergreedy, do_sample=False
Creative writingsampling, temperature ~0.7
Our projectgreedy
Whysame answer every time

Summary

Let's recap. Decoding chooses the next token. Greedy picks the top one. Sampling picks randomly, and temperature controls how random. Use greedy for tasks with right answers. Next, base models, and instruct models.

Key takeaways
  • Greedy vs sampling
  • temperature
  • Greedy for right answers
Next up: Base vs Instruct Models — continue text, or follow instructions