Every word an AI chatbot writes triggers billions of numbers moving across microscopic wires. Explore how quantization, caching, and smart batching shrink giant models onto everyday hardware.
Modern GPUs can multiply billions of numbers in a blink. The catch is getting those numbers out of memory fast enough.
When you ask an AI model a question, the model doesn’t look up an answer in a database. Instead, its knowledge lives inside billions of stored decimal numbers called parameters (or weights). A “70B” model stores 70 billion of them.
Generating a reply happens in two very different phases. First, during the prompt phase, the GPU reads your entire question at once in parallel—that part is fast and keeps the math engines busy. Next, during the generation phase, the model writes its reply one word-piece (token) at a time. To pick each new token, the GPU has to haul all 70 billion weights from its main memory bank (High-Bandwidth Memory, or HBM) into its tiny arithmetic cores, use each weight once, and repeat the whole trip for the next token.
Think of a lightning-fast chef standing at a tiny prep counter while the recipe encyclopedia sits in a warehouse down the hall. The chef isn’t slow at chopping—the runner carrying giant binders back and forth down the hallway is the bottleneck. Because GPUs spend most of token generation waiting on memory transfers rather than doing arithmetic, adding faster math chips barely helps, and naive deployments often leave 70–80% of the GPU’s compute sitting idle.
Adjust the model size, number precision, and simultaneous users to see how GPU memory fills up—and how much of each token’s generation time is spent waiting on the memory highway.
Instead of storing every weight with 32-bit scientific precision, quantization rounds numbers into compact integers—cutting memory and highway traffic by 2× to 8×.
Normally, neural networks are trained using FP32 (32-bit floating-point) or FP16 (16-bit) numbers—formats capable of expressing tiny fractions like 0.00048193. That extra precision helps during training when weights nudge by microscopic amounts. During everyday chat, however, all that decimal detail is largely overkill.
Quantization translates those sprawling decimals onto a short integer ruler—like INT8 (256 discrete steps from −128 to 127) or INT4 (just 16 steps). Fewer bits per weight means two huge wins at once: the model takes far less VRAM to store, and the memory runner hauls half or a quarter as many bytes per token.
Suppose your grocery receipt lists $19.94, $4.07, and $11.98. Rounding to $20, $4, and $12 drops exact cents, yet the total is $36.00 instead of $35.99—fast mental math, virtually zero error.
A raw camera file records millions of ultra-subtle color differences your eyes barely notice. Compressing it replaces similar shades with a smaller palette—shrinking file size 4× while keeping the photo sharp.
Fluffy winter coats take four giant trunks. Vacuum-sealing squeezes out empty air so the same wardrobe fits inside one carry-on—and takes one trip from baggage claim instead of four.
A pixel’s brightness works just like a model weight. Step down from smooth full precision to 4-bit (16 levels) or 2-bit (4 levels) to see where compression stays invisible—and where stair-step banding appears.
Quantization maps a range of floating-point weights onto whole integers using a step size called Scale (S) and an offset called Zero-Point (Z). Try dragging the outlier spike to see how one unusually large number stretches the ruler for every other weight.
| Weight role | Original float (x) | Stored integer (xq) | Restored value | Rounding gap |
|---|
You can quantize an already-trained model in minutes without retraining—a method called Post-Training Quantization (PTQ) using tools such as GPTQ or AWQ. To calibrate the step size S, engineers either measure activation ranges on the fly (dynamic quantization) or pass about 200 sample prompts through first (static quantization). When accuracy is critical, Quantization-Aware Training (QAT) simulates rounding errors during fine-tuning so the model adapts around them.
One important nuance from hardware benchmarks: smaller isn’t always lower-energy. On large models (5B+ parameters), 4-bit quantization saves massive memory with near-FP16 energy use. On tiny models under 3B, however, unpacking 4-bit integers back into floats can raise energy use by 25–56%—whereas simply grouping 8–64 requests into a batch reduces energy per token by 84–96%.
Caching past tokens avoids repeating billions of calculations—if you don’t waste 70% of your memory reserving empty space.
Remember that LLMs generate one token at a time. To pick word #201, the attention mechanism checks how word #200 relates to every single earlier word in the conversation. Without caching, the GPU would have to recompute the internal summary vectors—called Keys (K) and Values (V)—for words #1 through #200 all over again on every single step.
KV caching fixes that by keeping sticky-note summaries in GPU memory. You compute Keys and Values once per word, file them away, and only compute the brand-new word at each step. Problem solved? Almost. As chats grow longer and dozens of users connect at once, the KV cache balloons.
Older serving systems stored each user’s KV cache as one unbroken, contiguous strip of memory—and because nobody knows ahead of time whether a reply will be 20 words or 2,000 words, they pre-reserved giant chunks for the worst-case maximum length. That left huge reserved gaps unused, wasting 60–80% of KV memory. PagedAttention (introduced by vLLM, inspired by how your laptop’s operating system manages RAM) breaks each conversation into small fixed-size KV blocks (typically 16 tokens each) and tracks them with a Block Table—letting scattered memory slots hold one continuous chat with 96.3% efficiency.
Watch 24 physical GPU memory slots (4 tokens per slot) as two users generate answers—and see what happens when a third user arrives.
Moving 140 GB of weights to serve one user costs almost the same memory traffic as serving sixteen. The secret is swapping finished requests out after every single token.
Because loading weights from GPU memory dominates token generation, engineers group multiple users into a batch so one trip down the memory highway produces a new token for everybody at once. But standard static batching acts like a tour bus: you wait for all seats to fill, pad shorter prompts with dummy tokens so everyone lines up, and hold the entire bus until the user asking for a 500-word essay finishes—leaving seven users who only needed 30 words staring at a blinking cursor.
Continuous batching (also called iteration-level scheduling) turns the bus into a high-speed revolving door. At every single token step, the scheduler checks which requests hit an end-of-sequence marker (<eos>), sends them home immediately, and slots waiting requests from the queue straight into the freed GPU lanes on the very next iteration. In production, this single change delivers 2× to 10× higher throughput on identical GPUs.
Four GPU lanes process 6 user requests of mixed lengths (A: 3 tokens, B: 10, C: 4, D: 6, plus queued E: 4 and F: 3). Step through or play the race to watch idle padding disappear.
You rarely build quantization, PagedAttention, and continuous batching from scratch. Production teams rely on specialized serving engines tailored to their hardware.
All three implement continuous batching and quantization—their strengths differ by hardware lock-in and ecosystem.
| Framework | Key optimizations | Strengths | Tradeoffs |
|---|---|---|---|
| vLLM | PagedAttention, continuous batching, FlashAttention-2, GPTQ/AWQ quantization | Broad model support, OpenAI-compatible API, industry-standard throughput without complex tuning | Slightly lower peak single-card tuning than custom-compiled NVIDIA engines |
| TensorRT-LLM | Deep NVIDIA kernel fusion, FP8/INT8/INT4 quantization, in-flight batching | Maximum speed and hardware squeeze on dedicated NVIDIA GPUs | Requires more setup and compilation; tightly coupled to NVIDIA hardware |
| Text Generation Inference (TGI) | Continuous batching, optimized attention (SDPA/Flash), quantization | Seamless Hugging Face Hub integration and straightforward deployment | Tied closely to the Hugging Face model ecosystem |
Select a real-world goal to see which combination of optimization techniques makes the biggest difference first.