Sub topic 3
Fitting larger models with quantization
This chapter assumes you have run vllm serve, know what the KV cache is, and know how much memory your GPU has.
Quantization stores model weights in fewer bits. A 7B-parameter model in FP16 needs about 2 bytes per parameter, or roughly 14 GB. In 4-bit form it needs about 0.5 bytes per parameter, or roughly 4 GB, plus overhead.
vllm serve Qwen/Qwen2.5-7B-Instruct-AWQ \
--quantization awq \
--max-model-len 4096 \
--gpu-memory-utilization 0.85Scaling across GPUs
Tensor parallelism splits each layer's weight matrices across GPUs. Use --tensor-parallel-size 4 when a model does not fit on one GPU. The GPUs exchange results on every step, so they need fast interconnects.
Reusing shared prompt prefixes
Prefix caching keeps the KV cache for an identical leading text, such as a long system prompt. Add --enable-prefix-caching. It helps only when requests share the same starting tokens.
Monitoring a running server
curl -s http://localhost:8000/metrics | grep '^vllm:'Watch the running and waiting request counts. A growing waiting count means the server cannot keep up.
Benchmarking throughput and latency
import time
from concurrent.futures import ThreadPoolExecutor
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
MODEL = "Qwen/Qwen2.5-7B-Instruct-AWQ"
def one_request(_):
start = time.perf_counter()
r = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "Summarize KV caching in one paragraph."}],
max_tokens=128,
)
return time.perf_counter() - start, r.usage.completion_tokens
started = time.perf_counter()
with ThreadPoolExecutor(max_workers=16) as pool:
results = list(pool.map(one_request, range(64)))
wall = time.perf_counter() - started
latencies = sorted(lat for lat, _ in results)
tokens = sum(t for _, t in results)
print(f"p50 latency: {latencies[len(latencies) // 2]:.2f} s")
print(f"output tokens/s: {tokens / wall:.1f}")Deploying with the container image
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
-p 8000:8000 \
--ipc=host \
vllm/vllm-openai:latest \
--model Qwen/Qwen2.5-7B-Instruct-AWQServing LoRA adapters
A LoRA adapter is a small set of extra weights that changes a base model's behavior. Serve several adapters on one base model:
vllm serve Qwen/Qwen2.5-7B-Instruct \
--enable-lora \
--lora-modules support=/models/support-loraSend requests with model="support" to use the adapter.
Speculative decoding for lower latency
A small draft model proposes several tokens, and the main model checks them in one pass. This lowers latency when outputs are predictable. Flag names change between vLLM releases, so check the documentation for your version.
| Technique | Problem it solves | Main cost |
|---|---|---|
| Quantization | Weights do not fit in memory | Possible quality loss |
| Tensor parallelism | Model does not fit on one GPU | Inter-GPU communication |
| Prefix caching | Repeated prompt prefixes | Only helps with shared starts |
Exercise
A 13B-parameter model in FP16 needs about 26 GB for weights. Roughly how much memory do the weights need after 4-bit quantization, and is that enough for a 24 GB GPU?
Show answer
13 billion parameters at about 0.5 bytes each is about 6.5 GB, plus overhead. That fits on a 24 GB GPU, with room left for the KV cache.