[Model] Add LoRA support for TransformersModel (#13770)
Signed-off-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
+18
-7
@@ -401,6 +401,11 @@ class BaseLinearLayerWithLoRA(BaseLayerWithLoRA):
|
||||
self.output_slices)
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def get_source_layer(cls, source_layer: nn.Module) -> type:
|
||||
# Check parent_cls in case source_layer is a HFCompatibleLinear.
|
||||
return getattr(source_layer, "parent_cls", type(source_layer))
|
||||
|
||||
|
||||
class ReplicatedLinearWithLoRA(BaseLinearLayerWithLoRA):
|
||||
|
||||
@@ -443,7 +448,8 @@ class ReplicatedLinearWithLoRA(BaseLinearLayerWithLoRA):
|
||||
packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig],
|
||||
) -> bool:
|
||||
return type(source_layer) is ReplicatedLinear
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return source_layer is ReplicatedLinear
|
||||
|
||||
|
||||
class ColumnParallelLinearWithLoRA(BaseLinearLayerWithLoRA):
|
||||
@@ -539,8 +545,9 @@ class ColumnParallelLinearWithLoRA(BaseLinearLayerWithLoRA):
|
||||
packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig],
|
||||
) -> bool:
|
||||
return type(source_layer) is ColumnParallelLinear or (
|
||||
type(source_layer) is MergedColumnParallelLinear
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return source_layer is ColumnParallelLinear or (
|
||||
source_layer is MergedColumnParallelLinear
|
||||
and len(packed_modules_list) == 1)
|
||||
|
||||
|
||||
@@ -682,7 +689,8 @@ class MergedColumnParallelLinearWithLoRA(ColumnParallelLinearWithLoRA):
|
||||
packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig],
|
||||
) -> bool:
|
||||
return (type(source_layer) is MergedColumnParallelLinear
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return (source_layer is MergedColumnParallelLinear
|
||||
and len(packed_modules_list) == 2)
|
||||
|
||||
|
||||
@@ -750,7 +758,8 @@ class QKVParallelLinearWithLoRA(ColumnParallelLinearWithLoRA):
|
||||
def can_replace_layer(cls, source_layer: nn.Module,
|
||||
lora_config: LoRAConfig, packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig]) -> bool:
|
||||
return type(source_layer) is QKVParallelLinear and len(
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return source_layer is QKVParallelLinear and len(
|
||||
packed_modules_list) == 1
|
||||
|
||||
|
||||
@@ -811,7 +820,8 @@ class MergedQKVParallelLinearWithLoRA(MergedColumnParallelLinearWithLoRA):
|
||||
packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig],
|
||||
) -> bool:
|
||||
return (type(source_layer) is QKVParallelLinear
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return (source_layer is QKVParallelLinear
|
||||
and len(packed_modules_list) == 3)
|
||||
|
||||
|
||||
@@ -896,7 +906,8 @@ class RowParallelLinearWithLoRA(BaseLinearLayerWithLoRA):
|
||||
packed_modules_list: List,
|
||||
model_config: Optional[PretrainedConfig],
|
||||
) -> bool:
|
||||
return type(source_layer) is RowParallelLinear
|
||||
source_layer = cls.get_source_layer(source_layer)
|
||||
return source_layer is RowParallelLinear
|
||||
|
||||
|
||||
class LogitsProcessorWithLoRA(BaseLayerWithLoRA):
|
||||
|
||||
+13
-10
@@ -66,17 +66,20 @@ def from_layer(layer: nn.Module,
|
||||
lora_config=lora_config,
|
||||
packed_modules_list=packed_modules_list,
|
||||
model_config=model_config):
|
||||
ret = lora_cls(layer)
|
||||
ret.create_lora_weights(max_loras, lora_config, model_config)
|
||||
return ret
|
||||
instance_layer = lora_cls(layer)
|
||||
if layer.__class__.__name__ == "HFCompatibleLinear":
|
||||
# HACK: Make the forward method compatible with the original
|
||||
# forward method of the instance_layer.
|
||||
original_forward = instance_layer.forward
|
||||
|
||||
# The Case for HFCompatibleLinear
|
||||
if (hasattr(layer, "get_lora_class")
|
||||
and layer.__class__.__name__ == "HFCompatibleLinear"):
|
||||
lora_cls = layer.get_lora_class(lora_config.fully_sharded_loras)
|
||||
ret = lora_cls(layer)
|
||||
ret.create_lora_weights(max_loras, lora_config, model_config)
|
||||
return ret
|
||||
def new_forward(input):
|
||||
input = input.squeeze(0)
|
||||
return original_forward(input)[0] # noqa: B023
|
||||
|
||||
instance_layer.forward = new_forward
|
||||
instance_layer.create_lora_weights(max_loras, lora_config,
|
||||
model_config)
|
||||
return instance_layer
|
||||
return layer
|
||||
|
||||
|
||||
|
||||
@@ -27,11 +27,6 @@ from vllm.config import VllmConfig
|
||||
from vllm.distributed import get_tensor_model_parallel_world_size
|
||||
from vllm.distributed.utils import divide
|
||||
from vllm.logger import init_logger
|
||||
from vllm.lora.fully_sharded_layers import (
|
||||
ColumnParallelLinearWithShardedLoRA, RowParallelLinearWithShardedLoRA)
|
||||
from vllm.lora.layers import (ColumnParallelLinearWithLoRA,
|
||||
ReplicatedLinearWithLoRA,
|
||||
RowParallelLinearWithLoRA)
|
||||
from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
||||
ReplicatedLinear,
|
||||
RowParallelLinear)
|
||||
@@ -43,7 +38,7 @@ from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
||||
from vllm.model_executor.sampling_metadata import SamplingMetadata
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
from .interfaces import SupportsQuant
|
||||
from .interfaces import SupportsLoRA, SupportsQuant
|
||||
from .utils import maybe_prefix
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -102,44 +97,18 @@ def replace_linear_class(
|
||||
"rowwise": RowParallelLinear,
|
||||
}.get(style, ReplicatedLinear)
|
||||
|
||||
lora_linear_cls = {
|
||||
ColumnParallelLinear: {
|
||||
True: ColumnParallelLinearWithShardedLoRA, # fully sharded
|
||||
False: ColumnParallelLinearWithLoRA # not fully sharded
|
||||
},
|
||||
RowParallelLinear: {
|
||||
True: RowParallelLinearWithShardedLoRA,
|
||||
False: RowParallelLinearWithLoRA
|
||||
},
|
||||
# ReplicatedLinear doesn't support fully sharded LoRA yet,
|
||||
# so we use the same class for both cases.
|
||||
ReplicatedLinear: {
|
||||
True: ReplicatedLinearWithLoRA,
|
||||
False: ReplicatedLinearWithLoRA
|
||||
}
|
||||
}
|
||||
|
||||
class HFCompatibleLinear(vllm_linear_cls):
|
||||
"""
|
||||
Wrapper class that removes `output_bias` from returned output.
|
||||
"""
|
||||
# NOTE: The LoRA layer needs to use `parent_cls`.
|
||||
@property
|
||||
def parent_cls(self) -> type:
|
||||
return vllm_linear_cls
|
||||
|
||||
def forward(self, input: torch.Tensor) -> torch.Tensor:
|
||||
return super().forward(input)[0]
|
||||
|
||||
@classmethod
|
||||
def get_lora_class(cls, fully_sharded: bool = False):
|
||||
"""
|
||||
Get the LoRA class corresponding to the current transformer
|
||||
linear class.
|
||||
|
||||
Args:
|
||||
fully_sharded (bool): If True, select the LoRA class variant
|
||||
that supports fully sharded LoRA. Defaults to False.
|
||||
|
||||
"""
|
||||
return lora_linear_cls[vllm_linear_cls][fully_sharded]
|
||||
|
||||
return HFCompatibleLinear(
|
||||
input_size=linear.in_features,
|
||||
output_size=linear.out_features,
|
||||
@@ -148,7 +117,7 @@ def replace_linear_class(
|
||||
)
|
||||
|
||||
|
||||
class TransformersModel(nn.Module, SupportsQuant):
|
||||
class TransformersModel(nn.Module, SupportsQuant, SupportsLoRA):
|
||||
embedding_padding_modules = ["lm_head"]
|
||||
embedding_modules = ["embed_tokens"
|
||||
] # TODO transformers will have a util to get it
|
||||
|
||||
Reference in New Issue
Block a user