Speed

A Real Configuration

Audit every weight matrix, bias vector, LayerNorm pair, and embedding table in a 124-million-parameter Transformer configuration.

The problem

Marketing labels round real checkpoints to headlines like "124M parameters," making large architectures sound mysterious. We are not loading or training a language model in the browser here; instead, we can audit the exact five-number configuration of GPT-2 small, [d_model, d_ff, layers, vocab, max_seq] =[768,3072,12,50257,1024]= [768, 3072, 12, 50257, 1024], using the same layer-counting algebra from earlier pages.

Where does every scalar in that 124,439,808-parameter checkpoint come from, and how much sits inside the 12 repeated Transformer blocks versus the vocabulary and position embedding tables?

Work through the numbers

Start inside one Transformer block with dmodel=768d_{\mathrm{model}} = 768 and dff=3,072d_{\mathrm{ff}} = 3{,}072. Self-attention uses four biased 768→768768 \to 768 linear projections (query, key, value, output), contributing 4⋅768⋅769=2,362,3684 \cdot 768 \cdot 769 = 2{,}362{,}368 parameters. The feed-forward sublayer maps 768→3,072→768768 \to 3{,}072 \to 768 with biases, contributing 2⋅768⋅3,072+3,072+768=4,722,4322 \cdot 768 \cdot 3{,}072 + 3{,}072 + 768 = 4{,}722{,}432 parameters.

Each block also contains two LayerNorm layers, and each LayerNorm stores a scale vector and shift vector of length 768768, adding 4⋅768=3,0724 \cdot 768 = 3{,}072 scalars. Summing 2,362,368+4,722,432+3,072=7,087,8722{,}362{,}368 + 4{,}722{,}432 + 3{,}072 = 7{,}087{,}872 gives 7,087,872 parameters per block. Stacking 12 identical blocks contributes 12⋅7,087,872=85,054,46412 \cdot 7{,}087{,}872 = 85{,}054{,}464 block parameters.

Now count the shared tables outside the blocks. Token embeddings store 50,257⋅768=38,597,37650{,}257 \cdot 768 = 38{,}597{,}376 parameters, learned position embeddings store 1,024⋅768=786,4321{,}024 \cdot 768 = 786{,}432 parameters, and the final LayerNorm adds 2⋅768=1,5362 \cdot 768 = 1{,}536 scalars (the output vocabulary head reuses the token embedding matrix). Those non-block tables total 39,385,344 parameters, and 85,054,464+39,385,344=124,439,80885{,}054{,}464 + 39{,}385{,}344 = 124{,}439{,}808 parameters in full ([7087872, 85054464, 39385344, 124439808]).

The live model

The four bars display [per_block, all_blocks, embeds_and_lnf, grand_total] as n_layers varies on [768, 3072, n_layers, 50257, 1024]. At n_layers =2= 2, stacked blocks hold 14,175,744 parameters while embeddings hold 39,385,344 (total 53,561,088), so vocabulary tables dominate shallow models. At n_layers =6= 6, blocks reach 42,527,232 and pass embeddings (total 81,912,576). At default n_layers =12= 12, total hits 124,439,808, and at 24 layers it reaches 209,494,272.

Four horizontal bars auditing [768, 3072, 12, 50257, 1024]: per-block 7087872, 12 blocks 85054464, embeddings 39385344, and total 124439808.
let n_layers = scene.parameter("n_layers", 12.0, 2.0, 24.0, 2.0);
let d = input.slice(0, 0, 1);
let d_ff = input.slice(0, 1, 2);
let vocab = input.slice(0, 3, 4);
let max_seq = input.slice(0, 4, 5);
let attn = 4.0 * d * (d + 1.0);
let mlp = 2.0 * d * d_ff + d_ff + d;
let per_block = attn + mlp + 4.0 * d;
let all_blocks = n_layers * per_block;
let embeds = vocab * d + max_seq * d + 2.0 * d;
let total = all_blocks + embeds;
let lst = torch.list().add(per_block).add(all_blocks).add(embeds).add(total);
let answer = torch.cat(lst, 0);
out.set("config_audit", answer);
{
  "config_audit": [
    7087872,
    85054464,
    39385344,
    124439808
  ]
}
Watch the parameter change

Your turn

The starter audit outputs [7087872, 7087872, 39385344, 46473216] on the 12-layer configuration instead of [7087872, 85054464, 39385344, 124439808]. Return [per_block, all_blocks, embeds_plus_lnf, grand_total] for any five-element [d_model, d_ff, layers, vocab, max_seq] vector with biased projections, two LayerNorms per block, a final LayerNorm, and tied token embeddings; Check tests both this checkpoint and unseen architectures to exact integer precision.

Starter bars counting only one block (7087872) instead of 12 stacked blocks (85054464).
let d = input.slice(0, 0, 1);
let d_ff = input.slice(0, 1, 2);
let layers = input.slice(0, 2, 3);
let vocab = input.slice(0, 3, 4);
let max_seq = input.slice(0, 4, 5);
let attn = 4.0 * d * (d + 1.0);
let mlp = 2.0 * d * d_ff + d_ff + d;
let per_block = attn + mlp + 4.0 * d;
let all_blocks = per_block;
let embeds = vocab * d + max_seq * d + 2.0 * d;
let total = all_blocks + embeds;
let lst = torch.list().add(per_block).add(all_blocks).add(embeds).add(total);
let answer = torch.cat(lst, 0);
out.set("config_audit", answer);
{
  "config_audit": [
    7087872,
    7087872,
    39385344,
    46473216
  ]
}

Return [per_block, all_blocks, embeds_plus_lnf, grand_total] for any [d_model, d_ff, layers, vocab, max_seq] configuration with biased projections and tied token embeddings.

Read the result carefully

Two proportions stand out in [7087872, 85054464, 39385344, 124439808]. First, inside every block, the two-layer feed-forward network holds 4,722,432 of the 7,087,872 scalars, almost two-thirds of the block, while the four attention projections hold one-third.

Second, storing 124,439,808 parameters in 32-bit floating point (44 bytes each) already occupies 124,439,808⋅4=497,759,232124{,}439{,}808 \cdot 4 = 497{,}759{,}232 bytes, nearly half a gigabyte before gradients, optimizer states, or activations are allocated. Exact paper formulas still explain every single weight, yet no human hand will ever inspect a 124-million-cell array directly.

Recap

  • One Transformer block with biased projections and two LayerNorms stores 4d(d+1)+(2d⋅dff+dff+d)+4d4d(d + 1) + (2d \cdot d_{\mathrm{ff}} + d_{\mathrm{ff}} + d) + 4d scalars.
  • Weight-tied token embeddings, learned position embeddings, and final LayerNorm add vocab⋅d+max_seq⋅d+2d\mathrm{vocab} \cdot d + \mathrm{max\_seq} \cdot d + 2d scalars outside the repeated stack.
  • For GPT-2 small ([768, 3072, 12, 50257, 1024]), twelve 7,087,872-parameter blocks plus 39,385,344 embedding scalars sum to 124,439,808 parameters.