Skip to content

Commit

Permalink
expose max_cache_size in config
Browse files Browse the repository at this point in the history
  • Loading branch information
ebr committed Jul 4, 2023
1 parent 92b163e commit dc3e306
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
19 changes: 10 additions & 9 deletions invokeai/app/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ def add_parser_arguments(cls, parser):
upcase_environ = dict()
for key,value in os.environ.items():
upcase_environ[key.upper()] = value

fields = cls.__fields__
cls.argparse_groups = {}

for name, field in fields.items():
if name not in cls._excluded():
current_default = field.default
Expand Down Expand Up @@ -348,7 +348,7 @@ class InvokeAIAppConfig(InvokeAISettings):
'''
singleton_config: ClassVar[InvokeAIAppConfig] = None
singleton_init: ClassVar[Dict] = None

#fmt: off
type: Literal["InvokeAI"] = "InvokeAI"
host : str = Field(default="127.0.0.1", description="IP address to bind to", category='Web Server')
Expand All @@ -367,7 +367,8 @@ class InvokeAIAppConfig(InvokeAISettings):

always_use_cpu : bool = Field(default=False, description="If true, use the CPU for rendering even if a GPU is available.", category='Memory/Performance')
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="Maximum number of models to keep in memory for rapid switching", 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='Memory/Performance')
max_cache_size : float = Field(default=6.0, gt=0, description="Maximum memory amount used by model cache for rapid switching", category='Memory/Performance')
precision : Literal[tuple(['auto','float16','float32','autocast'])] = Field(default='float16',description='Floating point precision', category='Memory/Performance')
sequential_guidance : bool = Field(default=False, description="Whether to calculate guidance in serial instead of in parallel, lowering memory requirements", category='Memory/Performance')
xformers_enabled : bool = Field(default=True, description="Enable/disable memory-efficient attention", category='Memory/Performance')
Expand All @@ -385,9 +386,9 @@ class InvokeAIAppConfig(InvokeAISettings):
outdir : Path = Field(default='outputs', description='Default folder for output images', category='Paths')
from_file : Path = Field(default=None, description='Take command input from the indicated file (command-line client only)', category='Paths')
use_memory_db : bool = Field(default=False, description='Use in-memory database for storing image metadata', category='Paths')

model : str = Field(default='stable-diffusion-1.5', description='Initial model name', category='Models')

log_handlers : List[str] = Field(default=["console"], description='Log handler. Valid options are "console", "file=<path>", "syslog=path|address:host:port", "http=<url>"', category="Logging")
# note - would be better to read the log_format values from logging.py, but this creates circular dependencies issues
log_format : Literal[tuple(['plain','color','syslog','legacy'])] = Field(default="color", description='Log format. Use "plain" for text-only, "color" for colorized output, "legacy" for 2.3-style logging and "syslog" for syslog-style', category="Logging")
Expand All @@ -396,7 +397,7 @@ class InvokeAIAppConfig(InvokeAISettings):

def parse_args(self, argv: List[str]=None, conf: DictConfig = None, clobber=False):
'''
Update settings with contents of init file, environment, and
Update settings with contents of init file, environment, and
command-line settings.
:param conf: alternate Omegaconf dictionary object
:param argv: aternate sys.argv list
Expand All @@ -411,7 +412,7 @@ def parse_args(self, argv: List[str]=None, conf: DictConfig = None, clobber=Fals
except:
pass
InvokeAISettings.initconf = conf

# parse args again in order to pick up settings in configuration file
super().parse_args(argv)

Expand All @@ -431,7 +432,7 @@ def get_config(cls,**kwargs)->InvokeAIAppConfig:
cls.singleton_config = cls(**kwargs)
cls.singleton_init = kwargs
return cls.singleton_config

@property
def root_path(self)->Path:
'''
Expand Down
34 changes: 18 additions & 16 deletions invokeai/app/services/model_manager_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def __init__(
logger: types.ModuleType,
):
"""
Initialize with the path to the models.yaml config file.
Initialize with the path to the models.yaml config file.
Optional parameters are the torch device type, precision, max_models,
and sequential_offload boolean. Note that the default device
type and precision are set up for a CUDA system running at half precision.
"""
pass

@abstractmethod
def get_model(
self,
Expand All @@ -50,8 +50,8 @@ def get_model(
node: Optional[BaseInvocation] = None,
context: Optional[InvocationContext] = None,
) -> ModelInfo:
"""Retrieve the indicated model with name and type.
submodel can be used to get a part (such as the vae)
"""Retrieve the indicated model with name and type.
submodel can be used to get a part (such as the vae)
of a diffusers pipeline."""
pass

Expand Down Expand Up @@ -115,8 +115,8 @@ def add_model(
"""
Update the named model with a dictionary of attributes. Will fail with an
assertion error if the name already exists. Pass clobber=True to overwrite.
On a successful update, the config will be changed in memory. Will fail
with an assertion error if provided attributes are incorrect or
On a successful update, the config will be changed in memory. Will fail
with an assertion error if provided attributes are incorrect or
the model name is missing. Call commit() to write changes to disk.
"""
pass
Expand All @@ -129,8 +129,8 @@ def del_model(
model_type: ModelType,
):
"""
Delete the named model from configuration. If delete_files is true,
then the underlying weight file or diffusers directory will be deleted
Delete the named model from configuration. If delete_files is true,
then the underlying weight file or diffusers directory will be deleted
as well. Call commit() to write to disk.
"""
pass
Expand Down Expand Up @@ -176,7 +176,7 @@ def __init__(
logger: types.ModuleType,
):
"""
Initialize with the path to the models.yaml config file.
Initialize with the path to the models.yaml config file.
Optional parameters are the torch device type, precision, max_models,
and sequential_offload boolean. Note that the default device
type and precision are set up for a CUDA system running at half precision.
Expand Down Expand Up @@ -206,6 +206,8 @@ def __init__(
if hasattr(config,'max_cache_size') \
else config.max_loaded_models * 2.5

logger.debug("Maximum RAM cache size: %f GiB", max_cache_size)

sequential_offload = config.sequential_guidance

self.mgr = ModelManager(
Expand Down Expand Up @@ -261,7 +263,7 @@ def get_model(
submodel=submodel,
model_info=model_info
)

return model_info

def model_exists(
Expand Down Expand Up @@ -314,8 +316,8 @@ def add_model(
"""
Update the named model with a dictionary of attributes. Will fail with an
assertion error if the name already exists. Pass clobber=True to overwrite.
On a successful update, the config will be changed in memory. Will fail
with an assertion error if provided attributes are incorrect or
On a successful update, the config will be changed in memory. Will fail
with an assertion error if provided attributes are incorrect or
the model name is missing. Call commit() to write changes to disk.
"""
return self.mgr.add_model(model_name, base_model, model_type, model_attributes, clobber)
Expand All @@ -328,8 +330,8 @@ def del_model(
model_type: ModelType,
):
"""
Delete the named model from configuration. If delete_files is true,
then the underlying weight file or diffusers directory will be deleted
Delete the named model from configuration. If delete_files is true,
then the underlying weight file or diffusers directory will be deleted
as well. Call commit() to write to disk.
"""
self.mgr.del_model(model_name, base_model, model_type)
Expand Down Expand Up @@ -383,7 +385,7 @@ def _emit_load_event(
@property
def logger(self):
return self.mgr.logger

def heuristic_import(self,
items_to_import: Set[str],
prediction_type_helper: Callable[[Path],SchedulerPredictionType]=None,
Expand All @@ -404,4 +406,4 @@ def heuristic_import(self,
of the set is a dict corresponding to the newly-created OmegaConf stanza for
that model.
'''
return self.mgr.heuristic_import(items_to_import, prediction_type_helper)
return self.mgr.heuristic_import(items_to_import, prediction_type_helper)

0 comments on commit dc3e306

Please sign in to comment.