Local LLM inference is divided into two steps: "prefill" and "decode". In the prefill phase, which generates the first token, the full QKV calculation for the question text is required, making it computationally heavy. From the second token onward, KV cache is used, so only additional calculations are needed. This time, we examined high-speed prefill techniques.
Prefix Caching
Concatenating the same system prompt (such as "Answer concisely" or "Answer in Japanese") with the question text and computing every time is wasteful. The idea is to pre-compute and save the KV cache, and start computation only from the changed parts.
Organization via Radix Tree
To address the issue of past history and system prompts accumulating repeatedly, algorithms using a Radix Tree (a tree structure) are employed. This manages common prefixes as branches and reuses the KV cache up to the branching point.
Observations from Testing on Local Models
Recent models incorporate linear structures like Mamba, which may require managing a separate parameter called S in addition to KV. This also touches on techniques that divide attention calculations into smaller matrices and compute them sequentially to fit within VRAM.
Table of Contents
0:00 Difference between prefill and decode
1:15 Role of KV cache
2:03 Tokenization and embedding before prefill
2:57 What is prefix caching
3:29 Saving recalculation of system prompts
6:52 Organizing history with Radix Tree
8:18 Observations from testing on local models
10:28 Splitting attention calculations
You can watch the video here: Watch on YouTube