---
title: "Unlocking Longer Generation with Key-Value Cache Quantization"
description: "Hugging Face Transformers adds KV cache quantization, inspired by the KIVI paper, that stores keys and values per-token in int2/int4/int8 via quanto or HQQ backends."
url: "https://swe.quest/articles/unlocking-longer-generation-with-key-value-cache-quantization-c7f36c464d98f5fa"
canonical: "https://huggingface.co/blog/kv-cache-quantization"
source: "Hugging Face"
published: "2024-05-16"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 11
tags: ["inference","llms","python"]
---

# Unlocking Longer Generation with Key-Value Cache Quantization

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-05-16, 11 min read, Deep dive. Original: [https://huggingface.co/blog/kv-cache-quantization](https://huggingface.co/blog/kv-cache-quantization)

## Summary

Hugging Face Transformers adds KV cache quantization, inspired by the KIVI paper, that stores keys and values per-token in int2/int4/int8 via quanto or HQQ backends. A residual cache of the most recent tokens (128 by default) stays in full precision to avoid re-quantizing on every step and to preserve accuracy. On Llama-2-7B, int4 matches fp16 on PG-19 perplexity and LongBench, while int2 degrades noticeably. Int4 gives roughly 2.5x memory savings and, with FlashAttention on an 80GB A100, raises max context from 40k to 128k tokens, at the cost of slower generation at larger batch sizes and about 3x slowdown when combined with weight quantization.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- transformer self-attention
- autoregressive decoding
- quantization basics ([easy posts on Inference & serving](https://swe.quest/tags/inference.md?difficulty=easy))

## Keywords

KV cache, Hugging Face Transformers, quanto, HQQ, KIVI, Llama-2, FlashAttention, LongBench

## Related posts

- [KV Cache from scratch in nanoVLM](https://swe.quest/articles/kv-cache-from-scratch-in-nanovlm-f1d92b90631a1366.md): Hugging Face · 2025-06-04 · Medium · 9 min read · Inference & serving, LLMs, Performance engineering, Python
  KV caching avoids recomputing keys and values for earlier tokens during autoregressive generation, and implementing it from scratch in the pure-PyTorch nanoVLM codebase gave a 38% speedup in generation.
- [Mastering Long Contexts in LLMs with KVPress](https://swe.quest/articles/mastering-long-contexts-in-llms-with-kvpress-778c6dc9287ed60d.md): Hugging Face · 2025-01-23 · Medium · 7 min read · Inference & serving, LLMs, Python, Performance engineering
  KV cache memory grows linearly with context length: Llama 3-70B at 1M tokens in bfloat16 needs about 330GB of KV cache, roughly 70% of total memory.
- [Continuous batching from first principles](https://swe.quest/articles/continuous-batching-from-first-principles-b2a5a3d9c00e56be.md): Hugging Face · 2025-11-25 · Medium · 17 min read · Inference & serving, LLMs, Performance engineering
  Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill.
- [Making LLMs lighter with AutoGPTQ and transformers](https://swe.quest/articles/making-llms-lighter-with-autogptq-and-transformers-6a1ce27ee3288303.md): Hugging Face · 2023-08-23 · Medium · 10 min read · Inference & serving, LLMs, Performance engineering, Hardware
  AutoGPTQ is now integrated into Hugging Face Transformers (via Optimum), letting users quantize and run LLMs at 8, 4, 3 or 2 bits with the GPTQ post-training method.
- [TurboQuant: Redefining AI efficiency with extreme compression](https://swe.quest/articles/turboquant-redefining-ai-efficiency-with-extreme-compression-d5471afe1cc3ed09.md): Google Research · 2026-03-24 · Hard · 7 min read · Inference & serving, Vector search, LLMs, Algorithms
  TurboQuant is a data-oblivious vector quantization algorithm that removes the per-block quantization-constant overhead of traditional methods by combining PolarQuant (random rotation plus recursive polar-coordinate transforms with a fixed angle grid) and a 1-bit Quantized Johnson-Lindenstrauss residual stage that removes bias in inner-product estimates.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
