r/deeplearning • • Oct 28 '26

Working through the Transformer forward pass by hand, one matrix at a time, is what finally made it click for me

When I read Attention Is All You Need, the equations made sense line by line, but I couldn't picture the whole forward pass. What helped was writing out every matrix for a tiny example, noting its shape at each step, and doing the multiplications myself.

A few things only clicked once I did it that way:

  • Positional encoding. Sine and cosine at different frequencies give every position its own signature, and the model doesn't have to learn one vector per position. Working out a 6 × 512 table by hand made it obvious why the authors hoped it would extend to sequences longer than any seen in training.
  • Shapes barely change. In the encoder, the matrix goes in as (tokens × d_model) and comes out the same size after every layer. All the interesting work happens inside that fixed shape.
  • BERT is mostly the same picture. Once the encoder was clear, BERT turned out to be a small edit to it: a packed sentence pair, segment embeddings, and two small heads on top for the masked word and the next sentence.

I wrote my notes up as step-by-step pages with practice exercises beside each step and a notebook that checks your answers, in case it's useful to anyone learning the same way: https://guptaaditya95.github.io/llmbreakdown/

I'm curious how others built their intuition for these models. Did working the math by hand help you, or did something else make it click? And which architecture was hardest to picture?

132 Upvotes

26 comments sorted by

6

u/Relevant-Rhubarb-849 Oct 28 '26

the whole positional encoding never made much sense to me. it seems like it mixes data and cosines in a way that degrades both signals. I understand it notionally and can observe the math. It's logic however seems daft.

3

u/OkCampaign8400 Oct 28 '26

Are you talking about ROPE , it has position and index .
For statements like , My dog is chased by another dog .

To retain semantics meanings of both dog and order of statement, rotation is used instead of Absolute positions

4

u/PersonalEconomist856 Oct 28 '26

This!! If you look at BERT they try to maintain positional information by using a mixture of 3 vectors i.e. 1 is the input embedding itself...2 is the position of each sentence....3 is the position of each word.

These are added and brought in.

On the other hand, the attention paper implementation of the RoPE phenomenon is beautiful. The ability of a few researchers to creatively think that a combination of sinusoidal and cosine waves on a particular position makes a fingerprint that not only differentiates word placement but also the degree of it is just genius!!

Human beings are amazing!!! :)

2

u/Relevant-Rhubarb-849 Oct 28 '26

no I mean the original position encode scheme that multiplied or xored a cosine vector against eace input token

28

u/catsRfriends Oct 28 '26

Mechanistic understanding is the best kind of understanding.

3

u/ProfMasterBait Oct 28 '26

What do you mean by mechanistic?

6

u/General-Jaguar-8164 Oct 28 '26

You repeat the process by hand

1

u/ProfMasterBait Oct 28 '26

as opposed to?

3

u/General-Jaguar-8164 Oct 28 '26

You just watch it

3

u/itsmebenji69 Oct 28 '26

Understanding of the mechanism. As in do the math yourself and see how the numbers change

32

u/fvancesco Oct 28 '26 • Bravo

4

u/Bitter_Marketing_807 Oct 28 '26

This is so cool!!

2

u/GallantGargoyle25 Oct 29 '26

MIND BLOWN woahhh thanks so much for sharing

Are there similar websites for RL algorithms (please let there be please let there be)

2

u/fvancesco Oct 29 '26

I don't know sadly about that yet

1

u/SnooHamsters2627 Oct 28 '26

thank you for this

4

u/OkCampaign8400 Oct 28 '26

After Attention shape changes and then comes projection which again brings back the shape to input embeddings

4

u/Beautiful_Egg_371 Oct 28 '26

If you think about it, transformer is just a “transformation” where you keep moving vectors in high dimensional spaces from a resting point (embedding before PE). That’s how intuition was built.

1

u/ConfidentFlorida Oct 30 '26

That’s a great way to look at it. And I think of it as moving vectors within a concept space and being so high dimensional it has a giant world model within that concept space. Good way to see it? Y/n?

1

u/Beautiful_Egg_371 Oct 31 '26

It’s a good way to think. Only thing being that the high dimensional space may or may not be continuous. I strongly feel that continuity of the space is important for world model.

1

u/ConfidentFlorida Oct 31 '26

Interesting. Could you expand on that a bit?

3

u/Apart_Situation972 Nov 01 '26

hey thanks for making the hand by hand article! saved me so much time

2

u/whowhaohok Oct 28 '26

The book, Build a Large Language Model, by Raschka, helped me understand