Contents Projects Code Course home

Chapter 34Project 7: Steering Language Models

What you will build

A study of a language model treated as a system with knobs. You compute by hand what decoding settings do to a distribution, write a prompt precise enough to survive documents you have never seen, measure what chain of thought actually changes, and count what a LoRA adapter trains.

Allow eight to ten hours. Read Chapters 20, 21 and 25 first.

What you need

Access to a language model, either through an API or through a chat interface where you paste prompts and save replies. Everything else is arithmetic and short written answers. Score your own prompts locally.

Stages

  1. Decoding by hand. Choose a distribution over six tokens. Compute the greedy token, the size of the top-pp nucleus for a pp of your choice, the probability of one token after temperature rescaling pi′=pi1/T/∑jpj1/Tp_i' = p_i^{1/T}/\sum_j p_j^{1/T}, and the probability of the most probable token outside the top three. Ties break alphabetically.

    Check. For the distribution (0.40,0.25,0.15,0.10,0.06,0.04)(0.40, 0.25, 0.15, 0.10, 0.06, 0.04) the nucleus at p=0.9p = 0.9 has four tokens, the top token after rescaling at T=0.5T = 0.5 has probability 0.61490.6149, and the highest token outside the top three has probability 0.100.10.

  2. A prompt to a target. Write a prompt that reads a campus notice and returns one JSON object with an organisation, an event date in the form year-month-day, a venue, a contact address and an integer fee that is zero if the event is free. Collect about five notices and write the correct answers yourself. Run your prompt on them and measure the mean field accuracy: for each notice the fraction of the five fields that exactly match your answer, averaged over the notices.

    The traps are the ones of Chapter 24: registration dates against event dates, member fees against general fees, numbers spelled out in words, and decoy addresses. Vague prompts plateau. State your formats, your tie-breaking rules and what to do with distractors, explicitly. Keep a log of one concrete failure on one document and the exact prompt change that fixed it.

  3. Chain of thought on and off. Write six word problems, some with one step and some with several. Run each twice, once asking only for the final number and once asking for step-by-step reasoning followed by the number. Record the answers. Report which mode scored higher on the multi-step problems, and explain why reasoning helps multi-step arithmetic but barely moves single-step problems, in terms of tokens and forward passes.

    Predict first. Write down which mode you expect to win before you run anything. Six problems is a small sample, so say how much you would trust the result.

  4. LoRA arithmetic. A transformer has width dd and nn layers. LoRA, a method that freezes the model and trains only small added matrices, is applied to ntargetsn_{\mathrm{targets}} chosen weight matrices per layer, each d×dd \times d. It adds two matrices per target matrix, AA of shape d×rd \times r and BB of shape r×dr \times d, with no biases, so the trainable count is n⋅ntargets⋅2rdn \cdot n_{\mathrm{targets}} \cdot 2rd.

    Check. For d=768d = 768, 1212 layers and two target matrices, the counts at r=2r = 2, 88 and 3232 are 73,72873{,}728, 294,912294{,}912 and 1,179,6481{,}179{,}648. Compare each with the size of the frozen model you are adapting. That small ratio is what the low-rank hypothesis buys: the change to a weight matrix is assumed to need only a few directions.

Extensions

  • Run your prompt at several temperatures and measure how accuracy changes.

  • Rewrite the prompt three ways that mean the same and measure the spread, as in Chapter 20.

  • Add a validation and retry loop that feeds a parse error back to the model, and measure how many failures it repairs.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.