Deep Papers

KV Cache Explained

Arize AI

In this episode, we dive into the intriguing mechanics behind why chat experiences with models like GPT often start slow but then rapidly pick up speed. The key? The KV cache. This essential but under-discussed component enables the seamless and snappy interactions we expect from modern AI systems.

Harrison Chu breaks down how the KV cache works, how it relates to the transformer architecture, and why it's crucial for efficient AI responses. By the end of the episode, you'll have a clearer understanding of how top AI products leverage this technology to deliver fast, high-quality user experiences. Tune in for a simplified explanation of attention heads, KQV matrices, and the computational complexities they present.









Learn more about AI observability and evaluation in our course, join the Arize AI Slack community or get the latest on LinkedIn and X.

Have you ever wondered why when you're talking to chatGPT, you might be waiting around for the first word to come, but once the first word does come, the rest of the chat just rips? Well, there's a very good reason for that. It comes from something called the KV cache. To me, this is a super interesting topic because relative to the other components of an LLM, it's pretty under discussed, but it's a component that without it, our current interaction paradigm of chat on LLMs just would not happen.

And knowing how it works, not only gives you a more intuitive understanding of how LLM works in general, But also about how the best AI products out there exploit it to deliver experiences that feel snappy and feel just a cut above the rest. If you're this far into the video, I'm just going to assume that, you know, about this thing called the transformer, that it's this neural network architecture that underpins all the progress we're seeing in LLMs today.

We're going to focus our attention specifically on the attention head, which is  the simplest version of the story is that there are three matrices. The query. Key. And value matrix. The only job of these matrices is to take the input token and transform it to three vectors. K Q V in this video, we're not going to go into all the gory details of what the matrix means, what the vector means, but you do need to know two things.

One is that these weight matrices are really, really big. In fact, They're the square of the total context length. If the context length is 10,000, the weight matrix is going to have 100 million parameters. And this leads to compute that is pretty, pretty costly, pretty significant. Second is that when you multiply the KQV vectors, you get what's called the attention vector.

And this is the vector that gets added back to the original token to modify its meaning. Now, this is all getting pretty abstract. So let's ground ourselves with a specific example. Let's take the phrase Santa Claus. When I read this phrase, I know the word Claus is referring to the last name of a person named Santa Claus.

Not the unit of grammar in the English language, and I know this because of the attention I'm paying to the previous word, Santa, informs how I interpret the meaning of clause. That's it. I mean, it's pretty simple, right? And all that stuff I was just talking about earlier is just a mathematical formalization of this very intuitive thing that we do every day.

So what's the big problem? Well, to get intuitive understanding, let's go back to our previous example, but expand it. Now, let's take the phrase, Santa Claus lives at the North Pole. Pole can mean multiple different things. It can mean a stick, or it can mean a place. But in order for me to understand that pole in this context means a place, I need to apply the attention I pay to Santa lives North Pole.

alter that word to correctly make that distinction. It requires me to hold more context while I read the sentence. The mathematical formulation of paying more attention to previous words as the sentence gets longer is that for the second token, the second Q vector needs to be multiplied by the second KV vector, which And the first KV vector.

And this pattern repeats. By the third token, the compute needs the KV vectors from the first, second, and third tokens. You get the idea. It's a quadratic growth in computational complexity. By the time you're computing the hundredth token, it is 10, 000 times the compute you need to compute the first token.

And remember the upstream compute required to take the token into the KQ vectors itself is already enormous. The numbers here are just staggering. This is where the KV cache comes to the rescue with. Honestly, a very simple idea as the compute for the tokens go further and further along into the context, the previous KV vectors, they don't change.

And so just cash them when you're computing the KV values for the first token, cash it so that when you're computing the KV values for the second token, you just use the previous ones. You do not have to repeat the expensive matrix computation. The way this ties back to the observation we had at the beginning of the video is that you can think of the time between when you first press enter and when you To when you get the first word back from chat GPT as the time it takes for the LLM to warm up its KB cache.

And once the KB cache is warm, all the words in the subsequent response come out really, really fast. Cause the compute is very efficient. So it's a pretty simple idea, but like all things in computer science, there are trade offs and the trade offs involved here have GPU design, data center design, as well as where all the productive frontier research is going, but I think we're running pretty long this video, so we'll save that












People on this episode