News

日本語 · English · 中文

[Video] Accelerating Prefill for Local LLMs: Prefix Caching and Radix Tree

Inference with local LLMs is divided into two steps: "prefill" and "decode". During prefill, which generates the first token, the QKV calculation for the entire question text is required, making it heavy; from the second token onward, only additional computations are needed by using KV cache. This time, we investigated prefill-side acceleration techniques.

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

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

← Latest articles