Skip to main content

ICW Key-Value Quantization library

Project description

Dynamic INT8 KV Cache Quantization for Modern LLMs

1. Overview

This module provides a lightweight, dynamic patching mechanism to enable INT8 quantization for the Key-Value (KV) cache in modern Hugging Face language models.

By quantizing the KV cache from float16/bfloat16 to int8 on-the-fly, this solution dramatically reduces the memory footprint required for long sequences. This allows models to handle significantly longer context lengths on the same hardware, unlocking new capabilities for applications requiring extended context.

The quantization is performed per-head, which preserves more information compared to per-tensor quantization, leading to better model performance.

2. Features

  • Unlock Longer Contexts: Handle 2-4x longer sequences on the same GPU.
  • Broad Support: Works out-of-the-box for a wide range of popular models, including Llama, Mistral, Gemma, Phi-3, and Qwen2.
  • Dynamic Patching: No complex model conversion is required. The patch is applied at runtime to a standard Hugging Face model with a single function call.
  • Seamless Integration: Works within the existing PyTorch and Hugging Face ecosystem.
  • High Precision: Per-head quantization ensures better preservation of model performance.

3. File Structure

The entire solution is self-contained in a single file, making it incredibly easy to integrate.

icw-kv-quant/
├── pyproject.toml
├── README.md
├── LICENSE
├── icw_kv_quant.py


4. How It Works

The solution uses a technique called "monkey-patching" to modify the behavior of a model at runtime.

  1. A standard model (e.g., LlamaForCausalLM, GemmaForCausalLM) is loaded from the Hugging Face Hub.
  2. Our patch_model_with_int8_kv_cache function is called on the model instance.
  3. This function automatically detects the model type, iterates through its modules, finds all compatible attention layers (e.g., LlamaAttention, GemmaAttention, Phi3Attention), and replaces their default .forward() method with our custom, memory-efficient implementation.
  4. The new forward method intercepts the Key and Value states, quantizes them to int8 using a per-head scaling factor, and saves the compact int8 tensor and its scale to the cache. On subsequent steps, it dequantizes them back to a float tensor for the attention calculation.

This process is transparent to the end-user, who interacts with the model as usual but benefits from the massive memory savings.

5. Usage Example

from icw import patch_model_with_int8_kv_cache

Applying the patch is a simple, two-step process that works on any supported model.

import torch
from transformers import AutoModelForCausalLM

# Assuming icw_kv_quant.py is in your project's 'icw' directory
<<<<<<< HEAD
from icw_kv_quant import patch_model_with_int8_kv_cache
=======
from icw.attention import patch_model_with_int8_kv_cache
>>>>>>> 7059d73 (Save local changes before rebase)

# 1. Load any standard supported model
# model_name = "meta-llama/Llama-2-7b-chat-hf"
# model_name = "mistralai/Mistral-7B-Instruct-v0.2"
model_name = "google/gemma-2b"
model = AutoModelForCausalLM.from_pretrained(model_name)

# 2. Apply the universal patch with a single function call
patch_model_with_int8_kv_cache(model_name)

# 3. Done! The model is now ready for long-context inference.
model.to("cuda")

print("Model patched successfully and is ready for long-context inference!")

6. Benchmark Results

The following benchmark was run on a TinyLlama model to compare the baseline bfloat16 model versus our patched int8 KV cache model.

The results clearly demonstrate two key points:

  1. Memory Efficiency: The patched model successfully handles a context of 6144 tokens on hardware where the baseline model runs out of memory (OOM).
  2. Performance Trade-off: This memory saving comes with a computational cost, resulting in higher inference latency.
Context Baseline Mem (MB) Patched Mem (MB) Mem Saved (%) Baseline Latency (ms) Patched Latency (ms) Latency Overhead (%)
512 2226.82 2221.34 0.2% 656.54 2015.78 207.0%
1024 2487.33 2476.35 0.4% 871.51 3241.65 272.0%
2048 3498.34 3476.36 0.6% 2115.94 8587.53 305.8%
3072 5180.23 5126.88 1.0% 3986.98 7831.22 96.4%
4096 7466.37 7421.40 0.6% 11153.75 23844.05 113.8%
6144 OOM 13953.80 UNLOCKED OOM 94114.13 N/A
8192 OOM OOM N/A OOM OOM N/A

7. Limitations & Future Work

  • Model Support: The patch currently supports Llama, Mistral, Gemma, Phi-3, and Qwen2 family models. It can be extended to other architectures by adding their respective attention modules to the patcher.
  • Latency Overhead: The on-the-fly quantization and dequantization steps introduce a noticeable computational overhead, as shown in the benchmark results. This solution is best suited for applications where handling long contexts is more critical than achieving the lowest possible latency. For latency-sensitive applications, a fully-fused CUDA kernel could be developed to minimize this overhead.

Installation

pip install icw-kv-quant

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

icw_kv_quant-0.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

icw_kv_quant-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file icw_kv_quant-0.1.0.tar.gz.

File metadata

  • Download URL: icw_kv_quant-0.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for icw_kv_quant-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f20e36cf89280e4ffd9eaf52f07bc0f5797a4a9a7f97f4edb423980c6cdaa6da
MD5 9ba94e08fc9f58c5dbd7d75150300433
BLAKE2b-256 0a1be5ff6ab0f9a7ceb4027335b3c1a5c7ad47806c8626523874738e44f1eec2

See more details on using hashes here.

File details

Details for the file icw_kv_quant-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: icw_kv_quant-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.6

File hashes

Hashes for icw_kv_quant-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b72c55958dc6835fa5ee8ac7790244ff676035ecd1130d5c397e5379d665425
MD5 7f389d12ed7b5373b6cc3fd0a0794795
BLAKE2b-256 27b0959a94ac086ebe03a31fdcd0920036fb18f124a26156d2fd7fcb063cf519

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page