⌂ Contents
Session 2
Note: This page's design and content were created and enhanced using Claude (Anthropic's AI assistant); the reading list has been reference-checked. Spot an error? Email jonathan.shock@uct.ac.za.
Week 1 • Session 2.5

Lab: scaling laws and a first look inside a model

Fit a power law, explore the compute trends, and open up GPT-2 in TransformerLens

What we'll cover

The term's first lab does three things: it gets your toolchain working on Colab, it makes the scaling laws from 2.3 tangible by fitting one yourself, and it gives you a first hands-on look inside a real transformer using TransformerLens, the library we'll lean on heavily for interpretability in Weeks 7–8.

Setup Colab — no GPU needed

Allow about an hour, more if the exploring catches you. A Google account is the only prerequisite: everything runs on the free tier, and a GPU runtime only makes part 3 quicker.

  1. Open the starter notebook in Colab. It opens read-only from GitHub, so use File → Save a copy in Drive before you change anything; the copy in your Drive is the one you edit and submit.
  2. Run the first cell. Colab will warn you that the notebook "was not authored by Google", which it says of anything loaded from GitHub; choose Run anyway. The cell installs TransformerLens and prints Colab: True when it has finished.
  3. Work down the notebook with Shift+Enter, reading each cell before you run it. All of it together is only a couple of minutes of compute, including downloading GPT-2; the hour is for thinking about what comes out.

You can also read the whole notebook on this site before you start, or download the .ipynb for Jupyter.

The code is written for you, so this lab is about running it and pulling at it rather than building it. Each of the three parts ends with an "Explore" cell: two or three things to change and re-run, and one short question to answer. Changing a number and watching the answer move is the whole exercise. If you would rather build it from a blank notebook, do that instead and treat this one as the worked answer; you will learn more, and it will cost you an evening.

① Fit a scaling law

From data to a power-law exponent

Session 2.3 gave you the law. Running the fit on real runs is the only way to feel how small its exponent is.

What the notebook does: loads five measured Cerebras-GPT runs (parameters, training tokens and Pile test loss, from Tables 1 and 3 of arXiv:2304.03208), works out each run's training compute as \(C = 6ND\), plots loss against compute on log–log axes, where a power law is a straight line and nothing else is, fits \(\log L = a - \alpha \log C\), and extrapolates an order of magnitude past the largest run. Point CSV_PATH at your own file to use different data.

You should see \(\alpha \approx 0.054\), against the \(\alpha_C^{\min} \approx 0.050\) that Kaplan et al. report: a different lab, different models and different data, landing close. Feel the size of it: a tenfold increase in compute multiplies the loss by \(10^{-\alpha} \approx 0.88\), an improvement of about 12%. Tenfold, for 12%.

Then the notebook shows you something more useful than agreement. The Cerebras authors fitted their own frontier and published it as \(L(f) = (f/5.984 \times 10^{22})^{-0.0737} + 0.5066\). Their exponent is 0.0737, not your 0.054, on the very runs you just fitted. That is not a disagreement about the data but about the shape: they fit a floor \(E\) and you did not, and with five points you cannot fit three parameters without them trading off freely against each other. The scaling exponent is not a property of the data alone; it depends on the functional form you assumed before you started. Worth remembering the next time you meet a headline exponent, this course included.

Then explore, on a much bigger sample: 245 points from the real training runs behind Chinchilla, extracted from that paper's Figure 4 by Epoch AI. Two things change once you have a cloud rather than five points. The law is the frontier of the cloud, not a line through the middle, because each compute budget was spent several ways and only the best of them sits on the curve. And the loss cannot fall to zero, since language has entropy no model predicts away, so the real curve is \(L(C) = E + (C_c/C)^{\alpha}\) with Chinchilla estimating \(E = 1.69\). Fit the cloud naively and you get 0.058; fit the frontier and you get 0.054, which is almost exactly what your five Cerebras runs gave; subtract the floor and fit what is left and you get 0.156. That last number is the one to argue about: change \(E\) from 1.69 to 1.5 or 1.9 and it moves between 0.13 and 0.21, so how fast the reducible loss falls depends heavily on a constant somebody else estimated. Then answer, in one sentence: why should you distrust your own extrapolation? Session 2.4 is where to look for what a smooth loss curve hides about capabilities, and 2.3 for the cautionary case, where Chinchilla re-ran Kaplan's compute-optimal experiment more carefully and got a different answer.

② Explore the compute trends

Doubling time from Epoch AI data

A second measurement, this time on real data you did not collect, which brings its own problems.

What the notebook does: reads Epoch AI's notable-models dataset straight from the URL, keeps the rows that have both a publication date and a training-compute estimate, plots compute against date on a log axis, and converts the fitted slope into a doubling time. Roughly half the dataset has no compute estimate at all, which is worth noticing before you trust the half that does.

From 2010 you should see a doubling time of about 6 months, near enough 4x per year, and a list of the largest runs showing how few models carry the trend.

Then explore. One cell refits over several starting years at once, so the answer moves in front of you: 2010 gives about 5.7 months, 2018 about 4.7. Another switch keeps only the largest run per quarter, which is the frontier rather than the field, and that is the number people usually quote. Then answer in two sentences: which quantity you actually measured, stated precisely, and over what window, and what would change your answer. This is the Session 1.4 checklist turned on a plot you made yourself, which is harder than turning it on someone else's.

