Files
enginex-ascend-910-vllm/vllm_ascend/lora/utils.py

89 lines
2.8 KiB
Python
Raw Normal View History

2025-10-14 10:38:28 +08:00
import vllm
from torch import nn
from transformers import PretrainedConfig
from vllm.config import LoRAConfig
from vllm.lora.layers import (
MergedQKVParallelLinearWithLoRA,
MergedQKVParallelLinearWithShardedLoRA,
QKVParallelLinearWithLoRA,
QKVParallelLinearWithShardedLoRA,
)
from vllm.lora.layers.utils import _fully_sharded_can_replace, _not_fully_sharded_can_replace
from vllm_ascend.lora.fused_moe import (
AscendFusedMoE3DWithLoRA,
AscendFusedMoEWithLoRA,
)
from vllm_ascend.ops.linear import (
AscendQKVParallelLinear,
)
2025-10-14 10:38:28 +08:00
class AscendQKVParallelLinearWithLoRA(QKVParallelLinearWithLoRA):
2025-10-14 10:38:28 +08:00
@classmethod
@_not_fully_sharded_can_replace
2025-10-14 10:38:28 +08:00
def can_replace_layer(
cls,
source_layer: nn.Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: PretrainedConfig | None,
2025-10-14 10:38:28 +08:00
) -> bool:
return type(source_layer) is AscendQKVParallelLinear and len(packed_modules_list) == 1
2025-10-14 10:38:28 +08:00
class AscendMergedQKVParallelLinearWithLoRA(MergedQKVParallelLinearWithLoRA):
2025-10-14 10:38:28 +08:00
@classmethod
@_not_fully_sharded_can_replace
2025-10-14 10:38:28 +08:00
def can_replace_layer(
cls,
source_layer: nn.Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: PretrainedConfig | None,
2025-10-14 10:38:28 +08:00
) -> bool:
return type(source_layer) is AscendQKVParallelLinear and len(packed_modules_list) == 3
2025-10-14 10:38:28 +08:00
class AscendMergedQKVParallelLinearWithShardedLoRA(MergedQKVParallelLinearWithShardedLoRA):
2025-10-14 10:38:28 +08:00
@classmethod
@_fully_sharded_can_replace
2025-10-14 10:38:28 +08:00
def can_replace_layer(
cls,
source_layer: nn.Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: PretrainedConfig | None = None,
2025-10-14 10:38:28 +08:00
) -> bool:
return type(source_layer) is AscendQKVParallelLinear and len(packed_modules_list) == 3
2025-10-14 10:38:28 +08:00
class AscendQKVParallelLinearWithShardedLoRA(QKVParallelLinearWithShardedLoRA):
2025-10-14 10:38:28 +08:00
@classmethod
@_fully_sharded_can_replace
2025-10-14 10:38:28 +08:00
def can_replace_layer(
cls,
source_layer: nn.Module,
lora_config: LoRAConfig,
packed_modules_list: list,
model_config: PretrainedConfig | None = None,
2025-10-14 10:38:28 +08:00
) -> bool:
return type(source_layer) is AscendQKVParallelLinear and len(packed_modules_list) == 1
2025-10-14 10:38:28 +08:00
def refresh_all_lora_classes():
ascend_classes = (
AscendQKVParallelLinearWithLoRA,
AscendMergedQKVParallelLinearWithLoRA,
AscendMergedQKVParallelLinearWithShardedLoRA,
AscendQKVParallelLinearWithShardedLoRA,
AscendFusedMoEWithLoRA,
AscendFusedMoE3DWithLoRA,
)
# vLLM #35077 changed _all_lora_classes from set to ordered tuple.
# Append the Ascend classes in a deterministic order.
vllm.lora.utils._all_lora_classes = (
*ascend_classes,
*vllm.lora.utils._all_lora_classes,
)