This document is relevant for: Trn2, Trn3
Deep dive: Multi-LoRA serving on Neuron#
This topic explains how vLLM Neuron serves several LoRA adapters on one base model. It covers the adapter slots, the configuration path, adapter loading, the scheduler, the model runner, and the kernels. Some experience with LoRA and with the vLLM LoRA interface is necessary to understand it in full.
For user configuration, see Multi-LoRA serving. For a step-by-step example, see the multi-LoRA tutorial.
What you should know before reading#
LoRA: A LoRA adapter adds
x @ A @ B * scalingto the output of a projection.Ahas the shape[in, r]andBhas the shape[r, out], whereris the rank.vLLM LoRA interface:
--enable-lora,--max-loras,--max-lora-rank,--lora-modules, andLoRARequest. See the vLLM LoRA documentation.Prefill and decode graphs: vLLM Neuron compiles fixed-shape graphs for prefill and decode. See Bucketing and dynamic shapes.
Overview#
vLLM on GPU wraps each linear layer in a LoRA module. Neuron models use
functional operations and plain nn.Parameter weights, not nn.Linear
modules. Thus vLLM Neuron uses a different design:
Each target projection gets LoRA weights for a fixed number of slots. The shapes are
[max_loras + 1, in, max_lora_rank]forAand[max_loras + 1, max_lora_rank, out]forB.Slot 0 always holds zeros. It serves the base model, because it adds no LoRA contribution.
Each step gives the model an
adapter_idstensor with one slot index for each token. The kernels read theAandBof that slot for the token.The slot weights are inputs of the compiled graphs, not constants. A new adapter changes the values in a slot, and the compiled graphs stay the same.
Thus a load or a swap of an adapter does not cause a recompile. One decode batch can mix base requests and requests for different adapters.
Configuration path#
The configuration path runs once, when the model loads. Model Runner V1 and
Model Runner V2 use the same functions. vLLM Neuron reads only the vLLM LoRA
arguments, as vLLM on GPU does. No LoRA key is in neuron_config.
vLLM builds its
LoRAConfigfrom--enable-lora,--max-loras,--max-lora-rank,--lora-target-modules, and--lora-dtype. Theneuron_confighas no LoRA keys. vLLM Neuron ignores--max-cpu-loras.validate_lora_configrejects a configuration that the LoRA path does not support. It collects all problems into oneValueError: decode-context parallelism, attention or MLP data parallelism, speculative decoding, jump-forward decoding, and pooling models.The model factory checks its own limits. The Llama factory rejects a quantized model, a model dtype other than bfloat16, and a LoRA dtype other than bfloat16.
build_lora_model_configruns on the built model, before the weight load. It callscheck_model_supports_lora, which requires vLLM’sSupportsLoRAand the Neuron methodget_lora_layout(). Then it resolves--lora-target-modulesagainst the LoRA modules of the model, and sets the slot count, the rank, and the LoRA dtype.apply_lora_to_modeladds the zero-filled slot weights to the module of each LoRA base weight, after the base checkpoint loads and before the model moves to the device.
get_lora_layout() lists the base weights whose kernels apply LoRA. For
Llama, these are qkv_proj_weight, o_proj_weight, gate_proj_weight,
up_proj_weight, and down_proj_weight. The framework derives the rest of the
LoRA plan from the model:
The weight loader of each base weight gives the tensor-parallel split.
packed_modules_mappinggives the PEFT projection names of a fused weight, as on upstream vLLM. For example,qkv_projholdsq_proj,k_proj, andv_proj. One target projection of a packed weight selects all of its projections.
The slot weights are nn.Parameter(requires_grad=False) tensors, not buffers.
The compiler folds a buffer value into the graph as a constant, so an adapter
in a buffer would need a recompile. A parameter stays a graph input.
Tensor-parallel sharding#
The adapter loader shards the LoRA weights with the weight loader of the base
weight. Thus the LoRA weights get the same shards and the same padding as the
base weight. At startup, check_lora_sharding tests each loader.
Base weight |
Parallel type |
|
|
|---|---|---|---|
Column-parallel, for example |
Column-parallel |
Full |
Sharded on the output dimension |
Row-parallel, for example |
Row-parallel |
Sharded on the input dimension |
Full |
The column-parallel output needs no reduction across ranks. The row-parallel
output uses the same all-reduce as the base projection. With grouped-query
attention, several ranks can share one KV head. These ranks then read the same
rows of the k_proj and v_proj B weights, as the base loader does.
Multi-LoRA does not support MoE expert weights.
Adapter loading#
vLLM Neuron loads an adapter in two cases. add_lora loads it at once: the
server calls add_lora at startup for each --lora-modules adapter, and for
each /v1/load_lora_adapter call. A step loads each adapter of its requests
that is not resident. The load does these steps:
Resolve
LoRARequest.lora_pathwith vLLM’sget_adapter_absolute_path, as the vLLMWorkerLoRAManagerdoes. A local directory is used as it is. Another relative path is a Hugging Face Hub repo ID, and vLLM downloads it to the local cache. The directory must holdadapter_config.json.Read
adapter_config.json. Reject DoRA,rank_pattern,alpha_pattern,modules_to_save, a LoRA bias, and an adapter without a rank. Reject a rank larger than--max-lora-rank.Compute the scaling:
lora_alpha / rfor LoRA, orlora_alpha / sqrt(r)for rsLoRA. The loader multipliesBby the scaling, so the kernels do not apply it.Read the weights from
adapter_model.safetensors, or fromadapter_model.binif no.safetensorsfile exists. The loader memory-maps a.safetensorsfile and slices the shard of each tensor-parallel worker before it reads the data.Zero-pad the rank dimension to
--max-lora-rank. The kernel always reads the full rank width. The device copy of a slot changes only where the host write covers it, so a partial write would leave old values in the padding.Write each slot tensor whole from a host block. Set each slot tensor that the adapter does not cover to zeros.
Skip the adapter weights of a projection that is not a target, with a warning. Reject an adapter that has weights only outside the targets. Ignore full
embed_tokensandlm_headweights, with a warning.
All tensor-parallel workers on a host load the same adapter at the same time. Two measures keep this load fast:
Host staging: The first worker copies the
.safetensorsfile into a host-memory (tmpfs) directory under a file lock. The other workers wait on the lock and use the copy, so the network file is read once for each host. The cache key includes the path, the size, and the modification time. The cache removes the least recently used copies when it has more files or more bytes than its limits.One CPU thread for each worker: Each worker does its host tensor work on one CPU thread. Many workers with many threads each would use more threads than the host has cores.
The load is synchronous, inside the model execution of the step.
Slot manager#
NeuronLoRAManager has the same external contract as the vLLM
WorkerLoRAManager: add, remove, list, and set the active adapters.
The manager keys each adapter by its
lora_int_id. Alora_int_idbelongs to its adapter name until the adapter is removed, also after an eviction. A request that uses the ID under another name raises an error, because the slot would serve the wrong weights.The manager resolves the path before it takes a slot, so a bad path evicts nothing.
Before a step, the manager makes all adapters of the step resident. It marks the adapters of the step and the adapters of all live requests as most recently used, so a new load cannot evict them.
When no slot is free, the manager evicts the least recently used adapter. Before it overwrites the slot, it waits for the device to finish the queued steps. A step in flight can still read the old slot, for example with asynchronous scheduling.
If a load fails, the manager sets the slot to zeros and frees it. A free slot always holds zeros.
add_lorawithload_inplace=Trueloads a resident adapter again from itslora_pathinto its slot. A generate request does not load a resident adapter again.
All slots are in HBM. The manager has no CPU LoRA cache, so --max-cpu-loras
has no effect. A reload of an evicted adapter reads the adapter file again. The
manager does not support pinned adapters.
Scheduler#
The vLLM scheduler limits the number of different adapters to max_loras
among the requests that it can see. A Neuron prefill step holds one request
and does not show the running decodes to the vLLM scheduler. Without more
control, a new prefill could add one more adapter than the slots can hold. The
next decode step would then fail.
The Neuron scheduler checks each waiting request against the adapters of all
running requests. If the request needs a new adapter and max_loras adapters
are in use, the request waits. The scheduler continues with the requests
behind it, so one blocked request does not stop the queue. The blocked request
goes back to the front of the queue for the next step.
Model runner#
The model runner builds the adapter_ids tensor for each step:
Prefill: The step holds one request. Every token, padding included, gets the slot of that request. A prefill step with more than one request raises an error.
Decode: Each row gets the slot of its request. A base request and a padding row get slot 0.
Warmup: A synthetic warmup input has no requests. It gets slots 0 and 1 in turn, so the trace sees the same shape and dtype as a real step.
Model Runner V2 also gives a data-parallel dummy step slot 0.
Kernel path#
The NKI Library kernels apply the LoRA weights. The base projection and the LoRA contribution run in the same kernel call.
Decode attention: The fused decode attention block applies the QKV LoRA and the
o_projLoRA. It reads the slot of each token fromadapter_ids. With an FP8 KV cache, vLLM Neuron divides theo_projLoRAAby the V scale, the same as the baseo_projweight.Decode MLP: At or below 96 tokens, the MLP decode kernel applies the gate, up, and down LoRA for each token. Above 96 tokens, the MLP prefill kernel runs the step. This kernel also routes the LoRA for each token.
Prefill: The QKV projection kernel applies the QKV LoRA. The MLP prefill kernel gets
lora_shared_adapter=True, because all tokens use one adapter. Theo_projLoRA runs in PyTorch: it selects theAandBof the slot once and does two dense matrix multiplications.
Above 128 tokens, the decode attention block computes the LoRA in blocks of 128 tokens. Thus multi-LoRA sets no limit on the number of sequences in one step.
Limits and future work#
The current limits are in Multi-LoRA. Areas for future work include:
LoRA on MoE expert weights and on quantized base weights.
Speculative decoding with LoRA.
Faster prefill for large hidden sizes at LNC 1.