# What Is Weight Tying?

> What weight tying is, why a language model can reuse one table for reading and writing words, and its trade-off, explained simply for your AI interview.

**In short:** Weight tying means a language model uses one table of numbers both to read its input words and to score its output words. The model then learns one big table instead of two, so it has fewer numbers to store and train.

AI and LLM interview question Q067, Transformer architecture. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-weight-tying

## Say it in 30 seconds

Weight tying means a language model uses one table of numbers twice. It reads each input word from the table, and at the end it scores every word against the same table. That removes a whole table of numbers to learn. Press and Wolf recommended it for language models in 2016. The trade-off is that one table has to serve both jobs, so I would test it on my own data before I trust it.

## Say it in 2 minutes

A language model has two big tables that touch every word. The first, the input embedding table, turns each word piece into a vector the model can work with. The second, the output layer, turns the model's last vector into a score for every word piece in its vocabulary. Weight tying says these two can be one table. The same rows are read going in, and the same numbers are used going out.

Why bother? Each table has one row per word piece, and each row can be hundreds of numbers long. In a made-up support-bot model with 50,000 word pieces and 768 numbers per row, one table holds about 38 million numbers. Tying keeps one table, so the model learns about 38 million fewer numbers. Press and Wolf recommended this for language models in 2016, and the 2017 Transformer paper shares one weight matrix between its input and output layers, similar to their method. The saving is largest when the vocabulary is big, because the table grows with the number of word pieces.

The cost is that one table must serve both jobs. Press and Wolf found that the shared table learns more like the output side than the input side. So I treat tying as a design choice to test, and I check the model's config to see whether it is on.

## What is weight tying, in plain words?

A language model works with numbers. Every word piece it knows, called its vocabulary, has one row in a big table. Each row is a list of numbers, called a vector, that stands for that piece.

Two steps touch that table. Going in, the model looks up the row for each piece it reads. Coming out, it has to choose the next piece, so it scores every row against what it has worked out so far. Weight tying means one table does both jobs.

Think of a bilingual clerk at an insurance claims desk who keeps one glossary. They look up an English term to find its French, and a French term to find its English. One glossary serves both directions, so there is only one list to keep up to date. A real model works the same way: the same rows of numbers are used to read words in and to score words out.

- **Vocabulary:** The list of word pieces a model knows. GPT-2's default is 50,257.
- **Embedding table:** The table with one row of numbers per word piece. Reading a word means looking up its row.
- **Output layer:** The last step, which scores every word piece to pick the next one.
- **Parameter:** One learned number inside the model. More parameters mean more to store and train.

## How does one table do both jobs?

Follow one word piece through a model that ties its weights. The arrows show the order of the steps.

*Diagram, One table, used at both ends: A word piece comes in as an ID, and its row is read from the shared table. After the model's layers, the last step scores every row of that same table, and the best choice becomes the next piece. Only one table is stored and learned.*
1. The word piece's ID finds its row in the shared table.
2. The row's numbers go into the model's layers.
3. The model's final vector goes to the scoring step.
4. The chosen word piece is the next one the model writes.

1. **Going in:** Look up the row for the word piece. Its numbers become the vector the model works with.
2. **Going out:** Compare the model's final vector with every row of the same table. A higher score means a closer match. The model turns the scores into probabilities and usually picks a likely one.

Worked example. Take a made-up support-bot model with 50,000 word pieces and 768 numbers per row. One table holds 50,000 times 768, which is 38,400,000 numbers. An untied model keeps two of these tables, so tying saves about 38.4 million numbers. The code below uses that same arithmetic, then runs both steps on a small table with three word pieces.

```python
# Weight tying: one shared table, read going in and used again going out.
VOCAB = 50000   # made-up support-bot vocabulary: 50,000 word pieces
WIDTH = 768     # numbers per row

def table_numbers(tied):
    tables = 1 if tied else 2   # untied: an input table and an output table
    return tables * VOCAB * WIDTH

print("untied:", table_numbers(False))
print("tied:", table_numbers(True))
print("saved:", table_numbers(False) - table_numbers(True))

# A made-up mini table: 3 word pieces, 2 numbers each, to show the two steps.
shared = [[0.9, 0.1], [0.2, 0.8], [-0.5, 0.4]]

def embed(word_id):          # going in: read the row
    return shared[word_id]

def score_all(vector):       # going out: dot product with every row
    return [round(sum(a * b for a, b in zip(row, vector)), 2) for row in shared]

vec = embed(1)
print("vector for word 1:", vec)
print("scores:", score_all(vec))
```

Output:

