A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using transformers, accelerate and bitsandbytes
LLM.int8() makes 8-bit inference of 175B-class transformers possible with no measurable quality loss. It handles the systematic outlier features that emerge beyond about 6B parameters through mixed-precision decomposition: hidden-state columns with values of magnitude 6 or more are multiplied in FP16, and everything else goes through vector-wise int8 quantization before being dequantized and summed. The method is built on FP32/FP16/BF16 trade-offs and absmax and zero-point quantization, and it halves memory relative to half precision. On OPT-175B and BLOOM-176B, lm-eval-harness benchmarks stay within standard error. It is integrated into Hugging Face transformers via bitsandbytes and Accelerate.
Related posts
- Smaller is better: Q8-Chat, an efficient generative AI experience on Xeon
Hugging Face easyNoviceSmoothQuant enables accurate 8-bit (INT8) post-training quantization of LLMs by mathematically shifting activation outliers into the weights, which makes Transformer layers quantization-friendly. Intel applied it to OPT, LLaMA, Alpaca, Vicuna, BloomZ and MPT-7B-chat models, roughly halving model size. On many LM Evaluation Harness tasks the quantized models matched or slightly improved accuracy, and most remaining tasks degraded only marginally. Running on a single 32-core Sapphire Rapids Xeon socket at batch size 1, the quantized models deliver low per-token latency, demonstrated in the Q8-Chat Space, with Optimum Intel integration via Intel Neural Compressor planned.
- Making LLMs lighter with AutoGPTQ and transformers
Hugging Face mediumAdeptAutoGPTQ is now integrated into Hugging Face Transformers (via Optimum), letting users quantize and run LLMs at 8, 4, 3 or 2 bits with the GPTQ post-training method. GPTQ needs a calibration dataset and quantizes only the weights: int4 weights are dequantized on the fly in a fused kernel while compute stays in fp16. That gives nearly 4x memory savings with negligible accuracy loss at 4-bit. On an A100 at batch size 1, Llama-2-7B with the exllama kernel dropped peak memory from about 29 GB to 10.5 GB with slightly better per-token latency than fp16. Text-Generation-Inference can now serve a 70B model on a single 80GB GPU and PEFT adapters can be fine-tuned on top, but the kernels slow down at larger batch sizes and W4A8 or Triton kernels remain open work.
- Fine-tuning LLMs to 1.58bit: extreme quantization made easy
Hugging Face hardMasterExisting LLMs can be fine-tuned into BitNet's 1.58-bit ternary format (-1, 0, 1) instead of pre-training from scratch, and a Llama3 8B fine-tuned this way on 100B tokens outperforms Llama 1 7B on MMLU. Training uses BitLinear layers with absmean per-tensor weight quantization, absmax per-token 8-bit activation quantization, layer normalization before activation quantization, and a straight-through estimator implemented via detach(). Inference quantizes weights without rescaling and runs matrix multiplication through efficient kernels. The resulting models load in Transformers through a new 'bitnet' quantization method with no API changes, and pre-training experiments show layer normalization lowering validation perplexity from 6.3 to 5.9.
- Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA
Hugging Face mediumAdeptbitsandbytes now integrates 4-bit quantization into Hugging Face transformers, letting most accelerate-loadable models (text, vision, multimodal) run in 4-bit and be fine-tuned via QLoRA, which backpropagates through a frozen 4-bit base model into LoRA adapters. QLoRA stores weights in NF4 and dequantizes to bfloat16 for compute, adds double quantization (saving ~0.4 bits per parameter) and paged optimizers, and matches 16-bit fine-tuning while fitting a 33B model on a 24GB GPU or 65B on a 48GB GPU. The post explains FP8/FP4 bit layouts and shows BitsAndBytesConfig usage. Recommended defaults are NF4 for precision, double quant for memory pressure, and a 16-bit compute dtype for speed.
- Optimizing your LLM in production
Hugging Face mediumAdeptServing billion-parameter LLMs efficiently comes down to three levers: lower precision, Flash Attention, and inference-friendly architectures such as ALiBi, rotary embeddings, Multi-Query Attention and Grouped-Query Attention. Weights need roughly 2 GB of VRAM per billion parameters in bfloat16, and measurements on OctoCoder (~15B parameters) show peak memory dropping from about 29 GB in bfloat16 to 15 GB with 8-bit and 9.5 GB with 4-bit bitsandbytes quantization, with little output change but slightly slower inference. Flash Attention addresses self-attention's quadratic memory growth with sequence length by restructuring the computation to make better use of GPU memory. Code examples in Hugging Face Transformers accompany each technique.