| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import torch |
| 5 | |
| 6 | |
| 7 | def sgmv_expand( |
| 8 | inputs: torch.Tensor, |
| 9 | lora_b_weights: torch.Tensor, |
| 10 | output_tensor: torch.Tensor, |
| 11 | b_seq_start_loc: torch.Tensor, |
| 12 | seq_len_tensor: torch.Tensor, |
| 13 | lora_indices_tensor: torch.Tensor, |
| 14 | batches: int, |
| 15 | max_seq_length: int, |
| 16 | token_nums: int, |
| 17 | add_inputs: bool = False, |
| 18 | ): |
| 19 | exploded_indices = torch.repeat_interleave(lora_indices_tensor, seq_len_tensor) |
| 20 | |
| 21 | bgmv_expand(inputs, lora_b_weights, output_tensor, exploded_indices, add_inputs) |
| 22 | |
| 23 | |
| 24 | def bgmv_expand( |
| 25 | inputs: torch.Tensor, |
| 26 | lora_b_weights: torch.Tensor, |
| 27 | output_tensor: torch.Tensor, |
| 28 | lora_indices_tensor: torch.Tensor, |
| 29 | add_inputs: bool = True, |
| 30 | ): |
| 31 | selected_loras = lora_b_weights[lora_indices_tensor].to(dtype=output_tensor.dtype) |
| 32 | if len(selected_loras.shape) == 4: |
| 33 | selected_loras = selected_loras.squeeze(dim=1) |
| 34 | inputs = inputs.to(dtype=output_tensor.dtype) |
| 35 | outputs = torch.einsum("bi, boi -> bo", inputs, selected_loras) |
| 36 | |
| 37 | limit = output_tensor.shape[0] |
| 38 | if outputs.shape[0] == 1 and output_tensor.shape[0] != 1: |
| 39 | limit = 1 |
| 40 | |
| 41 | # LoRA adapter and model may add different amounts of padding to output |
| 42 | common_len = min(outputs.shape[1], output_tensor.shape[1]) |
| 43 | |
| 44 | if add_inputs: |
| 45 | output_tensor[:, :common_len] += outputs[:limit, :common_len] |
| 46 | else: |
| 47 | output_tensor[:, :common_len] = outputs[:limit, :common_len] |
| 48 | |
| 49 | |
| 50 | def sgmv_shrink( |
| 51 | inputs: torch.Tensor, |
| 52 | lora_a_weights: torch.Tensor, |
| 53 | output_tensor: torch.Tensor, |
| 54 | b_seq_start_loc: torch.Tensor, |
| 55 | seq_len_tensor: torch.Tensor, |
| 56 | lora_indices_tensor: torch.Tensor, |
| 57 | batches: int, |
| 58 | max_seq_length: int, |
| 59 | token_nums: int, |
| 60 | scaling: float, |
| 61 | ): |
| 62 | exploded_indices = torch.repeat_interleave(lora_indices_tensor, seq_len_tensor) |
| 63 | |
| 64 | bgmv_shrink(inputs, lora_a_weights, output_tensor, exploded_indices, scaling) |
| 65 | |
| 66 | |
| 67 | def bgmv_shrink( |
| 68 | inputs: torch.Tensor, |
| 69 | lora_b_weights: torch.Tensor, |
| 70 | output_tensor: torch.Tensor, |
| 71 | lora_indices_tensor: torch.Tensor, |
| 72 | scaling: float = 1.0, |
| 73 | ): |
| 74 | selected_loras = lora_b_weights[lora_indices_tensor].to(dtype=output_tensor.dtype) |
| 75 | if len(selected_loras.shape) == 4: |
| 76 | selected_loras = selected_loras.squeeze(dim=1) |
| 77 | inputs = inputs.to(dtype=output_tensor.dtype) |
| 78 | outputs = torch.einsum("bi, boi -> bo", inputs, selected_loras) |
| 79 | |
| 80 | output_tensor[:, : outputs.shape[1]] = scaling * outputs[:] |
| 81 | |
| 82 | |
| 83 | def sgmv_expand_slice( |
| 84 | inputs: torch.Tensor, |
| 85 | lora_b_weights: torch.Tensor, |
| 86 | output_tensor: torch.Tensor, |
| 87 | b_seq_start_loc: torch.Tensor, |
| 88 | seq_len_tensor: torch.Tensor, |
| 89 | lora_indices_tensor: torch.Tensor, |
| 90 | batches: int, |
| 91 | max_seq_length: int, |
| 92 | token_nums: int, |
| 93 | slice_offset: int, |
| 94 | slice_size: int, |
| 95 | add_inputs: bool = False, |
| 96 | ): |
| 97 | exploded_indices = torch.repeat_interleave(lora_indices_tensor, seq_len_tensor) |
| 98 | |
| 99 | bgmv_expand_slice( |
| 100 | inputs, |
| 101 | lora_b_weights, |
| 102 | output_tensor, |
| 103 | exploded_indices, |
| 104 | slice_offset, |
| 105 | slice_size, |
| 106 | add_inputs, |
| 107 | ) |
| 108 | |
| 109 | |
| 110 | def bgmv_expand_slice( |
| 111 | inputs: torch.Tensor, |
| 112 | lora_b_weights: torch.Tensor, |
| 113 | output_tensor: torch.Tensor, |
| 114 | lora_indices_tensor: torch.Tensor, |
| 115 | slice_offset: int, |
| 116 | slice_size: int, |
| 117 | add_inputs: bool = True, |
| 118 | ): |
| 119 | selected_loras = lora_b_weights[lora_indices_tensor].to(dtype=output_tensor.dtype) |
| 120 | inputs = inputs.to(dtype=output_tensor.dtype) |
| 121 | if len(selected_loras.shape) == 4: |
| 122 | selected_loras = selected_loras.squeeze(dim=1) |
| 123 | outputs = torch.einsum("bi, boi -> bo", inputs, selected_loras) |
| 124 | |
| 125 | if add_inputs: |
| 126 | output_tensor[:, slice_offset : slice_offset + slice_size] += outputs[:] |
| 127 | else: |
| 128 | output_tensor[:, slice_offset : slice_offset + slice_size] = outputs[:] |