kv-cache llm-inference quantization llama memory-optimization tuning-free asymmetric-quantization batching
Abstract
Efficiently serving large language models (LLMs) requires batching of many requests to reduce the cost per request. Yet, with larger batch sizes and longer context lengths, the key-value (KV) cache, which stores attention keys and values to avoid re-computations, significantly increases memory demands and becomes the new bottleneck in speed and memory usage. Additionally, the loading of the KV cache causes the computational core to be idle, which limits the inference speed. A straightforward and effective solution to reduce KV cache size is quantization, which decreases the total bytes taken by KV cache. However, there is a lack of in-depth studies that explore the element distribution of KV cache to understand the hardness and limitation of KV cache quantization. To fill the gap, we conducted a comprehensive study on the element distribution in KV cache of popular LLMs. Our findings indicate that the key cache should be quantized per-channel, i.e., group elements along the channel dimension and quantize them together. In contrast, the value cache should be quantized per-token. From this analysis, we developed a tuning-free 2bit KV cache quantization algorithm named KIVI. With hardware-friendly implementation, KIVI can enable Llama, Falcon, and Mistral models to maintain almost the same quality while using $\mathbf{2.6\times}$ less peak memory (including model weight). This reduction in memory usage enables up to $\mathbf{4\times}$ larger batch size, bringing $\mathbf{2.35\times \sim 3.47\times}$ throughput on real LLM inference workload. The source code is available at https://github.com/jy-yuan/KIVI.
한국어 요약
한 줄 요약
KIVI는 튜닝 없이 키-벨류 캐시를 2비트로 양자화하여 LLM 추론 메모리 사용량을 2.6배 감소시키는 알고리즘이다.
핵심 기여도
- 키 캐시는 채널 단위, 벨류 캐시는 토큰 단위로 양자화해야 한다는 분포 분석 결과 제시.
- 튜닝 없이 2비트 양자화를 구현한 KIVI 알고리즘 제안.
- Llama-2-7B 모델에서 2.6× 메모리 절감, 4× 배치 크기 증가, 2.35×~3.47× 처리량 향상 달성.
- 그룹화 및 잔여 캐시 분할 기법을 통해 실시간 추론 환경에 적합한 구현 제공.
핵심 아이디어
기존 KV 캐시 양자화 연구는 키와 벨류 캐시의 분포 차이를 고려하지 않았다. KIVI는 키 캐시가 채널 단위로 큰 아웃라이어를 가지며, 벨류 캐시는 토큰 단위로 분산된다는 점을 분석하여 각각 다른 양자화 전략을 적용한다. 키 캐시는 채널 단위로 그룹화하여 양자화함으로써 오류가 다른 채널에 영향을 주지 않도록 설계되었다. 벨류 캐시는 토큰 단위로 양자화하여, 어텐션 스코어의 희소성에 기반한 오류 제한을 달성한다. 이는 어텐션 출력이 중요한 토큰의 값에만 의존하기 때문이다.
기술적 접근법
- **양자화 전략**: 키 캐시는 채널 단위, 벨류 캐시는 토큰 단위로 2비트 양자화.
- **캐시 분할**: 그룹화된 캐시와 잔여 캐시로 분할. 그룹화된 부분만 양자화, 잔여 부분은 원본 정밀도 유지.
- **계산 방식**: 어텐션 점수 계산 시, 그룹화된 캐시와 잔여 캐시를 타일드 행렬 곱셈으로 결합.
- **하이퍼파라미터**: 그룹 크기(G)와 잔여 길이(R) 실험. 그룹 크기 32, 64가 성능에 유리함을 확인.
- **모델 적용**: Llama-2, Falcon, Mistral 모델에 적용 가능.
주요 결과
- **메모리 절감**: Llama-2-7B에서 2.6× 감소 (모델 가중치 포함).
- **배치 크기 증가**: 최대 4× 증가.
- **처리량 향상**: 실제 LLM 추론 워크로드에서 2.35×~3.47× 향상.
- **정확도 유지**: GSM8K 등 어려운 생성 작업에서도 정확도 저하 없이 성능 유지.
의의 및 한계
KIVI는 KV 캐시 양자화의 기초적인 분포 분석을 통해, 기존 연구에서 간과되었던 키-벨류 캐시의 차이를 명확히 규명하고, 이를 기반으로 실용적인 2비트 양자화 알고리즘을 제안한 점에서 학술적 의의가 있다. 또한, 튜닝 없이 적용 가능한 plug-and-play 방식으로, 실제 서비스 환경에서 즉시 활용 가능하다는 실용적 가치를 가진다. 다만, 그룹 크기와 잔여 길이에 따라 성능이 변동하며, 특정 조건에서 최적의 결과를 얻기 위해 추가 실험 필요. 또한, 더 큰 모델(예: 540B 파름)에서의 성능 검증이 미흡하다는 한계가 있다.
실용적 활용
KIVI는 대규모 언어 모델의 배치 추론 성능을 향상시키는 데 유용하며, 특히 GPU 메모리 제한이 있는 클라우드 인프라, 실시간 대화형 서비스, 멀티유저 지원 시스템 등에서 활용 가능하다. 추론 속도 향상과 메모리 절감을 통해 비용 효율적인 모델 배포를 가능하게 한다.