PyTorch Counting Parameters
Counting parameters is the audit that lists each part's learned numbers and adds them into one total. The census shows where the budget sits inside the GPT model, part by part.
One table, counted exactly
numel counts every number in a tensor. The token embedding table holds 47 rows of 128 numbers, and the count prints both plain and with a comma.
import torch
from torch import nn
table = nn.Embedding(47, 128)
print(table.weight.numel())
print(f"{table.weight.numel():,}")A Linear layer counts weight plus bias
A Linear layer owns a weight table and one bias number per output. The first feed-forward layer counts its 65,536 weights and its 512 biases separately, then together.
import torch
from torch import nn
layer = nn.Linear(128, 512)
print("weight:", layer.weight.numel())
print("bias:", layer.bias.numel())
print("total:", sum(p.numel() for p in layer.parameters()))A tied weight is counted once
The parameters method lists each shared tensor a single time. A toy model with a tied head counts 6,063 numbers, while counting the shared table twice would claim 12,079.
import torch
from torch import nn
class Toy(nn.Module):
def __init__(self):
super().__init__()
self.embedding = nn.Embedding(47, 128)
self.head = nn.Linear(128, 47)
self.head.weight = self.embedding.weight
toy = Toy()
counted = sum(p.numel() for p in toy.parameters())
naive = 47 * 128 + 128 * 47 + 47
print("counted once:", counted)
print("counted twice would be:", naive)Example
import torch
from torch import nn
from torch.nn import functional as F
CORPUS = """The sun came up over the hill. Moss the cat woke in the warm house. Nan put milk in a blue bowl. Moss drank the milk and licked her lips. Then Moss went out to the garden.
Pip the sparrow sat on the wall. Good morning, Moss, said Pip. Good morning, Pip, said Moss. The sun is warm, said Pip. The sun is warm and the sky is clear, said Moss.
Bram the dog ran down the lane. Bram had a stick in his mouth. Drop the stick, Bram, said Nan. Bram dropped the stick. Good dog, said Nan. Bram wagged his tail and barked at the sky.
The three friends walked to the river. The river ran cold and clear over the white stones. A silver fish jumped in the river. Splash! The fish jumped again. Moss watched the fish. Pip watched the fish. Bram put one paw in the water. The water was cold. Bram shook his wet paw. The friends laughed in the sun.
At noon, Nan called the friends home. Bread and cheese waited on the table. The friends ate in the garden. The garden smelled of rain and roses. Bees hummed in the warm air. It was a good day, and the day was not over yet.
The rain came in the night. Rain tapped on the roof. Rain ran down the lane. Rain filled the blue bowl in the garden. Moss sat at the window and watched the rain. Pip hid under the wall. Bram slept by the fire and dreamed of sticks.
In the morning, the rain was gone. The sun came up over the hill. The lane was wet and the garden was green. A snail crossed the step, slow and silver. Good morning, snail, said Moss. The snail said nothing. The snail had far to go.
Nan gave Moss a fish for lunch. Nan gave Bram a bone for lunch. Nan gave Pip a crust of bread. Pip pecked the crust. Bram chewed the bone. Moss ate the fish, bite by bite. Then all three slept in the sun, full and warm and glad.
One day, a red ball rolled into the garden. The ball rolled past Moss. The ball rolled past Bram. The ball rolled under the wall and stopped by the river. Whose ball is this? said Pip. Tom came down the lane. Tom was a small boy with red boots. My ball! said Tom. Thank you, friends!
Tom threw the ball. Bram ran and caught the ball. Tom threw the ball again. Pip flew over the ball. Moss hid in the grass and jumped out. Boo! said Moss. Tom laughed and laughed. The friends played until the sun went down.
When the sun went down, the sky turned red and gold. The friends walked home in the soft light. Nan had soup on the fire. The soup was hot and good. Eat, said Nan, and rest. Tomorrow is a new day.
The moon came up over the hill. The stars came out, one by one. Moss curled on the step. Pip slept in the tree. Bram slept by the door, one ear up. The house was quiet. The river ran on in the dark, cold and clear. Sleep well, little friends, said the moon. And they did.
In the spring, the garden woke up. Green shoots came out of the dark earth. A bee came to the garden. The bee hummed from rose to rose. Pip sang in the tree. Bram dug a hole by the wall. Moss rolled in the warm grass. Spring is here, said Moss. Spring is here at last.
Nan planted seeds in the garden. Small seeds, brown and round. Moss watched the seeds go into the earth. Pip watched for worms. Bram watched for Tom. Rain came, soft and warm. Sun came, warm and gold. The seeds slept in the dark. Then, one morning, a green shoot! And then two! And then ten!
The shoots grew tall in the summer sun. Red roses opened on the wall. The garden was full of bees and birds. Tom picked peas in the garden. Nan picked beans. Moss picked a warm spot and slept in it. It was a good summer, a long, warm summer.
One hot day, the friends went up the hill. The hill was high and the grass was dry. From the top of the hill, the friends saw the river. They saw the lane and the house and the garden. They saw the whole world, small and bright. I can see my house, said Tom. I can see my river, said Moss. I can see everything, said Pip, and Pip flew up, up, up into the blue.
At the top of the hill, the wind blew. The wind blew the grass. The wind blew the tree. The wind took the song of Pip and carried it over the river. Sing again, Pip, said Bram. Pip sang again, loud and sweet. The sun shone. The wind blew. It was good to be alive.
When autumn came, the leaves turned gold and red. Leaves fell from the tree. Leaves covered the lane. Bram jumped in the leaves. Moss hid in the leaves. Pip rode on a leaf down to the river. Splash! Pip shook the water off and laughed. Autumn is fun, said Pip. Autumn is cold, said Moss, and Moss went home to the fire.
Nan made a pie in the autumn. The pie was full of apples. The house smelled of apples and spice. Tom ate two slices of pie. Bram ate the crumbs. Moss drank warm milk. Pip pecked at a seed cake. Outside, the leaves fell. Inside, the fire burned, warm and bright.
One cold morning, frost covered the garden. The grass was white. The bowl was ice. The river ran slow under a skin of ice. Moss put one paw on the ice. The ice was cold and hard. Winter is coming, said Moss. Winter is coming soon.
Snow fell in the night, soft and deep. In the morning, the world was white. The hill was white. The lane was white. The tree was white, and the wall was white. Bram ran in the snow. Tom rolled in the snow. Pip sat in the tree and shivered. Come down, Pip, said Nan. Come in by the fire.
By the fire, the friends were warm. Nan read a story out loud. The story was about a cat, a dog, and a bird. That is us! said Tom. That is us, said Moss, and Moss purred. The snow fell outside. The fire burned inside. It was the best place in the whole white world.
And when the snow melted, the river ran fast and full. The sun came up over the hill, warm as ever. Moss sat on the step. Pip sang in the tree. Bram ran down the lane with a stick. Spring had come again, and the friends were glad. The end."""
batch_size = 32
block_size = 64
n_embd = 128
n_head = 4
n_layer = 4
chars = sorted(set(CORPUS))
vocab_size = len(chars)
stoi = {ch: i for i, ch in enumerate(chars)}
itos = {i: ch for ch, i in stoi.items()}
data = torch.tensor([stoi[ch] for ch in CORPUS], dtype=torch.long)
split = int(0.9 * len(data))
train_data = data[:split]
val_data = data[split:]
def get_batch(split_name):
source = train_data if split_name == "train" else val_data
ix = torch.randint(len(source) - block_size, (batch_size,))
x = torch.stack([source[i:i + block_size] for i in ix])
y = torch.stack([source[i + 1:i + block_size + 1] for i in ix])
return x, y
class Head(nn.Module):
def __init__(self, head_size):
super().__init__()
self.query = nn.Linear(n_embd, head_size, bias=False)
self.key = nn.Linear(n_embd, head_size, bias=False)
self.value = nn.Linear(n_embd, head_size, bias=False)
self.register_buffer("mask", torch.tril(torch.ones(block_size, block_size)))
def forward(self, x):
B, T, C = x.shape
q = self.query(x)
k = self.key(x)
v = self.value(x)
scores = q @ k.transpose(-2, -1) * k.shape[-1] ** -0.5
scores = scores.masked_fill(self.mask[:T, :T] == 0, float("-inf"))
weights = F.softmax(scores, dim=-1)
return weights @ v
class MultiHeadAttention(nn.Module):
def __init__(self, n_head, head_size):
super().__init__()
self.heads = nn.ModuleList([Head(head_size) for _ in range(n_head)])
self.proj = nn.Linear(n_embd, n_embd)
def forward(self, x):
out = torch.cat([h(x) for h in self.heads], dim=-1)
return self.proj(out)
class FeedForward(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(n_embd, 4 * n_embd),
nn.ReLU(),
nn.Linear(4 * n_embd, n_embd),
)
def forward(self, x):
return self.net(x)
class Block(nn.Module):
def __init__(self):
super().__init__()
head_size = n_embd // n_head
self.attn = MultiHeadAttention(n_head, head_size)
self.ff = FeedForward()
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
def forward(self, x):
x = x + self.attn(self.ln1(x))
x = x + self.ff(self.ln2(x))
return x
class GPT(nn.Module):
def __init__(self):
super().__init__()
self.token_embedding = nn.Embedding(vocab_size, n_embd)
self.position_embedding = nn.Embedding(block_size, n_embd)
self.blocks = nn.Sequential(*[Block() for _ in range(n_layer)])
self.ln_f = nn.LayerNorm(n_embd)
self.lm_head = nn.Linear(n_embd, vocab_size)
self.lm_head.weight = self.token_embedding.weight
self.apply(self._init_weights)
def _init_weights(self, module):
if isinstance(module, nn.Linear):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
def forward(self, idx, targets=None):
B, T = idx.shape
x = self.token_embedding(idx) + self.position_embedding(torch.arange(T))
x = self.blocks(x)
x = self.ln_f(x)
logits = self.lm_head(x)
loss = None
if targets is not None:
loss = F.cross_entropy(logits.view(B * T, vocab_size), targets.view(B * T))
return logits, loss
@torch.no_grad()
def generate(self, idx, max_new):
for _ in range(max_new):
window = idx[:, -block_size:]
logits, _ = self(window)
probs = F.softmax(logits[:, -1, :], dim=-1)
nxt = torch.multinomial(probs, 1)
idx = torch.cat([idx, nxt], dim=1)
return idx
torch.manual_seed(0)
model = GPT()
named = dict(model.named_parameters())
def part(prefix):
return sum(t.numel() for name, t in named.items() if name.startswith(prefix))
tok = part("token_embedding")
pos = part("position_embedding")
qkv = part("blocks.0.attn.heads")
proj = part("blocks.0.attn.proj")
ff = part("blocks.0.ff")
norms = part("blocks.0.ln1") + part("blocks.0.ln2")
per_block = qkv + proj + ff + norms
blocks_total = per_block * n_layer
final_norm = part("ln_f")
head_bias = model.lm_head.bias.numel()
census_total = tok + pos + blocks_total + final_norm + head_bias
model_total = sum(p.numel() for p in model.parameters())
print("Token embedding:", f"{tok:,}")
print("Position embedding:", f"{pos:,}")
print("Attention projections in one block:", f"{qkv:,}")
print("Attention output projection in one block:", f"{proj:,}")
print("Feed-forward in one block:", f"{ff:,}")
print("Two norms in one block:", f"{norms:,}")
print("One block total:", f"{per_block:,}")
print("Four blocks total:", f"{blocks_total:,}")
print("Final norm:", f"{final_norm:,}")
print("Output head bias:", head_bias)
print("Output head weight: 0 (tied to the token embedding)")
print("Census total:", f"{census_total:,}")
print("Model total:", f"{model_total:,}")
print("Counts agree:", census_total == model_total)
print("Share in the four blocks:", f"{100 * blocks_total / model_total:.1f}%")
print("Share in the two embeddings:", f"{100 * (tok + pos) / model_total:.1f}%")
Key points
- Parameter: a learned number that training can adjust
- numel counts the numbers in one tensor, whatever its shape
- The two embeddings own 14,208 numbers between the token table and the position table
- One block owns 197,888 numbers across attention, feed-forward, and its two norms
- The four blocks together own 98.2 percent of the whole model
- A tied weight appears once in the census, under the embedding table
In this example, the census walks every named parameter of a seeded GPT and groups the counts by part. The two embeddings hold 6,016 and 8,192 numbers. Inside one block, the attention projections hold 49,152 and the output projection holds 16,512. The feed-forward holds 131,712, and the two norms hold 512. The block sums to 197,888. Four blocks hold 791,552, and the final norm plus the head bias add 303 more. The census total and the model total both print 806,063, and the agreement check prints True. The shares print the story of the model, with 98.2 percent of the numbers in the blocks. The embeddings hold the other 1.8 percent.
Note
Bias numbers count too, and the output head contributes only its 47 biases
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which part owns the largest share of the model's parameters?