# How Many Parameters Are in One Transformer Layer?

> How to estimate the parameters in one transformer layer from the model's width, checked against GPT-3, explained simply for your AI interview.

**In short:** A transformer layer holds about 12 times the model width squared in parameters. Attention uses four square grids of that width, the feed-forward block uses two grids that reach four times the width, and GPT-3's 96 layers hold most of its 175 billion parameters.

AI and LLM interview question Q064, Transformer architecture. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/parameters-in-one-transformer-layer

## Say it in 30 seconds

Roughly twelve times the model width, squared. A layer has an attention block with four square grids of that width, and a feed-forward block with two grids four times wider. For GPT-3, that comes to about 1.8 billion per layer, and its 96 layers hold almost all of its 175 billion parameters. The formula leaves out small terms, so I'd call it an estimate.

## Say it in 2 minutes

A transformer is a stack of identical layers, and each layer is a set of learned numbers, which we call parameters. This assumes a plain dense layer, the standard design these numbers come from. Mixture-of-experts models store more feed-forward blocks per layer. Most of the parameters sit in two blocks: attention, which lets each word look at the other words, and a feed-forward block, which then works on each word on its own. Both blocks are built from big grids of weights.

The rule of thumb I use is about twelve times the model width, squared. Attention has four square grids of that width: queries, keys, values and an output. The feed-forward block has two grids, one going up to four times the width and one coming back down. Four plus eight gives twelve. The eight comes from those two feed-forward grids, each four times the width. Biases and other small terms add a little more, and the formula leaves them out.

Check it on a real model. GPT-3 has 96 layers and a width of 12,288. Twelve times that width squared is about 1.8 billion per layer, and 96 layers come to about 174 billion. Add the word tables and small terms, and the total lands close to the paper's 175 billion.

Why it matters for a team: at 16-bit precision each number takes two bytes, so one layer of that size needs about 3.6 GB just for its weights. A 24 GB card could not hold the whole model. So I would size the memory before choosing hardware, and say that the count is an estimate.

## What is one transformer layer, in plain words?

A transformer is built from the same layer, stacked many times. Each layer holds parameters, which are the learned numbers the model uses to turn its input into an output. Training fills them in, and answering reads them.

Think of a bank's back office with 96 identical review desks in a row, each built from the same drawers and shelves. Count the parts on one desk, multiply by 96, and you know the room's total. In a model, the desks are the layers, and the drawers are the grids of weights.

- **Parameter:** One learned number inside the model. A model's size is counted in these.
- **Model width:** The length of the vector that stands for one word inside the model.
- **Weight grid:** A rectangle of parameters that multiplies a vector. Its size is rows times columns.
- **Attention:** The part of a layer where each word looks at the other words to pick up context.
- **Feed-forward block:** The part of a layer that works on each word on its own, after attention.

## How many parameters are in each block?

Each layer has two blocks. The paper that introduced the transformer describes them this way:

> "The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network." (Vaswani et al. (2017))
>
> "This consists of two linear transformations with a ReLU activation in between." (Vaswani et al. (2017))

*Diagram, Where one layer's weights sit: A layer has two blocks. The attention block holds four square grids of the model's width, and the feed-forward block holds two grids, one four times wider. Counting every entry gives about 12 times the width, squared.*
1. Each word's vector enters the layer.
2. The attention block mixes information between the words.
3. The feed-forward block works on each word, then the result leaves the layer.

1. **Count the attention block:** Four square grids of width × width: query, key, value and output. That is 4 × width², about 604 million for GPT-3.
2. **Count the feed-forward block:** Two grids: up to four times the width, then back down. That is 8 × width², about 1.21 billion for GPT-3.
3. **Add them, then multiply by the layers:** 4 + 8 = 12, so one layer is about 12 × width², or 1.81 billion for GPT-3. Multiply by 96 layers for the layer total.

The feed-forward block is the bigger share, twice the attention block, because each of its grids holds four times as many numbers as one attention grid. The GPT-3 paper says it always uses a feed-forward layer four times the size of the model width, so check the settings of any model you are given.

## Does the count match a real model?

Here is the same arithmetic in Python. It uses the width and layer count that the GPT-3 paper lists for its largest model.

```python
# One transformer layer, counted by hand. The width is GPT-3 175B's (table in the GPT-3 paper).
width = 12288          # model width, the size of each token's vector
inner = 4 * width      # feed-forward inner size: four times the width (GPT-3 paper)
attention = 4 * width * width   # the Q, K, V and output projections, each width x width
feed_forward = 2 * width * inner  # up to the inner size, then back down to the width
layer = attention + feed_forward
print(f"attention: {attention:,}")
print(f"feed-forward: {feed_forward:,}")
print(f"one layer: {layer:,}")
print(f"about {layer / 1e9:.2f} billion per layer")
print(f"96 layers: {96 * layer:,}")
# Stored as 16-bit numbers, 2 bytes each, so one layer's weights take:
print(f"one layer at 2 bytes each: {layer * 2 / 1e9:.1f} GB")
# A made-up 24 GB card, for the same question on a smaller budget.
print(f"96 layers at 2 bytes each: {96 * layer * 2 / 1e9:.0f} GB, far more than 24 GB")
```

