News

日本語 · English · 中文

[Ask the Teacher] How KV Cache Works and How Its Size Is Determined

This news article about running a 270-billion-parameter LLM on PS5 mentions the term "KV cache." It explains what this mechanism stores, how it relates to layers and K and V, and what determines the size of its storage space.

Hinata: I don’t understand the term “KV cache.” What exactly is being stored? I came across it in the article Running a 270-billion-parameter LLM on PS5.

Saeki: Let me put it this way, Hinata: it’s like a notebook where you stick post-it notes on the important parts of a book you’ve read. That way, you don’t have to reread the entire thing. More precisely, KV cache is a place to store intermediate computation results as the text is processed. What is stored are only two types of numbers, K and V, for each layer. A layer refers to each individual stage when computations are stacked in multiple steps. K acts as a “heading” used for comparison when something is later looked up. V contains the “what’s written there” content. The query from the searching side is called Q, but since it is recreated for each new token, it is not stored. When a new token arrives, it is simply appended to the end. A token is a single unit when text is finely divided. Because previous calculations do not need to be redone, generating subsequent output becomes faster.

Hinata: I see, the post-it note analogy helped a bit. K is the heading for comparison, and V is the content. These are stored per layer, correct? What caught me off guard here was the term “layer.” When you say layer, Saeki-san, do you mean a single stage in a multi-stage computation process?

Saeki: Yes, Hinata, that understanding is correct. A layer is indeed a single stage in a multi-stage computation process. K and V have different values for each layer, so they are stored for every layer. The reason they differ per layer is that the learned weights vary by stage. Even for the same sentence, the features being attended to change between the first and second layers.

Hinata: I understand that different values are used per layer. Even for the same sentence, the first and second layers focus on different features, so they need to be stored separately. That brings up another question. If they are stored per layer, doesn’t the storage space grow larger for models with more layers?

Saeki: Exactly, Hinata. The more layers there are, the more data needs to be stored. This directly follows from the mechanism of “storing K and V for each layer.” In fact, the article Prefill and Decode also explains that K and V computed in each layer are saved. Prefill refers to the stage where text is ingested all at once. Decode refers to the stage where the next word is generated one by one. The storage destination is VRAM. VRAM is specialized memory used by the GPU. A GPU is a component that accelerates computations. K and V are saved per layer there so they can be reused in the subsequent decode stage. However, the size is determined not only by the number of layers. It also depends on the length of each K and V, as well as the length of the text.

Hinata: So the amount stored isn’t determined solely by the number of layers. It’s also influenced by the length of each K and V and the length of the text. I feel like I’ve just added two reasons for why the size changes. But here’s what puzzled me: I get that it increases when the text is longer, but what determines the length of each K and V? Is that fixed from the start for each model?

Saeki: You’re correct there, Hinata. The length of each K and V is determined at the time the model is designed. It is predecided, before the model is built, how long each layer’s K and V should be. Therefore, as long as it’s the same model, the length doesn’t change even if the text changes. Only the count changes. The count increases in proportion to the length of the text. Thus, the total amount is determined by “2 types of K and V × length per item × count × number of layers.” Furthermore, the overall size scales depending on how many bits are used to store each number. Using 16 bits versus 8 bits results in exactly a twofold difference in size.

Hinata: To summarize what you just said: KV cache is a mechanism that saves intermediate computation results in a notebook. Only K and V per layer are saved, and the amount is determined by 2 types of K and V × length per item × count × number of layers, plus whether each number is stored in how many bits. As the text gets longer, the count increases, and the storage space expands. Finally, I’d like to ask: if this storage space could be made smaller, what would change? Could it handle longer texts, or would it become faster?

Saeki: Your summary is accurate, Hinata. Reducing the storage space benefits both. With freed-up space, longer texts can be stored, and since less data needs to be read out, generating subsequent output also becomes faster. However, the figures in the article PS5 represent generation speeds measured by developers on a single console. The speed when processing long texts is not visible from publicly released tables. It has not yet been measured how much the quality of the answers drops when reducing these numbers. Rounding to 2 bits means roughly summarizing the numbers rather than discretizing them in fine detail. Once that is demonstrated, the discussion will move to the next stage.

This article was translated from the Japanese original. 日本語で読む

← Latest articles