From 880727436c9e3b5c531bf7034c30bbd30b231579 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Wed, 2 Aug 2023 09:43:52 -0400 Subject: [PATCH] fix default vram cache size calculation --- invokeai/app/services/config.py | 4 ++-- invokeai/backend/install/invokeai_configure.py | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/invokeai/app/services/config.py b/invokeai/app/services/config.py index a9a4a64f75..264a93955c 100644 --- a/invokeai/app/services/config.py +++ b/invokeai/app/services/config.py @@ -173,7 +173,7 @@ from typing import ClassVar, Dict, List, Set, Literal, Union, get_origin, get_ty INIT_FILE = Path("invokeai.yaml") DB_FILE = Path("invokeai.db") LEGACY_INIT_FILE = Path("invokeai.init") - +DEFAULT_MAX_VRAM = 2.75 class InvokeAISettings(BaseSettings): """ @@ -395,7 +395,7 @@ class InvokeAIAppConfig(InvokeAISettings): free_gpu_mem : bool = Field(default=False, description="If true, purge model from GPU after each generation.", category='Memory/Performance') max_loaded_models : int = Field(default=3, gt=0, description="(DEPRECATED: use max_cache_size) Maximum number of models to keep in memory for rapid switching", category='DEPRECATED') max_cache_size : float = Field(default=6.0, gt=0, description="Maximum memory amount used by model cache for rapid switching", category='Memory/Performance') - max_vram_cache_size : float = Field(default=2.75, ge=0, description="Amount of VRAM reserved for model storage", category='Memory/Performance') + max_vram_cache_size : float = Field(default=DEFAULT_MAX_VRAM, ge=0, description="Amount of VRAM reserved for model storage", category='Memory/Performance') gpu_mem_reserved : float = Field(default=2.75, ge=0, description="DEPRECATED: use max_vram_cache_size. Amount of VRAM reserved for model storage", category='DEPRECATED') nsfw_checker : bool = Field(default=True, description="DEPRECATED: use Web settings to enable/disable", category='DEPRECATED') precision : Literal[tuple(['auto','float16','float32','autocast'])] = Field(default='auto',description='Floating point precision', category='Memory/Performance') diff --git a/invokeai/backend/install/invokeai_configure.py b/invokeai/backend/install/invokeai_configure.py index 9136604b14..2e95993d75 100755 --- a/invokeai/backend/install/invokeai_configure.py +++ b/invokeai/backend/install/invokeai_configure.py @@ -82,6 +82,7 @@ PRECISION_CHOICES = ["auto", "float16", "float32"] HAS_CUDA = torch.cuda.is_available() _, MAX_VRAM = torch.cuda.mem_get_info() if HAS_CUDA else (0, 0) MAX_VRAM /= 1073741824 # GB in bytes +MAX_VRAM_CACHE_RATIO = 0.55 # first guess of optimal vram cache based on total available INIT_FILE_PREAMBLE = """# InvokeAI initialization file # This is the InvokeAI initialization file, which contains command-line default values. @@ -568,9 +569,11 @@ def edit_opts(program_opts: Namespace, invokeai_opts: Namespace) -> argparse.Nam def default_startup_options(init_file: Path) -> Namespace: opts = InvokeAIAppConfig.get_config() + # dynamically adust vram for memory size + if not init_file.exists(): + opts.max_vram_cache_size = round((MAX_VRAM * MAX_VRAM_CACHE_RATIO)*4) / 4 return opts - def default_user_selections(program_opts: Namespace) -> InstallSelections: try: installer = ModelInstall(config) @@ -628,7 +631,6 @@ def maybe_create_models_yaml(root: Path): # ------------------------------------- def run_console_ui(program_opts: Namespace, initfile: Path = None) -> (Namespace, Namespace): - # parse_args() will read from init file if present invokeai_opts = default_startup_options(initfile) invokeai_opts.root = program_opts.root @@ -711,7 +713,6 @@ def migrate_init_file(legacy_format: Path): # ------------------------------------- def migrate_models(root: Path): from invokeai.backend.install.migrate_to_3 import do_migrate - do_migrate(root, root) @@ -813,6 +814,7 @@ def main(): models_to_download = default_user_selections(opt) new_init_file = config.root_path / "invokeai.yaml" + if opt.yes_to_all: write_default_options(opt, new_init_file) init_options = Namespace(precision="float32" if opt.full_precision else "float16")