Output:

```text
attention: 603,979,776
feed-forward: 1,207,959,552
one layer: 1,811,939,328
about 1.81 billion per layer
96 layers: 173,946,175,488
one layer at 2 bytes each: 3.6 GB
96 layers at 2 bytes each: 348 GB, far more than 24 GB
```

- **One layer: About 1.81 billion.** Twelve times the width squared, with a width of 12,288.
- **All 96 layers: About 174 billion.** Add the word tables and small terms, and the total lands close to the paper's 175 billion.

> **Tip:** Say the rule, then check it against a real model's width and layer count, so you can redo it on paper.

### How would you fit a model like this on a small GPU?

A team wants to run a GPT-3-sized model on one made-up 24 GB card, to search 40,000 contract files for a legal group. At two bytes per number, the 96 layers, about 174 billion numbers by this count, need about 348 GB, so one card cannot hold them.

The options are to split the model across many cards, to use a smaller model and test it on the team's own contracts, or to store each number in fewer bits. Fewer bits save memory but can cost answer quality, so pick by measuring on their own files.

> **Tip:** Give the memory number first. It decides which hardware is possible before anyone argues about quality.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A rule of thumb: about 12 times the width, squared, per layer.
- [ ] Where it comes from: four attention grids and two feed-forward grids.
- [ ] A real check: GPT-3's 96 layers and width of 12,288 give about 1.8 billion per layer.
- [ ] Honest limits: biases and small terms are left out, so it is an estimate.
- [ ] Why it matters: memory, cost and which hardware can run the model.
- [ ] The whole model adds up its layers, plus the word tables.

**Common traps**

- Trap: "Every layer is about as big as the whole model." Better: One layer is a small slice. GPT-3's 96 layers together hold almost all of its 175 billion.
- Trap: "Attention holds most of the parameters." Better: The feed-forward block holds about twice as many: 8 × width² against 4 × width².
- Trap: "Adding more heads adds many parameters." Better: Heads split the same width into smaller pieces, so the grids stay the same size. The paper says the total cost stays similar.

## Can you spot the wrong estimate?

**Spot the wrong estimate:** A capacity planner wrote four lines for a made-up claims model with width 4,096 and 32 layers. Which line uses the wrong formula?

A. Attention: 4 × 4,096² is about 67 million weights per layer.
B. Feed-forward: 2 × 4,096² is about 34 million weights per layer.
C. Whole layer: about 12 × 4,096², so about 201 million weights.
D. All 32 layers: about 6.4 billion weights, before the word tables.

**Answer:** B. The feed-forward line uses the wrong formula. The block has two grids, each four times the width, so it holds 8 × width², about 134 million here, not 2 × width².

**Quick check:** A platform team plans to run GPT-3-sized layers on its own servers. Roughly how many weights does one layer hold?

A. About 12 million
B. About 600 million, attention only
C. About 1.8 billion
D. About 175 billion

**Answer:** C. Twelve times the width squared, with width 12,288, is about 1.8 billion. The 175 billion is the whole model across 96 layers, not one layer.

## Can you answer these from memory?

**Flash cards**

- **How many parameters per layer?** About 12 × width², from the attention and feed-forward grids.
- **Attention block size?** Four square grids of width × width: query, key, value, output.
- **Feed-forward block size?** Two grids: width up to 4 × width, then back down to width.
- **GPT-3's layers and width?** 96 layers and width 12,288, per the paper's model table.
- **Are biases counted?** Not in the formula. Biases and small terms add a little more.
- **Why does the count matter?** It sets the memory, cost and hardware a model needs to run.

## Likely follow-up questions

- Q018 What are parameters (weights)? https://designervenkat.online/interview/ai/what-are-parameters-weights
- Q046 What is multi-head attention? https://designervenkat.online/interview/ai/what-is-multi-head-attention
- Q054 What does the feed-forward block do? https://designervenkat.online/interview/ai/what-does-the-feed-forward-block-do
- Q311 What is quantization? https://designervenkat.online/interview/ai/what-is-quantization
- Q327 GPU basics: why does memory bandwidth limit decoding? https://designervenkat.online/interview/ai/gpu-basics-why-does-memory-bandwidth-limit-decoding

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
