Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/models/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
_SPECULATIVE_DECODING_MODELS,
_TEXT_GENERATION_MODELS,
ModelRegistry,
_LazyRegisteredModel,
)
from vllm.platforms import current_platform

Expand Down Expand Up @@ -127,6 +128,22 @@ def test_registry_is_pp(model_arch, is_pp, init_cuda):
)


def test_lazy_modelinfo_package_hash_includes_submodules(tmp_path):
package_dir = tmp_path / "model_package"
package_dir.mkdir()
init_file = package_dir / "__init__.py"
init_file.write_text("from .model import Model\n", encoding="utf-8")
model_file = package_dir / "model.py"
model_file.write_text("class Model: pass\n", encoding="utf-8")

first_hash = _LazyRegisteredModel._get_modelinfo_module_hash(init_file)

model_file.write_text("class Model:\n supports_pp = True\n", encoding="utf-8")
second_hash = _LazyRegisteredModel._get_modelinfo_module_hash(init_file)

assert first_hash != second_hash


def test_hf_registry_coverage():
untested_archs = (
ModelRegistry.get_supported_archs() - HF_EXAMPLE_MODELS.get_supported_archs()
Expand Down
22 changes: 20 additions & 2 deletions vllm/model_executor/models/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,25 @@ def _get_cache_filename(self) -> str:
cls_name = f"{self.module_name}-{self.class_name}".replace(".", "-")
return f"{cls_name}.json"

@staticmethod
def _get_modelinfo_module_hash(model_path: Path) -> str:
if model_path.name == "__init__.py":
# Package entry points often re-export classes implemented in
# submodules, so include the package contents in the cache key.
module_paths = sorted(model_path.parent.rglob("*.py"))
root_path = model_path.parent
else:
module_paths = [model_path]
root_path = model_path.parent

hasher = safe_hash(b"", usedforsecurity=False)
for path in module_paths:
hasher.update(path.relative_to(root_path).as_posix().encode("utf-8"))
hasher.update(b"\0")
hasher.update(path.read_bytes())
hasher.update(b"\0")
return hasher.hexdigest()

def _load_modelinfo_from_cache(self, module_hash: str) -> _ModelInfo | None:
try:
try:
Expand Down Expand Up @@ -915,8 +934,7 @@ def inspect_model_cls(self) -> _ModelInfo:
module_hash = None

if model_path is not None and model_path.exists():
with open(model_path, "rb") as f:
module_hash = safe_hash(f.read(), usedforsecurity=False).hexdigest()
module_hash = self._get_modelinfo_module_hash(model_path)

mi = self._load_modelinfo_from_cache(module_hash)
if mi is not None:
Expand Down
Loading