[RFC] implementing Expected Attention in llama.cpp?#19183
[RFC] implementing Expected Attention in llama.cpp?#19183ddh0 wants to merge 3 commits intoggml-org:masterfrom
Conversation
|
If I understand correctly, the idea can be explained in more simple terms:
If we view the model as a pure function However, it might sounds more complicated than that. First off, flash attention won't expose the attention score to the outside world. And secondly, this may make it more tricky to reuse cache cross multiple sequences, same problem with KV shifting/SWA. Also, just to mention that there were some similar works in #16817 , but might not very useful as the other PR is AI-coded and the author doesn't seem to fully understand it. |
Recently, NVIDIA put out a paper describing a method to reduce the size of the KV cache. They call their preferred method Expected Attention.
Expected Attention: KV Cache Compression by Estimating Attention from Future Queries Distribution
From my reading of the paper, the thought process is like this:
cur) at each layer follow a gaussian distribution (and they re-confirmed this here too)wq) can be thought of as a linear transformation oncur, which maintains this same distribution for the resultingQcurprojectioncb(), we can observe the query projectionsQcurin all layers over an interval, in order to model a distribution of the expected future queries (the paper also takes into account RoPE / positional encoding for future positions, but I could not figure out how to do that, so I skipped it for now)So for example if the user specified a "compression ratio" of 20%, we would discard the 20% of KV entries that are expected to have the least impact on the hidden state in the future.
I had hoped to be able to get this working and submit a PR for it on my own, but there are some problems that I don't know how to solve:
llama_context? Thellama_memorystructure abstracts away the implementation of the different cache types, so this is not straightforward.defraglogic for this?I am posting this thread in hopes of starting a discussion to see if this method will even be feasible to implement in llama.cpp, or if it would be too complicated. The code is certainly NOT in a state that is ready for use, or even guaranteed to work at all. I am just including my demo code as a starting point. I would be pleasantly surprised if this PR ever leaves the draft stage. :)
Make sure to read the contributing guidelines before submitting a PR