2023-05-18 14:48:23 +00:00
# Copyright (c) 2023 Lincoln Stein (https://github.com/lstein) and the InvokeAI Development Team
2023-05-04 05:20:30 +00:00
""" Invokeai configuration system.
Arguments and fields are taken from the pydantic definition of the
model . Defaults can be set by creating a yaml configuration file that
2023-05-17 16:19:19 +00:00
has a top - level key of " InvokeAI " and subheadings for each of the
categories returned by ` invokeai - - help ` . The file looks like this :
2023-05-04 05:20:30 +00:00
[ file : invokeai . yaml ]
2023-05-17 16:19:19 +00:00
InvokeAI :
Paths :
root : / home / lstein / invokeai - main
conf_path : configs / models . yaml
legacy_conf_dir : configs / stable - diffusion
outdir : outputs
2023-06-25 22:50:15 +00:00
autoimport_dir : null
2023-05-17 16:19:19 +00:00
Models :
model : stable - diffusion - 1.5
embeddings : true
Memory / Performance :
xformers_enabled : false
sequential_guidance : false
precision : float16
2023-07-09 22:35:04 +00:00
max_cache_size : 6
2023-08-02 18:27:13 +00:00
max_vram_cache_size : 0.5
2023-05-17 16:19:19 +00:00
always_use_cpu : false
free_gpu_mem : false
Features :
restore : true
esrgan : true
patchmatch : true
internet_available : true
log_tokenization : false
2023-05-17 23:45:51 +00:00
Web Server :
host : 127.0 .0 .1
port : 8081
2023-05-17 16:19:19 +00:00
allow_origins : [ ]
allow_credentials : true
allow_methods :
- ' * '
allow_headers :
- ' * '
2023-05-04 05:20:30 +00:00
The default name of the configuration file is ` invokeai . yaml ` , located
2023-05-17 16:19:19 +00:00
in INVOKEAI_ROOT . You can replace supersede this by providing any
OmegaConf dictionary object initialization time :
2023-05-04 05:20:30 +00:00
omegaconf = OmegaConf . load ( ' /tmp/init.yaml ' )
2023-05-26 01:10:00 +00:00
conf = InvokeAIAppConfig ( )
conf . parse_args ( conf = omegaconf )
2023-05-04 05:20:30 +00:00
2023-05-26 01:10:00 +00:00
InvokeAIAppConfig . parse_args ( ) will parse the contents of ` sys . argv `
at initialization time . You may pass a list of strings in the optional
2023-05-04 05:20:30 +00:00
` argv ` argument to use instead of the system argv :
2023-05-26 01:10:00 +00:00
conf . parse_args ( argv = [ ' --xformers_enabled ' ] )
2023-05-04 05:20:30 +00:00
2023-05-26 01:10:00 +00:00
It is also possible to set a value at initialization time . However , if
you call parse_args ( ) it may be overwritten .
2023-05-04 05:20:30 +00:00
conf = InvokeAIAppConfig ( xformers_enabled = True )
2023-05-26 01:10:00 +00:00
conf . parse_args ( argv = [ ' --no-xformers ' ] )
conf . xformers_enabled
# False
To avoid this , use ` get_config ( ) ` to retrieve the application - wide
configuration object . This will retain any properties set at object
creation time :
conf = InvokeAIAppConfig . get_config ( xformers_enabled = True )
conf . parse_args ( argv = [ ' --no-xformers ' ] )
conf . xformers_enabled
# True
2023-05-04 05:20:30 +00:00
Any setting can be overwritten by setting an environment variable of
2023-05-17 16:19:19 +00:00
form : " INVOKEAI_<setting> " , as in :
2023-05-04 05:20:30 +00:00
2023-05-17 16:19:19 +00:00
export INVOKEAI_port = 8080
2023-05-04 05:20:30 +00:00
Order of precedence ( from highest ) :
1 ) initialization options
2 ) command line options
3 ) environment variable options
4 ) config file options
5 ) pydantic defaults
2023-05-26 01:10:00 +00:00
Typical usage at the top level file :
2023-05-04 05:20:30 +00:00
from invokeai . app . services . config import InvokeAIAppConfig
2023-07-26 10:53:35 +00:00
# get global configuration and print its cache size
2023-05-26 00:41:26 +00:00
conf = InvokeAIAppConfig . get_config ( )
conf . parse_args ( )
2023-07-26 10:53:35 +00:00
print ( conf . max_cache_size )
2023-05-04 05:20:30 +00:00
2023-05-26 01:10:00 +00:00
Typical usage in a backend module :
from invokeai . app . services . config import InvokeAIAppConfig
2023-07-26 10:53:35 +00:00
# get global configuration and print its cache size value
2023-05-26 01:10:00 +00:00
conf = InvokeAIAppConfig . get_config ( )
2023-07-26 10:53:35 +00:00
print ( conf . max_cache_size )
2023-05-04 05:20:30 +00:00
Computed properties :
The InvokeAIAppConfig object has a series of properties that
resolve paths relative to the runtime root directory . They each return
a Path object :
root_path - path to InvokeAI root
output_path - path to default outputs directory
model_conf_path - path to models . yaml
conf - alias for the above
embedding_path - path to the embeddings directory
lora_path - path to the LoRA directory
2023-05-17 23:12:01 +00:00
2023-05-04 05:20:30 +00:00
In most cases , you will want to create a single InvokeAIAppConfig
2023-05-26 00:41:26 +00:00
object for the entire application . The InvokeAIAppConfig . get_config ( ) function
2023-05-04 05:20:30 +00:00
does this :
2023-05-26 00:41:26 +00:00
config = InvokeAIAppConfig . get_config ( )
config . parse_args ( ) # read values from the command line/config file
2023-05-04 05:20:30 +00:00
print ( config . root )
2023-05-17 16:19:19 +00:00
# Subclassing
If you wish to create a similar class , please subclass the
` InvokeAISettings ` class and define a Literal field named " type " ,
which is set to the desired top - level name . For example , to create a
" InvokeBatch " configuration , define like this :
class InvokeBatch ( InvokeAISettings ) :
type : Literal [ " InvokeBatch " ] = " InvokeBatch "
node_count : int = Field ( default = 1 , description = " Number of nodes to run on " , category = ' Resources ' )
cpu_count : int = Field ( default = 8 , description = " Number of GPUs to run on per node " , category = ' Resources ' )
This will now read and write from the " InvokeBatch " section of the
config file , look for environment variables named INVOKEBATCH_ * , and
accept the command - line arguments ` - - node_count ` and ` - - cpu_count ` . The
two configs are kept in separate sections of the config file :
# invokeai.yaml
InvokeBatch :
Resources :
node_count : 1
cpu_count : 8
InvokeAI :
Paths :
root : / home / lstein / invokeai - main
conf_path : configs / models . yaml
legacy_conf_dir : configs / stable - diffusion
outdir : outputs
. . .
2023-05-26 01:10:00 +00:00
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
from __future__ import annotations
2023-05-04 05:20:30 +00:00
import argparse
2023-05-17 19:22:58 +00:00
import pydoc
2023-05-04 05:20:30 +00:00
import os
import sys
from argparse import ArgumentParser
from omegaconf import OmegaConf , DictConfig
from pathlib import Path
from pydantic import BaseSettings , Field , parse_obj_as
2023-07-08 16:06:23 +00:00
from typing import ClassVar , Dict , List , Set , Literal , Union , get_origin , get_type_hints , get_args
2023-05-04 05:20:30 +00:00
INIT_FILE = Path ( " invokeai.yaml " )
2023-06-04 00:24:41 +00:00
DB_FILE = Path ( " invokeai.db " )
2023-05-04 05:20:30 +00:00
LEGACY_INIT_FILE = Path ( " invokeai.init " )
2023-08-02 18:27:13 +00:00
DEFAULT_MAX_VRAM = 0.5
2023-08-02 13:44:06 +00:00
2023-05-04 05:20:30 +00:00
class InvokeAISettings ( BaseSettings ) :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Runtime configuration settings in which default values are
read from an omegaconf . yaml file .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
initconf : ClassVar [ DictConfig ] = None
argparse_groups : ClassVar [ Dict ] = { }
def parse_args ( self , argv : list = sys . argv [ 1 : ] ) :
parser = self . get_parser ( )
2023-05-25 03:57:15 +00:00
opt = parser . parse_args ( argv )
2023-05-04 05:20:30 +00:00
for name in self . __fields__ :
if name not in self . _excluded ( ) :
setattr ( self , name , getattr ( opt , name ) )
2023-05-17 16:19:19 +00:00
def to_yaml ( self ) - > str :
"""
Return a YAML string representing our settings . This can be used
as the contents of ` invokeai . yaml ` to restore settings later .
"""
cls = self . __class__
type = get_args ( get_type_hints ( cls ) [ " type " ] ) [ 0 ]
field_dict = dict ( { type : dict ( ) } )
for name , field in self . __fields__ . items ( ) :
2023-07-15 22:15:59 +00:00
if name in cls . _excluded_from_yaml ( ) :
2023-05-17 16:19:19 +00:00
continue
category = field . field_info . extra . get ( " category " ) or " Uncategorized "
value = getattr ( self , name )
if category not in field_dict [ type ] :
field_dict [ type ] [ category ] = dict ( )
# keep paths as strings to make it easier to read
field_dict [ type ] [ category ] [ name ] = str ( value ) if isinstance ( value , Path ) else value
conf = OmegaConf . create ( field_dict )
return OmegaConf . to_yaml ( conf )
2023-05-04 05:20:30 +00:00
@classmethod
def add_parser_arguments ( cls , parser ) :
if " type " in get_type_hints ( cls ) :
2023-05-17 16:19:19 +00:00
settings_stanza = get_args ( get_type_hints ( cls ) [ " type " ] ) [ 0 ]
2023-05-04 05:20:30 +00:00
else :
2023-05-17 16:19:19 +00:00
settings_stanza = " Uncategorized "
2023-05-17 23:12:01 +00:00
2023-05-17 16:19:19 +00:00
env_prefix = cls . Config . env_prefix if hasattr ( cls . Config , " env_prefix " ) else settings_stanza . upper ( )
initconf = (
cls . initconf . get ( settings_stanza )
if cls . initconf and settings_stanza in cls . initconf
else OmegaConf . create ( )
2023-07-27 14:54:01 +00:00
)
2023-05-04 05:20:30 +00:00
2023-05-18 14:48:23 +00:00
# create an upcase version of the environment in
# order to achieve case-insensitive environment
# variables (the way Windows does)
upcase_environ = dict ( )
for key , value in os . environ . items ( ) :
upcase_environ [ key . upper ( ) ] = value
2023-07-04 21:05:35 +00:00
2023-05-04 05:20:30 +00:00
fields = cls . __fields__
cls . argparse_groups = { }
2023-07-04 21:05:35 +00:00
2023-05-04 05:20:30 +00:00
for name , field in fields . items ( ) :
if name not in cls . _excluded ( ) :
2023-05-17 16:19:19 +00:00
current_default = field . default
2023-05-17 23:12:01 +00:00
2023-05-17 16:19:19 +00:00
category = field . field_info . extra . get ( " category " , " Uncategorized " )
env_name = env_prefix + " _ " + name
if category in initconf and name in initconf . get ( category ) :
field . default = initconf . get ( category ) . get ( name )
2023-05-18 14:48:23 +00:00
if env_name . upper ( ) in upcase_environ :
field . default = upcase_environ [ env_name . upper ( ) ]
2023-05-04 05:20:30 +00:00
cls . add_field_argument ( parser , name , field )
2023-05-17 16:19:19 +00:00
field . default = current_default
2023-05-04 05:20:30 +00:00
@classmethod
def cmd_name ( self , command_field : str = " type " ) - > str :
hints = get_type_hints ( self )
if command_field in hints :
return get_args ( hints [ command_field ] ) [ 0 ]
else :
2023-05-17 16:19:19 +00:00
return " Uncategorized "
2023-05-04 05:20:30 +00:00
@classmethod
def get_parser ( cls ) - > ArgumentParser :
2023-05-17 19:22:58 +00:00
parser = PagingArgumentParser (
2023-05-04 05:20:30 +00:00
prog = cls . cmd_name ( ) ,
description = cls . __doc__ ,
)
cls . add_parser_arguments ( parser )
return parser
@classmethod
def add_subparser ( cls , parser : argparse . ArgumentParser ) :
parser . add_parser ( cls . cmd_name ( ) , help = cls . __doc__ )
@classmethod
def _excluded ( self ) - > List [ str ] :
2023-07-15 22:26:19 +00:00
# internal fields that shouldn't be exposed as command line options
2023-08-01 01:15:44 +00:00
return [ " type " , " initconf " ]
2023-07-27 14:54:01 +00:00
2023-07-15 22:15:59 +00:00
@classmethod
def _excluded_from_yaml ( self ) - > List [ str ] :
2023-07-15 22:26:19 +00:00
# combination of deprecated parameters and internal ones that shouldn't be exposed as invokeai.yaml options
2023-07-26 10:53:35 +00:00
return [
" type " ,
" initconf " ,
" gpu_mem_reserved " ,
" max_loaded_models " ,
" version " ,
" from_file " ,
" model " ,
" restore " ,
" root " ,
" nsfw_checker " ,
]
2023-05-17 23:12:01 +00:00
2023-05-04 05:20:30 +00:00
class Config :
env_file_encoding = " utf-8 "
arbitrary_types_allowed = True
case_sensitive = True
@classmethod
def add_field_argument ( cls , command_parser , name : str , field , default_override = None ) :
2023-05-17 16:19:19 +00:00
field_type = get_type_hints ( cls ) . get ( name )
2023-05-04 05:20:30 +00:00
default = (
default_override
if default_override is not None
else field . default
if field . default_factory is None
else field . default_factory ( )
2023-07-27 14:54:01 +00:00
)
2023-05-04 05:20:30 +00:00
if category := field . field_info . extra . get ( " category " ) :
if category not in cls . argparse_groups :
cls . argparse_groups [ category ] = command_parser . add_argument_group ( category )
argparse_group = cls . argparse_groups [ category ]
else :
argparse_group = command_parser
2023-05-17 23:12:01 +00:00
2023-05-17 16:19:19 +00:00
if get_origin ( field_type ) == Literal :
2023-05-04 05:20:30 +00:00
allowed_values = get_args ( field . type_ )
allowed_types = set ( )
for val in allowed_values :
allowed_types . add ( type ( val ) )
allowed_types_list = list ( allowed_types )
field_type = allowed_types_list [ 0 ] if len ( allowed_types ) == 1 else Union [ allowed_types_list ] # type: ignore
argparse_group . add_argument (
f " -- { name } " ,
dest = name ,
type = field_type ,
default = default ,
choices = allowed_values ,
help = field . field_info . description ,
)
2023-05-17 16:19:19 +00:00
elif get_origin ( field_type ) == list :
argparse_group . add_argument (
f " -- { name } " ,
dest = name ,
nargs = " * " ,
type = field . type_ ,
default = default ,
action = argparse . BooleanOptionalAction if field . type_ == bool else " store " ,
help = field . field_info . description ,
)
2023-05-04 05:20:30 +00:00
else :
argparse_group . add_argument (
f " -- { name } " ,
dest = name ,
type = field . type_ ,
default = default ,
action = argparse . BooleanOptionalAction if field . type_ == bool else " store " ,
help = field . field_info . description ,
)
2023-07-27 14:54:01 +00:00
2023-05-04 05:20:30 +00:00
def _find_root ( ) - > Path :
2023-07-08 12:38:45 +00:00
venv = Path ( os . environ . get ( " VIRTUAL_ENV " ) or " . " )
2023-05-04 05:20:30 +00:00
if os . environ . get ( " INVOKEAI_ROOT " ) :
2023-08-01 02:36:11 +00:00
root = Path ( os . environ [ " INVOKEAI_ROOT " ] )
2023-07-29 14:30:27 +00:00
elif any ( [ ( venv . parent / x ) . exists ( ) for x in [ INIT_FILE , LEGACY_INIT_FILE ] ] ) :
2023-07-08 12:38:45 +00:00
root = ( venv . parent ) . resolve ( )
2023-05-04 05:20:30 +00:00
else :
root = Path ( " ~/invokeai " ) . expanduser ( ) . resolve ( )
return root
2023-07-30 20:25:06 +00:00
2023-05-04 05:20:30 +00:00
class InvokeAIAppConfig ( InvokeAISettings ) :
2023-07-27 14:54:01 +00:00
"""
2023-05-17 19:22:58 +00:00
Generate images using Stable Diffusion . Use " invokeai " to launch
the command - line client ( recommended for experts only ) , or
" invokeai-web " to launch the web server . Global options
can be changed by editing the file " INVOKEAI_ROOT/invokeai.yaml " or by
setting environment variables INVOKEAI_ < setting > .
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
singleton_config : ClassVar [ InvokeAIAppConfig ] = None
singleton_init : ClassVar [ Dict ] = None
2023-07-04 21:05:35 +00:00
2023-05-04 05:20:30 +00:00
# fmt: off
2023-05-17 16:19:19 +00:00
type : Literal [ " InvokeAI " ] = " InvokeAI "
2023-05-17 19:22:58 +00:00
host : str = Field ( default = " 127.0.0.1 " , description = " IP address to bind to " , category = ' Web Server ' )
port : int = Field ( default = 9090 , description = " Port to bind to " , category = ' Web Server ' )
allow_origins : List [ str ] = Field ( default = [ ] , description = " Allowed CORS origins " , category = ' Web Server ' )
allow_credentials : bool = Field ( default = True , description = " Allow CORS credentials " , category = ' Web Server ' )
allow_methods : List [ str ] = Field ( default = [ " * " ] , description = " Methods allowed for CORS " , category = ' Web Server ' )
allow_headers : List [ str ] = Field ( default = [ " * " ] , description = " Headers allowed for CORS " , category = ' Web Server ' )
esrgan : bool = Field ( default = True , description = " Enable/disable upscaling code " , category = ' Features ' )
internet_available : bool = Field ( default = True , description = " If true, attempt to download models on the fly; otherwise only use local models " , category = ' Features ' )
log_tokenization : bool = Field ( default = False , description = " Enable logging of parsed prompt tokens. " , category = ' Features ' )
patchmatch : bool = Field ( default = True , description = " Enable/disable patchmatch inpaint code " , category = ' Features ' )
2023-07-15 22:23:32 +00:00
restore : bool = Field ( default = True , description = " Enable/disable face restoration code (DEPRECATED) " , category = ' DEPRECATED ' )
2023-05-17 19:22:58 +00:00
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 ' )
2023-07-11 19:25:39 +00:00
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 ' )
2023-07-04 21:05:35 +00:00
max_cache_size : float = Field ( default = 6.0 , gt = 0 , description = " Maximum memory amount used by model cache for rapid switching " , category = ' Memory/Performance ' )
2023-08-02 13:43:52 +00:00
max_vram_cache_size : float = Field ( default = DEFAULT_MAX_VRAM , ge = 0 , description = " Amount of VRAM reserved for model storage " , category = ' Memory/Performance ' )
2023-07-11 19:25:39 +00:00
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 ' )
2023-07-26 10:53:35 +00:00
nsfw_checker : bool = Field ( default = True , description = " DEPRECATED: use Web settings to enable/disable " , category = ' DEPRECATED ' )
2023-07-19 22:23:00 +00:00
precision : Literal [ tuple ( [ ' auto ' , ' float16 ' , ' float32 ' , ' autocast ' ] ) ] = Field ( default = ' auto ' , description = ' Floating point precision ' , category = ' Memory/Performance ' )
2023-05-17 19:22:58 +00:00
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 ' )
2023-05-26 03:29:16 +00:00
tiled_decode : bool = Field ( default = False , description = " Whether to enable tiled VAE decode (reduces memory consumption with some performance penalty) " , category = ' Memory/Performance ' )
2023-05-25 01:53:02 +00:00
2023-08-01 02:27:07 +00:00
root : Path = Field ( default = None , description = ' InvokeAI runtime root directory ' , category = ' Paths ' )
2023-07-20 13:01:49 +00:00
autoimport_dir : Path = Field ( default = ' autoimport ' , description = ' Path to a directory of models files to be imported on startup. ' , category = ' Paths ' )
lora_dir : Path = Field ( default = None , description = ' Path to a directory of LoRA/LyCORIS models to be imported on startup. ' , category = ' Paths ' )
embedding_dir : Path = Field ( default = None , description = ' Path to a directory of Textual Inversion embeddings to be imported on startup. ' , category = ' Paths ' )
controlnet_dir : Path = Field ( default = None , description = ' Path to a directory of ControlNet embeddings to be imported on startup. ' , category = ' Paths ' )
2023-05-04 05:20:30 +00:00
conf_path : Path = Field ( default = ' configs/models.yaml ' , description = ' Path to models definition file ' , category = ' Paths ' )
2023-06-25 22:50:15 +00:00
models_dir : Path = Field ( default = ' models ' , description = ' Path to the models directory ' , category = ' Paths ' )
2023-05-17 19:22:58 +00:00
legacy_conf_dir : Path = Field ( default = ' configs/stable-diffusion ' , description = ' Path to directory of legacy checkpoint config files ' , category = ' Paths ' )
2023-06-04 00:24:41 +00:00
db_dir : Path = Field ( default = ' databases ' , description = ' Path to InvokeAI databases directory ' , category = ' Paths ' )
2023-05-17 19:22:58 +00:00
outdir : Path = Field ( default = ' outputs ' , description = ' Default folder for output images ' , category = ' Paths ' )
2023-05-18 14:48:23 +00:00
from_file : Path = Field ( default = None , description = ' Take command input from the indicated file (command-line client only) ' , category = ' Paths ' )
2023-05-25 01:53:02 +00:00
use_memory_db : bool = Field ( default = False , description = ' Use in-memory database for storing image metadata ' , category = ' Paths ' )
2023-07-04 21:05:35 +00:00
2023-05-17 19:22:58 +00:00
model : str = Field ( default = ' stable-diffusion-1.5 ' , description = ' Initial model name ' , category = ' Models ' )
2023-07-04 21:05:35 +00:00
2023-05-25 03:57:15 +00:00
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 " )
2023-07-21 02:45:35 +00:00
log_level : Literal [ tuple ( [ " debug " , " info " , " warning " , " error " , " critical " ] ) ] = Field ( default = " info " , description = " Emit logging messages at this level or higher " , category = " Logging " )
2023-07-08 00:47:29 +00:00
version : bool = Field ( default = False , description = " Show InvokeAI version and exit " , category = " Other " )
2023-05-04 05:20:30 +00:00
# fmt: on
2023-05-26 00:41:26 +00:00
def parse_args ( self , argv : List [ str ] = None , conf : DictConfig = None , clobber = False ) :
2023-07-27 14:54:01 +00:00
"""
2023-07-04 21:05:35 +00:00
Update settings with contents of init file , environment , and
2023-05-26 00:41:26 +00:00
command - line settings .
2023-05-04 05:20:30 +00:00
: param conf : alternate Omegaconf dictionary object
: param argv : aternate sys . argv list
2023-05-26 00:41:26 +00:00
: param clobber : ovewrite any initialization parameters passed during initialization
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
# Set the runtime root directory. We parse command-line switches here
# in order to pick up the --root_dir option.
2023-05-26 00:41:26 +00:00
super ( ) . parse_args ( argv )
2023-05-17 16:19:19 +00:00
if conf is None :
2023-05-04 05:20:30 +00:00
try :
conf = OmegaConf . load ( self . root_dir / INIT_FILE )
except :
pass
InvokeAISettings . initconf = conf
2023-07-04 21:05:35 +00:00
2023-05-04 05:20:30 +00:00
# parse args again in order to pick up settings in configuration file
2023-05-26 00:41:26 +00:00
super ( ) . parse_args ( argv )
2023-05-04 05:20:30 +00:00
2023-05-26 00:41:26 +00:00
if self . singleton_init and not clobber :
hints = get_type_hints ( self . __class__ )
for k in self . singleton_init :
setattr ( self , k , parse_obj_as ( hints [ k ] , self . singleton_init [ k ] ) )
2023-05-04 05:20:30 +00:00
2023-05-26 00:41:26 +00:00
@classmethod
def get_config ( cls , * * kwargs ) - > InvokeAIAppConfig :
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
This returns a singleton InvokeAIAppConfig configuration object .
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
if (
cls . singleton_config is None
or type ( cls . singleton_config ) != cls
or ( kwargs and cls . singleton_init != kwargs )
) :
cls . singleton_config = cls ( * * kwargs )
cls . singleton_init = kwargs
return cls . singleton_config
2023-07-04 21:05:35 +00:00
2023-05-04 05:20:30 +00:00
@property
def root_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to the runtime root directory
2023-07-27 14:54:01 +00:00
"""
2023-08-01 01:15:44 +00:00
if self . root :
2023-07-30 17:37:18 +00:00
root = Path ( self . root ) . expanduser ( ) . absolute ( )
2023-05-04 05:20:30 +00:00
else :
2023-08-01 02:36:11 +00:00
root = self . find_root ( ) . expanduser ( ) . absolute ( )
2023-08-01 02:27:07 +00:00
self . root = root # insulate ourselves from relative paths that may change
2023-08-01 01:15:44 +00:00
return root
2023-05-04 05:20:30 +00:00
@property
def root_dir ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Alias for above .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return self . root_path
def _resolve ( self , partial_path : Path ) - > Path :
return ( self . root_path / partial_path ) . resolve ( )
2023-05-30 17:49:43 +00:00
@property
def init_file_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-30 17:49:43 +00:00
Path to invokeai . yaml
2023-07-27 14:54:01 +00:00
"""
2023-05-30 17:49:43 +00:00
return self . _resolve ( INIT_FILE )
2023-05-04 05:20:30 +00:00
@property
def output_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to defaults outputs directory .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return self . _resolve ( self . outdir )
@property
2023-06-04 00:24:41 +00:00
def db_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-06-04 00:24:41 +00:00
Path to the invokeai . db file .
2023-07-27 14:54:01 +00:00
"""
2023-06-04 00:24:41 +00:00
return self . _resolve ( self . db_dir ) / DB_FILE
2023-05-04 05:20:30 +00:00
@property
def model_conf_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to models configuration file .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return self . _resolve ( self . conf_path )
@property
def legacy_conf_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to directory of legacy configuration files ( e . g . v1 - inference . yaml )
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return self . _resolve ( self . legacy_conf_dir )
@property
2023-06-09 03:11:53 +00:00
def models_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to the models directory
2023-07-27 14:54:01 +00:00
"""
2023-06-09 03:11:53 +00:00
return self . _resolve ( self . models_dir )
2023-05-30 04:38:37 +00:00
2023-05-04 05:20:30 +00:00
@property
def autoconvert_path ( self ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Path to the directory containing models to be imported automatically at startup .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return self . _resolve ( self . autoconvert_dir ) if self . autoconvert_dir else None
# the following methods support legacy calls leftover from the Globals era
@property
def full_precision ( self ) - > bool :
""" Return true if precision set to float32 """
return self . precision == " float32 "
@property
def disable_xformers ( self ) - > bool :
""" Return true if xformers_enabled is false """
return not self . xformers_enabled
2023-05-16 05:50:01 +00:00
@property
def try_patchmatch ( self ) - > bool :
""" Return true if patchmatch true """
return self . patchmatch
2023-07-26 10:53:35 +00:00
@property
def nsfw_checker ( self ) - > bool :
""" NSFW node is always active and disabled from Web UIe """
return True
@property
def invisible_watermark ( self ) - > bool :
""" invisible watermark node is always active and disabled from Web UIe """
return True
2023-07-27 14:54:01 +00:00
2023-05-04 05:20:30 +00:00
@staticmethod
def find_root ( ) - > Path :
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
Choose the runtime root directory when not specified on command line or
init file .
2023-07-27 14:54:01 +00:00
"""
2023-05-04 05:20:30 +00:00
return _find_root ( )
2023-05-17 19:22:58 +00:00
class PagingArgumentParser ( argparse . ArgumentParser ) :
2023-07-27 14:54:01 +00:00
"""
2023-05-17 19:22:58 +00:00
A custom ArgumentParser that uses pydoc to page its output .
It also supports reading defaults from an init file .
2023-07-27 14:54:01 +00:00
"""
2023-05-17 19:22:58 +00:00
def print_help ( self , file = None ) :
text = self . format_help ( )
pydoc . pager ( text )
2023-07-27 14:54:01 +00:00
2023-05-26 00:41:26 +00:00
def get_invokeai_config ( * * kwargs ) - > InvokeAIAppConfig :
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
Legacy function which returns InvokeAIAppConfig . get_config ( )
2023-07-27 14:54:01 +00:00
"""
2023-05-26 00:41:26 +00:00
return InvokeAIAppConfig . get_config ( * * kwargs )