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
+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: