2024-03-20 00:17:11 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-03-24 02:00:16 -06:00
|
|
|
import functools
|
2024-05-01 11:25:16 -06:00
|
|
|
import logging
|
2024-03-24 02:00:16 -06:00
|
|
|
from modules import sd_samplers_kdiffusion, sd_samplers_timesteps, sd_samplers_lcm, shared, sd_samplers_common, sd_schedulers
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-01-29 23:51:06 -07:00
|
|
|
# imports for functions that previously were here and are used by other modules
|
2024-03-20 00:17:11 -06:00
|
|
|
samples_to_image_grid = sd_samplers_common.samples_to_image_grid
|
|
|
|
sample_to_image = sd_samplers_common.sample_to_image
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-10-06 03:08:48 -06:00
|
|
|
all_samplers = [
|
2023-01-30 00:11:30 -07:00
|
|
|
*sd_samplers_kdiffusion.samplers_data_k_diffusion,
|
2023-08-08 10:20:11 -06:00
|
|
|
*sd_samplers_timesteps.samplers_data_timesteps,
|
2024-01-08 05:43:39 -07:00
|
|
|
*sd_samplers_lcm.samplers_data_lcm,
|
2022-09-03 03:08:45 -06:00
|
|
|
]
|
2022-11-19 02:01:51 -07:00
|
|
|
all_samplers_map = {x.name: x for x in all_samplers}
|
2022-10-06 03:08:48 -06:00
|
|
|
|
2024-03-20 00:17:11 -06:00
|
|
|
samplers: list[sd_samplers_common.SamplerData] = []
|
|
|
|
samplers_for_img2img: list[sd_samplers_common.SamplerData] = []
|
2022-11-27 03:43:10 -07:00
|
|
|
samplers_map = {}
|
2023-08-08 12:28:34 -06:00
|
|
|
samplers_hidden = {}
|
2022-10-06 03:08:48 -06:00
|
|
|
|
|
|
|
|
2023-05-16 02:54:02 -06:00
|
|
|
def find_sampler_config(name):
|
2022-11-19 02:01:51 -07:00
|
|
|
if name is not None:
|
|
|
|
config = all_samplers_map.get(name, None)
|
|
|
|
else:
|
|
|
|
config = all_samplers[0]
|
|
|
|
|
2023-05-16 02:54:02 -06:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def create_sampler(name, model):
|
|
|
|
config = find_sampler_config(name)
|
|
|
|
|
2022-11-19 02:01:51 -07:00
|
|
|
assert config is not None, f'bad sampler name: {name}'
|
|
|
|
|
2023-07-14 12:41:21 -06:00
|
|
|
if model.is_sdxl and config.options.get("no_sdxl", False):
|
|
|
|
raise Exception(f"Sampler {config.name} is not supported for SDXL")
|
|
|
|
|
2022-10-06 05:12:52 -06:00
|
|
|
sampler = config.constructor(model)
|
|
|
|
sampler.config = config
|
2022-11-19 02:01:51 -07:00
|
|
|
|
2022-10-06 05:12:52 -06:00
|
|
|
return sampler
|
|
|
|
|
|
|
|
|
2022-10-06 03:08:48 -06:00
|
|
|
def set_samplers():
|
2023-08-08 12:28:34 -06:00
|
|
|
global samplers, samplers_for_img2img, samplers_hidden
|
2022-10-06 03:08:48 -06:00
|
|
|
|
2023-08-08 12:28:34 -06:00
|
|
|
samplers_hidden = set(shared.opts.hide_samplers)
|
|
|
|
samplers = all_samplers
|
|
|
|
samplers_for_img2img = all_samplers
|
2022-10-06 03:08:48 -06:00
|
|
|
|
2022-11-27 03:43:10 -07:00
|
|
|
samplers_map.clear()
|
|
|
|
for sampler in all_samplers:
|
|
|
|
samplers_map[sampler.name.lower()] = sampler.name
|
|
|
|
for alias in sampler.aliases:
|
|
|
|
samplers_map[alias.lower()] = sampler.name
|
|
|
|
|
2022-10-06 03:08:48 -06:00
|
|
|
|
2023-08-08 12:28:34 -06:00
|
|
|
def visible_sampler_names():
|
|
|
|
return [x.name for x in samplers if x.name not in samplers_hidden]
|
|
|
|
|
|
|
|
|
2024-03-20 00:17:11 -06:00
|
|
|
def visible_samplers():
|
|
|
|
return [x for x in samplers if x.name not in samplers_hidden]
|
|
|
|
|
|
|
|
|
2024-03-24 02:00:16 -06:00
|
|
|
def get_sampler_from_infotext(d: dict):
|
|
|
|
return get_sampler_and_scheduler(d.get("Sampler"), d.get("Schedule type"))[0]
|
|
|
|
|
|
|
|
|
|
|
|
def get_scheduler_from_infotext(d: dict):
|
|
|
|
return get_sampler_and_scheduler(d.get("Sampler"), d.get("Schedule type"))[1]
|
|
|
|
|
|
|
|
|
|
|
|
def get_hr_sampler_and_scheduler(d: dict):
|
|
|
|
hr_sampler = d.get("Hires sampler", "Use same sampler")
|
|
|
|
sampler = d.get("Sampler") if hr_sampler == "Use same sampler" else hr_sampler
|
|
|
|
|
|
|
|
hr_scheduler = d.get("Hires schedule type", "Use same scheduler")
|
|
|
|
scheduler = d.get("Schedule type") if hr_scheduler == "Use same scheduler" else hr_scheduler
|
|
|
|
|
|
|
|
sampler, scheduler = get_sampler_and_scheduler(sampler, scheduler)
|
|
|
|
|
|
|
|
sampler = sampler if sampler != d.get("Sampler") else "Use same sampler"
|
|
|
|
scheduler = scheduler if scheduler != d.get("Schedule type") else "Use same scheduler"
|
|
|
|
|
|
|
|
return sampler, scheduler
|
|
|
|
|
|
|
|
|
|
|
|
def get_hr_sampler_from_infotext(d: dict):
|
|
|
|
return get_hr_sampler_and_scheduler(d)[0]
|
|
|
|
|
|
|
|
|
|
|
|
def get_hr_scheduler_from_infotext(d: dict):
|
|
|
|
return get_hr_sampler_and_scheduler(d)[1]
|
|
|
|
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
def get_sampler_and_scheduler(sampler_name, scheduler_name):
|
|
|
|
default_sampler = samplers[0]
|
|
|
|
found_scheduler = sd_schedulers.schedulers_map.get(scheduler_name, sd_schedulers.schedulers[0])
|
|
|
|
|
|
|
|
name = sampler_name or default_sampler.name
|
|
|
|
|
|
|
|
for scheduler in sd_schedulers.schedulers:
|
|
|
|
name_options = [scheduler.label, scheduler.name, *(scheduler.aliases or [])]
|
|
|
|
|
|
|
|
for name_option in name_options:
|
|
|
|
if name.endswith(" " + name_option):
|
|
|
|
found_scheduler = scheduler
|
|
|
|
name = name[0:-(len(name_option) + 1)]
|
|
|
|
break
|
|
|
|
|
|
|
|
sampler = all_samplers_map.get(name, default_sampler)
|
|
|
|
|
|
|
|
# revert back to Automatic if it's the default scheduler for the selected sampler
|
|
|
|
if sampler.options.get('scheduler', None) == found_scheduler.name:
|
|
|
|
found_scheduler = sd_schedulers.schedulers[0]
|
|
|
|
|
|
|
|
return sampler.name, found_scheduler.label
|
|
|
|
|
|
|
|
|
2024-05-01 11:25:16 -06:00
|
|
|
def fix_p_invalid_sampler_and_scheduler(p):
|
|
|
|
i_sampler_name, i_scheduler = p.sampler_name, p.scheduler
|
|
|
|
p.sampler_name, p.scheduler = get_sampler_and_scheduler(p.sampler_name, p.scheduler)
|
|
|
|
if p.sampler_name != i_sampler_name or i_scheduler != p.scheduler:
|
|
|
|
logging.warning(f'Sampler Scheduler autocorrection: "{i_sampler_name}" -> "{p.sampler_name}", "{i_scheduler}" -> "{p.scheduler}"')
|
|
|
|
|
|
|
|
|
2022-10-06 03:08:48 -06:00
|
|
|
set_samplers()
|