๐ Inside Mistral 7B: Following a Single Token Through the Entire Model (With Real Tensor Shapes!)
๐ค Ever Wondered What Actually Happens Inside an LLM?
๐ 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