Skip to content

Commit d6ca261

Browse files
Make it clear that we use unofficial turboquant implementation
1 parent d6b2570 commit d6ca261

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

tutorials/49_TurboQuant_Quantization_with_HuggingFace.ipynb

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,41 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {},
6-
"source": "# Compress the KV Cache with TurboQuant and Haystack\n\n- **Level**: Advanced\n- **Time to complete**: 20 min\n- **Nodes Used**: [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator)\n- **Goal**: Apply TurboQuant KV cache compression to a local LLM and measure its memory and throughput impact with Haystack."
6+
"source": [
7+
"# Compress the KV Cache with TurboQuant and Haystack\n",
8+
"\n",
9+
"- **Level**: Advanced\n",
10+
"- **Time to complete**: 20 min\n",
11+
"- **Nodes Used**: [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator)\n",
12+
"- **Goal**: Apply TurboQuant KV cache compression to a local LLM and measure its memory and throughput impact with Haystack."
13+
]
714
},
815
{
916
"cell_type": "markdown",
1017
"metadata": {},
11-
"source": "## Overview\n\nEvery time an LLM generates a token, it reads and writes a **key-value (KV) cache** - a growing table of intermediate activations that lets the model attend to previous tokens without recomputing them. On long contexts or large models, this cache becomes the dominant consumer of GPU memory.\n\n[TurboQuant](https://research.google/blog/turboquant-redefining-ai-efficiency-with-extreme-compression/) is a KV cache compression algorithm from Google Research (ICLR 2026) that shrinks those vectors to 3–4 bits per coordinate without any retraining. It works in two stages:\n\n1. **PolarQuant** - a random orthogonal rotation maps cache vectors to a more uniform distribution, then quantizes them in polar coordinates using Lloyd-Max optimal centroids.\n2. **QJL** (Quantized Johnson-Lindenstrauss) - a single extra bit per vector corrects residual errors in attention score computation, preserving accuracy at extreme compression ratios.\n\nThe result: KV memory can drop from 1,639 MiB to 435 MiB (3.76x) on an RTX 4090, with ≥6x reduction validated on server hardware, and near-identical output quality.\n\nIn this tutorial you will wire TurboQuant's `CompressedDynamicCache` into Haystack's [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator), run a generation, and measure time-to-first-token, throughput, and live VRAM usage."
18+
"source": [
19+
"## Overview\n",
20+
"\n",
21+
"Every time an LLM generates a token, it reads and writes a **key-value (KV) cache** - a growing table of intermediate activations that lets the model attend to previous tokens without recomputing them. On long contexts or large models, this cache becomes the dominant consumer of GPU memory.\n",
22+
"\n",
23+
"[TurboQuant](https://research.google/blog/turboquant-redefining-ai-efficiency-with-extreme-compression/) is a KV cache compression algorithm from Google Research (ICLR 2026) that shrinks those vectors to 3–4 bits per coordinate without any retraining. It works in two stages:\n",
24+
"\n",
25+
"1. **PolarQuant** - a random orthogonal rotation maps cache vectors to a more uniform distribution, then quantizes them in polar coordinates using Lloyd-Max optimal centroids.\n",
26+
"2. **QJL** (Quantized Johnson-Lindenstrauss) - a single extra bit per vector corrects residual errors in attention score computation, preserving accuracy at extreme compression ratios.\n",
27+
"\n",
28+
"The result: KV memory can drop from 1,639 MiB to 435 MiB (3.76x) on an RTX 4090, with ≥6x reduction validated on server hardware, and near-identical output quality.\n",
29+
"\n",
30+
"In this tutorial you will use [`turboquant-vllm`](https://github.com/Alberto-Codes/turboquant-vllm), a community implementation of the TurboQuant algorithm, to wire `CompressedDynamicCache` into Haystack's [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator), run a generation, and measure time-to-first-token, throughput, and live VRAM usage."
31+
]
1232
},
1333
{
1434
"cell_type": "markdown",
1535
"metadata": {},
16-
"source": "## Installing Haystack and TurboQuant\n\nFirst, let's install `haystack-ai` and `turboquant-vllm`, which provides the `CompressedDynamicCache` wrapper."
36+
"source": [
37+
"## Installing Haystack and TurboQuant\n",
38+
"\n",
39+
"First, let's install `haystack-ai` and [`turboquant-vllm`](https://github.com/Alberto-Codes/turboquant-vllm), a community implementation of the TurboQuant algorithm that provides the `CompressedDynamicCache` wrapper."
40+
]
1741
},
1842
{
1943
"cell_type": "code",
@@ -29,7 +53,11 @@
2953
{
3054
"cell_type": "markdown",
3155
"metadata": {},
32-
"source": "## Setting Up a Streaming Callback\n\nTo measure **time-to-first-token (TTFT)** and throughput, we pass a streaming callback that timestamps each arriving token. The first call marks TTFT; the last marks the end of generation."
56+
"source": [
57+
"## Setting Up a Streaming Callback\n",
58+
"\n",
59+
"To measure **time-to-first-token (TTFT)** and throughput, we pass a streaming callback that timestamps each arriving token. The first call marks TTFT, while the last marks the end of generation."
60+
]
3361
},
3462
{
3563
"cell_type": "code",
@@ -84,7 +112,11 @@
84112
{
85113
"cell_type": "markdown",
86114
"metadata": {},
87-
"source": "## Initializing the Generator\n\nNow let's set up [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator) with a selected model, like `Qwen/Qwen3-4B-Thinking-2507`. We pass the compressed `cache` via `generation_kwargs` so that every decoding step writes through TurboQuant."
115+
"source": [
116+
"## Initializing the Generator\n",
117+
"\n",
118+
"Now let's set up [`HuggingFaceLocalChatGenerator`](https://docs.haystack.deepset.ai/docs/huggingfacelocalchatgenerator) with a selected model, like `Qwen/Qwen3-4B-Thinking-2507`. We pass the compressed `cache` via `generation_kwargs` so that every decoding step writes through TurboQuant."
119+
]
88120
},
89121
{
90122
"cell_type": "code",
@@ -144,7 +176,15 @@
144176
{
145177
"cell_type": "markdown",
146178
"metadata": {},
147-
"source": "## Reading the Metrics\n\nThree metrics to check:\n\n- **TTFT** (time-to-first-token) - latency to the first output token; a proxy for perceived responsiveness.\n- **Throughput** (tok/s) - tokens decoded per second. TurboQuant's memory savings reduce cache read pressure, which can improve this on memory-bandwidth-bound hardware.\n- **Total time** - end-to-end wall time including model loading overhead."
179+
"source": [
180+
"## Reading the Metrics\n",
181+
"\n",
182+
"Three metrics to check:\n",
183+
"\n",
184+
"- **TTFT** (time-to-first-token) - latency to the first output token - a proxy for perceived responsiveness.\n",
185+
"- **Throughput** (tok/s) - tokens decoded per second. TurboQuant's memory savings reduce cache read pressure, which can improve this on memory-bandwidth-bound hardware.\n",
186+
"- **Total time** - end-to-end wall time including model loading overhead."
187+
]
148188
},
149189
{
150190
"cell_type": "code",
@@ -164,7 +204,11 @@
164204
{
165205
"cell_type": "markdown",
166206
"metadata": {},
167-
"source": "## Checking VRAM Usage\n\n`vram_bytes()` returns the byte footprint of all compressed KV tensors. Compare it against an uncompressed `DynamicCache` to verify the reduction reported in the TurboQuant paper."
207+
"source": [
208+
"## Checking VRAM Usage\n",
209+
"\n",
210+
"`vram_bytes()` returns the byte footprint of all compressed KV tensors. Compare it against an uncompressed `DynamicCache` to verify the reduction reported in the TurboQuant paper."
211+
]
168212
},
169213
{
170214
"cell_type": "code",
@@ -196,4 +240,4 @@
196240
},
197241
"nbformat": 4,
198242
"nbformat_minor": 4
199-
}
243+
}

0 commit comments

Comments
 (0)