```text
untied: 76800000
tied: 38400000
saved: 38400000
vector for word 1: [0.2, 0.8]
scores: [0.26, 0.68, 0.22]
```

The output shows the same rows doing both jobs. The row for word 1 goes in, and the scores come out of those same rows.

## What does it save, and what does it cost?

- **Saves: Fewer numbers.** One table to store and train instead of two. Press and Wolf found that tying can shrink a translation model to less than half its size without harming its performance.
- **Costs: One table, two jobs.** The shared table has to serve both jobs. Press and Wolf found it learns more like the output side than the input side. Test it on your own data.

*Diagram, Two tables or one: Without tying, the model keeps and trains two tables. With tying, one table is learned once and used at both ends, so the saving is roughly one whole table. The trade-off is that this one table has to serve both jobs.*
1. The input table is learned on its own.
2. The output table is learned on its own too.
3. The one shared table is learned once and serves both jobs.

The idea goes back to 2016. Press and Wolf showed it for language models, and the 2017 Transformer paper used it too.

> "When training language models, we recommend tying the input embedding and this output embedding." (Press and Wolf, 2016)
>
> "In our model, we share the same weight matrix between the two embedding layers and the pre-softmax linear transformation" (Vaswani and others, 2017)

### How would you decide whether to tie in a new model?

1. **Count the saving:** Multiply vocabulary size by row width. That is the size of one table, and tying saves about one of them. Tying also needs each row to be as wide as the model's final vector, so the two ends fit.
2. **Read the setting:** Check the model's config for a tie setting. Hugging Face's GPT-2 docs list one that defaults to True, and say it ties weight embeddings according to a mapping in the model.
3. **Test on your data:** Train a tied and an untied model on the same data, then compare quality and memory. Decide on the numbers, not on the theory.

> **Tip:** Asked whether tying is always better? Say no. It saves space, but Press and Wolf found the shared table learns more like the output side. Say you would measure it before you ship.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] One table, read going in and scored again going out.
- [ ] Why it saves: the table is stored and trained once, not twice.
- [ ] The origin: Press and Wolf in 2016, and the Transformer in 2017.
- [ ] The trade-off: one table has to serve both jobs.
- [ ] How to check a model: the tie setting in its config.
- [ ] A design choice to test, not a guaranteed win.

**Common traps**

- Trap: "Weight tying always makes the model smarter." Better: Tying mainly saves numbers. Press and Wolf found no harm to performance in their translation models, but results depend on the model and data, so test it.
- Trap: "It copies the input table into the output layer, then trains both." Better: That keeps two tables to learn, so nothing is saved. Tying means one table, used both ways.
- Trap: "The two layers are the same layer." Better: They are two different steps that read the same table. The steps differ, and the numbers are shared.

## Can you spot weight tying?

**Spot weight tying:** A claims team writes this in its design notes. Which line describes weight tying?

A. We copy the input table into the output layer, then train both copies.
B. We use one table to read words in and to score words out.
C. We cut the vocabulary to 1,000 word pieces to make the model smaller.
D. We add a second model that checks the first model's answers.

**Answer:** B. One table serves both jobs, so there is only one set of numbers to learn. Copying and training both gives two tables, which saves nothing. Cutting the vocabulary and adding a checker are different changes.

**Quick check:** The made-up support-bot model has 50,000 word pieces and 768 numbers per row. Roughly how many numbers does tying save?

A. About 768
B. About 38 million
C. About 50,000
D. About 77 million

**Answer:** B. One table is 50,000 times 768, about 38.4 million numbers. Tying keeps one table instead of two, so about that many are saved.

## Can you answer these from memory?

**Flash cards**

- **What is weight tying?** One table of numbers serves as the input lookup and the output scoring.
- **Where does the output step get its numbers?** From the same table the model reads words with, used the other way round.
- **What does tying save?** One whole table: vocabulary size times row width.
- **Who recommended it for language models?** Press and Wolf, in their 2016 paper on the output embedding.
- **What is the trade-off?** One table has to serve both jobs, so test it on your own data.
- **Is it always on?** No. It is a setting in the model's config, and GPT-2's docs default it to True.

## Likely follow-up questions

- Q064 Roughly how many parameters are in one transformer layer? https://designervenkat.online/interview/ai/parameters-in-one-transformer-layer
- Q068 How does the final hidden state become the next token? https://designervenkat.online/interview/ai/how-does-the-final-hidden-state-become-the-next-token
- Q073 Walk through one decoder block's forward pass. https://designervenkat.online/interview/ai/walk-through-one-decoder-block-forward-pass
- Q062 How does BERT differ from GPT? https://designervenkat.online/interview/ai/how-does-bert-differ-from-gpt
