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
PunicaWrapperABC
¶
Bases: ABC
PunicaWrapper ABC.
Source code in vllm/lora/punica_wrapper/punica_base.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
add_expand
abstractmethod
¶
add_expand(
y: Tensor,
x: Union[tuple[Tensor, ...], Tensor],
lora_b_stacked: tuple[Tensor, ...],
lora_bias_stacked: Optional[tuple[Tensor, ...]],
output_slices: tuple[int, ...],
offset_start: int = 0,
add_inputs=True,
**kwargs,
) -> Optional[Tensor]
Performs GEMM and bias addition for multiple slices of lora_b.
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_embedding
abstractmethod
¶
add_lora_embedding(
y: Tensor,
x: Tensor,
lora_b_stacked: Tensor,
add_inputs: bool = True,
**kwargs,
) -> Optional[Tensor]
Applies lora specifically for VocabParallelEmbeddingWithLoRA, and this layer only requires the expand operation.
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_linear
abstractmethod
¶
add_lora_linear(
y: Tensor,
x: Tensor,
lora_a_stacked: tuple[Tensor, ...],
lora_b_stacked: tuple[Tensor, ...],
lora_bias_stacked: Optional[tuple[Tensor, ...]],
scale: float,
output_slices: tuple[int, ...],
*,
buffer: Optional[tuple[Tensor, ...]] = None,
**kwargs,
) -> Optional[Tensor]
Applicable to linear-related lora.
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_logits
abstractmethod
¶
add_lora_logits(
y: Tensor,
x: Tensor,
lora_a_stacked: Tensor,
lora_b_stacked: Tensor,
scale,
*,
buffer: Optional[Tensor] = None,
**kwargs,
) -> Optional[Tensor]
Applies lora specifically for LogitsProcessorWithLoRA.
Source code in vllm/lora/punica_wrapper/punica_base.py
add_shrink
abstractmethod
¶
add_shrink(
y: Union[tuple[Tensor, ...], Tensor],
x: Tensor,
lora_a_stacked: tuple[Tensor, ...],
scale: float,
**kwargs,
) -> Optional[Tensor]
Performs GEMM for multiple slices of lora_a.
Source code in vllm/lora/punica_wrapper/punica_base.py
update_metadata
abstractmethod
¶
update_metadata(
mapping: LoRAMapping,
lora_index_to_id: list[Optional[int]],
max_loras: int,
vocab_size: int,
extra_vocab_size: int,
long_lora_context: Optional[
LongContextLoRAContext
] = None,
**kwargs,
) -> None
Update the lora-related metadata
Source code in vllm/lora/punica_wrapper/punica_base.py
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.
Source code in vllm/lora/punica_wrapper/punica_base.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
_embeddings_indices
instance-attribute
¶
_embeddings_indices = empty(
2, max_num_batched_tokens, dtype=long, device=device
)
_long_lora_indices
instance-attribute
¶
_long_lora_indices = empty(
max_num_batched_tokens, dtype=long, device=device
)
_lora_indices_per_batch
instance-attribute
¶
_lora_indices_per_batch = empty(
max_batches, dtype=long, device=device
)
_sampler_indices
instance-attribute
¶
_sampler_indices = empty(
max_num_batched_tokens, dtype=long, device=device
)
_sampler_indices_padded
instance-attribute
¶
_sampler_indices_padded = empty(
max_num_batched_tokens, dtype=long, device=device
)
_seq_start_locs
instance-attribute
¶
_seq_start_locs = empty(
max_batches, dtype=long, device=device
)
_token_lora_indices
instance-attribute
¶
_token_lora_indices = empty(
max_num_batched_tokens, dtype=long, device=device
)
embeddings_indices
property
¶
embeddings_indices: Tensor
This property provides access to the indices used for lora embeddings, specifically for VocabParallelEmbeddingWithLoRA.
long_lora_indices
property
¶
long_lora_indices: Tensor
This property provides access to the indices used for long context lora, specifically for LinearScalingRotaryEmbeddingWithLoRA.
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
¶
sampler_indices: Tensor
This property is used to access the lora indices specifically for LogitsProcessorWithLoRA.
sampler_indices_padded
property
¶
sampler_indices_padded: Tensor
This property provides access to padded sampler indices.
token_lora_indices
property
¶
token_lora_indices: Tensor
This property provides the lora indices corresponding to each token in the batch. An index of -1 means no lora should be applied.
__init__
¶
Source code in vllm/lora/punica_wrapper/punica_base.py
_apply_bias
¶
_apply_bias(
indices: Tensor,
output: Tensor,
output_slices: tuple[int, ...],
lora_bias_stacked: tuple[Optional[Tensor], ...],
)
Applies bias to output
Input shapes
lora_bias_stacked: 3 element tuple of (num_loras, output_dim) indices: (batch_size) output: (batch_size, q_slice_size + 2*kv_slice_size) output_slices: n-1 element tuple of (slice_size...), where n is number of slices
Source code in vllm/lora/punica_wrapper/punica_base.py
_update_base_metadata
¶
_update_base_metadata(
mapping: LoRAMapping,
lora_index_to_id: list[Optional[int]],
max_loras: int,
vocab_size: int,
extra_vocab_size: int,
long_lora_context: Optional[
LongContextLoRAContext
] = None,
)
Source code in vllm/lora/punica_wrapper/punica_base.py
_update_prefill_metadata
¶
_update_prefill_metadata(token_lora_tensor: Tensor) -> None
Source code in vllm/lora/punica_wrapper/punica_base.py
add_expand
abstractmethod
¶
add_expand(
y: Tensor,
x: Union[tuple[Tensor, ...], Tensor],
lora_b_stacked: tuple[Tensor, ...],
lora_bias_stacked: Optional[tuple[Tensor, ...]],
output_slices: tuple[int, ...],
offset_start: int = 0,
add_inputs=True,
**kwargs,
) -> Optional[Tensor]
Performs GEMM and bias addition 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] + lora_bias_stacked[i] offset += slice
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y
|
Tensor
|
Output tensor. |
required |
x
|
Union[tuple[Tensor, ...], Tensor]
|
Input tensors |
required |
lora_b_stacked
|
tuple[Tensor, ...]
|
lora_b's weight |
required |
lora_bias_stacked
|
Optional[tuple[Tensor, ...]]
|
bias's weight |
required |
output_slices
|
tuple[int, ...]
|
Every slice's size |
required |
offset_start
|
int
|
The starting position of y, defaults to 0 |
0
|
add_inputs
|
bool
|
Defaults to True. |
True
|
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_embedding
abstractmethod
¶
add_lora_embedding(
y: Tensor,
x: Tensor,
lora_b_stacked: Tensor,
add_inputs: bool = True,
**kwargs,
) -> Optional[Tensor]
Applies lora specifically for VocabParallelEmbeddingWithLoRA. and this layer only requires the expand operation. Semantics: y += x @ lora_b_stacked
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y
|
Tensor
|
Output tensor. |
required |
x
|
Tensor
|
Input tensor. |
required |
lora_b_stacked
|
Tensor
|
lora_b's weights. |
required |
add_inputs
|
bool
|
Default to True. |
True
|
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_linear
abstractmethod
¶
add_lora_linear(
y: Tensor,
x: Tensor,
lora_a_stacked: tuple[Tensor, ...],
lora_b_stacked: tuple[Tensor, ...],
lora_bias_stacked: Optional[tuple[Tensor, ...]],
scale: float,
output_slices: tuple[int, ...],
*,
buffer: Optional[tuple[Tensor, ...]] = None,
**kwargs,
) -> Optional[Tensor]
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)+lora_bias_stacked[i]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y
|
Tensor
|
Output tensor. Will be changed in-place. |
required |
x
|
Tensor
|
Input tensor |
required |
lora_a_stacked
|
tuple[Tensor, ...]
|
lora_a's weight. |
required |
lora_b_stacked
|
tuple[Tensor, ...]
|
lora_b's weight. |
required |
lora_bias_stacked
|
Optional[tuple[Tensor, ...]]
|
lora's bias. |
required |
scale
|
float
|
Scaling factor. |
required |
output_slices
|
tuple[int, ...]
|
Every slice's size. |
required |
buffer
|
Optional[tuple[Tensor, ...]]
|
Defaults to None. |
None
|
Source code in vllm/lora/punica_wrapper/punica_base.py
add_lora_logits
abstractmethod
¶
add_lora_logits(
y: Tensor,
x: Tensor,
lora_a_stacked: Tensor,
lora_b_stacked: Tensor,
scale,
*,
buffer: Optional[Tensor] = None,
**kwargs,
) -> Optional[Tensor]
Applies lora specifically for LogitsProcessorWithLoRA.
Semantics
buffer = (x @ lora_a_stacked) * scale y += buffer @ lora_b_stacked
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y
|
Tensor
|
Output tensor. |
required |
x
|
Tensor
|
Input tensor. |
required |
lora_a_stacked
|
Tensor
|
lora_a's weights. |
required |
lora_b_stacked
|
Tensor
|
lora_b's weights. |
required |
scale
|
float
|
Scaling factor. |
required |
buffer
|
Optional[Tensor]
|
Default to None. |
None
|
Source code in vllm/lora/punica_wrapper/punica_base.py
add_shrink
abstractmethod
¶
add_shrink(
y: Union[tuple[Tensor, ...], Tensor],
x: Tensor,
lora_a_stacked: tuple[Tensor, ...],
scale: float,
**kwargs,
) -> Optional[Tensor]
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:
Name | Type | Description | Default |
---|---|---|---|
y
|
Union[tuple[Tensor, ...], Tensor]
|
Output tensors |
required |
x
|
Tensor
|
Input tensor |
required |
lora_a_stacked
|
tuple[Tensor, ...]
|
lora_a's weights |
required |
scale
|
float
|
Scaling factor for the operation |
required |
Source code in vllm/lora/punica_wrapper/punica_base.py
update_metadata
¶
update_metadata(
mapping: LoRAMapping,
lora_index_to_id: list[Optional[int]],
max_loras: int,
vocab_size: int,
extra_vocab_size: int,
long_lora_context: Optional[
LongContextLoRAContext
] = None,
**kwargs,
)