Ask HN: Can someone ELI5 transformers and the “Attention is all we need” paper?

644 points by Sai_ ↗ HN
I have zero AI/ML knowledge but Steve Yegge on Medium thinks that the team behind Transformers deserves a Nobel.

Makes me want to better understand this tech.

Edit: thank you for some amazing top level responses and links to valuable content on this subject.

239 comments

[ 2.5 ms ] story [ 296 ms ] thread
[flagged]
GPT-4 Prompt: Can you ELI5 Transformers and the “Attention is all we need” paper

Sure! Imagine you're in school, and your teacher asks you and your friends to work on a group project. Each of you has a different set of skills, and you need to work together to complete the project successfully. In this analogy, the group project is like a sentence or a piece of text that a language model is trying to understand.

Transformers are a type of model that helps computers understand and generate text, like in our group project. The key idea behind transformers is something called "attention." Attention helps the model figure out which words in a sentence are the most important to focus on, just like you and your friends pay attention to each other's skills to complete your project.

The "Attention is all we need" paper introduced transformers and showed that by using attention, the model can learn how to understand and generate text more effectively. Instead of focusing on each word one at a time, transformers can look at all the words together, decide which ones are the most important, and use that information to understand the text better.

So, in our group project example, transformers help the model work like a team, where everyone pays attention to each other's strengths and helps each other out. This makes the model more powerful and able to do a better job at understanding and generating text.

GPT-4 seems to take "explain like I'm five" quite seriously.
I just asked ChatGPT and this is what I got:

"Imagine you have a really smart computer friend that can understand and talk to you just like a person. This computer friend is called a language transformer.

The language transformer is like a super good reader. It reads lots and lots of books, stories, and articles to learn how people talk and write. It pays attention to how words go together and what they mean in different situations. It becomes really good at understanding what people are saying or asking.

Once the language transformer has learned so much, it can help with many things. For example, if you want to write a story, it can help you come up with ideas and write sentences that make sense. If you have a question, it can try to find the answer for you. It can also help translate words from one language to another, like when you want to understand what someone is saying in a different language.

The language transformer is like a big brain that knows a lot about words and how they fit together. It uses all that knowledge to talk and help you with different things."

Asked to provide some code samples:

""" Imagine you have a sentence: "The cat sat on the mat." In traditional models, each word is processed one by one, and the model might miss important relationships between words that are far apart. Transformers solve this problem using self-attention.

In self-attention, the model pays attention to all the words in the sentence simultaneously, giving more importance to different words depending on their relevance to each other. Here's a code snippet to illustrate self-attention:

    import torch
    import torch.nn as nn

    # Assume we have an input sentence with 5 words
    input_sentence = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5]])

    # Define the self-attention layer
    self_attention = nn.MultiheadAttention(embed_dim=5, num_heads=1)

    # Apply self-attention to the input sentence
    attention_output, _ = self_attention(input_sentence, input_sentence, input_sentence)

    # The attention_output represents the transformed sentence with attention
    print(attention_output)

In this code, we use a simple example with one self-attention head. The input sentence is represented as a tensor with shape (1, 5), where each number represents the word's features. We pass the input sentence to the self-attention layer twice, using it as the query, key, and value inputs.

The self-attention layer computes the attention scores between each word in the sentence, determining how much attention each word should receive from the other words. It then combines the input sentence with these attention weights to produce the attention_output.

The attention_output contains the transformed sentence, where each word now carries information from other words based on their importance. This allows the model to capture relationships and dependencies between words effectively.

The "Attention is All You Need" paper introduced this self-attention mechanism as a fundamental building block in the Transformer model. The Transformer architecture then incorporates multiple layers of self-attention, along with other components like positional encoding, to process sequences and achieve impressive results in various NLP tasks.

Remember, this code snippet is just a simplified illustration to help you understand the concept of self-attention. The actual implementation of Transformers is more complex, involving multiple layers, feed-forward networks, and additional optimizations. """

The nn.MultiheadAttention documentation [1], the output of print(attention_output) is:

    tensor([[ 0.1756, -0.2273, -0.0787,  0.0383, -0.0779]], grad_fn=<SqueezeBackward1>)

If you badger ChatGPT it will give you an example with different query, key, and value inputs

    # Define distinct query, key, and value inputs
    query = torch.tensor([[0.2, 0.3, 0.4, 0.5, 0.6]])
    key = torch.tensor([[0.3, 0.4, 0.5, 0.6, 0.7]])
    value = torch.tensor([[0.4, 0.5, 0.6, 0.7, 0.8]])

    # Apply self-attention to the input sentence using distinct inputs
    attention_output, _ = self_attention(query, key, value)

outputting:

    tensor([[-0.2294,  0.1336, -0.1445, -0.7275, -0.1232]], grad_fn=<SqueezeBackward1>)

Pushed it then further to explain how does nn.MultiheadAttention works:

