Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(image-index): evict a replaced encoder from the model cache before reindexing
Josh Corbett53002f9affe2
diff --git a/invokeai/app/services/image_index/image_index_default.py b/invokeai/app/services/image_index/image_index_default.pyindex 1178b225ff..16412d601e 100644--- a/invokeai/app/services/image_index/image_index_default.py+++ b/invokeai/app/services/image_index/image_index_default.py@@ -4,7 +4,7 @@ import timefrom contextlib import contextmanagerfrom pathlib import Pathfrom queue import Empty, Queue-from typing import TYPE_CHECKING, AbstractSet, Any, Callable, Iterator, Optional+from typing import TYPE_CHECKING, AbstractSet, Any, Callable, Iterable, Iterator, Optionalimport numpy as npimport torch@@ -287,7 +287,7 @@ class ImageIndexService(ImageIndexServiceBase):self._activation_lock = threading.Lock()# Request threads may still be using an encoder after it is removed.# Retirement publishes unavailability immediately; replacement waits- # for these users and the worker without blocking a polling request.+ # for these users and the worker without a polling request joining them.self._model_users = 0self._users_drained = threading.Condition(self._activation_lock)# A retired model whose configured name now resolves to another@@ -855,10 +855,10 @@ class ImageIndexService(ImageIndexServiceBase):Called off the event loop by map and search requests and by theworker. Rechecks are throttled in both the running and missing states.- No join or encoder lock is taken here: a deleted model becomes- unavailable at once, while its in-flight users keep their stable- resources until the exiting worker, or a later request, can safely- replace them.+ No join is taken here: a deleted model becomes unavailable at once,+ while its in-flight users keep their stable resources until the+ exiting worker, or a later request, can safely replace them. Only that+ swap waits, for the model-load lock (_drop_cached_models)."""invoker = self._invokerif invoker is None or not invoker.services.configuration.image_index_enabled:@@ -906,6 +906,7 @@ class ImageIndexService(ImageIndexServiceBase):def _swap_model(self, invoker: "Invoker", model_config: Optional["AnyModelConfig"]) -> bool:"""Retire the drained model and start `model_config`, if any. Called under _activation_lock."""config = invoker.services.configuration+ self._drop_cached_models(invoker, (self._model_config, model_config))self._reset_model_resources()self._replacement_pending = Falseself._model_config = None@@ -964,6 +965,26 @@ class ImageIndexService(ImageIndexServiceBase):returnself._swap_model(invoker, model_config)+ def _drop_cached_models(self, invoker: "Invoker", configs: Iterable[Optional["AnyModelConfig"]]) -> None:+ """Evict the retired and replacement encoders from every per-device model cache.++ The shared cache is keyed by model key alone, so an encoder that keeps its key across a+ reinstall would be served from the retired weights while its embeddings are stored under+ the replacement's hash, and those survive a restart. The write lock keeps a concurrent+ load from re-registering the old shared CPU weights around the drop. It is taken under+ _activation_lock, so map polls, searches and stop() wait out any model load or VRAM move+ in flight; that cannot deadlock, because the worker and request users have drained and+ nothing takes _activation_lock while holding the load lock.+ """+ keys = {config.key for config in configs if config is not None}+ if not keys:+ # Still missing: this runs on every throttled recheck, and must not queue behind a load.+ return+ with MODEL_LOAD_LOCK.write_lock():+ for cache in set(invoker.services.model_manager.load.ram_caches.values()):+ for key in keys:+ cache.drop_model(key)+def _reset_model_resources(self) -> None:"""Retire caches only after the worker and request users have drained.diff --git a/tests/app/services/image_index/test_image_index_service.py b/tests/app/services/image_index/test_image_index_service.pyindex 6ac8c530b7..2f78fbfea9 100644--- a/tests/app/services/image_index/test_image_index_service.py+++ b/tests/app/services/image_index/test_image_index_service.py@@ -30,6 +30,7 @@ from invokeai.app.services.image_records.image_records_common import ImageCategofrom invokeai.app.services.image_records.image_records_sqlite import SqliteImageRecordStoragefrom invokeai.app.services.images.images_common import image_record_to_dtofrom invokeai.app.services.images.images_default import ImageService+from invokeai.app.services.model_load.model_load_default import ModelLoadServicefrom invokeai.app.services.model_records.model_records_sql import ModelRecordServiceSQLfrom invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabasefrom invokeai.app.services.video_records.video_records_sqlite import SqliteVideoRecordStorage@@ -37,6 +38,7 @@ from invokeai.app.services.videos.videos_common import VideoDTO, video_record_tofrom invokeai.app.services.videos.videos_default import VideoServicefrom invokeai.backend.model_manager.configs.clip_vision import CLIPVision_Diffusers_Configfrom invokeai.backend.model_manager.configs.siglip import SigLIP_Diffusers_Config+from invokeai.backend.model_manager.load.model_cache.model_cache import ModelCachefrom invokeai.backend.model_manager.taxonomy import ModelRepoVariant, ModelSourceTypefrom invokeai.backend.util.devices import TorchDevicefrom invokeai.backend.util.logging import InvokeAILogger@@ -45,6 +47,8 @@ from tests.test_nodes import TestEventServiceMODEL_ID = "test-model-hash"DIM = 8+# An encoder swap evicts the retired model from every per-device cache; most tests have none.+_NO_MODEL_CACHES = SimpleNamespace(ram_caches={})def _wait_until(predicate: Callable[[], bool], timeout: float = 10.0) -> None:@@ -178,7 +182,7 @@ def _make_invoker(image_index_records=index_records,events=TestEventService(),session_queue=session_queue,- model_manager=model_manager,+ model_manager=model_manager if model_manager is not None else SimpleNamespace(load=_NO_MODEL_CACHES),)return SimpleNamespace(services=services)@@ -500,7 +504,9 @@ def test_try_activate_picks_up_a_model_installed_after_startup(monkeypatch.setattr(ImageIndexService, "_resolve_model_config", resolve)store = SimpleNamespace(search_by_attr=lambda **kwargs: [])- invoker = _make_invoker(images_service, index_records, model_manager=SimpleNamespace(store=store))+ invoker = _make_invoker(+ images_service, index_records, model_manager=SimpleNamespace(store=store, load=_NO_MODEL_CACHES)+ )service = ImageIndexService()try:service.start(invoker)@@ -558,7 +564,9 @@ def test_failed_late_activation_rolls_back_rather_than_wedging_the_service(original_launch = ImageIndexService._launch_workerstore = SimpleNamespace(search_by_attr=lambda **kwargs: [])- invoker = _make_invoker(images_service, index_records, model_manager=SimpleNamespace(store=store))+ invoker = _make_invoker(+ images_service, index_records, model_manager=SimpleNamespace(store=store, load=_NO_MODEL_CACHES)+ )service = ImageIndexService()try:# Inert at start: the encoder was not installed yet.@@ -600,7 +608,10 @@ def test_encoder_metadata_edits_keep_worker_indexing_without_map_requests(store = ModelRecordServiceSQL(db, InvokeAILogger.get_logger())store.add_model(encoder_config)invoker = _make_invoker(- images_service, index_records, image_records=image_records, model_manager=SimpleNamespace(store=store)+ images_service,+ index_records,+ image_records=image_records,+ model_manager=SimpleNamespace(store=store, load=_NO_MODEL_CACHES),)invoker.services.configuration.image_index_model = encoder_config.nameinvoker.services.configuration.models_dir = models_dir@@ -791,6 +802,91 @@ def test_encoder_replacement_waits_for_inflight_embedding_and_discards_retired_bservice.stop()+def _write_clip_vision_weights(path: Path, seed: int) -> None:+ from transformers import CLIPVisionConfig, CLIPVisionModelWithProjection++ config = CLIPVisionConfig(+ hidden_size=8,+ intermediate_size=16,+ num_hidden_layers=1,+ num_attention_heads=2,+ image_size=224,+ patch_size=112,+ projection_dim=DIM,+ )+ with torch.random.fork_rng(devices=[]):+ torch.manual_seed(seed)+ CLIPVisionModelWithProjection(config).save_pretrained(path)+++def test_reinstalled_encoder_keeping_its_key_embeds_with_the_new_weights(+ tmp_path: Path,+ image_records: SqliteImageRecordStorage,+ images_service: ImageService,+ index_records: ImageIndexRecordsSqlite,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The shared model cache is keyed by model key alone. Reinstalling the encoder in place changes+ # its hash but not its key, and embeddings computed by the cached, retired weights would be+ # stored under the replacement's hash and survive a restart.+ from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection++ encoder_path = tmp_path / "encoder"+ _write_clip_vision_weights(encoder_path, seed=1)+ retired = CLIPVision_Diffusers_Config(+ key="encoder-key",+ hash="retired-hash",+ path=str(encoder_path),+ file_size=1,+ name="encoder",+ source=str(encoder_path),+ source_type=ModelSourceType.Path,+ )+ installed = [retired]+ monkeypatch.setattr(ImageIndexService, "_resolve_model_config", lambda self, name: installed[0])+ # Embed through the shared cache, as an accelerator host does, but on CPU tensors.+ monkeypatch.setattr(TorchDevice, "choose_torch_device", staticmethod(lambda: torch.device("cpu")))+ monkeypatch.setattr(ImageIndexService, "_cpu_mode", lambda self: False)+ invoker = _make_invoker(images_service, index_records, image_records=image_records, device=None)+ cache = ModelCache(+ execution_device_working_mem_gb=1,+ enable_partial_loading=False,+ keep_ram_copy_of_weights=True,+ execution_device=torch.device("cpu"),+ logger=invoker.services.logger,+ # The default store is process-global; keep this encoder's weights out of it.+ shared_cpu_weights=None,+ )+ invoker.services.model_manager = SimpleNamespace(+ load=ModelLoadService(app_config=invoker.services.configuration, ram_cache=cache)+ )+ item = IndexedItem("image", "a.png")+ service = ImageIndexService()+ try:+ _save_image(image_records, item.name)+ service.start(invoker)+ _wait_until(lambda: index_records.count_index_status(retired.hash).embedded == 1)+ retired_embedding = index_records.get_embeddings([item], retired.hash)[1][0]++ _write_clip_vision_weights(encoder_path, seed=2)+ replacement = retired.model_copy(update={"hash": "replacement-hash"})+ with service._activation_lock:+ installed[0] = replacement+ service._last_activation_attempt = time.monotonic() - _ACTIVATION_RETRY_INTERVAL_S - 1+ _wait_until(lambda: index_records.count_index_status(replacement.hash).embedded == 1)++ with torch.no_grad():+ pixel_values = CLIPImageProcessor()(images=images_service.get_pil_image(item.name), return_tensors="pt")+ expected = CLIPVisionModelWithProjection.from_pretrained(encoder_path)(**pixel_values).image_embeds[0]+ expected = (expected / expected.norm()).numpy()+ stored = index_records.get_embeddings([item], replacement.hash)[1][0]+ assert not np.allclose(expected, retired_embedding, atol=1e-3), "the two encoders must disagree"+ np.testing.assert_allclose(stored, expected, atol=1e-5)+ finally:+ service.stop()+ cache.shutdown()++def test_worker_detected_replacement_resumes_indexing_without_requests(encoder_config: CLIPVision_Diffusers_Config | SigLIP_Diffusers_Config,image_records: SqliteImageRecordStorage,@@ -906,7 +1002,9 @@ def test_late_activation_survives_a_model_store_failure(raise RuntimeError("model store unavailable")store = SimpleNamespace(search_by_attr=lambda **kwargs: [])- invoker = _make_invoker(images_service, index_records, model_manager=SimpleNamespace(store=store))+ invoker = _make_invoker(+ images_service, index_records, model_manager=SimpleNamespace(store=store, load=_NO_MODEL_CACHES)+ )service = ImageIndexService()monkeypatch.setattr(ImageIndexService, "_resolve_model_config", lambda self, model_name: None)service.start(invoker)@@ -1339,7 +1437,7 @@ def test_empty_model_name_does_not_resolve_to_an_arbitrary_model("""installed = SimpleNamespace(key="some-key", name="clip-vit-large-patch14", hash="some-hash")store = SimpleNamespace(search_by_attr=lambda model_name, model_type: [installed])- model_manager = SimpleNamespace(store=store)+ model_manager = SimpleNamespace(store=store, load=_NO_MODEL_CACHES)service = ImageIndexService()service._invoker = _make_invoker(images_service, index_records, model_manager=model_manager)@@ -1366,7 +1464,9 @@ def test_duplicate_model_names_resolve_deterministically(def resolve(order: list[SimpleNamespace]) -> SimpleNamespace:store = SimpleNamespace(search_by_attr=lambda model_name, model_type: list(order))service = ImageIndexService()- service._invoker = _make_invoker(images_service, index_records, model_manager=SimpleNamespace(store=store))+ service._invoker = _make_invoker(+ images_service, index_records, model_manager=SimpleNamespace(store=store, load=_NO_MODEL_CACHES)+ )return service._resolve_model_config("clip-vit-large-patch14")# Same set, either insertion order — the winner must not move.