โ† Back to list

๐Ÿš€ Inside Mistral 7B: Following a Single Token Through the Entire Model (With Real Tensor Shapes!)

๐Ÿค” Ever Wondered What Actually Happens Inside an LLM?

jeet joshi ยท 2026-06-11 08:38 ยท 0 claps ยท 5.3 min read
#llm #mistral #transformers
Open on Medium โ†—
Wiki topics: LLM ยท Large Language Models

๐Ÿš€ Inside Mistral 7B: Following a Single Token Through the Entire Model (With Real Tensor Shapes!)

๐Ÿค” Ever Wondered What Actually Happens Inside an LLM?

When people talk about Large Language Models, youโ€™ll often hear terms like:

  • Attention
  • Query, Key, Value
  • Transformer
  • Self-Attention
  • KV Cache
  • Logits

But what do these things actually look like inside a real model?

Letโ€™s take a real-world model:

Mistral 7B Instruct v0.3

And follow a sequence through the entire model step by step, tracking:

โœ… Tensor shapes โœ… Matrix multiplications โœ… Attention calculations โœ… MLP operations โœ… Next-token prediction

By the end, youโ€™ll understand exactly how a prompt becomes a response. ๐ŸŽ‰

๐Ÿ—๏ธ The Architecture We Are Exploring

Hereโ€™s the important part of Mistral 7B:

Embedding(32768, 4096)
q_proj: 4096 โ†’ 4096
k_proj: 4096 โ†’ 1024
v_proj: 4096 โ†’ 1024
o_proj: 4096 โ†’ 4096
gate_proj: 4096 โ†’ 14336
up_proj:   4096 โ†’ 14336
down_proj: 14336 โ†’ 4096
32 Decoder Layers

Important numbers:

๐ŸŽฏ Letโ€™s Use a Real Example

Suppose our prompt contains exactly:

100 tokens

For simplicity:

Batch Size = 1
Sequence Length = 100

Initial shape:

[1, 100]

Meaning:

1 sequence
100 tokens

๐Ÿค Single Token vs 100 Tokens

Throughout this article weโ€™ll compare:

This makes it easier to understand how tensor dimensions grow.

๐Ÿ“š Step 1: Embedding Lookup

The embedding table is:

Embedding(32768, 4096)

Think of it like a giant dictionary:

32768 words
4096 numbers per word

Each token gets converted into a vector.

Single Token

Input:
[1]
Output:
[1, 4096]

100 Tokens

Input:
[1, 100]
Output:
[1, 100, 4096]

Now every token is represented by 4,096 learned numbers.

๐ŸŽ‰ Congratulations! The model can finally understand numbers instead of raw token IDs.

๐Ÿ” Step 2: Create Queries (Q)

The Query projection is:

q_proj
4096 โ†’ 4096

Weight matrix (Wq):

[4096 ร— 4096]

Single Token

[1 ร— 4096]
ร—
[4096 ร— 4096]
=
[1 ร— 4096]

100 Tokens

[1 ร— 100 ร— 4096]
ร—
[4096 ร— 4096]
=
[1 ร— 100 ร— 4096]

๐Ÿ”‘ Step 3: Create Keys (K)

k_proj
4096 โ†’ 1024

Weight matrix (Wk):

[4096 ร— 1024]

Single Token

[1 ร— 4096]
โ†“
[1 ร— 1024]

100 Tokens

[1 ร— 100 ร— 4096]
โ†“
[1 ร— 100 ร— 1024]

๐Ÿ“ฆ Step 4: Create Values (V)

v_proj
4096 โ†’ 1024

Weight matrix (Wv):

[4096 ร— 1024]

Single Token

[1 ร— 1024]

100 Tokens

[1 ร— 100 ร— 1024]

At this point we have:

Q = [1 ร— 100 ร— 4096]
K = [1 ร— 100 ร— 1024]
V = [1 ร— 100 ร— 1024]

๐Ÿงฉ Step 5: Split Into Attention Heads

Mistral uses:

32 Query Heads
8 KV Heads

Head size:

4096 รท 32 = 128

Query Heads

[1 ร— 100 ร— 4096]
โ†“
[1 ร— 32 ร— 100 ร— 128]

Key Heads

[1 ร— 100 ร— 1024]
โ†“
[1 ร— 8 ร— 100 ร— 128]

Value Heads

[1 ร— 8 ร— 100 ร— 128]

๐Ÿคฏ Why Only 8 KV Heads?

This is called:

Grouped Query Attention (GQA)

Instead of storing:

32 Query Heads
32 Key Heads
32 Value Heads

Mistral stores:

32 Query Heads
8 Key Heads
8 Value Heads

Result:

โœ… Less memory โœ… Faster inference โœ… Smaller KV Cache

Very clever engineering! ๐Ÿš€

๐ŸŒ€ Step 6: Rotary Positional Embeddings (RoPE)

RoPE injects position information.

Without it:

Token 1
Token 50
Token 100

would all look similar.

RoPE teaches the model:

This token came before that token.

Important:

๐Ÿ“ Tensor shapes do NOT change and this will get applied on Q and K.

๐Ÿง  Step 7: Compute Attention Scores

This is where the magic happens.

For one head:

Q = [100 ร— 128]
K = [100 ร— 128]

Compute:

Q ร— Kแต€

Shape:

[100 ร— 128]
ร—
[128 ร— 100]
=
[100 ร— 100]

๐ŸŽฏ What Does 100ร—100 Mean?

Each row:

Current token

Each column:

Token being attended to

Example:

Token 57
asks:
"Which previous tokens are important?"

The answer becomes one row in the attention matrix.

๐Ÿคฏ Attention Matrix Size

One head:

[100 ร— 100]

