Skip to content

vllm.lora.punica_wrapper.punica_cpu

Classes:

  • PunicaWrapperCPU –

    PunicaWrapperCPU is designed to manage and provide metadata for the punica

PunicaWrapperCPU

Bases: PunicaWrapperBase

PunicaWrapperCPU 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 pytorch punica ops.

Methods:

_apply_expand(y, x, w_t_all, y_offset, y_slice_size, add_inputs=True)

Perform the y[:,y_offset:y_offset+y_slice_size]+=x@w_t_all computation, which is suitable for the GEMM of lora'b.

_apply_shrink(y, x, w_t_all, scale)

Perform the y+=x@w_t_all computation, which is suitable for the GEMM of lora'a. When is_prefill is true, it indicates that it is currently the prefill stage, and the _shrink_prefill function should be called. Otherwise, it is the decode stage, and the _shrink_decode function should be called.

add_expand(y, x, lora_b_stacked, output_slices, offset_start=0, add_inputs=True, **kwargs)

Performs GEMM for multiple slices of lora_b.

Semantics

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.

  • **kwargs

    (Any, default: {} ) –

    Unused; accepted for compatibility with the base class signature.

add_lora_embedding(y, x, lora_b_stacked, add_inputs=True, **kwargs)

Applies lora specifically for VocabParallelEmbeddingWithLoRA.

Semantics

y += x @ lora_b_stacked

Parameters:

  • y

    (Tensor) –

    Output tensor.

  • x

    (Tensor) –

    Input tensor.

  • lora_b_stacked

    (Tensor) –

    lora_b's weights.

  • add_inputs

    (bool, default: True ) –

    Default to True.

  • **kwargs

    (Any, default: {} ) –

    Unused; accepted for compatibility with the base class signature.

add_lora_linear(y, x, lora_a_stacked, lora_b_stacked, scale, output_slices, *, buffer=None, **kwargs)

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.

  • **kwargs

    (Any, default: {} ) –

    Unused; accepted for compatibility with the base class signature.

add_lora_logits(y, x, lora_a_stacked, lora_b_stacked, scale, *, buffer=None, **kwargs)

Applies lora specifically for LogitsProcessorWithLoRA.

Semantics

buffer = (x @ lora_a_stacked) * scale y += buffer @ lora_b_stacked

Parameters:

  • y

    (Tensor) –

    Output tensor.

  • x

    (Tensor) –

    Input tensor.

  • lora_a_stacked

    (Tensor) –

    lora_a's weights.

  • lora_b_stacked

    (Tensor) –

    lora_b's weights.

  • scale

    (float) –

    Scaling factor.

  • buffer

    (Optional[Tensor], default: None ) –

    Default to None.

  • **kwargs

    (Any, default: {} ) –

    Forwarded to add_shrink and add_expand.

add_shrink(y, x, lora_a_stacked, scale, **kwargs)

Performs GEMM for multiple slices of lora_a. When is_prefill is true, it indicates that it is currently the prefill stage, and the _shrink_prefill function should be called. Otherwise, it is the decode stage, and the _shrink_decode function should be called.

Semantics: for i in range(len(lora_a_stacked)): y[i] += (x @ lora_a_stacked[i]) * scale

Parameters:

  • y

    (Union[tuple[Tensor, ...], Tensor]) –

    Output tensors

  • x

    (Tensor) –

    Input tensor

  • lora_a_stacked

    (tuple[Tensor, ...]) –

    lora_a's weights

  • scale

    (float) –

    Scaling factor for the operation

  • **kwargs

    (Any, default: {} ) –

    Unused; accepted for compatibility with the base class signature.