Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 31 min read Deep dive mediumAdept

Optimizing your LLM in production

Serving 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.

Related posts

  • Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA

    Hugging Face mediumAdept

    bitsandbytes 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.

  • Making FlashAttention-4 faster for inference

    Modal hardMaster

    Modal engineers extended the FlashAttention-4 kernel for decode-heavy LLM inference on Blackwell GPUs through two kinds of change: new parallelism strategies, such as split-KV parallelism across the key/value dimension, and support for irregular memory access via cp.async in place of TMA bulk loads. FP8 inputs gave up to 1.16x throughput over bf16, likely limited by the softmax, which still runs at higher precision. Arbitrary KV page sizes help prefix sharing, reduce fragmentation and suit speculative decoding, and a transposed warp-level address-generation scheme made them up to 2.4x faster. Split-KV raised small-query memory throughput by up to 4.37x, keeping SMs busy that the original query-parallel design left idle.

  • Making LLMs lighter with AutoGPTQ and transformers

    Hugging Face mediumAdept

    AutoGPTQ 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.

  • Improving Hugging Face Training Efficiency Through Packing with Flash Attention 2

    Hugging Face hardMaster

    Hugging Face Transformers now supports padding-free packing of instruction-tuning examples with Flash Attention 2 via the new DataCollatorWithFlattening, which concatenates sequences and passes position_ids so flash_attn_varlen_func respects example boundaries and avoids cross-example attention. On 8 A100s, throughput doubled on FLAN (short, highly variable lengths) and rose 1.4x on OrcaMath for Llama2-7B, Mistral-7B and Granite-8B-code, with peak memory down 20% and 6% respectively. Validation loss matched padded training because the number of optimization steps is unchanged. Usage requires loading the model with attn_implementation="flash_attention_2" and using the new collator, or setting padding_free=True on TRL's DataCollatorForCompletionOnlyLM. It works only for models that expose position_ids.

  • A Gentle Introduction to 8-bit Matrix Multiplication for transformers at scale using transformers, accelerate and bitsandbytes

    Hugging Face mediumAdept

    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.

The quest for software engineering knowledge.

Look:
Layout: