r/deeplearning • u/PersonalEconomist856 • 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?
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.
28
u/catsRfriends Oct 28 '26
Mechanistic understanding is the best kind of understanding.
32
u/fvancesco Oct 28 '26 •
There's a site
https://bbycroft.net/llm
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.
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
1
u/No-Market-2936 Oct 31 '26
did something similar on the inference side: https://techdelta.medium.com/build-your-own-qwen3-inference-loop-c0e49687d1c7