Module 7 · AI infrastructureLesson 75 of 888 minAdvanced

Inference optimization

Make models cheaper and faster to run by storing weights in fewer bits, doing less repeated work, and using hardware better.

Try it

Start with the 70B model in 16-bit on the 80 GB GPU. Switch to 8-bit, then 4-bit, and watch it fit and get faster.

Running large models is expensive. Inference optimization is about getting the same answers using less memory, less time and fewer GPUs.

Quantization

Store weights (and sometimes activations) in fewer bits:

PrecisionBytes per weight70B model
16-bit (FP16/BF16)2140 GB
8-bit (INT8/FP8)170 GB
4-bit (INT4, GPTQ, AWQ)0.535 GB

Smaller weights mean the model fits on fewer or cheaper GPUs, and because decoding is memory-bandwidth bound, it also generates faster. Good 8-bit quantization is nearly lossless; 4-bit costs a little quality, which matters more for some tasks than others. Always evaluate on your own use case.

Doing less work

  • KV caching: don't recompute attention for earlier tokens (its own lesson).
  • Prefix caching: many requests share the same system prompt; compute it once and reuse it.
  • Speculative decoding: a small, fast "draft" model guesses several tokens ahead, and the big model checks them all in one pass. Accepted guesses come almost free.

Using the GPU better

  • Batching and continuous batching: serve many users per pass.
  • Paged KV cache: fit more concurrent requests in the same memory.
  • Fused kernels like FlashAttention: fewer trips to memory.

Smaller models

  • Distillation: train a small model to imitate a large one on your task.
  • Routing: send easy prompts to a small model (coming soon).

The biggest savings often come from choosing the smallest model that does the job well, then quantizing and batching it.

Quick check

What does 4-bit quantization of a 16-bit model mainly do?