[Misc] Add placeholder module (#11501)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -6,10 +6,12 @@ import datetime
|
||||
import enum
|
||||
import gc
|
||||
import getpass
|
||||
import importlib.metadata
|
||||
import importlib.util
|
||||
import inspect
|
||||
import ipaddress
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import socket
|
||||
import subprocess
|
||||
@@ -1550,6 +1552,67 @@ def import_from_path(module_name: str, file_path: Union[str, os.PathLike]):
|
||||
return module
|
||||
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def get_vllm_optional_dependencies():
|
||||
metadata = importlib.metadata.metadata("vllm")
|
||||
requirements = metadata.get_all("Requires-Dist", [])
|
||||
extras = metadata.get_all("Provides-Extra", [])
|
||||
|
||||
return {
|
||||
extra: [
|
||||
re.split(r";|>=|<=|==", req)[0] for req in requirements
|
||||
if req.endswith(f'extra == "{extra}"')
|
||||
]
|
||||
for extra in extras
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PlaceholderModule:
|
||||
"""
|
||||
A placeholder object to use when a module does not exist.
|
||||
|
||||
This enables more informative errors when trying to access attributes
|
||||
of a module that does not exists.
|
||||
"""
|
||||
name: str
|
||||
|
||||
def placeholder_attr(self, attr_path: str):
|
||||
return _PlaceholderModuleAttr(self, attr_path)
|
||||
|
||||
def __getattr__(self, key: str):
|
||||
name = self.name
|
||||
|
||||
try:
|
||||
importlib.import_module(self.name)
|
||||
except ImportError as exc:
|
||||
for extra, names in get_vllm_optional_dependencies().items():
|
||||
if name in names:
|
||||
msg = f"Please install vllm[{extra}] for {extra} support"
|
||||
raise ImportError(msg) from exc
|
||||
|
||||
raise exc
|
||||
|
||||
raise AssertionError("PlaceholderModule should not be used "
|
||||
"when the original module can be imported")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _PlaceholderModuleAttr:
|
||||
module: PlaceholderModule
|
||||
attr_path: str
|
||||
|
||||
def placeholder_attr(self, attr_path: str):
|
||||
return _PlaceholderModuleAttr(self.module,
|
||||
f"{self.attr_path}.{attr_path}")
|
||||
|
||||
def __getattr__(self, key: str):
|
||||
getattr(self.module, f"{self.attr_path}.{key}")
|
||||
|
||||
raise AssertionError("PlaceholderModule should not be used "
|
||||
"when the original module can be imported")
|
||||
|
||||
|
||||
# create a library to hold the custom op
|
||||
vllm_lib = Library("vllm", "FRAGMENT") # noqa
|
||||
|
||||
|
||||
Reference in New Issue
Block a user