The missing 14.5 billion parameters in my GPT-3 estimate

My first pass at counting GPT-3 came to 160 billion parameters. The paper says 175 billion. I blamed the gap on embeddings and other details. A quick check should have stopped me. At 12,288 weights per token, even a whole extra table of 50,257 tokens buys only 618 million weights. Where could another 15 billion fit?

In a matrix I had counted zero times. After the attention heads produce their outputs, GPT concatenates them and applies a learned projection back to the model width. At GPT-3 scale, that step alone costs 96 × 12,288² = 14,495,514,624 weights. You can see the heads merge and the projection immediately follow in OpenAI’s GPT-2 implementation.

Count one block

Take GPT-2 small, with a residual stream 768 numbers wide. Call that width d. Its 12 attention heads each get 64 channels. Together they use all 768, so the query projection is one 768 × 768 matrix. Splitting it into heads adds no weights. The same arithmetic applies to keys and values.

Part of the blockParameters
Query, key, value projections3d² + 3d
Attention output projectiond² + d
MLP, from d to 4d and back8d² + 5d
Two LayerNorms, each with scale and shift4d
One block12d² + 13d

Each linear layer has one bias per output channel. That gives the MLP 4d biases on the way up and d on the way down. The earlier post overcounted those biases and missed one LayerNorm. By coincidence it still landed on 13d. The consequential error was the coefficient on d².

From one block to a checkpoint

GPT-2 has L blocks, a vocabulary of V tokens, and a learned embedding for each of its T supported positions. It reuses the token table to score the next token, so count that table once. The final LayerNorm adds another 2d. For the whole model, the count is

P = L(12d² + 13d) + Vd + Td + 2d

For GPT-2 small, the 12 blocks hold 85,054,464 parameters. The token table adds 38,597,376, the 1,024 positions add 786,432, and the final norm adds 1,536. That comes to 124,439,808. The vocabulary alone accounts for 31% of this model. For GPT-2 XL, the vocabulary stays at 50,257, but 48 blocks at width 1,600 bring the total to 1,557,611,200. Both counts match the learned tensors in the released checkpoints. Small config · XL config

For GPT-3, the paper gives 96 blocks, width 12,288, a fourfold MLP expansion and 2,048 positions. Using GPT-2’s vocabulary and weight tying gives an estimate of 174.6 billion, which rounds to the published 175 billion. GPT-3 alternates dense and locally banded attention. That changes which token pairs a head attends to. Its projection matrices keep the same shapes. GPT-3 paper, §2.1 and Table 2.1

Why GPT-1 fooled me

The old formula gave 116.4 million for GPT-1, close enough to the familiar 117M label that I took it as a check. But I had given GPT-1 a vocabulary of 50,000. Its actual token table has 40,478 rows. Those 9,522 invented rows added 7.31 million weights, nearly canceling the 7.08 million I had lost by dropping the attention output projection from all 12 blocks.

OpenAI published a small JSON file listing the shapes of the original pretrained tensors. You can count the whole model without downloading a single weight. GPT-1 parameter shapes

import json
from math import prod
from urllib.request import urlopen

repo = "https://raw.githubusercontent.com/openai/finetune-transformer-lm"
with urlopen(repo + "/master/model/params_shapes.json") as response:
    shapes = json.load(response)

print(sum(prod(shape) for shape in shapes))  # 116,534,784

The first two shapes in that file are [512, 768] and [40478, 768]. Reading just those two lines would have caught the vocabulary error. Checking the shape of the matrix after the heads merge would have caught the other one.