Live data from Hacker News

Revealing example of self-attention, the building block of transformer AI models

github.com

11–20 of 39 posts

Re: Revealing example of self-attention, the building block of transformer AI models

#11
post #9
post #8

Earlier quoted context omitted.

You may be right. I was not expecting this to be at the top of HN UPDATE: I have updated the repo, attempting to address the issue raised by GaggiX

[flagged]

No one here actually checks anything before upvoting it. Having said that, this repo seems worthwhile and I appreciate the effort of OP. It's open source which empowers people to fix such problems themselves and make a PR, or at least an issue.

Re: Revealing example of self-attention, the building block of transformer AI models

#13
post #9

Earlier quoted context omitted.

[flagged]

No one here actually checks anything before upvoting it. Having said that, this repo seems worthwhile and I appreciate the effort of OP. It's open source which empowers people to fix such problems themselves and make a PR, or at least an issue.

The discussions are sometimes more interesting than the submission itself.

Re: Revealing example of self-attention, the building block of transformer AI models

#14

Earlier quoted context omitted.

No one here actually checks anything before upvoting it. Having said that, this repo seems worthwhile and I appreciate the effort of OP. It's open source which empowers people to fix such problems themselves and make a PR, or at least an issue.

The discussions are sometimes more interesting than the submission itself.

Once I got this feedback from GaggiX, I was actually looking in HN for a way to remove my post, but then discovered I could not! So I have tried to fix it as soon as possible.

Re: Revealing example of self-attention, the building block of transformer AI models

#15
post #8
post #5

Wait, is this implementation just wrong? The "num_channel" is the embed_d, the K, Q should be applied to every embedded vector, here the embedded vector has length 1 so K, Q and V should be a single scalar and being applied like torch.matmul(x_i, self.K) [sequence_dim, 1] x [1, 1], this is also intuitive because the sequence can have different size and self attention needs to work anyway. If you want insead treat the…

You may be right. I was not expecting this to be at the top of HN UPDATE: I have updated the repo, attempting to address the issue raised by GaggiX

I made the same mistake recently! It doesn’t help that there are so many poorly documented repos that make similar mistakes (and worse yet, half baked medium articles that blatantly give false information). Directly translating papers to PyTorch is hard too, I really wish there was a better API to express these models in that aligns better with how they’re actually documented in papers.

Re: Revealing example of self-attention, the building block of transformer AI models

#16
post #14

Earlier quoted context omitted.

The discussions are sometimes more interesting than the submission itself.

Once I got this feedback from GaggiX, I was actually looking in HN for a way to remove my post, but then discovered I could not! So I have tried to fix it as soon as possible.

For the best, IMO. That was a great interaction, and anybody running into issues in the future related to this problem actually stand a chance of running into this thread now.

Re: Revealing example of self-attention, the building block of transformer AI models

#17
post #8
post #5

Wait, is this implementation just wrong? The "num_channel" is the embed_d, the K, Q should be applied to every embedded vector, here the embedded vector has length 1 so K, Q and V should be a single scalar and being applied like torch.matmul(x_i, self.K) [sequence_dim, 1] x [1, 1], this is also intuitive because the sequence can have different size and self attention needs to work anyway. If you want insead treat the…

You may be right. I was not expecting this to be at the top of HN UPDATE: I have updated the repo, attempting to address the issue raised by GaggiX

Now that you have updated the code I think you have just flipped the error, it's still there, just doing the opposite (still wrong) thing: for example, if "num_steps" is 768 and "num_inputs" is 1 then K, Q, V is 1x1, okay but the the attention matrix cannot also be 1x1, it needs to be 768x768; you can also do the opposite K, Q, V being 768x768 and the attention matrix 1x1, before the error was that you made both the K, Q, V and the attention matrix 768x768 by applying the matrices on the wrong dimension, now the error is that you have made the the K, Q, V and the attention matrix 1x1.

