Sub topic 2
Running offline inference with the LLM class
This chapter assumes you know basic Python, can run commands in a terminal, and have vLLM installed (pip install vllm). You should also understand that a chat model receives a list of role-tagged messages and returns text. You need a GPU with enough memory for the model. The example uses a 1.5B-parameter model, which fits on a 6 GB or larger card.
Offline inference with the LLM class
The LLM class loads a model once and generates text inside your own Python process. Use it for batch jobs such as evaluation or bulk generation.
from vllm import LLM, SamplingParams
llm = LLM(
model="Qwen/Qwen2.5-1.5B-Instruct",
max_model_len=4096,
gpu_memory_utilization=0.85,
)
params = SamplingParams(temperature=0.7, top_p=0.95, max_tokens=128)
outputs = llm.generate(["Explain the KV cache in two sentences."], params)
print(outputs[0].outputs[0].text)Passing a list of prompts lets vLLM batch them together, which raises throughput.
Controlling generation with SamplingParams
SamplingParams sets how the model picks each next token.
temperature: higher values give more varied output. Use 0 for greedy decoding.top_p: samples only from the smallest set of tokens whose probabilities sum to this value.max_tokens: caps the length of the reply.
Chat templates and instruction-tuned models
An instruction-tuned model expects its messages wrapped in a specific template, with special tokens that mark each role. llm.chat applies that template for you, so you pass messages instead of raw text.
messages = [{"role": "user", "content": "What is continuous batching?"}]
outputs = llm.chat(messages, params)
print(outputs[0].outputs[0].text)Serving an OpenAI-compatible API with vllm serve
The vllm serve command starts an HTTP server that accepts the same requests as the OpenAI API. Existing client code works after you change base_url.
vllm serve Qwen/Qwen2.5-1.5B-Instruct --max-model-len 4096 --port 8000from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
response = client.chat.completions.create(
model="Qwen/Qwen2.5-1.5B-Instruct",
messages=[{"role": "user", "content": "What is continuous batching?"}],
max_tokens=150,
)
print(response.choices[0].message.content)Tuning memory and concurrency
The KV cache stores attention data for every token in flight, and it uses the GPU memory left after the weights load. Three settings control it:
| Setting | Effect |
|---|---|
| `gpu_memory_utilization` | Fraction of GPU memory vLLM may use. Raise it for more cache, lower it to leave room for other processes. |
| `max_model_len` | Longest sequence (prompt plus reply). Lower values free cache for more concurrent requests. |
| `max_num_seqs` | Maximum sequences scheduled at once. Raise it for throughput, but it needs more cache. |
If the server reports that the cache cannot hold one full-length request, lower max_model_len or raise gpu_memory_utilization.
Exercise
A 24 GB GPU runs out of cache when many long requests arrive at once. Which two settings would you change first, and why?
Show answer
Lower maxmodellen so each sequence reserves less cache, and lower maxnumseqs so fewer sequences compete for it. Raising gpumemoryutilization is another option if other processes do not need the memory.