Decomposing transformers layers and attention heads For better internals


In one of the previous posts we managed to understand that the input for a LLM is just a matrix, which holds a lot of vectors. Now this post is more about the path that those vectors have to pass in order for the LLM to generate a token.

If you go to Hugging Face and search for a model, for this example we are referring to Qwen/Qwen2.5-Coder-7B-Instruct-GGUF ( a small 7b parameters model used for programming tasks ) you might see a metric which is called Number of Layers. Now this is the number of transformers that will be involved in handling your input request:

To make this more visual your input will go on a trip inside the LLM on 28 different major destinations, as each one of those will try to relate the keywords, semantics, relationship between different part of the code. Inside each major destinations you also have a set of minor small places that your input visits in parallel, also known as Attention Heads.

Now for this article I wanted to be very concrete, so we are going to write some Python code to see what’s actually happening. You have this script and for some reason you do not know why it’s not working.

solve.py

def add_numbers(a, b):
return a + b
def calculate_total(x, y):
return add_number(x, y)
print(calculate_total(3,4))

Now if you are a vibe coder most likely even the explicit error message would not give you too much hint, but for the context of the example somehow you are a vibe coder running a local small llm.

Just to prove that it’s not working this is the screenshot. This NameError is not something you have seen so far in your life. As a good viber coder now you provide this piece of code to the Qwen local model. Interestingly enough the model is also not aware about such an Error, it only sees numbers. It does not have this concept of Error that you as a human have.

Now this above picture will be transformed into a matrix, before it enters the LLM. Whether it’s a picture/screenshot or pure text in your IDE it really does not matter.

Now of course those images are not generated by me, as Claude is just better at this. But here the most important takeaway is to understand.


Now if you pay attention the deeper we go the better the understanding of the model becomes. Initially it tries to get the keywords of the Python language right. Then it composes the functions and then starts to work on the semantics.


Inside an Attention Head.

Now the next thing that worth to be decomposed ( in a high level overview ) is something called An Attention Head. In each layer you have multiple attention heads working in parallel.

Each head relies on a set of learned weight matrices(Wq, Wk and Wv) optimized during training. Optimized is getting the most efficient numbers so you as an Attention head could do your work with confidence.

Imagine you are an attention head whose sole job is to track pronouns in a sentence:

“The cat didn’t cross the street because it was too tired.”

You do not know what is a cat, you do not know what is ‘it’, but with the trillions of examples you have seen in your training you have reached numbers that helps you a lot to make the final decision that ‘it’ is essentially ‘cat’. Basically there is a very strong correlation here and it’s better to pay attention.

Now when we talk about parameters of the model (in our particular case 7B) some of them are exactly those matrices values spread between different attention heads.


For our chosen model, Qwen2.5-Coder-7B-Instruct-GGUF, the architecture specifies:

  • Attention Heads (GQA): 28 for Queries, and 4 for Keys/Values.

What would be the takeaway ?

Being able to visualize the workflow from an input towards the token distribution is a great way to better understand how internals are working, so that you could build more effective harness around it.

  • Depth: The deeper the layer the better the semantics is.
  • Distributed attention: Dozens of attention heads in each layer act as specialized assistants, making sure no contextual relationship or dependency is missed.

One response to “Decomposing transformers layers and attention heads For better internals”

Leave a Reply