For a simple fix just make the attention matrix W [num_steps, num_steps] instead of [num_inputs, num_inputs] then "y_i = torch.matmul(x_v_i, w_i)" would be "y_i = torch.matmul(w_i, x_v_i)" and should be correct.

Re: Revealing example of self-attention, the building block of transformer AI models

#18
post #9
post #8

Earlier quoted context omitted.

You may be right. I was not expecting this to be at the top of HN UPDATE: I have updated the repo, attempting to address the issue raised by GaggiX

[flagged]

"Don't be snarky."

"Edit out swipes."

https://news.ycombinator.com/newsguidelines.html

Re: Revealing example of self-attention, the building block of transformer AI models

#19
TBH I don't find this exposition enlightening. If you don't already know the pytorch API, the code will be pretty obscure. And if you do understand the API, it's still not very clarifying.

Instead, it's a lot easier IMO to understand self attention in just plain English. If you understand the words, and also understand the torch API (or tensorflow or numpy, they're all more or less fungible), then it should also be straightforward to implement it.

Here's how I explain it to my grad students:

You have a collection of N things, with some fixed number of features each (doesn't matter how many, but call it d_f). Call that collection {x_i}. You have two learnable matrices Q and K, which learn to project those items into some collection of "questions" and "answers", respectively. Doesn't really matter what those questions or answers are, the NN will figure out what they should be during training.

These projection matrices map x_i -> k_i, and x_i -> q_i. k and q are each d_k-dimensional vectors (d_k is a number you choose) of features, so Q and K are d_k-by-d_f matrices. To measure the "compatibility" between questions q and answers k, we take the dot product of them. Ones which are similar (large, positive dot products) means the NN likes the answer k for question q.

So, here's what you do. For each token i, you compare it's "question" against every other token j's answer. I.e. you compute dot(q_i, k_j), which itself can be considered an NxN matrix of scalar numbers, qk_{ij}. Each element of this matrix contains the answer to the question "how well did item j answer the question of item i?"

Applying softmax to this matrix just converts the dot product values into a score from 0-1 over all the items. I.e., you convert the question to "on a scale from 0-1, how well did item j answer the question of item i?". The only subtlety here is that all the scores over all items j have to sum to 1 for each question i.

Finally, we have a third projection matrix, V, which maps x_j -> v_j. The v_j are d_v-dimensional vectors (doesn't have to equal d_k, and again something you arbitrarily choose). These represent the values that the NN would like to pass on from item j to any other item which decides it "likes" the answer of item j.

So now, for each item i we have a score for "how well does item j answer my question i?". And we also have the list of values v_j that each item j would like to pass to item i. So, to compute the new information to add to item i, we take the weighted sum of the values v_j that item i liked, using these compatibility scores as a weight. In math, this reads: y_i = sum_over_j [ softmax(qk_ij) v_j ]

* Technically it's also empirically found that normalizing the argument of softmax by a factor 1/sqrt(d_k) helps. But it's not really germane to understanding and also not strictly necessary since a NN is of course free to simply learn to include an overall factor proportional to 1/sqrt(d_k) in the parameters of one of the projection matrices. Actually the use of softmax at all is also arbitrary, but encourages the attention to be sparse, which again is empirically known to be helpful.

Re: Revealing example of self-attention, the building block of transformer AI models

#20
post #10
post #7

For context: The latest large language models (LLM) skip doing some complex things that previous good models did, and focus on attention. See: "Attention is all you need" https://arxiv.org/abs/1706.03762

This MIT lecture on recurrent neural networks provides a lot of context on why attention was so groundbreaking, and how things were solved before: https://www.youtube.com/watch?v=ySEx_Bqxvvo And for the Attention Is All You Need paper itself, Andrej Karpathy's "GPT From Scratch" talk is a really great walkthrough of how to make this work in practice, all condensed into two hours of live coding: https://www.youtube.co…

That whole MIT course, Intro to Deep Learning, is great and a really nice introduction to the field.
Post reply on HN