Skip to content

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.

feat(model-cache): add reserved VRAM budget

JPPhoto66c6f14a517e
diff --git a/invokeai/app/services/config/config_default.py b/invokeai/app/services/config/config_default.py
index 29ce47a5b7..e43499596d 100644
--- a/invokeai/app/services/config/config_default.py
+++ b/invokeai/app/services/config/config_default.py
@@ -110,6 +110,7 @@ class InvokeAIAppConfig(BaseSettings):
profiles_dir: Path to profiles output directory.
max_cache_ram_gb: The maximum amount of CPU RAM to use for model caching in GB. If unset, the limit will be configured based on the available RAM. In most cases, it is recommended to leave this unset.
max_cache_vram_gb: The amount of VRAM to use for model caching in GB. If unset, the limit will be configured based on the available VRAM and the device_working_mem_gb. In most cases, it is recommended to leave this unset.
+ reserve_vram_gb: The amount of VRAM (in GB) to subtract from the model cache's available memory budget. Defaults to 0.
log_memory_usage: If True, a memory snapshot will be captured before and after every model cache operation, and the result will be logged (at debug level). There is a time cost to capturing the memory snapshots, so it is recommended to only enable this feature if you are actively inspecting the model cache's behaviour.
model_cache_keep_alive_min: How long to keep models in cache after last use, in minutes. A value of 0 (the default) means models are kept in cache indefinitely. If no model generations occur within the timeout period, the model cache is cleared using the same logic as the 'Clear Model Cache' button.
device_working_mem_gb: The amount of working memory to keep available on the compute device (in GB). Has no effect if running on CPU. If you are experiencing OOM errors, try increasing this value.
@@ -232,6 +233,7 @@ class InvokeAIAppConfig(BaseSettings):
# CACHE
max_cache_ram_gb: Optional[float] = Field(default=None, gt=0, description="The maximum amount of CPU RAM to use for model caching in GB. If unset, the limit will be configured based on the available RAM. In most cases, it is recommended to leave this unset.")
max_cache_vram_gb: Optional[float] = Field(default=None, ge=0, description="The amount of VRAM to use for model caching in GB. If unset, the limit will be configured based on the available VRAM and the device_working_mem_gb. In most cases, it is recommended to leave this unset.")
+ reserve_vram_gb: float = Field(default=0.0, ge=0, description="The amount of VRAM (in GB) to subtract from the model cache's available memory budget. Defaults to 0.")
log_memory_usage: bool = Field(default=False, description="If True, a memory snapshot will be captured before and after every model cache operation, and the result will be logged (at debug level). There is a time cost to capturing the memory snapshots, so it is recommended to only enable this feature if you are actively inspecting the model cache's behaviour.")
model_cache_keep_alive_min: float = Field(default=0, ge=0, description="How long to keep models in cache after last use, in minutes. A value of 0 (the default) means models are kept in cache indefinitely. If no model generations occur within the timeout period, the model cache is cleared using the same logic as the 'Clear Model Cache' button.")
device_working_mem_gb: float = Field(default=3, description="The amount of working memory to keep available on the compute device (in GB). Has no effect if running on CPU. If you are experiencing OOM errors, try increasing this value.")
diff --git a/invokeai/app/services/model_manager/model_manager_default.py b/invokeai/app/services/model_manager/model_manager_default.py
index 4672a13906..59affc051f 100644
--- a/invokeai/app/services/model_manager/model_manager_default.py
+++ b/invokeai/app/services/model_manager/model_manager_default.py
@@ -100,6 +100,7 @@ class ModelManagerService(ModelManagerServiceBase):
keep_ram_copy_of_weights=app_config.keep_ram_copy_of_weights,
max_ram_cache_size_gb=app_config.max_cache_ram_gb,
max_vram_cache_size_gb=app_config.max_cache_vram_gb,
+ reserve_vram_gb=app_config.reserve_vram_gb,
execution_device=device,
storage_device="cpu",
log_memory_usage=app_config.log_memory_usage,
diff --git a/invokeai/backend/model_manager/load/model_cache/model_cache.py b/invokeai/backend/model_manager/load/model_cache/model_cache.py
index 5b13fb5980..3691f37442 100644
--- a/invokeai/backend/model_manager/load/model_cache/model_cache.py
+++ b/invokeai/backend/model_manager/load/model_cache/model_cache.py
@@ -578,6 +578,7 @@ class ModelCache:
keep_alive_minutes: float = 0,
shared_cpu_weights: SharedCpuWeightsStore | None = SHARED_CPU_WEIGHTS,
ram_budget: RamBudget | None = None,
+ reserve_vram_gb: float = 0.0,
):
"""Initialize the model RAM cache.
@@ -604,6 +605,7 @@ class ModelCache:
:param ram_budget: Optional shared RamBudget used as the single global RAM authority across all per-device
caches. When provided, eviction decisions are made against the deduplicated, system-wide RAM total rather
than this cache's local (double-counted) sum. When None, the cache uses its own local RAM accounting.
+ :param reserve_vram_gb: VRAM (in GB) to subtract from the available memory budget.
"""
self._shared_cpu_weights = shared_cpu_weights
self._ram_budget = ram_budget
@@ -618,6 +620,7 @@ class ModelCache:
self._max_ram_cache_size_gb = max_ram_cache_size_gb
self._max_vram_cache_size_gb = max_vram_cache_size_gb
+ self._reserve_vram_gb = reserve_vram_gb
self._logger = PrefixedLoggerAdapter(
logger or InvokeAILogger.get_logger(self.__class__.__name__), "MODEL CACHE"
@@ -2343,12 +2346,13 @@ class ModelCache:
`_get_physical_vram_available`.
"""
working_mem_bytes = self._working_mem_reserve(working_mem_bytes)
+ reserve_vram_bytes = int(self._reserve_vram_gb * GB)
# An explicit cache cap limits model residency, but operation-specific working
# memory still must remain free for activations and temporary tensors.
if honor_cap and self._max_vram_cache_size_gb is not None:
vram_total_available_to_cache = int(self._max_vram_cache_size_gb * GB) - working_mem_bytes
- return vram_total_available_to_cache - self._get_vram_in_use()
+ return vram_total_available_to_cache - self._get_vram_in_use() - reserve_vram_bytes
if self._execution_device.type == "cuda":
vram_allocated = torch.cuda.memory_allocated(self._execution_device)
@@ -2384,7 +2388,7 @@ class ModelCache:
vram_total_available_to_cache = vram_available_to_process - working_mem_bytes
vram_cur_available_to_cache = vram_total_available_to_cache - self._get_vram_in_use()
- return vram_cur_available_to_cache
+ return vram_cur_available_to_cache - reserve_vram_bytes
def _get_reclaimable_allocator_bytes(self) -> int:
"""Bytes the torch caching allocator holds for this device but is not using, excluding
diff --git a/tests/backend/model_manager/load/model_cache/test_model_cache_vram_accounting.py b/tests/backend/model_manager/load/model_cache/test_model_cache_vram_accounting.py
index f79c7ab239..a5f7f41ec8 100644
--- a/tests/backend/model_manager/load/model_cache/test_model_cache_vram_accounting.py
+++ b/tests/backend/model_manager/load/model_cache/test_model_cache_vram_accounting.py
@@ -216,6 +216,33 @@ def test_the_windows_video_memory_budget_caps_the_measured_free_vram(monkeypatch
assert cache._get_vram_available(None) == 6 * GB
[email protected]("cache_cap_gb", [None, 12.0])
[email protected]("reserve_vram_gb", [0.0, 1.5])
+def test_reserve_vram_reduces_available_memory(
+ monkeypatch: pytest.MonkeyPatch, cache_cap_gb: float | None, reserve_vram_gb: float
+):
+ cache = ModelCache(
+ execution_device_working_mem_gb=1.0,
+ enable_partial_loading=True,
+ keep_ram_copy_of_weights=True,
+ max_vram_cache_size_gb=cache_cap_gb,
+ reserve_vram_gb=reserve_vram_gb,
+ execution_device="cpu",
+ storage_device="cpu",
+ logger=MagicMock(),
+ shared_cpu_weights=None,
+ )
+ cache._execution_device = torch.device("cuda") # Avoid requiring a physical GPU.
+ monkeypatch.setattr(TorchDevice, "cuda_mem_get_info", classmethod(lambda cls, device: (8 * GB, 16 * GB)))
+ monkeypatch.setattr(torch.cuda, "memory_allocated", lambda device: 2 * GB)
+ monkeypatch.setattr(cache, "_get_reclaimable_allocator_bytes", lambda: 0)
+
+ reserved_bytes = int(reserve_vram_gb * GB)
+ expected_cache = (9 if cache_cap_gb is not None else 7) * GB - reserved_bytes
+ assert cache._get_vram_available(None) == expected_cache
+ assert cache._get_physical_vram_available() == 7 * GB - reserved_bytes
+
+
@pytest.mark.parametrize("peer_busy", [False, True], ids=["alone", "peer-device-busy"])
def test_offloading_under_expandable_segments_stops_once_enough_is_free(monkeypatch: pytest.MonkeyPatch, peer_busy):
"""Under expandable segments the driver sees an offloaded model's pages only after empty_cache(), and no