InvokeAI/tests/test_config.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
3.4 KiB
Python
Raw Normal View History

2023-05-04 04:45:52 +00:00
import os
import pytest
2023-05-25 03:57:15 +00:00
import sys
2023-05-04 04:45:52 +00:00
from omegaconf import OmegaConf
from pathlib import Path
os.environ["INVOKEAI_ROOT"] = "/tmp"
2023-05-25 03:57:15 +00:00
from invokeai.app.services.config import InvokeAIAppConfig
2023-05-25 03:57:15 +00:00
2023-05-04 04:45:52 +00:00
init1 = OmegaConf.create(
2023-07-27 14:54:01 +00:00
"""
InvokeAI:
Features:
always_use_cpu: false
Memory/Performance:
2023-07-11 21:37:02 +00:00
max_cache_size: 5
tiled_decode: false
2023-07-27 14:54:01 +00:00
"""
2023-05-04 04:45:52 +00:00
)
init2 = OmegaConf.create(
2023-07-27 14:54:01 +00:00
"""
InvokeAI:
Features:
always_use_cpu: true
Memory/Performance:
2023-07-11 21:37:02 +00:00
max_cache_size: 2
tiled_decode: true
2023-07-27 14:54:01 +00:00
"""
2023-05-04 04:45:52 +00:00
)
2023-08-17 17:47:26 +00:00
init3 = OmegaConf.create(
"""
InvokeAI:
Generation:
sequential_guidance: true
attention_type: xformers
attention_slice_size: 7
forced_tiled_decode: True
Device:
device: cpu
Cache:
ram: 1.25
"""
)
2023-07-27 14:54:01 +00:00
2023-05-04 04:45:52 +00:00
def test_use_init():
# note that we explicitly set omegaconf dict and argv here
# so that the values aren't read from ~invokeai/invokeai.yaml and
# sys.argv respectively.
conf1 = InvokeAIAppConfig.get_config()
2023-05-04 04:45:52 +00:00
assert conf1
2023-06-20 17:22:39 +00:00
conf1.parse_args(conf=init1, argv=[])
assert not conf1.tiled_decode
2023-07-11 21:37:02 +00:00
assert conf1.max_cache_size == 5
assert not conf1.always_use_cpu
2023-05-04 04:45:52 +00:00
conf2 = InvokeAIAppConfig.get_config()
2023-05-04 04:45:52 +00:00
assert conf2
2023-06-20 17:22:39 +00:00
conf2.parse_args(conf=init2, argv=[])
assert conf2.tiled_decode
2023-07-11 21:37:02 +00:00
assert conf2.max_cache_size == 2
2023-05-04 04:45:52 +00:00
assert not hasattr(conf2, "invalid_attribute")
2023-07-27 14:54:01 +00:00
2023-08-17 17:47:26 +00:00
def test_legacy():
conf = InvokeAIAppConfig.get_config()
assert conf
conf.parse_args(conf=init3, argv=[])
assert conf.xformers_enabled
assert conf.device == "cpu"
assert conf.use_cpu
assert conf.ram_cache_size == 1.25
2023-05-04 04:45:52 +00:00
def test_argv_override():
conf = InvokeAIAppConfig.get_config()
conf.parse_args(conf=init1, argv=["--always_use_cpu", "--max_cache=10"])
assert conf.always_use_cpu
2023-07-11 21:37:02 +00:00
assert conf.max_cache_size == 10
2023-05-04 04:45:52 +00:00
assert conf.outdir == Path("outputs") # this is the default
2023-07-27 14:54:01 +00:00
2023-05-04 04:45:52 +00:00
def test_env_override():
# argv overrides
conf = InvokeAIAppConfig()
2023-07-11 21:37:02 +00:00
conf.parse_args(conf=init1, argv=["--max_cache=10"])
assert conf.always_use_cpu == False
os.environ["INVOKEAI_always_use_cpu"] = "True"
2023-07-11 21:37:02 +00:00
conf.parse_args(conf=init1, argv=["--max_cache=10"])
assert conf.always_use_cpu == True
2023-05-04 04:45:52 +00:00
# environment variables should be case insensitive
2023-07-11 21:37:02 +00:00
os.environ["InvokeAI_Max_Cache_Size"] = "15"
conf = InvokeAIAppConfig()
2023-06-20 17:22:39 +00:00
conf.parse_args(conf=init1, argv=[])
2023-07-11 21:37:02 +00:00
assert conf.max_cache_size == 15
conf = InvokeAIAppConfig()
conf.parse_args(conf=init1, argv=["--no-always_use_cpu", "--max_cache=10"])
assert conf.always_use_cpu == False
2023-07-11 21:37:02 +00:00
assert conf.max_cache_size == 10
2023-05-04 04:45:52 +00:00
2023-07-11 21:37:02 +00:00
conf = InvokeAIAppConfig.get_config(max_cache_size=20)
conf.parse_args(conf=init1, argv=[])
2023-07-11 21:37:02 +00:00
assert conf.max_cache_size == 20
2023-07-27 14:54:01 +00:00
2023-05-04 04:45:52 +00:00
def test_root_resists_cwd():
previous = os.environ["INVOKEAI_ROOT"]
cwd = Path(os.getcwd()).resolve()
os.environ["INVOKEAI_ROOT"] = "."
conf = InvokeAIAppConfig.get_config()
conf.parse_args([])
assert conf.root_path == cwd
os.chdir("..")
assert conf.root_path == cwd
os.environ["INVOKEAI_ROOT"] = previous
os.chdir(cwd)
2023-05-04 04:45:52 +00:00
def test_type_coercion():
conf = InvokeAIAppConfig().get_config()
conf.parse_args(argv=["--root=/tmp/foobar"])
2023-05-04 04:45:52 +00:00
assert conf.root == Path("/tmp/foobar")
assert isinstance(conf.root, Path)
conf = InvokeAIAppConfig.get_config(root="/tmp/different")
conf.parse_args(argv=["--root=/tmp/foobar"])
2023-05-04 04:45:52 +00:00
assert conf.root == Path("/tmp/different")
assert isinstance(conf.root, Path)