""" In PyTorch, nn.MultiheadAttention is a module that implements the multi-head attention mechanism. Multi-head attention is a key component of the Transformer architecture, which is widely used in various natural language processing (NLP) tasks.

The nn.MultiheadAttention module allows you to apply multi-head attention to a set of queries, keys, and values. Here's a simplified explanation of what it does:

Inputs: The module takes three inputs: queries, keys, and values. These inputs are typically tensors that represent the encoded representations of the input sequence.

Linear Projections: Before applying attention, the module performs linear proj...

There is no Nobel prize for math or computer science, though...

I worked on a few projects that were trying to develop foundation models for health care, aviation, and other domains. In particular I trained an LSTM model to write fake abstracts for clinical case reports.

We ran into many problems, but maybe the worst one with the LSTM is that a real document repeats itself a lot. For instance, somebody's name might turn up multiple times and the LSTM was very bad at that kind of copying. The LSTM community was arguing about solutions to this problem, but the attention mechanism in transformers makes it easy.

The Yannic kilcher review is quite good.

https://youtu.be/iDulhoQ2pro

I can't ELI5 but I can ELI-junior-dev. Tl;dw:

Transformers work by basically being a differentiable lookup/hash table. First your input is tokenized and (N) tokens (this constitutes the attention frame) are encoded both based on token identity and position in the attention frame.

Then there is an NxN matrix that is applied to your attention frame "performing the lookup query" over all other tokens in the attention frame, so every token gets a "contextual semantic understanding" that takes in both all the other stuff in the attention frame and it's relative position.

Gpt is impressive because the N is really huge and it has many layers. A big N means you can potentially access information farther away. Each layer gives more opportunities to summarize and integrate long range information in a fractal process.

Two key takeaways:

- differentiable hash tables

- encoding relative position using periodic functions

NB: the attention frame tokens are actually K-vectors (so the frame is a KxN matrix) and the query matrix is an NxNxK tensor IIRC but it's easier to describe it this way

I came here to post this video. It’s a great primer on the topic and it gives you ideas to prompt gpt and have it output more.

It’s how I got an understanding of beam search, a technique employed in some of the response building.

I had to look up "tl;dw" and realized it meant "too long; didn't watch" and not my first AI-laden instinct of "too long, didn't write" :-D
One thing that might be worth pointing out is that the transformer architecture owes a great deal of its success to the fact that it can be implemented in a way that it can be massively parallelized in a very efficient manner.
Compared to rnns... maybe? The big nxn is really a killer.

I don't know how to judge parallelizability of different DNN models, you're comparing apples to oranges

When you train a transformer, you're training what the next expected token is. You can train all positions of the sequence each in parallel rather than having to sequentially build up the memory state as you generate the sequence with an LSTM. Mind you the inference portion of a transformer is still sequentially bottlenecked since you don't know what the output sequence is supposed to be.
I believe the other big thing is it allowed you to parallelize the training as well, so you could split those colossal training sets across many machines in a way you could not do with LSTMs.
I don't really believe this because in theory it's possible to split up training of arbitrary neural nets using decoupled neural interfaces (not that it's easy mind you) but at the point where you have openAI scale resources the bottleneck to achieving that is the difficulty of Python.
If you train in parallel, how do you combine the weights generated from different trainings in parallel?
Believe it or not, it's a simple as averaging or adding the gradients of each training result before adding it to the model weights. The same thing happens when you train a model using batches of inputs.
It actually isn't. You have to have a synchronizer, batchsize one or else "strange things" can happen and you waste a lot of cycles. Alternatively you can do non-simple changes to your network structure to enable distributed training.
It really is that simple. Yes, there's many different approaches to this (which can become quite clever and complex, which is true of training in general), but it all really boils down to adding or averaging the gradients in most cases.
How does N relate to the number of parameters that is frequently mentioned?
In my screed, N is the attention width. (How many token it looks at at a time) number of parameters is O(KxNxNxL) where k is the vector size of your tokens, and l is the # of layers. There are other parameters floating around, like in the encoder and decoder matrices, but the NXN matrix dominates.
This is an awesome explanation. You guys are the real heroes
What does it mean for a lookup/hash table to be differentiable?
I wanted to ask the same and especially I've always been wondering: How is the meaning of aforementioned 'differentiable' related to the same term in math?
Same thing. You need everything to be differentiable in order to run gradient descent. To first approximation, training a neural network is just gradient descent.
The neural net is just a math function, continuous even, fully differentiable in all input points. In order to "learn" anything we compute gradients towards the function parameters. They get "nudged" slightly towards a better response, and we do this billions of times. It's like carving a raw stone block into a complex scene. If you put your data into the system it flows towards the desired output because the right path has been engraved during training.

<rant> This explains a bit how neural nets work but from this to chatGPT is another whole leap. You'd have to assign some of the merits of the AI to the training data itself, it's not just the algorithm for learning, but what is being learned that matters. The neural net is the same, but using 1T tokens of text is making it smart. What's so magic about this data that it can turn a random init into a language programmable system? And the same language information make a baby into a modern human, instead of just another animal. </>

No, it's the loss function we differentiate. The input to the loss function are the network weights. The input to the network are samples and those we do not differentiate.
While it's true that we don't differentiate the input samples, we do differentiate the loss function's output with respect to each of the network weights. We use the chain rule to calculate each of these "gradients" and that process is known as backpropagation.

(You might have intended to say this, in which cases I'm just trying to add clarity.)

Not an expert (so this could well be slightly off), but here is a physics analogy:

You're rolling a ball down a plank of wood and you want it to roll off the plank and continue to a certain spot on the ground. You know that if the ball rolls past the target, you have to angle the ramp lower. If it fails to reach the target, you angle the ramp higher. If it goes past by a lot, you make a big change to the ramp angle. If it goes past by a little, you make a small change to the ramp angle. In this way your error tells you something about the adjustment you need to make to the system to produce the desired output.

Think of a function from the ramp angle (network weights) to how far the ball lands from the target (the error). The derivative of this function tells you what kind of change in angle results in what kind of change in the error, and you can use that to update the ramp until you hit the target. To say that the model is differentiable is to say that it's possible to infer from the error what kind of changes you need to make to the weights.

Going to go out on a limb and say they are probably referring to the gradient calculus required for updating the model.

https://en.wikipedia.org/wiki/Differentiable_programming

See automatic differentiation.

Correct, but note that if you subject a standard hash table algo to AD it won't magically become a transformer. (Hashes in the "normal construction" are discrete functions and thus aren't really continuous or differentiable, neither are lookup tables)
Differentiable in this context, means that it can be learned by gradient descent, which uses derivatives to adjust a neural network model's parameters to minimise some error measure. As for how that applies to a hash function, I think the lookup gives some sort of weighting for each possible output, for each input, with the largest weightings corresponding to a "match". But tbh I'm not certain on that last part...
> As for how that applies to a hash function, I think the lookup gives some sort of weighting for each possible output, for each input, with the largest weightings corresponding to a "match"

Note that in general one "matches" more than one thing in which case (IIRC) the result will be a weighted linear combination of the query results.

I do think that this collapses to a datastructure equivalent in function to the "normal hash table" (but not in performance, lol) in the degenerate case where every input yields strictly a single result with zero activation in all other results, so it's not invalid to call it a differentiable lookup table.

You can take a (calculus) derivative of the mathematical construct that represents the lookup table. And yeah, it is very not obvious how a "lookup/hash table" could be differentiable, based on how it's implemented say, in a leetcode exercise. That's part of the genius of the transformer.
The lookup/hashtable can be viewed as a matrix which is accessed by multiplying it with a vector.
It's not really doing hashing, which is random, but instead it makes meaningful connections, like connecting an adjective to its noun, or a pronoun to a previous reference of the name. That is why it is called "Attention is all you need" and not "Hashing is all you need". Attention includes some meaning, it is attentive to some aspect or another.
Hashing does not have to be random, it just has to compress the domain. Modulo number is a perfectly valid hash for some use cases.

Edit: just looked it up, doesn't even have to compress the domain, identity hash is a thing.

I'm not a ML expert but I know a bit about math.

It's "differentiable" in the same way that e.g. the "jump function" (Heaviside step function) is differentiable (not as a function from real numbers to real numbers, but as a distribution). It's derivative is the "point impulse function" (Dirac delta function), which, again, is a distribution, not a real function.

Distributions are nicely defined in math, but can't really be operated with numerically (at least not in the same way as real/float functions), but you can approximate them using continuous functions. So instead of having a function jump from 0 to 1, you "spread" the jump and implement it as a continuous transition from e.g. `0-epsilon` to `0+epsilon` for some tiny epsilon. Then you can differentiate it as usual, even numerically.

Similarly, hash table lookup is a dis-continuous function - the result of `hash.get(lookup)` is just `value` (or `null`). To make it continuous, you "spread" the value, so that nearby keys (for some definition of "nearby") will return nearby values.

One way to do this, is to use the scalar product between `lookup` and all keys in the hashtable (normalized, the scalar product is close to 1 if the arguments are "nearby"), and use the result as weights to multiply with all values in the hashtable. That's what the transformer does.

Thanks for this explanation. I couldn't wrap my mind around the "differentiable hash table" analogy, but "distribution of keys" -> "distribution of values" starts to click.

I'm not an ML expert either but I have taken graduate level courses and published papers with "machine learning" in the title, so I feel like I should be able to understand these things better. The field just moves so fast. It's a lot of work to keep up. Easy-to-digest explanations like this are underrated.

>The field just moves so fast. It's a lot of work to keep up. Easy-to-digest explanations like this are underrated.

This is really the truth. I can't possibly understand how people in this field who are talented can still keep up. I have a binder full of seminal papers that I have to cull to make room for more recent and relevant research every few months. I feel there is a lot of potential in simplifying the details of the mechanisms that drive a lot of it, but nobody has time to stop, consolidate the information and publish it. And if they did, it would just be another outdated textbook in a few years.

What actually happens is that each vector grabs a fraction of each other vector and adds it to itself (simplification omitting some transforms along the way). Equating this with the concept of a hash function does not make sense in my opinion but some authors do it anyway.

It's differentiable because how much of a fraction to grab is the result of a simple dot product followed by a softmax.

I appreciate the explanation, but I don't know what junior-dev would understand most of this. I may be just a web developer, but I couldn't understand most of this. I'd still have to read for 30m to grok it all.
Yeah sorry, it still requires math and probably some exposure to ML basics.
I think one hole in the description for simplicity is that "differentiable" it's not an adjective that applies to hash tables.

Differentiable relative to what? What is (x) in the d(hashtable)/d(x) equation?

I think it applies because lookups can be done by multiplying one-hot vectors (or masks) with matrices; this is roughly analogous to what happens when we multiply Q with K^T in a self-attention head.

Read this: https://e2eml.school/transformers.html#table_lookup

And then read: https://e2eml.school/transformers.html#attention

Matrix multiplication is differentiable as it is continuous. Therefore you can calculate partial derivatives of these operations. The ability to do that is what allows gradient descent optimization via the chain rule.

  > Differentiable relative to what?
  > What is (x) in the d(hashtable)/d(x) equation?
I think the derivative we actually care about is `∂L/∂(hashtable-lookup)` but here the `hashtable-lookup` is actually the matrix multiplication mentioned above. We want to know how much the "loss" (or error) changes with respect to each of our "lookups". Knowing how each of our "lookups" causes our model to err from the output we expect, allows us to optimise it.

Note: it's not really a "lookup" in the normal sense of the word. Hashtable lookups are discontinuous since we either have a value for a particular key or we do not. Our matrix multiplication is sort of like a fuzzy, probabilistic, continuous form of lookup in which nearby keys get partially activated based on their similarity to the query, and in which a small change in this query causes continous changes to the keys produced.

As far as my understanding of the self-attention equation (e.g. `softmax(QK^T / sqrt(d_k))V`) goes, its actually quite important that we get this fuzzy output in which lots of keys get partially activated for a particular query. If it only picked the maximum similarity and ignored the rest, there would be less information propagating through the network and it'd be harder for the network to learn relationships/interactions between inputs. This is why we scale `QK^T` by `sqrt(d_k)` in order to pass a tighter range of values into the `softmax()` function (which importantly generates probabilities that sum to 1, but contains exponentials which give it a tendency to over-emphasize the maximum value and ignore other values if they are too far apart).

That's exactly the point, though! It's surprising. A hashtable is a map from keys to values. Making it differentiable means that a small change in the key also makes a small change in the value!
differentiable relative to the model parameters. The attention mechanism does store(key(x), value(x)), followed by lookup(query(y)), where key(), value(), lookup(), query() are all designed to be differentiable.
what is a differentiable hash table? I understand differentiation, I don't understand differential hash table ... does that mean for every tiny gradient of a key, a corresponding gradient in value can be expected?
Having read the paper myself, I'm impressed with the quality of your explanation. Well done!
Check this explanation from Karpathy which provides really good intuition https://youtu.be/9uw3F6rndnA

Basically tokens “talk” to each other and say this is what i have and this is what i look for.

Your summary of this is very good. It’s a really simple and intuitive way to express something very complex.
ELI5 is tricky as details have to be sacrificed, but I'll try.

An attention mechanism is when you want a neural network to learn the function of how much attention to allocate to each item in a sequence, to learn which items should be looked at.

Transformers is a self-attention mechanism, where you ask the neural network to 'transform' each element by looking at its potential combination with every other element and using this (learnable, trainable) attention function to decide which combination(s) to apply.

And it turns out that this very general mechanism, although compute-intensive (it considers everything linking with everything, so complexity quadratic to sequence length) and data-intensive (it has lots and lots of parameters, so needs huge amounts of data to be useful) can actually represent many of things we care about in a manner which can be trained with the deep learning algorithms we already had.

And, really, that's the two big things ML needs, a model structure where there exists some configuration of parameters which can actually represent the thing you want to calculate, and that this configuration can actually be determined from training data reasonably.

Who is Yegge and why should I care? All I found on google is that he wrote code for google and blogs.
He's an engineers who's been involved (or close to) some of the most influential technologies/products in use in the last 10 years, and his blog posts are quite celebrated and respected.

Think of him as an essayist. Like Paul Graham (pg on this site)

He's been a popular tech writer for a couple of decades. He's written some very influential pieces in the past - here's a copy of one of his most famous (I don't think the original is available any more): https://gist.github.com/chitchcock/1281611
> (I don't think the original is available any more)

That's because this one was not a blog post, it was a Google+ post that he intended to share internally with Google only. He had other great rants on his actual blog though, about (vague memory here) Emacs, game development, language design, etc.

I first heard about him after an internal post/rant of his about Google+ got leaked and went viral.

My impression is that he is a Kardashian - famous for being famous - in the software world.

"Transformers" and "Attention is All You Need" refer to an important development in machine learning and artificial intelligence, particularly in the field of natural language processing (NLP). I'll try to explain them in a simple way.

Think of a conversation you had with a friend. While they were talking, you were probably not just listening to the words they were saying right now, but also remembering what they said a few minutes ago. Your brain was connecting the dots between different parts of the conversation to understand the full meaning. Now, imagine if you could only understand each word in isolation and couldn't remember anything from a few seconds ago. Conversations would be pretty hard to understand, right?

In early NLP models, this was a big problem. They couldn't easily look at the "context" of a conversation or a sentence. They could only look at a few words at a time, so they were a bit like our forgetful person. They were good at understanding the meaning of individual words, but not so good at understanding how those words fit together to create meaning.

Enlightening example of having a conversation. Makes thing clearer.
Did you use GPT to write this? (Not a bad thing! It's a decent answer)
Immediately thought this was gpt as well.

Assuming prompt was "Explain Transformers and 'Attention is all you need' in a simple way"

I copy pasted the attention is all you need paper into ChatGPT4 and gave it the prompt "Explain like I'm 5 years old".

The Transformer is a new type of computer program that helps translate languages and understand sentences. It works by paying attention to different parts of a sentence at the same time, instead of looking at one word after another like older programs. This makes it faster and better at understanding complicated sentences. It has been tested on translating English to German and English to French and did a really good job.

(Edit)

AFAIK Transformers and context size are orthogonal concepts. You could have large token contexts before. The transformer directs the “attention” to a specific word/token inside the context.
It helps to start with recurrent neural networks first, since those were the previous standard way of doing next-token-prediction. They worked, but training them was extremely slow because it couldn't be parallelized. Transformers are a way of getting mostly the same capabilities as RNNs but with a parallelizable architecture so you can actually train it with huge parameter numbers in a reasonable amount of time.
I agree it's very useful to look at RNNs. Even more because attention mechanisms were already getting popular in LSTM/GRU models before the transformers paper. One of the main insights of the transformer papers is that you can do with just an attention attention and you do not need the recurrent part of the network anymore (hence the title "Attention is all you need").
Not really an ELI5, but it you really want to understand the topic, I've gathered the best resources I could find to help me do the same: https://blog.senko.net/learn-ai

If you're a programmer, start with Karpathy's video series. For a somewhat gentler intro, take a look at the MIT intro lectures first to build up on the fundamentals.

Then you're ready for The Illustrated Transformer, and afterwards, if you're brave enough, the Annotated Transformer.

It's a fascinating subject, more so when you have a solid grasp! And you'll be able to quickly spot people who kinda stumble they way through but have big gaps in understanding.

It’s not really something you need to understand unless you’re an ML researcher.

I guess the ELI5 (with a BUNCH of details left out) is “Transformers: what if you didn’t have to process sentences as a sequence of words, but rather as a picture of words.”

>t’s not really something you need to understand unless you’re an ML researcher

What kind of an answer is this? No it’s something you should understand if you are at all interested.

It’s like saying “I have no basis in differential equations or complex numbers or Newtonian mechanics or experimental findings on the wavelike nature of light but could somebody explain the Schrödinger equation derivation like I’m five?”

Like, there’s a reason we don’t explain things to five year olds.

Also I have a good ELI5 answer, so I don’t know what else you want dude lol so petty

From the Yegge post:

> LoRA makes LLMs composable, piecewise, mathematically, so that if there are 10,000 LLMs in the wild, they will all eventually converge on having the same knowledge. This is what Geoffrey Hinton was referring to on his SkyNet tour.

I don't think that's right at all, LoRA freeze lots of the large model part and wouldn't let you just simply combine large models. Instead. I'm pretty sure Hinton is referring to data parallel training with batching:

> DataParallel (DP) - the same setup is replicated multiple times, and each being fed a slice of the data. The processing is done in parallel and all setups are synchronized at the end of each training step.

https://huggingface.co/docs/transformers/v4.15.0/parallelism

You can have many instances of the model training on different bits of data, and then just average the modified weights back together at the end. This combining of weights is what Hinton means when he says parallel copies of brains can learn things independently and then recombine them later a huge bandwidth speeds, whereas humans are far more limited to sharing separate experiences verbally or with like a multimedia presentation or something.

(comment deleted)
Transformers are about converting some input data (usually text) to numeric representations, then modifying those representations through several layers to generate a target representation.

In LLMs, this means go from prompt to answer. I'll cover inference only, not training.

I can't quite ELI5, but process is roughly:

  - Write a prompt
  - Convert each token in the prompt (roughly a word) into numbers.  So "the" might map to the number 45.
  - Get a vector representation of each word - go from 45 to [.1, -1, -2, ...]. These vector representations are how a transformer understands words.  
  - Combine vectors into a matrix, so the transformer can "see" the whole prompt at once.
  - Repeat the following several times (once for each layer):
  - Multiply the vectors by the other vectors.  This is attention - it's the magic of transformers, that enables combining information from multiple tokens together.  This generates a new matrix.
  - Feed the matrix into a linear regression.  Basically multiply each number in each vector by another number, then add them all together.  This will generate a new matrix, but with "projected" values.
  - Apply a nonlinear transformation like relu.  This helps model more complex functions (like text input -> output!)
Note that I really oversimplified the last few steps, and the ordering.

At the end, you'll have a matrix. You then convert this back into numbers, then into text.

I don't think this description of attention is correct.
You mean "Multiply the vectors by the other vectors. This is attention - it's the magic of transformers, that enables combining information from multiple tokens together. This generates a new matrix."?

It's really oversimplified, as I mentioned. A more granular look is:

  - Project the vectors with a linear regression.  In decoder-only attention (what we usually use), we project the same vectors twice with different coefficients.   We call the first projection queries, and the second keys.  This transforms the vectors linearly.
  - Find the dot product of each query vector against the key vectors (multiply them)
  - (training only) Mask out future vectors, so a token can't look at tokens that come after it
  - At this point, you will have a matrix indicating how important each query vector considers each other vector (how important each token considers the other tokens)
  - Take the softmax, which both ensures all of the attention values for a vector sum to 1, and penalizes small attention values
  - Use the softmax values to get a weighted sum of tokens according to the attention calc.
  - This will turn one vector into the weighted sum of the other vectors it considers important.
The goal of this is to incorporate information from multiple tokens into a single representation.
Thanks to everyone in this thread for all the great links and explanations!!
I'll throw my hat in the ring.

A transformer is a type of neural network that, like many networks before, is composed of two parts: the "encoder" that receives a text and builds an internal representation of what the text "means"[1], and the "decoder" that uses the internal representation built by the encoder to generate an output text. Let's say you want to translate the sentence "The train is arriving" to Spanish.

Both the encoder and decoder are built like Lego, with identical layers stacked on top of each other. The lowest lever of the encoder looks at the input text and identifies the role of individual words and how they interact with each other. This is passed to the layer above, which does the same but at a higher level. In our example it would be as if the first layer identified that "train" and "arrive" are important, then the second one identifies that "the train" and "is arriving" are core concepts, the third one links both concepts together, and so on.

All of these internal representations are then passed to the decoder (all of them, not just the last ones) which uses them to generate a single word, in this case "El". This word is then fed back to the decoder, that now needs to generate an appropriate continuation for "El", which in this case would be "tren". You repeat this procedure over and over until the transformer says "I'm done", hopefully having generated "El tren está llegando" in the process.

The attention mechanism already existed before transformers, typically coupled with an RNN. The key concept of the transformer was building an architecture that removed the RNN completely. The negative side is that it is a computationally inefficient architecture as there are plenty of n^2 operations on the length of the input [2]. Luckily for us, a bunch of companies started releasing for free giant models trained on lots of data, researchers learned how to "fine tune" them to specific tasks using way less data than what it would have taken to train from scratch, and transformers exploded in popularity.

[1] I use "mean" in quotes here because the transformer can only learn from word co-occurrences. It knows that "grass" and "green" go well together, but it doesn't have the data to properly say why. The paper "Climbing towards NLU" is a nice read if you care about the topic, but be aware that some people disagree with this point of view.

[2] The transformer is less efficient that an LSTM in the total number of operations but, simultaneously, it is easier to parallelize. If you are Google this is the kind of problem you can easily solve by throwing a data center or two at the problem.

> The negative side is that it is a computationally inefficient architecture as there are plenty of n^2 operations on the length of the input

Is this the reason for the limited token windows?

Yes, kinda. The transformer doesn't have a mechanism for dynamically adjusting its input size, so you need to strike a balance between the window being big enough for practical purposes but also small enough that you can still train the network.

Previous networks with RNNs could in theory receive inputs of arbitrary size, but in practice their performance decreased as the input got longer because they "forgot" the earlier input as they went on. The paper "Neural Machine Translation by Jointly Learning to Align and Translate" solved the forgetting problem by, you guessed it, adding attention to the model.

Eventually people realized that attention was all you needed (ha!), removed the RNN, and here we are.

I have some background but I'm probably not the best person in the world to explain.

The important thing about the transformers model is that it's the first one we have found which keeps unlocking more and more powerful and general cognitive abilities the more resources we throw at it (parameters, exaflops, datasets). I saw some interview with Ilya Sutskever where he says this; it almost certainly won't be the last or best one, but it was the first one.

--

Why was it the first one? How were these guys so clever and other ones couldn't figure it out?

OK so first you need some context. There is a lot of 'Newton standing on the shoulders of giants' going on here. If all of these giants were around in the 1970s, it probably would have been invented then. Heck for all we know something as good was invented in the 1970s but our computers were too smol to benefit from it. This is what John Carmack is currently looking into.

To really notice the scaling benefits of the transformer architecture, they needed to run billion parameter transformer models on linear-algebra-accelerating GPU chips using differentiable programming frameworks. These are some of the giants we are standing on. The research and development pipeline for these amazing GPUs like [thousands of tech companies -> ASML -> TSMC -> NVIDIA] didn't exist until not so long ago. The special properties of transformers wouldn't have been discovered so soon without this hardware stack.

Another giant we are standing on is the differentiable programming linear algebra libraries and frameworks similar to theano or tensorflow or pytorch or jax. They have had things like this under the name 'mathematical programming' like CPLEX but it wasn't as accessible. 'Differentiable programming' is a newish terminology for what used to be called 'automatic differentiation' where 'differentiation' means essentially the same as calculus derivative. Informally it means that these libraries can predict any tiny output effect of any tiny input change as a computationally cheap side-effect of computing the given output, even for complicated calculations. This capability makes optimization easier, in particular it generalizes the 'backpropagation' algorithm of traditional artificial neural networks.

--

What is the transformer model in more nerdy terms.

At one level, it's just a complicatedly parameterized function, where you can fit the parameters by training on data. This viewpoint puts the importance on the computational power applied to training the model with the advantage of differentiable programming. Some will probably guess that the details of the model architecture don't really matter as long as it has sickening amount of parameters and exaflops and dataset. Some version of this viewpoint is probably true in my opinion.

More specifically, the transformer architecture is like a chain of black box differentiable 'soft' lookup tables. The soft queries and keys and values are each lists of floating point numbers (for example a single soft query is a list of numbers, called a vector) and these vectors are stacked into matrices and the soft lookup is processed quickly with fast matrix multiplication tricks. Importantly, all of this is happening inside of a differentiable programming framework which lets you cheaply answer questions about how any small change to the input will affect the output. This capability is used for training, by making trillions of billions of tiny changes to the floating point numbers in the multiplication matrices in the boxes. At the end, the fully trained chain of black box functions can be used to compute a probability distribution over the next token in the message, which lets you generate messages or translate between languages or whatever.

Feedforward: y=Wx

Attention: y=W(x)x

W is Matrix, x & y Are vectors. In the second case, W is a function of the input.

You must be from a planet with very long years!

There is no way I can even begin to digest what you have said in your comment.

Sorry maybe I should have added more explanation. One way to think about attention, which is the main distinguishing element in a transformer, is as an adaptable matrix. A feedforward layer is a matrix with static entries that do not change at inference time (only during training). The attention mechanism offers a way to have adaptable weight matrices at inference time (this is implemented by using three different matrices, K,Q & V called keys query and value in case you want to dig deeper).
I think in your notation it should have been:

y=Wx_0

y=W(x)x_0

I guess I was more thinking about self attention, so yes. The more general case is covered by your notation!
[flagged]
Maybe ELI5 was the wrong level for my ask. I still want to understand it from a technical perspective.

Maybe ELI am a software engineer but not in AI/ML?

*Prompt:* I'm a software engineer who has never done anything with AI/ML. Explain to me transformers and the "Attention is all you need" paper.

*GPT4:* "Attention is All You Need" is a seminal paper published in 2017 by Vaswani et al. that introduced the Transformer architecture, a breakthrough in the field of AI and natural language processing (NLP). Transformers have since become the foundation for many state-of-the-art models, including GPT (Generative Pre-trained Transformer) series, BERT (Bidirectional Encoder Representations from Transformers), and more.

At a high level, the Transformer architecture is designed to handle sequential data, like text, in parallel rather than sequentially, as was common with earlier models like Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks. This parallelization allows for faster training and better scalability. The key innovation in Transformers is the attention mechanism, which is used to weigh the importance of different input tokens when generating an output token.

Here's a simplified explanation of the main components of a Transformer:

    Input Embeddings: Transformers take input data in the form of text tokens. These tokens are converted into continuous vectors through an embedding layer.

    Positional Encoding: Unlike RNNs and LSTMs, Transformers don't have an inherent sense of sequence order. To address this, positional encoding is added to the input embeddings to provide information about the position of each token in the sequence.

    Encoder and Decoder: The Transformer architecture consists of an encoder and a decoder. The encoder is a stack of identical layers, each containing two primary components: a multi-head self-attention mechanism and a position-wise feed-forward network. The decoder is also composed of a stack of identical layers, but it has an additional multi-head attention mechanism that attends to the encoder's output.

    Multi-Head Attention: The attention mechanism is the core innovation in the Transformer architecture. It computes a weighted average of input tokens to generate context-aware representations. Multi-head attention allows the model to focus on different aspects of the input by employing multiple attention heads, each with its own set of learnable parameters.

    Feed-Forward Networks: These are simple fully connected layers that follow the attention mechanism in each layer of the encoder and decoder.

    Output: Finally, the decoder generates the output sequence token by token. This is done by applying a linear layer followed by a softmax activation function to get the probabilities of each possible output token.
In summary, the Transformer architecture introduced in the "Attention is All You Need" paper revolutionized NLP by enabling more efficient and powerful models. The key innovation is the attention mechanism, which helps the model learn complex relationships between input tokens and produce context-aware representations. This has led to a wide array of applications, from machine translation and text summarization to sentiment analysis and language modeling.

*Me:* It's okayish but you're probably interested in a more detailed low level explanation. Also transformers are widely used for computer vision nowadays (a bit forgivable because of ChatGPT 2021 cutoff, but they had already taken off by then)

Absolute BS. Transformers were literally introduced by that paper.
> I have zero AI/ML knowledge

This may make it difficult to explain and I already see many incorrect explanations here and even more lazy ones (why post the first Google result? You're just adding noise)

> Steve Yegge on Medium thinks that the team behind Transformers deserves a Nobel

First, Yegge needs to be able to tell me what Attention and Transformers are. More importantly, he needs to tell me who invented them.

That actually gets to our important point and why there are so many bad answers here and elsewhere. Because you're both missing a lot of context as well as there being murky definitions. This is also what makes it difficult to ELI5. I'll try, then try to give you resources to get an actually good answer.

== Bad Answer (ELI5) ==

A transformer is an algorithm that considers the relationship of all parts of a piece of data. It does this through 4 mechanisms and in two parts. The first part is composed of a normalization block and an attention block. The normalization block scales the data and ensures that the data is not too large. Then the attention mechanism takes all the data handed to it and considers how it is all related to one another. This is called "self-attention" when we only consider one input and it is called "cross-attention" when we have multiple inputs and compare. Both of these create a relationship that are similar to creating a lookup table. The second block is also composed of a normalization block followed by a linear layer. The linear layer reprocesses all the relationships it just learned and gives it context. But we haven't stated the 4th mechanism! This is called a residual layer or "skip" layer. This allows the data to pass right on by each of the above parts without being processed and this little side path is key to getting things to train efficiently.

Now that doesn't really do the work justice or give a good explanation of why or how things actually work. ELI5 isn't a good way to understand things for usage, but it is an okay place to start and learn abstract concepts. For the next level up I suggest Training Compact Transformers[0]. It'll give some illustrations and code to help you follow along. It is focused on vision transformers, but it is all the same. The next level I suggest Karpathy's video on GPT[1], where you will build transformers and he goes in a bit more depth. Both these are good for novices and people with little mathematical background. For more lore and understanding why we got here and the confusion over the definition of attention I suggest Lilian Wang's blog[2] (everything she does is gold). For a lecture and more depth I suggest Pascal Poupart's class. Lecture 19[3] is the one on attention and transformers but you need to at minimum watch Lecture 18 but if you actually have no ML experience or knowledge then you should probably start from the beginning.

The truth is that not everything can be explained in simple terms, at least not if one wants an adequate understanding. That misquotation of Einstein (probably originating from Nelson) is far from accurate and I wouldn't expect someone that introduced a highly abstract concept with complex mathematics (to such a degree that physicists argued he was a mathematician) would say something so silly. There is a lot lost when distilling a concept and neither the listener nor speaker should fool themselves into believing this makes them knowledgeable (armchair expertise is a frustrating point on the internet and has gotten our society in a lot of trouble).

[0] https://medium.com/pytorch/training-compact-transformers-fro...

[1] https://www.youtube.com/watch?v=kCc8FmEb1nY

[2]

As usual, the most helpful answer is burried among desperate platitudes full of inaccuracies trying to pander to the absurd reddit-esque ELI5 notion you've dismantled.
What is ELI5?

The idea behind the Transformer is nice - but by far not Nobel prize deserving.

Don't believe the hype or people like Yegge, whoever that is - in a few years a new architecture will be the "Nobel candidate".

Also, the original Transformer paper, if you read is, is horribly written.

Nobel prize for the neurel network or perceptron maybe?
Something seems Nobel related for the tech to be so useful.
Cosma Shalizi explains it very well.

"Again: Calling this "attention" at best a joke."

http://bactra.org/notebooks/nn-attention-and-transformers.ht...

Thanks a lot for sharing. I liked the explanation, although it seems to be not a perfect matching between attention and kernel (as far as I understand). Since as I undestand x_o = Qx and x_i = Qx and y_i = Vx but it doesn't map to Wu dot Wv.

Anyway just curious do you or someone else have more sources in this format?

At least this way it is very helpfull to think about the methods. Kind of agree that the formular does look very very similar to well known methods. But on the other hand, the author doesn't explain the transformers in a similar simple way since it is not obvious why you could stack kernels and get better results.

Let the model decide which words to focus on when producing a response