Skip to main content

TurboQuant KV cache compression for LLM inference — cuTile GPU kernels

Project description

turboquant-gpu

TurboQuant-GPU

5.02x KV cache compression for LLM inference — cuTile kernels with automatic PyTorch fallback.

pip install turboquant-gpu

Works on any NVIDIA GPU. Uses cuTile kernels when available, otherwise falls back to PyTorch automatically — no driver upgrades or manual config needed.

quick start

from transformers import AutoModelForCausalLM, AutoTokenizer
from turboquant_gpu import TurboQuantEngine
import torch

model_id = "mistralai/Mistral-7B-v0.1"
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="cuda")
tok   = AutoTokenizer.from_pretrained(model_id)

engine = TurboQuantEngine(head_dim=128, total_bits=3, device="cuda")
result = engine.generate(model, tok, "The University of Waterloo is known for ")

print(result["text"])
print(f"{result['tokens']} tokens | {result['stats']['ratio']:.2f}x compression")

install

pip install turboquant-gpu

For cuTile acceleration (optional, requires CUDA 13.0+ driver):

pip install cuda-tile[tileiras] --extra-index-url https://pypi.nvidia.com

If you skip cuda-tile or your driver is older, everything still works via PyTorch.

how it works

Implements the TurboQuant algorithm:

  1. normalize + rotate — random orthogonal rotation (Pi) makes coordinates near-Gaussian
  2. Lloyd-Max quantize — optimal 3-bit scalar quantization against N(0, 1/d), shared codebook for K and V

For HuggingFace integration, keys and values are both compressed and decompressed via fused kernels — a single kernel launch compresses both K and V, and a single launch decompresses both. The reconstructed FP16 tensors are packed into a standard DynamicCache that HuggingFace's attention uses directly. No model changes needed.

The package also ships fused attention kernels with QJL bias correction (2-bit Lloyd-Max keys + 1-bit sign sketch of the quantization residual). These perform scoring, online softmax, and V accumulation in one kernel with on-chip V decompression. They're fully implemented but not yet wired into the HuggingFace path — integrating them requires replacing the model's internal attention, which is model-specific. This is a candidate for a cuTile Gym contribution.

step-by-step api

engine = TurboQuantEngine(head_dim=128, total_bits=3, device="cuda")

# after model prefill:
compressed = engine.compress_kv_cache(out.past_key_values)
cache      = engine.build_cache(compressed)
stats      = engine.compression_stats(out.past_key_values)

# or just do it all in one call:
result = engine.generate(model, tokenizer, "your prompt here")

# auto-tune for your specific GPU:
engine.auto_tune(seq_len=512)

gpu support

Written in cuTile for cross-architecture portability. Falls back to PyTorch if cuTile or a compatible driver isn't available.

GPU cuTile kernels PyTorch fallback
A100 (Ampere, sm_80) CUDA 13.2+ driver always works
H100 (Hopper, sm_90) not yet supported by tileiras always works
RTX 4090 (Ada, sm_89) CUDA 13.2+ driver always works
B200/B300 (Blackwell, sm_100) CUDA 13.0+ driver always works
Any other CUDA GPU depends on tileiras always works

kernels

HuggingFace path (used by default):

kernel what it does
compress_kv_3bit fused K+V compression, 3-bit shared codebook, single launch
decompress_kv_3bit fused K+V decompression, single launch
compress_values_3bit / 2bit separate fallback for K or V individually
decompress_3bit / 2bit separate fallback decompression

Fused attention path (included, not in HuggingFace API):

kernel what it does
compress_keys_2bit_qjl 2-bit Lloyd-Max + 1-bit QJL signs for keys
fused_attention QJL-corrected scores + online softmax + V accumulation
fused_attention_vfused_3bit same + on-chip V decompression from compressed indices
attention_scores score-only (no softmax), for debugging

license

MIT

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_gpu-0.1.7.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

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

turboquant_gpu-0.1.7-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file turboquant_gpu-0.1.7.tar.gz.

File metadata

  • Download URL: turboquant_gpu-0.1.7.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for turboquant_gpu-0.1.7.tar.gz
Algorithm Hash digest
SHA256 05ded2151acee3331f73919ca7bc58a449b64b33158c084337de941b10417a50
MD5 c7aa95994d52e10c9e7b4300376e2ef2
BLAKE2b-256 75066362a94abdcb9944468bb9673e5d8767b3b8459c081e9a6606dd6f03841e

See more details on using hashes here.

File details

Details for the file turboquant_gpu-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: turboquant_gpu-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.1

File hashes

Hashes for turboquant_gpu-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ddc09651b2b18fe34817f67a780251e7c2df521c4aa08c3c6ef92fc676205bcc
MD5 e05a9baef289d6082088f341b717d0f8
BLAKE2b-256 0b03166d7d9cf3819db5976db86cc2fdd925e7371fd7f149823bac7a32b169b0

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