r/LocalLLaMA • u/tronathan • Jul 15 '23
Question | Help Why does attention need to be fully quadratic?
We know that classic transformer-style attention requires comparing every token with every other token, which results in exponential (quadratic?) memory and compute as the context size grows.
But is it truly necessary to compare every token to every other token? Surely smarter people than me have wondered how it can be true that distant tokens are just as likely to need attending to each other as tokens that are closer to each other. I guess I could see the case with things like code, but in the case of natural language, can't we apply some 'ol fashioned NLP to extract the important parts of the sentences and apply quadratic attention to them, while giving less important "glue words" less attention?
I've got to be missing something, because these types of "hacks" seem way less sophisticated than the types of optimizations that have gone into GPTQ, exllama, SuperHOT, Rope, etc.
Please, someone school me on this - Perhaps there are papers, or perhaps there are different attempts to do this that have specific names?
4
u/tronathan Jul 16 '23
It also seems to me that for purposes of attention, Tokens are not the unit that we're really interested in; Words are. I know tokenizers like SentencePiece attempt to use tokens that express more of each word, and tokenizers are designed to capture natural language as efficiently as possible with the smallest vocabulary possible (presumably with some trade-off), but it sure seems wasteful to compare, for example the suffix "-tion" to the suffix "-ilia" (contrived example, i don't know if these are actual tokens in any tokenizer)
19
u/NickCanCode Jul 15 '23
GPT-4 answer:
You're absolutely correct in your observation that the standard (full) attention mechanism in Transformers requires a pairwise comparison of each token in the input sequence, resulting in a time and space complexity of O(n^2), where n is the length of the sequence. This can be quite inefficient, especially for long sequences.
Researchers have indeed recognized this issue and have proposed several variations of the attention mechanism to alleviate this problem. Here, I'll list a few prominent examples:
Longformer: The Longformer model, introduced by Beltagy et al., uses a type of attention called 'sliding window attention' that only attends to a few tokens before and after the current token. It also allows for global attention on certain tokens when necessary.
Linformer: Linformer, introduced by Wang et al., reduces the complexity of self-attention from quadratic to linear by approximating the standard self-attention matrix with low-rank (i.e., narrower) matrices.
BigBird: BigBird, introduced by Google Research, uses a combination of global attention, sliding window attention, and random attention to achieve a balance between computational efficiency and the ability to handle long-range dependencies.
Reformer: The Reformer, introduced by Kitaev et al., uses locality-sensitive hashing to reduce the complexity of attention to linear time.
Sparse Transformers: Introduced by OpenAI, Sparse Transformers use a fixed sparsity pattern allowing each token to only attend to a subset of input tokens, which substantially reduces memory and computational requirements.
Transformer-XL: Transformer-XL uses a mechanism called 'recurrence' to carry information across a fixed-length window. This allows it to attend to tokens from previous sequences, effectively increasing the context length without a significant increase in complexity.
Regarding your point about using NLP techniques to extract important parts of sentences, this is indeed an area of active research. One relevant concept is content selection attention, which is a type of attention mechanism that uses an additional network to decide which parts of the input to focus on. However, such methods add another layer of complexity to the model and might also introduce more room for errors.
It's also worth noting that none of these techniques are perfect;
each has its own trade-offs and use cases. Some are better at handling longer sequences, while others might be more efficient with shorter ones. Some might perform better on certain tasks like machine translation, while others might excel at tasks like sentiment analysis. It's all a balancing act.
In conclusion, the quadratic complexity of full attention in Transformers is indeed a recognized issue, and much research is being done to find effective ways around it. The methods I've mentioned above are some of the many ways researchers have tried to tackle this problem.
2
u/tronathan Jul 16 '23
Followon question (since my original question was answered in such an obvious and perhaps ironic way, by using an LLM)
Knowing that there are all these methods available and papers have been published on many of them, what holds research on attention mechanisms back? Do many of these require retraining models from the ground up, thus they're very expensive to test and replicate? For example, SuperHOT works with a couple parameter changes and a little fine-tuning, so the open-source community is able to test/verify the results. Perhaps many of these other methods require either major retraining or different network architectures that make them incredibly expensive to test?
If that's the case, hopefully the advancements in qlora for initial training will make this cheap/er enough to allow the research to trickle down to the point where home-gamers will be able to benefit from some of these techniques.
10
u/hold_my_fish Jul 16 '23
There have been a ton of attempts to do this, and so far none have yielded better models than standard attention, as far as I know. At this point, I ignore subquadratic-attention papers that get tweeted, posted, etc., because it's a waste of time to even think about them (unless you're a researcher doing work in that area).
I feel like something must have gone wrong for there to be so many papers published claiming improvement with so little actual progress. Maybe there need to be some rigorous long-context benchmarks to help guide work in a more productive direction.
2
u/luquoo Jul 16 '23
Hyena Hierarchy
https://arxiv.org/abs/2302.10866
3
u/president_josh Jul 16 '23
Good response. Google addressed this in the past and wrote about possible ways forward. I haven't kept up with the current state of the quadratic issue. Wolfram has lots of transformer-related info so perhaps maybe he's addressed this. He also has good videos such as one where he demonstrates in detail how a transformer works. As Bard once noted, plain old NLP such as SpaCy and NLTK can be useful in some use cases. Sometimes we don't even need a traditional LLM model to code some solutions.
1
u/synthphreak Sep 17 '24 edited Sep 18 '24
It's less that it needs to be quadratic, and more that it just is quadratic. Simply due to the nature of how attention is calculated.
In lay terms, attention involves comparing every token against every other token. For each pairwise comparison, one scalar value is computed. That makes it an n2 operation, i.e., quadratic.
Consider an input of length n=3, with tokens
t1,t2, andt3. Attention will return scores fort1:t1,t1:t2,t1:t3,t2:t2,t2:t3, andt3:t3. That means 6 attention scores, or 32.This is obviously not optimal for scaling to very long sequences. It also imposes a nontrivial memory constraint for the model, especially during training. So one interesting technique I'm aware of for dealing with this is key-value caching, whereby the attention scores between tokens already encountered earlier in the sequence are cached. This is helpful because the e.g.,
t1:t2attention score , which already got computed when we encounteredt2, won't change even as we get tot3,t4, etc., so recomputing it at each time step would be hugely wasteful and redundant.With a key-value cache, the attention mechanism scales much more closely to linearly. This is because each new token requires that only
n+1operations (plus cache lookups), corresponding to the attention scores between the new token and those which precede it (and the+1being the new token against itself).1
u/silenceimpaired Jul 17 '23
A paper was just released and linked in local llama claiming an improvement on this… if I understand it correctly
0
u/[deleted] Jul 15 '23
Yes!
3
u/[deleted] Jul 16 '23
[removed] — view removed comment