CSCE 471 Natural Language Processing · Spring 2027

Alignment and Instruction Tuning

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

Ask a freshly pretrained language model to “Explain the moon landing to a 6 year old in a few sentences”, and you may get back… more questions: “Explain the theory of gravity to a 6 year old. Explain the theory of relativity to a 6 year old…”. The model is not broken. It was trained to continue text, and on the internet a line like this is often followed by similar lines, for example in a list of homework prompts.

This gap between predicting the next token and doing what the user wants is what alignment tries to close. This reading covers why the gap exists, how in-context learning partly works around it, and how instruction tuning trains models to follow instructions directly, along with its limitations.

After reading this, you should be able to explain why language modeling differs from assisting users · describe in-context learning and its limitations · explain how instruction tuning converts tasks into (instruction, input, output) examples · explain why the loss is usually computed only on the response · explain why training on many tasks helps generalization to unseen tasks · describe the limitations of instruction tuning that motivate learning from human preferences

1Why alignment?

A pretrained language model learns one thing: given the text so far, predict the next token. Give it a prompt, and it produces a completion that is a likely continuation. A likely continuation is not the same as a helpful answer:

Alignment means making a model's behavior match what people intend. A common summary of the goal is that a model should be helpful (does what the user asked), honest (does not make things up), and harmless (avoids harmful outputs).

Prompt:
Figure 1 (interactive). How a model that only does language modeling and a model trained to follow instructions respond to the same prompt. The moon-landing prompt and the GPT-3 completion come from OpenAI's InstructGPT work (Ouyang et al., 2022); the other outputs are illustrative examples of typical behavior.

2A first workaround: in-context learning

Large language models such as GPT-3 showed a surprising ability: if the prompt contains a task description and a few examples of the task, the model continues the pattern. This is in-context learning (ICL), also called few-shot prompting. No parameters are updated; the “learning” happens entirely inside the prompt.

Number of examples in the prompt:
Prompt
Figure 2 (interactive). Zero-, one-, and few-shot prompts for English-to-French translation, in the style of the GPT-3 paper (Brown et al., 2020). The examples show the model what kind of continuation is expected.

ICL works, but it has clear limitations:

And users do not want to write examples; they want to just ask. Could we instead train the model so that it follows instructions directly?

3Instruction tuning

The idea of instruction tuning: LLMs already have a lot of knowledge from pretraining, but they do not reliably produce the outputs we want. So we finetune them on examples of instructions paired with good responses. The data comes from two main sources:

Training is ordinary language modeling on these examples: the instruction and input are given as context, and the model learns to generate the output token by token. One common detail: the loss is usually computed only on the response tokens. The model should learn to answer instructions, not to generate them.

Source dataset:
Loss computed on:
Original example
Instruction format
Training sequence (tokens that contribute to the loss are solid)
Figure 3 (interactive). Turning examples from existing datasets into instruction-tuning data. The training sequence is the instruction and input (blue) followed by the output (green). With “response only”, the instruction tokens are context but are not predicted, so they do not contribute to the loss. Tokens are shown as whole words for simplicity.

4Scaling up instruction tuning

The training recipe has two stages:

  1. Pretrain on lots of text with language modeling: learn general knowledge and language.
  2. Finetune on many tasks, each phrased as instructions: learn to follow instructions.

The key finding is that the number and variety of tasks matters. A model instruction-tuned on many different tasks learns the general skill of “read an instruction, do what it says”, and this skill transfers to tasks it has never seen. To test this, researchers hold out entire task types (e.g., all natural language inference tasks) during finetuning and evaluate on them afterwards with zero-shot instructions.

WorkScale of instruction dataMain finding
FLAN (Wei et al., 2021)62 datasets in 12 task clustersInstruction tuning improves zero-shot performance on held-out task types
Super-NaturalInstructions (Wang et al., 2022)1,616 tasks with expert-written instructionsModels generalize to unseen tasks given only their instructions
Flan-PaLM / Flan-T5 (Chung et al., 2022)1,836 tasks, including chain-of-thought dataScaling the number of tasks and model size both help
Chain-of-thought data. Some instruction-tuning examples include step-by-step reasoning before the final answer (“The cafeteria had 23 apples. They used 20, so they had 3. They bought 6 more, so 3 + 6 = 9.”). Training on such examples teaches the model to reason step by step when asked, as in the example below.
Model input (disambiguation QA)
Q: In the following sentences, explain the antecedent of the pronoun (which thing the pronoun refers to), or state that it is ambiguous.
Sentence: The reporter and the chef will discuss their favorite dishes.
Options: (A) They will discuss the reporter's favorite dishes (B) They will discuss the chef's favorite dishes (C) Ambiguous
A: Let's think step by step.
Figure 4 (interactive). The same model before and after instruction finetuning, from Chung et al. (2022). Before finetuning, the model continues the text with sentence variations instead of answering. After finetuning, it reasons and answers (C).

5Limitations of instruction tuning

Model output for “Avatar is a …”:
Figure 5 (interactive). The reference continuation is Avatar is a fantasy TV show. Two outputs each have one wrong word. The bars show each token's loss ($-\log p$ of the reference token) under illustrative probabilities. The losses are similar, but a person would judge the errors very differently.

Even after instruction tuning, there is a mismatch between the language modeling objective (“predict the reference tokens”) and what we actually want (“produce responses people prefer”). This motivates methods that learn from human preferences: instead of writing the perfect answer, people compare model outputs and say which one is better, and the model is trained to produce the preferred kind of output.

6Summary

7Practice

Exercise 1: write an instruction example

Convert this example from a natural language inference dataset into (instruction, input, output) form. Premise: “A man is playing a guitar on stage.” Hypothesis: “A man is performing music.” Label: entailment.

One possibility. Instruction: “Does the premise imply the hypothesis? Answer yes, no, or maybe.” Input: “Premise: A man is playing a guitar on stage. Hypothesis: A man is performing music.” Output: “yes”. Many templates work; using several different templates per dataset makes the model more robust to phrasing.

Exercise 2: loss masking

A training sequence has 40 instruction tokens and 10 response tokens. With the loss computed on the response only, how many token predictions contribute to the loss? Why might this be preferred over all 50?

10. The goal is to learn to produce good responses given an instruction; learning to predict the instruction text itself does not help with that, and long instructions would otherwise dominate the loss.

Exercise 3: held-out evaluation

You instruction-tune a model on sentiment analysis, translation, and summarization, and then evaluate it on sentiment analysis with new examples. Does this show generalization to unseen tasks? How would you design a better test?

No: sentiment analysis was a training task, so this only tests new examples of a seen task. Hold out an entire task type (e.g., question answering or natural language inference) during finetuning and evaluate on it with instructions only.

Quick quiz

8Further reading