Fix model info cache for package models (#46567)

Signed-off-by: soaringk <k3vin.zhang@gmail.com>
This commit is contained in:
soaringk
2026-06-29 09:17:54 +00:00
committed by GitHub
parent e186107870
commit ab132ee98b
2 changed files with 37 additions and 2 deletions
+17
View File
@@ -20,6 +20,7 @@ from vllm.model_executor.models.registry import (
_SPECULATIVE_DECODING_MODELS,
_TEXT_GENERATION_MODELS,
ModelRegistry,
_LazyRegisteredModel,
)
from vllm.platforms import current_platform
@@ -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()
+20 -2
View File
@@ -849,6 +849,25 @@ class _LazyRegisteredModel(_BaseRegisteredModel):
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:
@@ -915,8 +934,7 @@ class _LazyRegisteredModel(_BaseRegisteredModel):
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: