Spaces:
Sleeping
Sleeping
Add KV cache object note
Browse files
src/pages/index.astro
CHANGED
|
@@ -23,6 +23,10 @@ import Layout from "../layouts/Layout.astro";
|
|
| 23 |
<a href="/notes/reproducing-minicache-in-pytorch">Reproducing MiniCache in PyTorch</a>
|
| 24 |
<p>The API, tests, benchmark command, and the exact scope of the current reproduction.</p>
|
| 25 |
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
</ol>
|
| 27 |
</section>
|
| 28 |
|
|
@@ -65,9 +69,8 @@ import Layout from "../layouts/Layout.astro";
|
|
| 65 |
<section class="toc">
|
| 66 |
<div class="label">next note</div>
|
| 67 |
<p>
|
| 68 |
-
Next up:
|
| 69 |
-
|
| 70 |
-
primitive worth implementing.
|
| 71 |
</p>
|
| 72 |
</section>
|
| 73 |
</main>
|
|
|
|
| 23 |
<a href="/notes/reproducing-minicache-in-pytorch">Reproducing MiniCache in PyTorch</a>
|
| 24 |
<p>The API, tests, benchmark command, and the exact scope of the current reproduction.</p>
|
| 25 |
</li>
|
| 26 |
+
<li>
|
| 27 |
+
<a href="/notes/adding-a-kv-cache-object">Adding a KV-cache object</a>
|
| 28 |
+
<p>Moving from tensor primitives toward a decode-time cache interface.</p>
|
| 29 |
+
</li>
|
| 30 |
</ol>
|
| 31 |
</section>
|
| 32 |
|
|
|
|
| 69 |
<section class="toc">
|
| 70 |
<div class="label">next note</div>
|
| 71 |
<p>
|
| 72 |
+
Next up: a decode benchmark. The useful version should measure longer sequence lengths, retained-token
|
| 73 |
+
fraction, and the cost of applying compression across selected layer pairs.
|
|
|
|
| 74 |
</p>
|
| 75 |
</section>
|
| 76 |
</main>
|
src/pages/notes/adding-a-kv-cache-object.mdx
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
layout: ../../layouts/Layout.astro
|
| 3 |
+
title: Adding a KV-cache object - Cache Atlas
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
<main class="article-shell">
|
| 7 |
+
|
| 8 |
+
<div class="eyebrow">note 03</div>
|
| 9 |
+
|
| 10 |
+
# Adding a KV-cache object
|
| 11 |
+
|
| 12 |
+
The first MiniCache reproduction was a tensor primitive. That was useful, but it still sat below the shape of an inference system.
|
| 13 |
+
|
| 14 |
+
The next step is a small cache object:
|
| 15 |
+
|
| 16 |
+
> store per-layer keys and values, then apply the MiniCache pairwise path to a selected adjacent layer pair.
|
| 17 |
+
|
| 18 |
+
The implementation lives in:
|
| 19 |
+
|
| 20 |
+
- <a class="inline-link" href="https://github.com/rishabhsai/minicache-pytorch" target="_blank" rel="noreferrer">minicache-pytorch</a>
|
| 21 |
+
|
| 22 |
+
## The new API
|
| 23 |
+
|
| 24 |
+
The new object is `LayerKVCache`. It stores key and value tensors with shape:
|
| 25 |
+
|
| 26 |
+
```txt
|
| 27 |
+
(layers, batch, sequence, hidden_dim)
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
The compression call is explicit about which adjacent layers are being merged:
|
| 31 |
+
|
| 32 |
+
```python
|
| 33 |
+
import torch
|
| 34 |
+
|
| 35 |
+
from minicache_pytorch import LayerKVCache
|
| 36 |
+
|
| 37 |
+
keys = torch.randn(8, 2, 128, 64)
|
| 38 |
+
values = torch.randn(8, 2, 128, 64)
|
| 39 |
+
cache = LayerKVCache(keys=keys, values=values)
|
| 40 |
+
|
| 41 |
+
result = cache.compress_layer_pair(
|
| 42 |
+
lower_layer=4,
|
| 43 |
+
upper_layer=5,
|
| 44 |
+
alpha=0.5,
|
| 45 |
+
threshold=0.98,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
compressed_cache = result.cache
|
| 49 |
+
retained_fraction = result.retained_token_fraction
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
The object does not mutate the original cache. It returns a new cache plus the key/value retention masks.
|
| 53 |
+
|
| 54 |
+
## Why this is a better boundary
|
| 55 |
+
|
| 56 |
+
The primitive from the previous note answered:
|
| 57 |
+
|
| 58 |
+
> can I merge two adjacent layer tensors?
|
| 59 |
+
|
| 60 |
+
The cache object asks a more useful systems question:
|
| 61 |
+
|
| 62 |
+
> where would this sit in a decode-time cache path?
|
| 63 |
+
|
| 64 |
+
That boundary gives the repo a place to grow:
|
| 65 |
+
|
| 66 |
+
- cache shape validation
|
| 67 |
+
- layer-pair validation
|
| 68 |
+
- memory accounting
|
| 69 |
+
- retained-token reporting
|
| 70 |
+
- future policy logic for selecting layer pairs
|
| 71 |
+
|
| 72 |
+
It is still small enough to read in one file.
|
| 73 |
+
|
| 74 |
+
## Reproduce locally
|
| 75 |
+
|
| 76 |
+
Run the full test suite:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
git clone https://github.com/rishabhsai/minicache-pytorch
|
| 80 |
+
cd minicache-pytorch
|
| 81 |
+
uv sync --extra dev
|
| 82 |
+
uv run --extra dev pytest
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
Current result:
|
| 86 |
+
|
| 87 |
+
```txt
|
| 88 |
+
15 passed
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
The cache-specific tests cover:
|
| 92 |
+
|
| 93 |
+
- key/value shape validation
|
| 94 |
+
- minimum cache rank validation
|
| 95 |
+
- shape preservation after compression
|
| 96 |
+
- only the upper layer in the selected pair is updated
|
| 97 |
+
- retained tokens stay on the original upper-layer path
|
| 98 |
+
- non-adjacent layer pairs fail clearly
|
| 99 |
+
- cache byte accounting
|
| 100 |
+
|
| 101 |
+
## Example output
|
| 102 |
+
|
| 103 |
+
The new example script creates a fake 8-layer cache, makes layers 4 and 5 partially similar, then compresses that pair:
|
| 104 |
+
|
| 105 |
+
```bash
|
| 106 |
+
uv run python examples/cache_object.py
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
Current local output:
|
| 110 |
+
|
| 111 |
+
```txt
|
| 112 |
+
cache shape: (8, 2, 128, 64)
|
| 113 |
+
cache bytes: 1,048,576
|
| 114 |
+
compressed pair: layers 4 -> 5
|
| 115 |
+
retained token fraction: 0.410
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
That retained fraction is the important signal. It means the cache path is not blindly compressing every token; the retention mask is visible enough to measure and debug.
|
| 119 |
+
|
| 120 |
+
## What next
|
| 121 |
+
|
| 122 |
+
The next note should be a decode benchmark.
|
| 123 |
+
|
| 124 |
+
The benchmark should vary:
|
| 125 |
+
|
| 126 |
+
1. sequence length
|
| 127 |
+
2. hidden dimension
|
| 128 |
+
3. retention threshold
|
| 129 |
+
4. number of layer pairs compressed
|
| 130 |
+
|
| 131 |
+
The output should be a small table, not a wall of prose. The goal is to make the tradeoff visible: how much retention happens, how much extra compute the compression path adds, and where the primitive starts to look too expensive.
|
| 132 |
+
|
| 133 |
+
After that, the project is ready for the PrefixKV track.
|
| 134 |
+
|
| 135 |
+
## Sources
|
| 136 |
+
|
| 137 |
+
<ul class="source-list">
|
| 138 |
+
<li><a href="https://github.com/rishabhsai/minicache-pytorch" target="_blank" rel="noreferrer">minicache-pytorch</a></li>
|
| 139 |
+
<li><a href="https://arxiv.org/abs/2405.14366" target="_blank" rel="noreferrer">MiniCache paper (arXiv:2405.14366)</a></li>
|
| 140 |
+
<li><a href="https://minicache.vmv.re/" target="_blank" rel="noreferrer">MiniCache project page</a></li>
|
| 141 |
+
</ul>
|
| 142 |
+
|
| 143 |
+
<nav class="article-nav" aria-label="Article navigation">
|
| 144 |
+
<a href="/notes/reproducing-minicache-in-pytorch">
|
| 145 |
+
<span>Previous</span>
|
| 146 |
+
<strong>Reproducing MiniCache in PyTorch</strong>
|
| 147 |
+
</a>
|
| 148 |
+
<a href="https://github.com/rishabhsai/minicache-pytorch" target="_blank" rel="noreferrer">
|
| 149 |
+
<span>Code</span>
|
| 150 |
+
<strong>Open minicache-pytorch</strong>
|
| 151 |
+
</a>
|
| 152 |
+
</nav>
|
| 153 |
+
|
| 154 |
+
</main>
|
src/pages/notes/index.astro
CHANGED
|
@@ -21,6 +21,10 @@ import Layout from "../../layouts/Layout.astro";
|
|
| 21 |
<a href="/notes/reproducing-minicache-in-pytorch">Reproducing MiniCache in PyTorch</a>
|
| 22 |
<p>The first implementation report, centered on <a href="https://arxiv.org/abs/2405.14366" target="_blank" rel="noreferrer">arXiv:2405.14366</a>.</p>
|
| 23 |
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
</ol>
|
| 25 |
</section>
|
| 26 |
|
|
@@ -41,8 +45,7 @@ import Layout from "../../layouts/Layout.astro";
|
|
| 41 |
<section class="toc">
|
| 42 |
<div class="label">next note</div>
|
| 43 |
<p>
|
| 44 |
-
Next up:
|
| 45 |
-
reproduction path.
|
| 46 |
</p>
|
| 47 |
</section>
|
| 48 |
</main>
|
|
|
|
| 21 |
<a href="/notes/reproducing-minicache-in-pytorch">Reproducing MiniCache in PyTorch</a>
|
| 22 |
<p>The first implementation report, centered on <a href="https://arxiv.org/abs/2405.14366" target="_blank" rel="noreferrer">arXiv:2405.14366</a>.</p>
|
| 23 |
</li>
|
| 24 |
+
<li>
|
| 25 |
+
<a href="/notes/adding-a-kv-cache-object">Adding a KV-cache object</a>
|
| 26 |
+
<p>The second implementation report: applying the pairwise path to stored keys and values.</p>
|
| 27 |
+
</li>
|
| 28 |
</ol>
|
| 29 |
</section>
|
| 30 |
|
|
|
|
| 45 |
<section class="toc">
|
| 46 |
<div class="label">next note</div>
|
| 47 |
<p>
|
| 48 |
+
Next up: a decode benchmark note that makes the latency and memory tradeoff visible.
|
|
|
|
| 49 |
</p>
|
| 50 |
</section>
|
| 51 |
</main>
|
src/pages/notes/reproducing-minicache-in-pytorch.mdx
CHANGED
|
@@ -171,9 +171,9 @@ The point of the series is to keep each step reproducible: one mechanism, one im
|
|
| 171 |
<span>Previous</span>
|
| 172 |
<strong>Why KV cache papers matter</strong>
|
| 173 |
</a>
|
| 174 |
-
<a href="
|
| 175 |
-
<span>
|
| 176 |
-
<strong>
|
| 177 |
</a>
|
| 178 |
</nav>
|
| 179 |
|
|
|
|
| 171 |
<span>Previous</span>
|
| 172 |
<strong>Why KV cache papers matter</strong>
|
| 173 |
</a>
|
| 174 |
+
<a href="/notes/adding-a-kv-cache-object">
|
| 175 |
+
<span>Next</span>
|
| 176 |
+
<strong>Adding a KV-cache object</strong>
|
| 177 |
</a>
|
| 178 |
</nav>
|
| 179 |
|