vllm.lora.layers ¶
Modules:
Name | Description |
---|---|
base | |
base_linear | |
column_parallel_linear | |
logits_processor | |
qkv_x_parallel_linear | |
replicated_linear | |
row_parallel_linear | |
utils | |
vocal_parallel_embedding | |
__all__ module-attribute
¶
__all__ = [
"BaseLayerWithLoRA",
"VocabParallelEmbeddingWithLoRA",
"LogitsProcessorWithLoRA",
"ColumnParallelLinearWithLoRA",
"ColumnParallelLinearWithShardedLoRA",
"MergedColumnParallelLinearWithLoRA",
"MergedColumnParallelLinearWithShardedLoRA",
"MergedQKVParallelLinearWithLoRA",
"MergedQKVParallelLinearWithShardedLoRA",
"QKVParallelLinearWithLoRA",
"QKVParallelLinearWithShardedLoRA",
"RowParallelLinearWithLoRA",
"RowParallelLinearWithShardedLoRA",
"ReplicatedLinearWithLoRA",
"LoRAMapping",
]
BaseLayerWithLoRA ¶
Bases: Module
Source code in vllm/lora/layers/base.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Returns True if the layer can be replaced by this LoRA layer.
Source code in vllm/lora/layers/base.py
create_lora_weights ¶
create_lora_weights(
max_loras: int,
lora_config: LoRAConfig,
model_config: Optional[PretrainedConfig] = None,
) -> None
set_lora ¶
set_lora(
index: int,
lora_a: Tensor,
lora_b: Tensor,
embeddings_tensor: Optional[Tensor],
bias: Optional[Tensor] = None,
)
Overwrites lora tensors at index.
set_mapping ¶
slice_lora_a ¶
slice_lora_a(
lora_a: Union[Tensor, list[Union[Tensor, None]]],
) -> Union[Tensor, list[Union[Tensor, None]]]
Slice lora a if splitting for tensor parallelism.
ColumnParallelLinearWithLoRA ¶
Bases: BaseLinearLayerWithLoRA
LoRA on top of ColumnParallelLinear layer. LoRA B is sliced for tensor parallelism. There are two types for the base_layer
: 1. ColumnParallelLinear, e.g.dense_h_to_4h
in FalconForCausalLM
. 2. MergedColumnParallelLinear, e.g.gate_up_proj
in Phi3ForCausalLM
.
Source code in vllm/lora/layers/column_parallel_linear.py
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 123 124 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 |
|
is_merged_col_linear instance-attribute
¶
is_merged_col_linear = (
type(base_layer) is MergedColumnParallelLinear
)
__init__ ¶
__init__(base_layer: ColumnParallelLinear) -> None
Source code in vllm/lora/layers/column_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
forward ¶
Forward of ColumnParallelLinear
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ | Tensor | Tensor whose last dimension is | required |
Returns:
Type | Description |
---|---|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Source code in vllm/lora/layers/column_parallel_linear.py
slice_bias ¶
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
slice_lora_b ¶
Source code in vllm/lora/layers/column_parallel_linear.py
ColumnParallelLinearWithShardedLoRA ¶
Bases: ColumnParallelLinearWithLoRA
Differs from ColumnParallelLinearWithLoRA by slicing LoRA A also.
Based on S-LoRA, slicing happens along the rank dim.
Source code in vllm/lora/layers/column_parallel_linear.py
apply ¶
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
LoRAMapping dataclass
¶
LogitsProcessorWithLoRA ¶
Bases: BaseLayerWithLoRA
LoRA wrapper for LogitsProcessor, with extra logic to handle the application of the LoRA adapter and added LoRA vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_layer | LogitsProcessor | LogitsProcessor layer | required |
hidden_size | int | hidden size of the model | required |
dtype | dtype | data type of the model | required |
device | device | device of the model | required |
sharded_to_full_mapping | Optional[list[int]] | index mapping from sharded vocab to full vocab received from base_layer.get_sharded_to_full_mapping(). If None, no reindexing will be done. | required |
Source code in vllm/lora/layers/logits_processor.py
22 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 123 124 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 |
|
__init__ ¶
__init__(
base_layer: LogitsProcessor,
hidden_size: int,
dtype: dtype,
device: device,
sharded_to_full_mapping: Optional[list[int]],
) -> None
Source code in vllm/lora/layers/logits_processor.py
_get_logits ¶
_get_logits(
hidden_states: Tensor,
lm_head: VocabParallelEmbedding,
embedding_bias: Optional[Tensor] = None,
) -> Optional[Tensor]
Source code in vllm/lora/layers/logits_processor.py
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 |
|
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/logits_processor.py
create_lora_weights ¶
create_lora_weights(
max_loras: int,
lora_config: LoRAConfig,
model_config: Optional[PretrainedConfig] = None,
) -> None
Source code in vllm/lora/layers/logits_processor.py
forward ¶
set_lora ¶
set_lora(
index: int,
lora_a: Tensor,
lora_b: Tensor,
embeddings_tensor: Optional[Tensor],
bias: Optional[Tensor] = None,
)
Source code in vllm/lora/layers/logits_processor.py
MergedColumnParallelLinearWithLoRA ¶
Bases: ColumnParallelLinearWithLoRA
ColumnParallelLinear layer that is composed of 2 sublayers (slices) packed together (e.g. gate_proj + up_proj -> gate_up_proj).
This means we have 2 LoRAs, each applied to one half of the layer.
Both slices must have the same size.
Source code in vllm/lora/layers/column_parallel_linear.py
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 |
|
output_slices instance-attribute
¶
__init__ ¶
__init__(
base_layer: Union[
MergedColumnParallelLinear, QKVParallelLinear
],
) -> None
Source code in vllm/lora/layers/column_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
create_lora_weights ¶
create_lora_weights(
max_loras: int,
lora_config: LoRAConfig,
model_config: Optional[PretrainedConfig] = None,
) -> None
The main reason for overriding this function is to enhance code maintainability.
Source code in vllm/lora/layers/column_parallel_linear.py
set_lora ¶
set_lora(
index: int,
lora_a: Tensor,
lora_b: Tensor,
embeddings_tensor: Optional[Tensor],
lora_bias: Optional[Tensor] = None,
)
Source code in vllm/lora/layers/column_parallel_linear.py
slice_bias ¶
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
slice_lora_b ¶
Source code in vllm/lora/layers/column_parallel_linear.py
MergedColumnParallelLinearWithShardedLoRA ¶
Bases: MergedColumnParallelLinearWithLoRA
Differs from MergedColumnParallelLinearWithLoRA by slicing the LoRA A's also.
Based on S-LoRA, slicing happens along the rank dim.
Source code in vllm/lora/layers/column_parallel_linear.py
apply ¶
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
Source code in vllm/lora/layers/column_parallel_linear.py
MergedQKVParallelLinearWithLoRA ¶
Bases: MergedColumnParallelLinearWithLoRA
MergedColumnParallelLinear layer that is composed of 3 sublayers (slices) packed together in qkv proj fashion (q_proj + k_proj + v_proj -> qkv_proj).
This means we have 3 LoRAs, each applied to one slice of the layer.
Q slice may have different shape than K and V slices (which both have the same shape).
Source code in vllm/lora/layers/column_parallel_linear.py
output_slices instance-attribute
¶
__init__ ¶
__init__(base_layer: QKVParallelLinear) -> None
Source code in vllm/lora/layers/column_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
create_lora_weights ¶
create_lora_weights(
max_loras: int,
lora_config: LoRAConfig,
model_config: Optional[PretrainedConfig] = None,
) -> None
The main reason for overloading this function is to handle inconsistent weight dimensions in qkv lora.
Source code in vllm/lora/layers/column_parallel_linear.py
MergedQKVParallelLinearWithShardedLoRA ¶
Bases: MergedQKVParallelLinearWithLoRA
Differs from MergedQKVParallelLinearWithLoRA by slicing the LoRA A's also.
Based on S-LoRA, slicing happens along the rank dim.
Source code in vllm/lora/layers/column_parallel_linear.py
apply ¶
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
Source code in vllm/lora/layers/column_parallel_linear.py
QKVParallelLinearWithLoRA ¶
Bases: ColumnParallelLinearWithLoRA
ColumnParallelLinear layer that is specifically designed for qkv_proj. Certain models, such as chatglm3 and baichuan-7b, only contains a single LoRA within their qkv_proj layer.
During inference with Tensor Parallel, the weights of lora_b must be accurately partitioned according to the respective ranks.
Q slice may have different shape than K and V slices (which both have the same shape).
Source code in vllm/lora/layers/column_parallel_linear.py
__init__ ¶
__init__(base_layer: QKVParallelLinear) -> None
Source code in vllm/lora/layers/column_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
slice_bias ¶
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_b ¶
Source code in vllm/lora/layers/column_parallel_linear.py
QKVParallelLinearWithShardedLoRA ¶
Bases: QKVParallelLinearWithLoRA
Differs from QKVParallelLinearWithLoRA by slicing the LoRA A's also.
Based on S-LoRA, slicing happens along the rank dim.
Source code in vllm/lora/layers/column_parallel_linear.py
apply ¶
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/column_parallel_linear.py
slice_lora_a ¶
ReplicatedLinearWithLoRA ¶
Bases: BaseLinearLayerWithLoRA
Source code in vllm/lora/layers/replicated_linear.py
__init__ ¶
__init__(base_layer: ReplicatedLinear) -> None
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
forward ¶
Forward of ReplicatedLinearWithLoRA
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ | Tensor | Tensor whose last dimension is | required |
Returns:
Type | Description |
---|---|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Source code in vllm/lora/layers/replicated_linear.py
RowParallelLinearWithLoRA ¶
Bases: BaseLinearLayerWithLoRA
Source code in vllm/lora/layers/row_parallel_linear.py
__init__ ¶
__init__(base_layer: RowParallelLinear) -> None
Source code in vllm/lora/layers/row_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/row_parallel_linear.py
forward ¶
Forward of RowParallelLinear
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ | Tensor | tensor whose last dimension is | required |
Returns:
Type | Description |
---|---|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Union[Tensor, tuple[Tensor, Optional[Tensor]]] |
|
Source code in vllm/lora/layers/row_parallel_linear.py
slice_bias ¶
slice_lora_a ¶
RowParallelLinearWithShardedLoRA ¶
Bases: RowParallelLinearWithLoRA
Differs from RowParallelLinearWithLoRA by slicing the LoRA B's also.
Based on S-LoRA, slicing happens along the output dim. This yields a combined partial sum from the row parallel base layer and column partitioned output from the LoRA.
Source code in vllm/lora/layers/row_parallel_linear.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 |
|
apply ¶
Source code in vllm/lora/layers/row_parallel_linear.py
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/row_parallel_linear.py
slice_bias ¶
Source code in vllm/lora/layers/row_parallel_linear.py
slice_lora_b ¶
Source code in vllm/lora/layers/row_parallel_linear.py
VocabParallelEmbeddingWithLoRA ¶
Bases: BaseLayerWithLoRA
Source code in vllm/lora/layers/vocal_parallel_embedding.py
19 20 21 22 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 123 124 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 |
|
__init__ ¶
__init__(base_layer: VocabParallelEmbedding) -> None
can_replace_layer classmethod
¶
can_replace_layer(
source_layer: Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: Optional[PretrainedConfig],
) -> bool
Source code in vllm/lora/layers/vocal_parallel_embedding.py
create_lora_weights ¶
create_lora_weights(
max_loras: int,
lora_config: LoRAConfig,
model_config: Optional[PretrainedConfig] = None,
) -> None
Source code in vllm/lora/layers/vocal_parallel_embedding.py
forward ¶
Source code in vllm/lora/layers/vocal_parallel_embedding.py
set_lora ¶
set_lora(
index: int,
lora_a: Tensor,
lora_b: Tensor,
embeddings_tensor: Optional[Tensor],
bias: Optional[Tensor] = None,
)