---
title: "Mastering Long Contexts in LLMs with KVPress"
description: "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."
url: "https://swe.quest/articles/mastering-long-contexts-in-llms-with-kvpress-778c6dc9287ed60d"
canonical: "https://huggingface.co/blog/nvidia/kvpress"
source: "Hugging Face"
published: "2025-01-23"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 7
tags: ["inference","llms","python","performance"]
---

# Mastering Long Contexts in LLMs with KVPress

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2025-01-23, 7 min read, Deep dive. Original: [https://huggingface.co/blog/nvidia/kvpress](https://huggingface.co/blog/nvidia/kvpress)

## Summary

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. NVIDIA's KVPress is a Python toolkit of "presses" that prune low-importance KV pairs during prefill via forward hooks on attention layers. Examples include KnormPress, SnapKVPress and ExpectedAttentionPress, each tuned with a compression\_ratio and usable through a custom transformers pipeline. On Llama 3.1 8B at 128k context, 50% compression cuts peak memory from 45GB to 37GB and raises decoding from 11 to 17 tokens/s on an A100, while RULER benchmarks show accuracy dropping as compression rises.

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)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- transformer attention
- KV cache basics ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))
- Hugging Face transformers

## Keywords

KVPress, KV cache, ExpectedAttentionPress, SnapKV, AdaKV, Llama 3, RULER, Hugging Face transformers

## Related posts

- [Unlocking Longer Generation with Key-Value Cache Quantization](https://swe.quest/articles/unlocking-longer-generation-with-key-value-cache-quantization-c7f36c464d98f5fa.md): Hugging Face · 2024-05-16 · Medium · 11 min read · Inference & serving, LLMs, Python
  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.
- [Serving DeepSeek-V4: why million-token context is an inference systems problem](https://swe.quest/articles/serving-deepseek-v4-why-million-token-context-is-an-inference-systems-problem-bfb1067f4f733f69.md): Together AI · 2026-05-11 · Hard · 11 min read · Inference & serving, LLMs, Caching, Performance engineering, Hardware
  DeepSeek-V4's 1M-token context comes from compressing the token axis of the KV cache: Compressed Sparse Attention (stride 4, top-k selection), Heavily Compressed Attention (stride 128, dense over ~8K entries) and a 128-token Sliding Window Attention path.
- [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.
- [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.
- [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)
