Skip to content

High performance self-hosted photo and video management solution.

fix(ml): preloaded model loaded twice (#32610)

Yaros166e93559f12
diff --git a/machine-learning/immich_ml/models/cache.py b/machine-learning/immich_ml/models/cache.py
index db2ae46e6c..580631d20d 100644
--- a/machine-learning/immich_ml/models/cache.py
+++ b/machine-learning/immich_ml/models/cache.py
@@ -3,6 +3,7 @@ from collections.abc import Hashable
from typing import Any
from immich_ml import allocator
+from immich_ml.config import clean_name
from immich_ml.models.base import InferenceEntry, InferenceModel
from immich_ml.schemas import Options
@@ -15,7 +16,8 @@ class ModelCache:
self._expiries: dict[Key, asyncio.TimerHandle] = {}
def get[O: Options](self, entry: InferenceEntry[O], ttl: int | None = None) -> InferenceModel[O]:
- key = (entry.model, entry.name, entry.model.graph(entry.options))
+ # names that differ only in what cleaning drops, like an `immich-app/` prefix, are the same model
+ key = (entry.model, clean_name(entry.name), entry.model.graph(entry.options))
if key not in self._models:
self._models[key] = entry.model.create(entry.name, entry.options)
elif key not in self._expiries:
diff --git a/machine-learning/test_main.py b/machine-learning/test_main.py
index 1a4fbc1a0a..a4ec85d4b2 100644
--- a/machine-learning/test_main.py
+++ b/machine-learning/test_main.py
@@ -1877,6 +1877,14 @@ class TestCache:
assert len(model_cache._models) == 1
model.create.assert_called_once()
+ async def test_shares_an_instance_across_names_of_the_same_model(self) -> None:
+ model_cache = ModelCache()
+ bare = model_cache.get(InferenceEntry(OpenClipTextualEncoder, "ViT-B-32__openai", TextualOptions()))
+ prefixed = model_cache.get(
+ InferenceEntry(OpenClipTextualEncoder, "immich-app/ViT-B-32__openai", TextualOptions(language="en-US"))
+ )
+ assert bare is prefixed
+
async def test_separate_instances_per_graph_option(self) -> None:
# the real TextDetector, so that keying it on anything but its resolution fails this
model_cache = ModelCache()