Optimization & decoding4 min read

Multi-LoRA serving in production: PEFT, vLLM, and SGLang

Spinning up a dedicated 70B GPU deployment for every customer fine-tune ruins utilization. Multi-LoRA serving keeps one frozen base model in GPU memory and applies lightweight low-rank adapters per request inside the same continuous batch—cutting idle hardware while preserving per-tenant specialization.

Conceptual illustration of one shared base model monolith paired with swappable low-rank adapter cartridges.
A single frozen base checkpoint serves heterogeneous fine-tuned adapters inside the same continuous batch.

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.

Choosing between offline weight merging and dynamic multi-LoRA serving.
Deployment patternHow it executesBest fit
Merged checkpoint (merge_and_unload)Folds (alpha / r) × B × A into base weights offline; zero runtime adapter overheadOne fine-tuned model serving steady traffic
Dynamic multi-LoRA poolRuns one shared W × x base matmul plus batched per-request B_i(A_i x) side pathsMany 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.

Run with Python 3; no GPU or packages required
# 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")
Calculated output
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 GiB

Notice 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.

Visual explanation

Shared base weights plus tiered adapter slots

GPU memory budgetConceptual components · sizes vary
  1. Frozen base model weightsLoaded once in GPU HBM

    All requests share one dense or quantized base checkpoint.

  2. Active GPU LoRA slotsBounded by --max-loras × --max-lora-rank

    Pre-allocated low-rank A and B buffers for adapters in the current batch.

  3. Adapter-scoped paged KV cacheIsolated per adapter ID

    Cached prefix blocks cannot be shared across distinct adapters.

  4. Host CPU adapter cacheBounded by --max-cpu-loras

    Holds warm inactive adapters in RAM for fast PCIe slot swaps.

Conceptual memory layout for dynamic multi-LoRA serving. Sizing --max-lora-rank higher than deployed adapters inflates the active GPU slot reservation even when smaller adapters are loaded.

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-legal cannot share KV blocks with Request B using adapter-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.

  1. LoRA: Low-Rank Adaptation of Large Language Models (2021)
  2. Hugging Face PEFT: LoRA conceptual guide
  3. Punica: Multi-Tenant LoRA Serving (2023)
  4. S-LoRA: Serving Thousands of Concurrent LoRA Adapters (2023)
  5. vLLM v0.30.0: LoRA adapters in production
  6. SGLang: Serving multiple LoRA adapters

Keep learning

Related guide · 5 min readPrefix caching explained: faster LLM prompts with KV reuseRelated guide · 5 min readGPU memory for LLM inference: how to size and choose a GPURelated guide · 6 min readAI cloud infrastructure for LLM inference: a practical guideRelated guide · 6 min readContinuous batching explained: how LLM servers scaleAcademy course · 13 unitsvLLMAcademy course · 13 unitsSGLangAcademy course · 20 unitsPyTorchAcademy course · 20 unitsHugging Face Transformers
Back to all articles