When to merge a LoRA adapter versus serving it dynamically
A Low-Rank Adaptation (LoRA) adapter represents a weight update to a d_out × d_in projection as two small matrices B (d_out × r) and A (r × d_in) with rank r ≪ min(d_in, d_out). LoRA paper; Hugging Face PEFT conceptual guide.
| Deployment pattern | How it executes | Best fit |
|---|---|---|
Merged checkpoint (merge_and_unload) | Folds (alpha / r) × B × A into base weights offline; zero runtime adapter overhead | One fine-tuned model serving steady traffic |
| Dynamic multi-LoRA pool | Runs one shared W × x base matmul plus batched per-request B_i(A_i x) side paths | Many specialized or per-tenant adapters sharing one GPU replica |
If every request uses the exact same fine-tune, merge the adapter offline so you pay zero extra kernel launches or VRAM slot overhead. Dynamic multi-LoRA wins when dozens of low- or spiky-traffic adapters would otherwise sit on mostly idle separate GPUs—check the utilization math in our cost per million tokens calculator.
Calculate GPU and host memory for an adapter pool
Each adapted linear layer stores r × (d_in + d_out) parameters. Across all transformer layers, even rank-32 or rank-64 adapters occupy tens or hundreds of megabytes rather than tens of gigabytes. Serving engines pre-allocate a fixed number of GPU adapter slots sized for --max-lora-rank so adapters can swap in from CPU RAM without fragmenting the KV cache.
# Llama-3.1-8B dimensions: 32 layers, hidden 4096, GQA KV 1024, MLP 14336.
layers = 32
modules = {
"q_proj": (4096, 4096),
"k_proj": (4096, 1024),
"v_proj": (4096, 1024),
"o_proj": (4096, 4096),
"gate_proj": (4096, 14336),
"up_proj": (4096, 14336),
"down_proj": (14336, 4096),
}
bytes_per_param = 2 # BF16 adapter weights
for rank in (8, 16, 64):
per_layer = sum(rank * (d_in + d_out) for d_in, d_out in modules.values())
adapter_mib = (layers * per_layer * bytes_per_param) / 2**20
pool_8_slots_gib = (adapter_mib * 8) / 1024
print(f"Rank {rank:>2}: {adapter_mib:6.1f} MiB per adapter | "
f"8 active GPU slots: {pool_8_slots_gib:4.2f} GiB")Rank 8: 40.0 MiB per adapter | 8 active GPU slots: 0.31 GiB
Rank 16: 80.0 MiB per adapter | 8 active GPU slots: 0.62 GiB
Rank 64: 320.0 MiB per adapter | 8 active GPU slots: 2.50 GiBNotice what happens if you set --max-lora-rank 64 when every production adapter only uses rank 16: the engine still reserves slot buffers wide enough for rank 64, quadrupling the GPU slot pool from 0.62 GiB to 2.50 GiB. Set --max-lora-rank to the largest rank you actually deploy, and include the slot pool inside the additional allowance field of the GPU memory calculator.
Shared base weights plus tiered adapter slots
- Frozen base model weightsLoaded once in GPU HBM
All requests share one dense or quantized base checkpoint.
- Active GPU LoRA slotsBounded by --max-loras × --max-lora-rank
Pre-allocated low-rank A and B buffers for adapters in the current batch.
- Adapter-scoped paged KV cacheIsolated per adapter ID
Cached prefix blocks cannot be shared across distinct adapters.
- Host CPU adapter cacheBounded by --max-cpu-loras
Holds warm inactive adapters in RAM for fast PCIe slot swaps.
How Punica and S-LoRA batch different adapters together
Without specialized kernels, eight concurrent requests requesting eight different adapters would split the batch into eight tiny sequential forward passes, destroying GPU utilization. Punica and S-LoRA solve this by keeping the heavy base projection W × X as one dense batched GEMM across all tokens, then running segmented gather-matmul kernels (Shrink A_i x_i down to rank r, followed by Expand B_i added back into the output).
Both vLLM’s LoRA serving support and SGLang’s multi-LoRA runtime implement this two-tier hierarchy: --max-loras caps how many distinct adapters may sit in active GPU slots during one scheduler step, while --max-cpu-loras caches a larger warm catalog in host memory to avoid disk or Hub downloads on adapter switches.
Prefix caching, tokenizers, and adapter routing caveats
- Prefix caches are scoped per adapter. Because an adapter changes every layer’s keys and values, Request A using
adapter-legalcannot share KV blocks with Request B usingadapter-medical, even when their system prompt text is identical. Review prefix caching explained. - Keep one shared tokenizer and vocabulary. Dynamic LoRA sharing works cleanly when adapters keep the base checkpoint’s tokenizer and embedding table rather than resizing vocabulary tokens.
- Route sticky tenants to warm replicas. Across multiple GPU replicas, route requests for the same adapter ID to the replica that already holds both its GPU LoRA slot and its adapter-specific KV prefix cache.
Train and serve adapters end to end
Learn how Hugging Face repositories package configs, tokenizers, and PEFT adapters in our free What is Transformers? lesson and the full Hugging Face Transformers course, then configure production adapter pools in the vLLM and SGLang courses.
Multi-LoRA serving FAQ
Can a single continuous-batching step mix base-model requests and multiple LoRA adapters?
Yes. Base-model tokens simply skip the low-rank side addition while still participating in the shared base-weight matrix multiplication.
Does multi-LoRA work with quantized base models (QLoRA / AWQ / FP8)?
Often yes: the frozen base weights remain quantized in 4-bit or 8-bit while the small A and B adapter matrices execute in BF16/FP16; verify supported combinations in your engine release notes.
What causes latency spikes when serving hundreds of LoRA adapters?
Frequent GPU slot thrashing when concurrent distinct adapters exceed --max-loras, forcing PCIe transfers from CPU RAM or disk pulls on the critical path.
Sources and further reading
Primary documentation and research behind this guide.
