← All posts

From Token Ids to attention Scores

So today i spent my time understanding how does an LLM take normal token values and converts them to attention scores.

So today i spent my time understanding how does an LLM take normal token values and converts them to attention scores. Lets walk through what we can learn

1. Token IDs become embeddings

LLM don’t just take raw words, they convert them into embeddings. The first step an LLM does is tokenization so in general you have a tokenizer in front of this which i will talk about in another blog. For now just know that the first step is mapping these token ids from an embeddings lookup table. For the purposes of my learning experiment i used random values from my embeddings

import torch

token_ids = torch.tensor([0, 3, 7])
layer = torch.nn.Embedding(8, 2)

x = layer(token_ids)
print(x.shape)  # [3, 2]

so basically after we select the embedding the output that we get is a set of values whose shape is determined by the shape of the embeddings so say if we use 4 values to be the output of the embeddings each token id would be represented by 6 values.

2. Transforming embeddings into Q, K, and V

Next we take those embeddings and convert them into projections and there are three formulas we care about:

The matrices Wq, Wk and Wv are all learned during training and in our case we take them as arbitrary values because we have not trained our model.

Each projection applies a matrix multiplication to the values. For example, our embedding tensor has shape [3, 2]. If our projection weight matrix has shape [2, 64], the output will have shape [3, 64]. The important part to remember here is this is not simply a dimensional change but rather a functional change because it is used to transform the embeddings into different representations that the attention mechanism uses.

2. Calculating attention scores

The final part of this is the attention score calculation, we take the K and Q values and then multiply them to get the values. this is the formula we used

So we take the transpose of K and multiply it by Q which is the projection that we got earlier our example, both Q and K have a shape of [3, 2], representing three tokens with two features each. After transposing K, its shape becomes [2, 3].Multiplying [3, 2] by [2, 3] gives us an attention score matrix of shape [3, 3].

These are raw scores. We haven’t yet applied scaling, causal masking, or softmax to turn them into attention weights.

What clicked

Three things stood out for me today

  1. Tensor shapes matter. Understanding matrix dimensions makes the operations much easier to follow.

  2. Weights and runtime activations are different. The model’s learned projection matrices stay fixed during normal inference, while Q, K, and V depend on the input.

  3. Attention starts with matrix multiplication. The QKᵀ operation computes compatibility scores between token positions.

There’s still more to implement: scaling, causal masking, softmax, and using attention weights to combine the value vectors and eventually, KV caching understanding why inference engines avoid recomputing keys and values for previously processed tokens.but today I went from token IDs to a working attention score matrix.

The next goal is understanding how those raw scores become actual attention outputs.

Views —

Read next

Oct 31, 2026Are you avoiding something ?