Rafhael Oliveira · IA

Reading GPT-2's original code: 174 lines against everything you learned

The primary source, the whole block in the original, and what the gpt-3 repository really contains.

1 min read· chapter: 6 min

Landed here from LinkedIn? This is chapter 13 of the course GPT-1 and GPT-2, taken apart. It reads on its own; if you want the beginning, it is chapter 00.

The whole of GPT-2 fits in 174 lines of TensorFlow. And you can read every one of them. The primary sources are public. They are not for running: they use a module removed from TensorFlow in 2019 and do not support Python above 3.7. They are for reading. After twelve chapters, you have the vocabulary to understand every line, and reading the original has a value no second-hand explanation replaces. There are three findings in there that only appear to whoever reads the source. One of them is about the causal mask, and it is not how the tutorials teach it.

The three points

  1. The Transformer block in the original, side by side with the course's version. The same pieces, with the names OpenAI gave them.
  2. The causal mask in the original is built in a way the tutorials do not show.
  3. The gpt-3 repository has neither code nor weights. It has text samples and statistics. There is no 'downloading GPT-3'.

From the chapter, the beginning

The whole of GPT-2 fits in 174 lines of TensorFlow. Reading the primary source, line by line, against everything you have learned.

It uses tensorflow.contrib, a module removed in TensorFlow 2.0 (2019). And TF 1.15, the last version that has it, does not support Python above 3.7 — we are on 3.14. Making it run would require a separate Python 3.7 environment and a 2019 version of TF.

It is not worth it, and the reason is a good one: the weights are the same. The gpt2 you load with transformers is exactly the model in this repository, converted from a TensorFlow checkpoint to safetensors. You are already running the original GPT-2 — just in an implementation that works on your Python.

Keep reading chapter 13 →Ler em portuguêsThe PDFs by e-mail →
What the chapter covers · 7 sections · 6 min
  1. What is in repos/
  2. The whole block, in the original
  3. The causal mask, in the original
  4. Three findings from reading the original
  5. The tokenizer: GPT-3-Encoder
  6. The gpt-3 repository: data, not code
  7. In the lab
All chapters of the course · GPT-1 and GPT-2, taken apart
  1. 00 · The seven fossils: why GPT-1, GPT-2 and RoBERTa are a historical cross-section, not a list
  2. Glossary · Course glossary: what token, layer and model mean in AI, and in the rest of IT
  3. 01 · Why GPT-2 sees neither letters nor words, and what that costs in Portuguese
  4. 02 · Embeddings: from a meaningless integer to a vector that knows king and queen are related
  5. 03 · Attention: the single operation that replaced twenty years of recurrent networks
  6. 04 · Twelve heads instead of one, and the single line of code that separates GPT from RoBERTa
  7. 05 · Why GPT-2 needs 48 floors to answer, and what happens inside each one
  8. 06 · What scale really buys: from 124 million to 1.5 billion, measured
  9. 07 · Temperature, top-k and top-p: the decision that changes the text more than switching models
  10. 08 · Perplexity: the one number that says what the model learned, without anecdotes
  11. 09 · GPT-1 to GPT-2: same size, half the perplexity. Four decisions explain it
  12. 10 · RoBERTa: when the advance is training better what already existed
  13. 11 · Encoder vs decoder: one line of code and every consequence that descends from it
  14. 12 · The detector: how a language model becomes a classifier, and why this one should decide nothing
  15. 13 · Reading GPT-2's original code: 174 lines against everything you learned
  16. 14 · Serving models: from function call to HTTP server, and what production does on top
  17. 15 · The lab guide: every command, every script, and everything that was measured
  18. 16 · The ceiling of this path: LeCun's argument, read from inside GPT-2

After this: the Antivision experiment

Rafhael Oliveira
Rafhael Oliveira

Writes the GPT-2 course and runs the Antivision experiment, measuring every run before publishing the number. The LinkedIn posts start here and end in the chapters.

Follow on LinkedIn →