Improve options management (#2078)

- `/sdapi/v1/options` GET now calls `get_config()` from **sysinfo** module, instead of from its own version of the function.

- Defined a new, flexible and more robust `set_config()` function in **sysinfo** module, which:
  - obsoletes redundant code
  - skips updating values that are unchanged
  - has flexible args for both API and UI use

- `/sdapi/v1/options` POST and `override_settings` now use the new `set_config()` function.  `set_config()` could possibly obsolete additional functions, but I'm not going to get into that just yet.

- Options for `forge_additional_modules` can now be provided either as the file path, or just the module name.

- Most importantly, `refresh_model_loading_parameters()` is now only called ONCE per request, and **only** if necessary.

- It is now much easier to call `shared.opts.save()` as needed
This commit is contained in:
altoiddealer
2024-10-16 06:21:54 -04:00
committed by GitHub
parent cce30d3340
commit 2c543719e3
4 changed files with 69 additions and 75 deletions

View File

@@ -6,6 +6,7 @@ import platform
import hashlib
import re
from pathlib import Path
from typing import Any
from modules import paths_internal, timer, shared_cmd_options, errors, launch_utils
@@ -213,3 +214,44 @@ def get_config():
return json.load(f)
except Exception as e:
return str(e)
def set_config(req: dict[str, Any], is_api=False, run_callbacks=True, save_config=True):
from modules import shared, sd_models
from modules_forge import main_entry
should_refresh_model_loading_params = False
memory_changes = {}
memory_keys = ['forge_inference_memory', 'forge_async_loading', 'forge_pin_shared_memory']
for k, v in req.items():
# ignore unchanged options
if v == shared.opts.data.get(k):
continue
# checkpoints, modules, and options pertaining to memory management are managed in dedicated functions
# If values for these options change, call refresh_model_loading_parameters()
if k == 'sd_model_checkpoint':
if v is not None and v not in sd_models.checkpoint_aliases:
raise RuntimeError(f"model {v!r} not found")
main_entry.checkpoint_change(v, save=False, refresh=False)
should_refresh_model_loading_params = True
elif k == 'forge_additional_modules':
should_refresh_model_loading_params = main_entry.modules_change(v, save=False, refresh=False)
elif k in memory_keys:
mem_key = k[len('forge_'):] # remove 'forge_' prefix
memory_changes[mem_key] = v
# set all other options
else:
shared.opts.set(k, v, is_api=is_api, run_callbacks=run_callbacks)
if memory_changes:
main_entry.refresh_memory_management_settings(**memory_changes)
should_refresh_model_loading_params = True
if should_refresh_model_loading_params:
main_entry.refresh_model_loading_parameters()
if save_config:
shared.opts.save(shared.config_filename)