Skip to main content

Near-optimal KV-cache compression for HuggingFace transformers using Lloyd-Max + QJL quantization

Project description

TurboQuant

Near-optimal KV-cache compression for HuggingFace transformers. Reduces KV cache memory by ~8x at 2 bits with attention quality within ~2.7x of the Shannon limit.

Installation

pip install turboquant-explained

Install PyTorch separately for your hardware first:

  • CPU: pip install torch
  • CUDA 12.x: pip install torch --index-url https://download.pytorch.org/whl/cu121
  • See pytorch.org for all variants.

Quick Start

from transformers import AutoModelForCausalLM, AutoTokenizer
import turboquant

model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")

# 2-bit keys + values → ~8x memory reduction
cache = turboquant.patch_model(model, b_key=2, b_value=2)

inputs = tokenizer("Hello, world!", return_tensors="pt").to("cuda")
output = model.generate(**inputs, past_key_values=cache, max_new_tokens=200)
print(tokenizer.decode(output[0]))

patch_model reads head_dim from model.config automatically — no manual configuration needed.

Memory savings at a glance

Bit-width Memory vs FP16 Typical use
b=2 ~1/8 Long contexts, aggressive compression
b=3 ~3/16 Balanced quality / savings
b=4 ~1/4 Near-lossless

For implementation details and theory, see the full README on GitHub.

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

turboquant_explained-0.1.1.tar.gz (35.9 MB view details)

Uploaded Source

Built Distribution

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

turboquant_explained-0.1.1-py3-none-any.whl (27.1 MB view details)

Uploaded Python 3

File details

Details for the file turboquant_explained-0.1.1.tar.gz.

File metadata

  • Download URL: turboquant_explained-0.1.1.tar.gz
  • Upload date:
  • Size: 35.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for turboquant_explained-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5d78bbddea67d78c9b4e6717b7c269e4b563816da6a1547279b6b028ea1cd2c3
MD5 ac4e6e095d53724599d5c2810317bac8
BLAKE2b-256 8df0237ddda6286d5821f36f23a87558c660b03a26c50bbee703de1760917cb2

See more details on using hashes here.

File details

Details for the file turboquant_explained-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for turboquant_explained-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50cd2c73e93094b17fe862152c0c5f000713ca7c04eb9c7fdd7f399271a92426
MD5 98022867aa71dcb2c6ccc0f6e533e408
BLAKE2b-256 53ad9e7461e082bcf2a2d68ab8bda1869d3faa0f2ebbec08e60d1d13ab55ffaf

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