SnapKV: LLM Knows What You are Looking for Before Generation

Yuhong Li, Yingbing Huang, Bowen Yang, Bharat Venkitesh, Acyr F. Locatelli, Hanchen Ye, Tianle Cai, Patrick Lewis, Deming Chen

arXiv:2404.14469 · 2026-07-27 공개 · arXiv · PDF

llm long-context kv-cache memory-efficiency needle-in-a-haystack generation-speed snapkv attention-head

Abstract

Large Language Models (LLMs) have made remarkable progress in processing extensive contexts, with the Key-Value (KV) cache playing a vital role in enhancing their performance. However, the growth of the KV cache in response to increasing input length poses challenges to memory and time efficiency. To address this problem, this paper introduces SnapKV, an innovative and fine-tuning-free approach that efficiently minimizes KV cache size while still delivering comparable performance in real-world applications. We discover that each attention head in the model consistently focuses on specific prompt attention features during generation. Meanwhile, this robust pattern can be obtained from an 'observation' window located at the end of the prompts. Drawing on this insight, SnapKV automatically compresses KV caches by selecting clustered important KV positions for each attention head. Our approach significantly reduces the growing computational overhead and memory footprint when processing long input sequences. Specifically, SnapKV achieves a consistent decoding speed with a 3.6x increase in generation speed and an 8.2x enhancement in memory efficiency compared to the baseline when processing inputs of 16K tokens. At the same time, it maintains comparable performance to the baseline models across 16 long sequence datasets. Moreover, SnapKV can process up to 380K context tokens on a single A100-80GB GPU using HuggingFace implementation with minor changes, exhibiting only a negligible accuracy drop in the Needle-in-a-Haystack test. Further comprehensive studies suggest SnapKV's potential for practical applications.

한국어 요약

한 줄 요약

SnapKV는 KV 캐시 압축을 통해 LLM의 메모리 효율과 생성 속도를 향상시키는 fine-tuning-free 방법이다.

핵심 기여도

핵심 아이디어

SnapKV는 LLM이 생성 전부터 특정 토큰에 집중하는 패턴을 활용하여 KV 캐시를 압축하는 방법이다. 모델의 각 어텐션 헤드가 생성 시 일관되게 특정 프롬프트 토큰에 집중하는 것을 발견했으며, 이 패턴은 프롬프트 끝부분의 'observation window'에서 추출할 수 있다. 이를 바탕으로 SnapKV는 각 어텐션 헤드에 대해 중요한 KV 위치를 클러스터링하여 선택적으로 압축한다. 이는 기존 KV 캐시 압축 방법과 달리, 생성 중에 추가되는 KV가 아닌 입력 시퀀스의 KV를 대상으로 하기 때문에, 실제 응용에서 메모리 효율 향상에 더 효과적이다.

기술적 접근법

주요 결과

의의 및 한계

SnapKV는 LLM의 KV 캐시 압축 문제를 fine-tuning 없이 해결하며, 실제 응용 시스템에서 메모리와 시간 효율을 동시에 개선할 수 있다. 특히, 대규모 컨텍스트를 처리하는 RAG 모델과 병용 시 효과가 크다. 그러나 압축 비율이 높아질수록 정확도 저하가 발생할 수 있으며, 모든 입력 시퀀스에서 동일한 패턴이 나타나지 않을 경우 성능이 저하될 수 있다. 또한, 풀링 방식의 선택이 정확도에 미치는 영향은 제한적이지만, 일부 시나리오에서는 풀링 없이도 충분한 성능을 보일 수 있다.

실용적 활용

SnapKV는 대규모 컨텍스트를 처리하는 챗봇, RAG 기반 검색 시스템, 코드 분석 및 문서 요약 등 다양한 응용 분야에서 활용 가능하다. 특히, GPU 메모리가 제한된 환경에서 장문 입력 처리를 효율적으로 수행할 수 있으며, 병렬 디코딩과 결합하면 추가적인 성능 향상이 기대된다.