| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import torch |
| 5 | |
| 6 | from vllm.utils.torch_utils import direct_register_custom_op |
| 7 | |
| 8 | |
| 9 | def _bgmv_shrink_impl( |
| 10 | inputs: torch.Tensor, |
| 11 | lora_a_weights: torch.Tensor, |
| 12 | output_tensor: torch.Tensor, |
| 13 | lora_indices_tensor: torch.Tensor, |
| 14 | scaling: float, |
| 15 | ) -> None: |
| 16 | torch.ops._xpu_C.bgmv_shrink( |
| 17 | output_tensor, inputs, lora_a_weights, lora_indices_tensor, scaling |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | def _bgmv_expand_impl( |
| 22 | inputs: torch.Tensor, |
| 23 | lora_b_weights: torch.Tensor, |
| 24 | output_tensor: torch.Tensor, |
| 25 | lora_indices_tensor: torch.Tensor, |
| 26 | add_inputs: bool, |
| 27 | ) -> None: |
| 28 | weight_out_dim = lora_b_weights.size(-2) |
| 29 | output_dim = output_tensor.size(1) |
| 30 | |
| 31 | if weight_out_dim == output_dim: |
| 32 | torch.ops._xpu_C.bgmv_expand( |
| 33 | output_tensor, |
| 34 | inputs, |
| 35 | lora_b_weights, |
| 36 | lora_indices_tensor, |
| 37 | add_inputs, |
| 38 | ) |
| 39 | elif weight_out_dim < output_dim: |
| 40 | # LoRA weight output dim can be smaller than the output tensor |
| 41 | # (e.g. vocab_size vs padded logits). Use expand_slice to write |
| 42 | # only the matching portion, mirroring torch_ops common_len logic. |
| 43 | torch.ops._xpu_C.bgmv_expand_slice( |
| 44 | output_tensor, |
| 45 | inputs, |
| 46 | lora_b_weights, |
| 47 | lora_indices_tensor, |
| 48 | 0, |
| 49 | weight_out_dim, |
| 50 | add_inputs, |
| 51 | ) |
| 52 | else: |
| 53 | # Weight output dim larger than output tensor: truncate weights. |
| 54 | lora_b_weights = lora_b_weights[..., :output_dim, :].contiguous() |
| 55 | torch.ops._xpu_C.bgmv_expand_slice( |
| 56 | output_tensor, |
| 57 | inputs, |
| 58 | lora_b_weights, |
| 59 | lora_indices_tensor, |
| 60 | 0, |
| 61 | output_dim, |
| 62 | add_inputs, |
| 63 | ) |
| 64 | |
| 65 | |
| 66 | def _bgmv_expand_slice_impl( |
| 67 | inputs: torch.Tensor, |
| 68 | lora_b_weights: torch.Tensor, |
| 69 | output_tensor: torch.Tensor, |
| 70 | lora_indices_tensor: torch.Tensor, |
| 71 | slice_offset: int, |
| 72 | slice_size: int, |
| 73 | add_inputs: bool, |
| 74 | ) -> None: |
| 75 | assert slice_size == lora_b_weights.size(-2) |
| 76 | assert slice_offset + slice_size <= output_tensor.size(1) |
| 77 | torch.ops._xpu_C.bgmv_expand_slice( |
| 78 | output_tensor, |
| 79 | inputs, |
| 80 | lora_b_weights, |
| 81 | lora_indices_tensor, |
| 82 | slice_offset, |
| 83 | slice_size, |
| 84 | add_inputs, |
| 85 | ) |
| 86 | |
| 87 | |
| 88 | direct_register_custom_op( |
| 89 | op_name="xpu_bgmv_shrink", |
| 90 | op_func=_bgmv_shrink_impl, |
| 91 | mutates_args=["output_tensor"], |
| 92 | ) |
| 93 | |
| 94 | direct_register_custom_op( |
| 95 | op_name="xpu_bgmv_expand", |
| 96 | op_func=_bgmv_expand_impl, |
| 97 | mutates_args=["output_tensor"], |
| 98 | ) |
| 99 | |
| 100 | direct_register_custom_op( |
| 101 | op_name="xpu_bgmv_expand_slice", |
| 102 | op_func=_bgmv_expand_slice_impl, |
| 103 | mutates_args=["output_tensor"], |
| 104 | ) |
| 105 | |
| 106 | |
| 107 | def bgmv_shrink( |
| 108 | inputs: torch.Tensor, |
| 109 | lora_a_weights: torch.Tensor, |
| 110 | output_tensor: torch.Tensor, |
| 111 | lora_indices_tensor: torch.Tensor, |
| 112 | scaling: float = 1.0, |
| 113 | ) -> None: |
| 114 | torch.ops.vllm.xpu_bgmv_shrink( |
| 115 | inputs, lora_a_weights, output_tensor, lora_indices_tensor, scaling |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | def bgmv_expand( |
| 120 | inputs: torch.Tensor, |
| 121 | lora_b_weights: torch.Tensor, |
| 122 | output_tensor: torch.Tensor, |
| 123 | lora_indices_tensor: torch.Tensor, |
| 124 | add_inputs: bool = True, |
| 125 | ) -> None: |
| 126 | torch.ops.vllm.xpu_bgmv_expand( |
| 127 | inputs, lora_b_weights, output_tensor, lora_indices_tensor, add_inputs |
| 128 | ) |
| 129 | |
| 130 | |
| 131 | def bgmv_expand_slice( |
| 132 | inputs: torch.Tensor, |
| 133 | lora_b_weights: torch.Tensor, |
| 134 | output_tensor: torch.Tensor, |
| 135 | lora_indices_tensor: torch.Tensor, |
| 136 | slice_offset: int, |
| 137 | slice_size: int, |
| 138 | add_inputs: bool = True, |
| 139 | ) -> None: |
| 140 | torch.ops.vllm.xpu_bgmv_expand_slice( |
| 141 | inputs, |
| 142 | lora_b_weights, |
| 143 | output_tensor, |
| 144 | lora_indices_tensor, |
| 145 | slice_offset, |
| 146 | slice_size, |
| 147 | add_inputs, |
| 148 | ) |