vllm.lora.punica_wrapper.punica_base
¶
Based on: Chen, L., Ye, Z., Wu, Y., Zhuo, D., Ceze, L., & Krishnamurthy, A. (2023). Punica: Multi-Tenant LoRA Serving. https://arxiv.org/abs/2310.18547
Classes:
-
PunicaWrapperABC–PunicaWrapper ABC.
-
PunicaWrapperBase–PunicaWrapperBase is designed to manage and provide metadata for the punica
PunicaWrapperABC
¶
Bases: ABC
PunicaWrapper ABC.
Methods:
-
add_expand–Performs GEMM for multiple slices of lora_b.
-
add_lora_embedding–Applies lora specifically for VocabParallelEmbeddingWithLoRA,
-
add_lora_linear–Applicable to linear-related lora.
-
add_lora_logits–Applies lora specifically for LogitsProcessorWithLoRA.
-
add_shrink–Performs GEMM for multiple slices of lora_a.
-
apply_lora_full_linear–Apply request-routed full linear weights to selected rows.
-
update_metadata–Update the lora-related metadata
add_expand(y, x, lora_b_stacked, output_slices, offset_start=0, add_inputs=True, **kwargs)
abstractmethod
¶
Performs GEMM for multiple slices of lora_b.
add_lora_embedding(y, x, lora_b_stacked, add_inputs=True, **kwargs)
abstractmethod
¶
Applies lora specifically for VocabParallelEmbeddingWithLoRA, and this layer only requires the expand operation.
add_lora_linear(y, x, lora_a_stacked, lora_b_stacked, scale, output_slices, *, buffer=None, **kwargs)
abstractmethod
¶
Applicable to linear-related lora.
add_lora_logits(y, x, lora_a_stacked, lora_b_stacked, scale, *, buffer=None, **kwargs)
abstractmethod
¶
Applies lora specifically for LogitsProcessorWithLoRA.
add_shrink(y, x, lora_a_stacked, scale, **kwargs)
abstractmethod
¶
Performs GEMM for multiple slices of lora_a.
apply_lora_full_linear(y, x, weight_stacked, bias_stacked, module_enabled)
abstractmethod
¶
Apply request-routed full linear weights to selected rows.
update_metadata(mapping, lora_index_to_id, max_loras, vocab_size, **kwargs)
abstractmethod
¶
Update the lora-related metadata
PunicaWrapperBase
¶
Bases: PunicaWrapperABC
PunicaWrapperBase is designed to manage and provide metadata for the punica kernel. The main function is to maintain the state information for Multi-LoRA, and to provide the interface for the punica.
Methods:
-
add_expand–Performs GEMM for multiple slices of lora_b.
-
add_lora_embedding–Applies lora specifically for VocabParallelEmbeddingWithLoRA.
-
add_lora_fused_moe–Performs a fused forward computation for LoRA of
-
add_lora_linear–Applicable to linear-related lora.
-
add_lora_logits–Applies lora specifically for LogitsProcessorWithLoRA.
-
add_lora_w13–Apply w13 LoRA to y (intermediate_cache1) in-place before activation.
-
add_lora_w2–Apply w2 LoRA to y (intermediate_cache3) in-place before moe_sum.
-
add_shrink–Performs GEMM for multiple slices of lora_a.
-
moe_lora_align_block_size–Aligns tokens and experts into block-sized chunks for LoRA-based
Attributes:
-
prefill_metadata(tuple[Tensor, Tensor, Tensor, int, int, int]) –This property provides a convenient way to access the necessary
-
sampler_indices(Tensor) –This property is used to access the lora indices specifically for
-
sampler_indices_padded(Tensor) –This property provides access to padded sampler indices.
-
token_lora_indices(Tensor) –This property provides the lora indices corresponding to each token
prefill_metadata
property
¶
This property provides a convenient way to access the necessary metadata for prefill-related kernel computations. 1. seq_start_locs: Tensor of sequence start positions. 2. seq_lengths: Tensor of sequence lengths. 3. lora_indices_per_batch: Tensor of lora indices, and an index of -1 means no lora should be applied. 4. batch_size: Batch size after clustering identical lora indices. 5. max_length: The maximum sequence length in the batch. 6. token_nums: The token numbers in the batch.
sampler_indices
property
¶
This property is used to access the lora indices specifically for LogitsProcessorWithLoRA.
sampler_indices_padded
property
¶
This property provides access to padded sampler indices.
token_lora_indices
property
¶
This property provides the lora indices corresponding to each token in the batch. An index of -1 means no lora should be applied.
add_expand(y, x, lora_b_stacked, output_slices, offset_start=0, add_inputs=True, **kwargs)
abstractmethod
¶
Performs GEMM for multiple slices of lora_b.
Semantics
offset = offset_start for i in range(len(lora_b_stacked)): slice = output_slices[i] y[:, offset:offset+slice] += x[i] @ lora_b_stacked[i] offset += slice
Parameters:
-
(y¶Tensor) –Output tensor.
-
(x¶Union[tuple[Tensor, ...], Tensor]) –Input tensors
-
(lora_b_stacked¶tuple[Tensor, ...]) –lora_b's weight
-
(output_slices¶tuple[int, ...]) –Every slice's size
-
(offset_start¶int, default:0) –The starting position of y, defaults to 0
-
(add_inputs¶bool, default:True) –Defaults to True.
add_lora_embedding(y, x, lora_b_stacked, add_inputs=True, **kwargs)
abstractmethod
¶
Applies lora specifically for VocabParallelEmbeddingWithLoRA. and this layer only requires the expand operation. Semantics: y += x @ lora_b_stacked
Parameters:
add_lora_fused_moe(y, x, lora_a_stacked, lora_b_stacked, topk_weights, sorted_token_ids, expert_ids, num_tokens_post_padded, max_lora_rank, top_k_num, shrink_config, expand_config, adapter_enabled, mul_routed_weight=False, fully_sharded=False, offset=0, token_lora_mapping=None)
¶
Performs a fused forward computation for LoRA of Mixture-of-Experts (MoE) layer.
add_lora_linear(y, x, lora_a_stacked, lora_b_stacked, scale, output_slices, *, buffer=None, **kwargs)
abstractmethod
¶
Applicable to linear-related lora.
Semantics
for i in range(len(lora_a_stacked)): y[i] += ( x[i].unsqueeze(0) @ lora_a_stacked[indices[i], layer_idx, :, :] @ lora_b_stacked[indices[i], layer_idx, :, :] * scale ).squeeze(0)
Parameters:
-
(y¶Tensor) –Output tensor. Will be changed in-place.
-
(x¶Tensor) –Input tensor
-
(lora_a_stacked¶tuple[Tensor, ...]) –lora_a's weight.
-
(lora_b_stacked¶tuple[Tensor, ...]) –lora_b's weight.
-
(scale¶float) –Scaling factor.
-
(output_slices¶tuple[int, ...]) –Every slice's size.
-
(buffer¶Optional[tuple[Tensor, ...]], default:None) –Defaults to None.
add_lora_logits(y, x, lora_a_stacked, lora_b_stacked, scale, *, buffer=None, **kwargs)
abstractmethod
¶
Applies lora specifically for LogitsProcessorWithLoRA.
Semantics
buffer = (x @ lora_a_stacked) * scale y += buffer @ lora_b_stacked
Parameters:
add_lora_w13(y, x, lora_a_stacked, lora_b_stacked, topk_ids, topk_weights, expert_map, w1, w2, num_tokens, top_k_num, max_loras, adapter_enabled, local_num_experts, top_k, num_slices, fully_sharded, use_tuned_config, add_inputs=True, token_lora_mapping=None)
¶
Apply w13 LoRA to y (intermediate_cache1) in-place before activation.
When token_lora_mapping is provided it overrides the punica_wrapper's
global mapping — used by EP+LoRA to pass the per-rank-local mapping
after all-to-all dispatch.
Returns (sorted_token_ids_lora, expert_ids_lora, num_tokens_post_padded_lora, token_lora_mapping) for reuse by add_lora_w2.
add_lora_w2(y, x, lora_a_stacked, lora_b_stacked, topk_weights, sorted_token_ids_lora, expert_ids_lora, num_tokens_post_padded_lora, token_lora_mapping, num_tokens, w1, w2, top_k_num, max_loras, adapter_enabled, top_k, fully_sharded, tp_rank, use_tuned_config, add_inputs=True)
¶
Apply w2 LoRA to y (intermediate_cache3) in-place before moe_sum.
Reuses routing tensors returned by add_lora_w13.
add_shrink(y, x, lora_a_stacked, scale, **kwargs)
abstractmethod
¶
Performs GEMM for multiple slices of lora_a.
Semantics: for i in range(len(lora_a_stacked)): y[i] += (x @ lora_a_stacked[i]) * scale
Parameters:
moe_lora_align_block_size(topk_ids, num_tokens, block_size, num_experts, max_loras, adapter_enabled, expert_map=None, pad_sorted_ids=False, naive_block_assignment=False)
¶
Aligns tokens and experts into block-sized chunks for LoRA-based mixture-of-experts (MoE) execution.