|
| 1 | +from typing import (ClassVar, Dict, List, Literal, Optional, Protocol, Type, |
| 2 | + Union, overload, runtime_checkable) |
| 3 | + |
| 4 | +from typing_extensions import TypeGuard |
| 5 | + |
| 6 | +from vllm.config import LoRAConfig, VisionLanguageConfig |
| 7 | +from vllm.logger import init_logger |
| 8 | + |
| 9 | +logger = init_logger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +@runtime_checkable |
| 13 | +class SupportsVision(Protocol): |
| 14 | + """The interface required for all vision language models (VLMs).""" |
| 15 | + |
| 16 | + supports_vision: ClassVar[Literal[True]] |
| 17 | + |
| 18 | + def __init__(self, *, vlm_config: VisionLanguageConfig) -> None: |
| 19 | + ... |
| 20 | + |
| 21 | + |
| 22 | +# We can't use runtime_checkable with ClassVar for issubclass checks |
| 23 | +# so we need to treat the class as an instance and use isinstance instead |
| 24 | +@runtime_checkable |
| 25 | +class _SupportsVisionType(Protocol): |
| 26 | + supports_vision: Literal[True] |
| 27 | + |
| 28 | + def __call__(self, *, vlm_config: VisionLanguageConfig) -> None: |
| 29 | + ... |
| 30 | + |
| 31 | + |
| 32 | +@overload |
| 33 | +def supports_vision(model: Type[object]) -> TypeGuard[Type[SupportsVision]]: |
| 34 | + ... |
| 35 | + |
| 36 | + |
| 37 | +@overload |
| 38 | +def supports_vision(model: object) -> TypeGuard[SupportsVision]: |
| 39 | + ... |
| 40 | + |
| 41 | + |
| 42 | +def supports_vision( |
| 43 | + model: Union[Type[object], object], |
| 44 | +) -> Union[TypeGuard[Type[SupportsVision]], TypeGuard[SupportsVision]]: |
| 45 | + if isinstance(model, type): |
| 46 | + return isinstance(model, _SupportsVisionType) |
| 47 | + |
| 48 | + return isinstance(model, SupportsVision) |
| 49 | + |
| 50 | + |
| 51 | +@runtime_checkable |
| 52 | +class SupportsLoRA(Protocol): |
| 53 | + """The interface required for all models that support LoRA.""" |
| 54 | + |
| 55 | + supports_lora: ClassVar[Literal[True]] |
| 56 | + |
| 57 | + packed_modules_mapping: ClassVar[Dict[str, List[str]]] |
| 58 | + supported_lora_modules: ClassVar[List[str]] |
| 59 | + embedding_modules: ClassVar[Dict[str, str]] |
| 60 | + embedding_padding_modules: ClassVar[List[str]] |
| 61 | + |
| 62 | + # lora_config is None when LoRA is not enabled |
| 63 | + def __init__(self, *, lora_config: Optional[LoRAConfig] = None) -> None: |
| 64 | + ... |
| 65 | + |
| 66 | + |
| 67 | +# We can't use runtime_checkable with ClassVar for issubclass checks |
| 68 | +# so we need to treat the class as an instance and use isinstance instead |
| 69 | +@runtime_checkable |
| 70 | +class _SupportsLoRAType(Protocol): |
| 71 | + supports_lora: Literal[True] |
| 72 | + |
| 73 | + packed_modules_mapping: Dict[str, List[str]] |
| 74 | + supported_lora_modules: List[str] |
| 75 | + embedding_modules: Dict[str, str] |
| 76 | + embedding_padding_modules: List[str] |
| 77 | + |
| 78 | + def __call__(self, *, lora_config: Optional[LoRAConfig] = None) -> None: |
| 79 | + ... |
| 80 | + |
| 81 | + |
| 82 | +@overload |
| 83 | +def supports_lora(model: Type[object]) -> TypeGuard[Type[SupportsLoRA]]: |
| 84 | + ... |
| 85 | + |
| 86 | + |
| 87 | +@overload |
| 88 | +def supports_lora(model: object) -> TypeGuard[SupportsLoRA]: |
| 89 | + ... |
| 90 | + |
| 91 | + |
| 92 | +def supports_lora( |
| 93 | + model: Union[Type[object], object], |
| 94 | +) -> Union[TypeGuard[Type[SupportsLoRA]], TypeGuard[SupportsLoRA]]: |
| 95 | + result = _supports_lora(model) |
| 96 | + |
| 97 | + if not result: |
| 98 | + lora_attrs = ( |
| 99 | + "packed_modules_mapping", |
| 100 | + "supported_lora_modules", |
| 101 | + "embedding_modules", |
| 102 | + "embedding_padding_modules", |
| 103 | + ) |
| 104 | + missing_attrs = tuple(attr for attr in lora_attrs |
| 105 | + if not hasattr(model, attr)) |
| 106 | + |
| 107 | + if getattr(model, "supports_lora", False): |
| 108 | + if missing_attrs: |
| 109 | + logger.warning( |
| 110 | + "The model (%s) sets `supports_lora=True`, " |
| 111 | + "but is missing LoRA-specific attributes: %s", |
| 112 | + model, |
| 113 | + missing_attrs, |
| 114 | + ) |
| 115 | + else: |
| 116 | + if not missing_attrs: |
| 117 | + logger.warning( |
| 118 | + "The model (%s) contains all LoRA-specific attributes, " |
| 119 | + "but does not set `supports_lora=True`.", model) |
| 120 | + |
| 121 | + return result |
| 122 | + |
| 123 | + |
| 124 | +def _supports_lora( |
| 125 | + model: Union[Type[object], object], |
| 126 | +) -> Union[TypeGuard[Type[SupportsLoRA]], TypeGuard[SupportsLoRA]]: |
| 127 | + if isinstance(model, type): |
| 128 | + return isinstance(model, _SupportsLoRAType) |
| 129 | + |
| 130 | + return isinstance(model, SupportsLoRA) |
0 commit comments