Sub topic 10
Tuning Memory and Concurrency
Assume you know basic Python, command-line use, GPUs, and HTTP APIs. You should also understand that vLLM generates text one token at a time after processing the prompt.
What consumes GPU memory
A vLLM server stores the model weights in GPU memory. During generation, it also stores a KV cache, which holds attention data for tokens already processed. The cache grows with the number of active requests and the number of tokens in those requests.
The main tuning settings are:
| Setting | Controls | Typical trade-off |
|---|---|---|
| `gpu_memory_utilization` | Fraction of GPU memory vLLM may use | More capacity, but a higher risk of out-of-memory errors |
| `max_model_len` | Maximum total tokens per request | Longer context, but more memory reserved for possible requests |
| `max_num_seqs` | Maximum active sequences | More concurrency, but more KV-cache pressure |
| `max_num_batched_tokens` | Tokens processed together during scheduling | Higher prompt throughput, but larger temporary memory use |
max_model_len includes both the input prompt and generated output. A value of 8192 does not mean every request uses 8192 tokens, but it allows that length and affects memory planning.
A practical starting command is:
vllm serve Qwen/Qwen2.5-1.5B-Instruct \
--gpu-memory-utilization 0.85 \
--max-model-len 4096 \
--max-num-seqs 8 \
--max-num-batched-tokens 4096 \
--port 8000Start conservatively. Raise one limit at a time while measuring latency, throughput, and errors.
A tuning workflow
Use the workload to choose limits rather than copying values from another machine.
- Measure the usual prompt length and maximum prompt length.
- Set
max_model_lenslightly above the required input-plus-output length. - Set
max_num_seqsto the concurrency your application actually needs. - Set
gpu_memory_utilizationhigh enough to use available memory, but leave room for CUDA runtime overhead and other GPU processes. - Send a representative request mix, including long prompts and concurrent requests.
- Reduce limits if the server reports out-of-memory errors. Increase them only when GPU memory remains available and the workload benefits.
For interactive traffic, a smaller batch and fewer concurrent sequences can reduce time to the first token. For offline workloads, more active sequences usually improve total throughput, although individual requests may wait longer.
Choosing a setting
If long-context requests are rare, do not set max_model_len to the model's theoretical maximum. That can reduce the space available for concurrent requests. If a model barely fits even before serving requests, use quantization or tensor parallelism instead of forcing concurrency settings.
Exercise
A server handles eight short requests well but fails when two requests contain very long prompts. Which setting should you inspect first, and why?
Show answer
Inspect maxmodellen and the available KV-cache capacity. Long prompts consume KV-cache memory, so lowering maxmodellen to the required limit or reducing concurrency may prevent the failure.