32 heads:

[32 ร— 100 ร— 100]

Add batch dimension:

[1 ร— 32 ร— 100 ร— 100]

This is the famous attention matrix.

๐ŸŽฒ Step 8: Softmax

Softmax converts raw attention scores into probabilities.

Example:

Before:
[5, 2, 1]
After:
[0.84, 0.11, 0.05]

Shape remains:

[1 ร— 32 ร— 100 ร— 100]

๐ŸŽจ Step 9: Apply Attention To Values

We now combine attention scores with V.

For one head:

Attention
[100 ร— 100]
ร—
Value
[100 ร— 128]
=
[100 ร— 128]

Output:

[100 ร— 128]

for each head.

๐Ÿ”— Step 10: Concatenate All Heads

32 heads:

32 ร— 128 = 4096

Combine:

[1 ร— 32 ร— 100 ร— 128]
โ†“
[1 ร— 100 ร— 4096]

Now every token has information gathered from all attention heads.

๐ŸŽ›๏ธ Step 11: The Mysterious o_proj Layer

Many engineers first hear about:

Wq
Wk
Wv

But then suddenly see:

o_proj

and get confused.

What Is o_proj?

Think of attention heads as specialists:

๐Ÿ‘จโ€๐Ÿ’ป Head 1 โ†’ Code expert

๐Ÿ“š Head 2 โ†’ Language expert

๐Ÿงฎ Head 3 โ†’ Math expert

๐ŸŽจ Head 4 โ†’ Creativity expert

โ€ฆ

After attention:

[Head1 | Head2 | Head3 | ...]

are simply stacked together.

o_proj mixes all those opinions together.

Weight:

[4096 ร— 4096]

Input:

[1 ร— 100 ร— 4096]

Output:

[1 ร— 100 ร— 4096]

Same shape.

Different information.

โœจ This is where all attention heads get blended into a unified representation.

โž• Step 12: Residual Connection

Transformer says:

Donโ€™t throw away the original information!

So:

Attention Output
+
Original Input

Result:

[1 ร— 100 ร— 4096]

๐Ÿง  Step 13: MLP โ€” The Modelโ€™s Brain

Attention is communication.

MLP is thinking.

This part is actually larger than attention.

Gate Projection

4096 โ†’ 14336

Shape:

[1 ร— 100 ร— 14336]

Up Projection

4096 โ†’ 14336

Shape:

[1 ร— 100 ร— 14336]

SwiGLU

Mistral uses:

SiLU(gate) ร— up

Shape remains:

[1 ร— 100 ร— 14336]

Down Projection

14336 โ†’ 4096

Result:

[1 ร— 100 ร— 4096]

๐Ÿ” Repeat 32 Times

Everything we discussed happens:

32 times

One decoder layer after another.

Shape stays:

[1 ร— 100 ร— 4096]

throughout the entire model.

Only the understanding becomes richer.

๐Ÿ Final Layer: LM Head

Weight:

[4096 ร— 32768]

Input:

[1 ร— 100 ร— 4096]

Output:

[1 ร— 100 ร— 32768]

๐Ÿค” Why 32,768?

Because:

Vocabulary Size = 32,768

The model gives a score for every possible token.

๐ŸŽฒ Example Logits

For the last token:

Paris   โ†’ 12.5
London  โ†’ 4.2
Berlin  โ†’ 1.8
Rome    โ†’ 0.5

These are called:

Logits

Not probabilities yet.

๐ŸŽฏ Softmax Converts Logits To Probabilities

Paris   โ†’ 82%
London  โ†’ 10%
Berlin  โ†’ 5%
Rome    โ†’ 3%

Now we can choose the next token.

๐Ÿ† How Is The Next Token Selected?

The model only looks at:

output[:, -1, :]

which means:

Last token
All vocabulary scores

Shape:

[32768]

Then one of these methods is used:

Greedy

Pick highest probability

Sampling

Randomly sample
based on probability

Top-K

Keep top K tokens

Top-P

Keep tokens whose cumulative probability reaches P

๐Ÿ”„ The Generation Loop

while not finished:
  logits = model(tokens)
      last_logits = logits[:, -1, :]
      probs = softmax(last_logits)
      next_token = sample(probs)
      tokens.append(next_token)

The model keeps repeating this process until:

โœ… EOS token appears

or

โœ… Maximum length is reached

๐ŸŽ‰ Final Takeaway

A Transformer like Mistral is surprisingly elegant:

1๏ธโƒฃ Convert tokens into vectors

2๏ธโƒฃ Create Queries, Keys, and Values

3๏ธโƒฃ Build attention scores

4๏ธโƒฃ Gather information from other tokens

5๏ธโƒฃ Mix attention heads with o_proj

6๏ธโƒฃ Think using the MLP

7๏ธโƒฃ Repeat 32 times

8๏ธโƒฃ Produce vocabulary scores

9๏ธโƒฃ Pick the next token

๐Ÿ”Ÿ Repeat again and again until a full response is generated

And thatโ€™s how a simple prompt turns into a detailed answer, one token at a time. ๐Ÿš€


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
44fc6e10ed9f
slug
inside-mistral-7b-following-a-single-token-through-the-entire-model-with-real-tensor-shapes-44fc6e10ed9f
url
https://medium.com/@jeetjoshi2000/inside-mistral-7b-following-a-single-token-through-the-entire-model-with-real-tensor-shapes-44fc6e10ed9f
canonical_url
https://medium.com/@jeetjoshi2000/inside-mistral-7b-following-a-single-token-through-the-entire-model-with-real-tensor-shapes-44fc6e10ed9f
author_url
https://medium.com/@jeetjoshi2000
status
ok
fetched_at
2026-06-12 18:14:10