AI Hints
Home›Fine-Tuning LLMs Tutorial›Fine-Tuning a Language Model
Fine-Tuning LLMs · Lesson 38 of 120

Labels for Causal LMs

What the model should learn.

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

For classification, each label was one number. But a language model writes many tokens. So what are the labels now? And which tokens should the model actually learn from?

  • Classification: one label
  • A writer: many tokens
  • What are the labels?

Labels are the text itself

For a causal language model, the labels are simply the token IDs of the text. At each position, the model tries to predict the next token. The library shifts the labels by one, for us. But there's a problem. We don't want the model to learn to write the customer's question. Only our answer.

keyvalue
Labelsthe token IDs themselves
At each positionpredict the next token
Shiftingdone for you
Problemdon't learn the question

Mask the prompt

Here's the fix. We build the prompt with the chat template. The answer is our JSON, followed by the end token. We tokenize both parts. The input IDs are the prompt, then the answer. But in the labels, every prompt token is replaced by minus one hundred. Minus one hundred means, ignore this token in the loss. Run it. The template added a default system message, so the prompt has thirty six tokens. The answer has fifteen. And only the answer tokens have real labels. Including the end token, so the model learns to stop.

app.pypython
from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")
prompt = tok.apply_chat_template([{"role": "user", "content": "Is Dune in stock?"}],
                                 tokenize=False, add_generation_prompt=True)
answer = '{"intent": "stock", "book": "Dune"}<|im_end|>'
prompt_ids = tok(prompt)["input_ids"]
answer_ids = tok(answer)["input_ids"]
input_ids = prompt_ids + answer_ids
labels = [-100] * len(prompt_ids) + answer_ids
print("Prompt tokens:", len(prompt_ids), " Answer tokens:", len(answer_ids))
for token, label in list(zip(input_ids, labels))[-16:]:
    print(f"{tok.decode([token])!r:14} label = {label}")
Output
Prompt tokens: 36  Answer tokens: 15
'\n'           label = -100
'{"'           label = 39428
'int'          label = 591
'ent'          label = 293
'":'           label = 1799
' "'           label = 476
'stock'        label = 14621
'",'           label = 1002
' "'           label = 476
'book'         label = 3767
'":'           label = 1799
' "'           label = 476
'D'            label = 52
'une'          label = 1935
'"}'           label = 23597
'<|im_end|>'   label = 2

Good news

You won't usually write this by hand. The trainer we'll use, from TRL, can do it for you. It's called training on completions only. But now you know what happens inside. Prompt tokens get minus one hundred. Answer tokens are learned.

keyvalue
By handrarely needed
TRLdoes it for you
Namecompletion-only loss
Inside-100 = ignore

Summary

Let's recap. For a language model, the labels are the tokens themselves. We set prompt tokens to minus one hundred, so only the answer is learned. And the end token teaches the model to stop. Next, why we need a cheaper way to fine-tune.

Key takeaways
  • labels = token IDs
  • -100 = ignore
  • Learn the answer + stop
Next up: Why We Need PEFT — a cheaper way