③ Open up a transformer

GPT-2 small in TransformerLens

Your first contact with the residual-stream picture from 2.2, on a real model: 12 layers, 12 heads, \(d_{\text{model}} = 768\). Loading the weights takes a minute or two and about 500 MB.

from transformer_lens import HookedTransformer
model = HookedTransformer.from_pretrained("gpt2")
logits = model("The Eiffel Tower is in the city of")
# inspect the next-token distribution

What the notebook does: loads GPT-2 small, prints the top five next tokens with their probabilities for several prompts, captures activations with run_with_cache and prints the residual stream's shape, then tokenises parallel English and isiZulu sentences and counts the tokens in each. The sentence pairs come from Masakhane's MAFAND-MT dataset, because a tokenisation comparison only means something on genuinely parallel text, and it averages over a couple of hundred pairs rather than trusting one.

Things to look at as it runs. The residual stream comes out as (1, 11, 768), which is [batch, position, d_model]: exactly the \(T \times d\) matrix from 2.2, one row per token. On the Eiffel Tower prompt GPT-2 small puts " London" slightly above " Paris" (0.081 against 0.069), and the reason is worth more than the curiosity: TransformerLens prepends a beginning-of-text token to your prompt, and without it the model puts " Paris" first (0.070 against 0.058). Three of five similar factual prompts flip the same way. Better still, put one ordinary sentence in front of it ("I visited France last summer") and Paris wins at 0.175 against London's 0.0004 either way: the model is not ignorant about the Eiffel Tower, the bare prompt was. A claim about what a model knows turned on setup nobody typed, which is the evaluation problem of Session 11 arriving early. And isiZulu costs about 2.2x the tokens of the same meaning in English, with words shattering into fragments: UKhomishana becomes ['UK', 'hom', 'ish', 'ana']. Across languages: roughly 2x for Swahili, isiXhosa and isiZulu, 2.5x for Hausa, over 4x for Yoruba. GPT-2's vocabulary of 50,257 tokens was fitted mostly to English, and that is what the decision costs everyone else.

Then explore. Put your own prompts through it, including ones where you know the answer and suspect the model does not, and put a sentence from a language you speak against its English translation to see where your language gets chopped hardest. Then answer with a short paragraph on what your ratio implies, covering cost (APIs bill per token, and context windows are counted in tokens), quality (the model sees the language in smaller, less meaningful pieces), and safety (if a language is under-represented enough to tokenise badly, what does that predict about how much safety training and red-teaming it received?). Sessions 9 and 18 return to this.

If something breaks

  • ModuleNotFoundError: transformer_lens after the install cell ran: Colab needs the runtime restarted occasionally after installing. Use Runtime → Restart session, then run the cells again from the top.
  • A deprecation warning about from_pretrained, or a note about unauthenticated Hugging Face requests: both are harmless. The model still loads.
  • The Epoch AI or MAFAND download fails: you are probably offline or behind a proxy. Download the file by hand and upload it to the session; the notebook says where each one comes from.
  • Everything is slow: check Runtime → Change runtime type. A GPU helps part 3 and nothing else, and the lab is designed to be fine without one.

Submit

One notebook, run from top to bottom with its outputs saved, containing:

  • ① one sentence on why to distrust your extrapolation;
  • ② two sentences on what you measured and over what window;
  • ③ a short paragraph on cost, quality and safety, plus your own text in the last cell.

Three short answers, then. The reading is the work; the code is there to give you something worth reading about.

In Colab, File → Download → Download .ipynb. Graded on completion and correctness, and resubmission is allowed, because we are after mastery rather than one-shot performance.

Tools and references

Core

TransformerLens (MIT-licensed): docs and "Getting Started in Mech Interp" (github.com/TransformerLensOrg/TransformerLens).

Epoch AI: notable-models data and trends (epoch.ai).

Dey et al. (2023), "Cerebras-GPT: Open Compute-Optimal Language Models" (arXiv:2304.03208): the five measured runs part ① fits, from Tables 1 and 3.

Epoch AI, "Chinchilla Scaling: A replication attempt" (epoch.ai; data at github.com/epoch-research/analyzing-chinchilla): the 245 Chinchilla runs the explore cell loads, extracted from Hoffmann et al.'s Figure 4. Read it as a reconstruction from a published figure, not the authors' own logs.

MAFAND-MT (Adelani et al., NAACL 2022): the Masakhane news-translation dataset the tokenisation comparison uses, covering 16 African languages (github.com/masakhane-io/lafand-mt; dataset CC BY-NC 4.0, downloaded at run time rather than redistributed here).

Session 2 and Week 1 summary: what's next

You've rebuilt the foundations: a network is a parametrised function fit by minimising a loss (2.1); the transformer routes information through a residual stream via attention (2.2); loss falls as a predictable power law in scale (2.3), while whether specific abilities "emerge" is metric-dependent and contested (2.4); and you've now fitted a law and opened a model yourself (2.5).

Next (Week 2, Session 3): with the foundations in place, we state the core alignment problem properly (proxies, Goodhart, instrumental convergence, deceptive alignment), the argument whose skeleton you met in Session 1.3, now made rigorous.