Sub topic 17
Serving LoRA adapters
This chapter assumes you can start vllm serve, call it with the OpenAI client, and set gpu_memory_utilization and max_model_len. It also assumes you know what fine-tuning is. You do not need to have trained an adapter yourself.
Why serve adapters instead of merged models
LoRA (low-rank adaptation) fine-tunes a model by training two small matrices per targeted layer instead of changing the full weights. At inference time, the layer output is the base output plus a low-rank correction. The adapter file holds only those small matrices. It is often tens of megabytes, while the base model is gigabytes.
You have two ways to deploy a fine-tuned model:
| Approach | What you load | Cost when you have many variants |
|---|---|---|
| Merge the adapter into the base | One full model copy per variant | One server or one GPU allocation per variant |
| Serve adapters on one base | One base model plus many small adapters | One server, one copy of the base weights |
vLLM supports the second approach. Requests for different adapters, and requests for the plain base model, can share the same batch. The KV cache and the scheduler are shared too. This pays off when you have several tenants, tasks, or languages and each has low or uneven traffic.
How it works
The base weights stay fixed in GPU memory. For each request, vLLM applies the matching adapter's low-rank correction to that request's tokens. Two settings control how many adapters can be active:
max_lorasis the number of adapters that can be used in a single batch.max_cpu_lorasis the number of adapters cached in CPU memory. Adapters outside the GPU slots are swapped in when a request needs them.
flowchart LRThe diagram below shows the request path.
Prepare an adapter
An adapter must be trained on the same base model you serve. An adapter trained on Qwen2.5-1.5B-Instruct will not work correctly on a different base, even a similar one. A Hugging Face PEFT adapter directory normally contains:
adapter_config.json, which records the rank (r),target_modules, and the base model name.adapter_model.safetensors, which holds the trained matrices.
Check the rank before you serve. vLLM rejects adapters whose rank is larger than max_lora_rank, which defaults to 16.
python -c "import json; c=json.load(open('/adapters/sql-lora/adapter_config.json')); print(c['r'], c['base_model_name_or_path'])"Serve adapters from the command line
Enable LoRA and register each adapter as name=path. The path can be a local directory or a Hugging Face repository ID.
vllm serve Qwen/Qwen2.5-1.5B-Instruct \
--enable-lora \
--lora-modules sql-lora=/adapters/sql-lora support-lora=/adapters/support-lora \
--max-loras 2 \
--max-lora-rank 16 \
--max-cpu-loras 8 \
--max-model-len 4096Each registered name appears in the model list. Confirm with a request:
curl -s http://localhost:8000/v1/modelsTo use an adapter, put its name in the model field. Nothing else about the client changes.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
for name in ["Qwen/Qwen2.5-1.5B-Instruct", "sql-lora", "support-lora"]:
response = client.chat.completions.create(
model=name,
messages=[{"role": "user", "content": "Show the 5 newest orders."}],
temperature=0.0,
max_tokens=100,
)
print(name, "->", response.choices[0].message.content)If the name matches neither the base model nor a registered adapter, the server returns a 404 error.
Use adapters in offline inference
In the Python API, set enable_lora=True on the LLM object. Then pass a LoRARequest with each call. The request takes a name, an integer ID that must be unique per adapter, and a path.
from vllm import LLM, SamplingParams
from vllm.lora.request import LoRARequest
llm = LLM(
model="Qwen/Qwen2.5-1.5B-Instruct",
enable_lora=True,
max_lora_rank=16,
max_model_len=4096,
gpu_memory_utilization=0.85,
)
params = SamplingParams(temperature=0.0, max_tokens=100)
sql = LoRARequest("sql-lora", 1, "/adapters/sql-lora")
messages = [{"role": "user", "content": "Show the 5 newest orders."}]
base_out = llm.chat(messages, params)
lora_out = llm.chat(messages, params, lora_request=sql)
print("base:", base_out[0].outputs[0].text)
print("lora:", lora_out[0].outputs[0].text)Comparing the base and adapter outputs for the same prompt is a quick check that the adapter is applied. With temperature=0.0 the comparison is deterministic enough to read.
Load and unload adapters at runtime
Restarting a server to add an adapter drops in-flight requests. vLLM has an opt-in mechanism for changing adapters while running. Set an environment variable at startup, then call the admin endpoints.
export VLLM_ALLOW_RUNTIME_LORA_UPDATING=True
vllm serve Qwen/Qwen2.5-1.5B-Instruct --enable-lora --max-lora-rank 16curl -X POST http://localhost:8000/v1/load_lora_adapter \
-H "Content-Type: application/json" \
-d '{"lora_name": "sql-lora", "lora_path": "/adapters/sql-lora"}'
curl -X POST http://localhost:8000/v1/unload_lora_adapter \
-H "Content-Type: application/json" \
-d '{"lora_name": "sql-lora"}'Sizing and tuning
Adapters cost memory in two places, and both come out of the budget you set earlier.
- GPU slots: vLLM reserves space for
max_lorasadapters at up tomax_lora_rank. Settingmax_lora_rankto 64 when your adapters use rank 8 wastes memory and slows the adapter computation. - Host memory:
max_cpu_lorasadapters are held in CPU RAM. It must be at leastmax_loras.
Practical guidance:
- Set
max_lora_rankto the largest rank you actually serve, not a large safe number. - Set
max_lorasto the number of adapters you expect in one batch. If traffic is spread over many adapters, a larger value reduces swapping but uses more GPU memory. - Lower
gpu_memory_utilizationslightly if the server fails at startup after you enable LoRA. LoRA slots reduce the room left for the KV cache. - Expect some throughput loss compared with the plain base model, since the extra computation runs on every targeted layer. Measure it with the benchmark from the earlier chapter instead of assuming a number.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that LoRA rank exceeds the maximum | Adapter `r` is larger than `max_lora_rank` | Raise `--max-lora-rank` to at least the adapter's `r` |
| 404 for the model name | The adapter name was not registered, or loading failed | Check `/v1/models` and the startup log |
| Output looks like the base model | The request used the base model name, or the wrong `LoRARequest` | Send the adapter name in `model`, or pass `lora_request` |
| Garbled output | Adapter trained on a different base model | Match `base_model_name_or_path` to the served model |
| Out-of-memory at startup | LoRA slots plus KV cache exceed the budget | Lower `max_loras`, `max_lora_rank`, or `gpu_memory_utilization` |
Adapter support depends on the model architecture and on the base model's quantization format. Check the vLLM documentation for your version before combining LoRA with a quantized base. Test the combination on your own prompts, because quality can differ from a full-precision base.
Monitoring
The metrics endpoint from the monitoring chapter also reports LoRA usage. Run the same filter and look for series with lora in the name, such as vllm:lora_requests_info. The exact set of series varies between vLLM versions.
curl -s http://localhost:8000/metrics | grep '^vllm:' | grep -i loraExercise
A server runs with --enable-lora --max-loras 2 --max-lora-rank 16. It has five adapters registered with rank 8, and traffic comes in evenly across all five. Name two settings you would change to reduce adapter swapping, and one cost of each.
Show answer
Raise --max-loras so more adapters fit in one batch, which uses more GPU memory and leaves less for the KV cache. Raise --max-cpu-loras so all five stay cached in host RAM, which uses more CPU memory. Lowering --max-lora-rank to 8 would also free GPU memory, so it can pay for a larger max_loras, as long as no served adapter has a rank above 8.
Exercise
You call the server with model="sql-lora" and get a 404 error. The adapter directory exists on the server. List two things to check.
Show answer
Check that sql-lora appears in the /v1/models response, since it only appears if it was registered with --lora-modules or loaded through /v1/load_lora_adapter. Check the startup or load log for an error such as a rank above max_lora_rank or a missing adapter_config.json. Also check that --enable-lora was set.