File size: 12,952 Bytes
f2f3b8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
from typing import Any
import gradio as gr
import modules.scripts as scripts
from modules.processing import StableDiffusionProcessing
from modules.script_callbacks import on_ui_settings, on_app_started
from modules.shared import OptionInfo, opts
import lib_es.const as consts
from lib_es.xyz import xyz_support
from lib_es.samplers import add_extra_samplers
from lib_es.schedulers import add_schedulers
from modules.script_callbacks import on_before_ui
def early_init():
add_extra_samplers()
add_schedulers()
on_before_ui(early_init)
def from_setting_or_default(key: str, default: None | Any) -> None | Any:
return opts.data.get(key, default)
def on_change_update_setting(key: str, value: Any) -> None:
opts.set(key, value)
class ExtraSamplerExtension(scripts.Script):
def __init__(self):
super().__init__()
self.xyz_cache = {}
xyz_support(self.xyz_cache)
def title(self):
return "Extra Samplers"
def show(self, is_img2img):
return scripts.AlwaysVisible
def ui(self, is_img2img):
with gr.Accordion(label="Extra Samplers", open=False):
with gr.Accordion(label="Adaptive Progressive", open=False):
gr.Markdown("Adaptive progressive sampler that automatically adjusts to different step counts. ")
gr.Markdown(
"Phase ends are automatically adjusted based on the total number of steps. These are approximations"
)
with gr.Row():
euler_a_end = gr.Slider(
minimum=0.0,
maximum=1.0,
step=0.05,
value=from_setting_or_default(consts.AP_EULER_A_END, 0.35),
label="Euler A end",
)
dpm_2m_end = gr.Slider(
minimum=0.0,
maximum=1.0,
step=0.05,
value=from_setting_or_default(consts.AP_DPM_2M_END, 0.75),
label="DPM++ 2M end",
)
with gr.Row():
ancestral_eta = gr.Slider(
minimum=0.0,
maximum=1.0,
step=0.05,
value=from_setting_or_default(consts.AP_ANCESTRAL_ETA, 0.4),
label="Ancestral Eta",
)
detail_strength = gr.Slider(
minimum=0.0,
maximum=10.0,
step=0.1,
value=from_setting_or_default(consts.AP_DETAIL_STRENGTH, 1.5),
label="Detail Strength",
)
euler_a_end.change(
fn=lambda value: on_change_update_setting(consts.AP_EULER_A_END, value), inputs=[euler_a_end]
)
dpm_2m_end.change(
fn=lambda value: on_change_update_setting(consts.AP_DPM_2M_END, value), inputs=[dpm_2m_end]
)
ancestral_eta.change(
fn=lambda value: on_change_update_setting(consts.AP_ANCESTRAL_ETA, value),
inputs=[ancestral_eta],
)
detail_strength.change(
fn=lambda value: on_change_update_setting(consts.AP_DETAIL_STRENGTH, value),
inputs=[detail_strength],
)
with gr.Accordion(label="Langevin Euler", open=False):
langevin_strength = gr.Slider(
minimum=0.0,
maximum=0.5,
step=0.01,
value=from_setting_or_default(consts.LANGEVIN_STRENGTH, 0.1),
label="Langevin Strength",
info="Langevin strength for Langevin Euler sampler. Adjust to control the amount of noise.",
)
langevin_strength.change(
fn=lambda value: on_change_update_setting(consts.LANGEVIN_STRENGTH, value),
inputs=[langevin_strength],
)
with gr.Accordion(label="Gradient Estimation", open=False):
use_adaptive_steps = from_setting_or_default(consts.GE_USE_ADAPTIVE_STEPS, False)
adaptive_steps = gr.Checkbox(
label="Use Adaptive Steps",
value=use_adaptive_steps,
info="Modify the number of steps based on the noise schedule.",
)
use_timestep_adaptive_gamma = gr.Checkbox(
label="Timestep-Based Adaptive Gamma",
value=from_setting_or_default(consts.GE_USE_TIMESTEP_ADAPTIVE_GAMMA, False),
info="Adjust gamma during generation.",
)
gamma = gr.Slider(
minimum=consts.GE_MIN_GAMMA,
maximum=consts.GE_MAX_GAMMA,
step=0.05,
value=from_setting_or_default(consts.GE_GAMMA, consts.GE_DEFAULT_GAMMA),
label="Gamma",
info="Gamma value for gradient estimation. Higher values increase the amount of noise.",
interactive=not use_adaptive_steps,
)
gamma_offset = gr.Slider(
minimum=consts.GE_MIN_GAMMA_OFFSET,
maximum=consts.GE_MAX_GAMMA_OFFSET,
step=0.05,
value=from_setting_or_default(consts.GE_GAMMA_OFFSET, consts.GE_DEFAULT_GAMMA_OFFSET),
label="Gamma Offset",
info="Offset to add to the calculated gamma when using adaptive steps.",
interactive=use_adaptive_steps,
)
gamma.change(fn=lambda value: on_change_update_setting(consts.GE_GAMMA, value), inputs=[gamma])
gamma_offset.change(
fn=lambda value: on_change_update_setting(consts.GE_GAMMA_OFFSET, value), inputs=[gamma_offset]
)
# Update interactivity when adaptive steps checkbox changes
adaptive_steps.change(
fn=lambda value: (gr.Slider(interactive=not value), gr.Slider(interactive=value)),
inputs=[adaptive_steps],
outputs=[gamma, gamma_offset],
).then(
fn=lambda value: on_change_update_setting(consts.GE_USE_ADAPTIVE_STEPS, value),
inputs=[adaptive_steps],
)
use_timestep_adaptive_gamma.change(
fn=lambda value: on_change_update_setting(consts.GE_USE_TIMESTEP_ADAPTIVE_GAMMA, value),
inputs=[use_timestep_adaptive_gamma],
)
validate_schedule = gr.Checkbox(
label="Validate Schedule",
value=from_setting_or_default(consts.GE_VALIDATE_SCHEDULE, False),
info="Validate the noise schedule (For debugging purposes).",
)
with gr.Accordion(label="Extended Reverse SDE", open=False):
gr.Markdown("Extended reverse SDE sampler.")
gr.Markdown("Max stage for extended reverse SDE.")
max_stage = gr.Slider(
minimum=1,
maximum=3,
step=1,
value=from_setting_or_default(consts.ER_MAX_STAGE, 3),
label="Max Stage",
)
max_stage.change(fn=lambda value: on_change_update_setting(consts.MAX_STAGE, value), inputs=[max_stage])
return [
euler_a_end,
dpm_2m_end,
ancestral_eta,
detail_strength,
langevin_strength,
max_stage,
adaptive_steps,
use_timestep_adaptive_gamma,
gamma,
gamma_offset,
validate_schedule,
]
def get_values_and_apply(self, p: StableDiffusionProcessing, values: dict):
for key, value in values.items():
value = self.xyz_cache.pop(key, value)
setattr(p, key, value)
p.extra_generation_params[key] = value
def process_batch(
self,
p: StableDiffusionProcessing,
euler_a_end: float,
dpm_2m_end: float,
ancestral_eta: float,
detail_strength: float,
langevin_strength: float,
max_stage: int,
use_adaptive_steps: bool,
use_timestep_adaptive_gamma: bool,
gamma: float,
gamma_offset: float,
validate_schedule: bool,
batch_number: int,
prompts: list[str],
seeds: list[int],
subseeds: list[int],
):
if p.sampler_name == "Adaptive Progressive":
self.get_values_and_apply(
p,
{
consts.AP_EULER_A_END: euler_a_end,
consts.AP_DPM_2M_END: dpm_2m_end,
consts.AP_ANCESTRAL_ETA: ancestral_eta,
consts.AP_DETAIL_STRENGTH: detail_strength,
},
)
elif p.sampler_name == "Langevin Euler":
self.get_values_and_apply(p, {consts.LANGEVIN_STRENGTH: langevin_strength})
elif p.sampler_name == "Gradient Estimation":
self.get_values_and_apply(
p,
{
consts.GE_GAMMA: gamma,
consts.GE_GAMMA_OFFSET: gamma_offset,
consts.GE_USE_ADAPTIVE_STEPS: use_adaptive_steps,
consts.GE_USE_TIMESTEP_ADAPTIVE_GAMMA: use_timestep_adaptive_gamma,
consts.GE_VALIDATE_SCHEDULE: validate_schedule,
},
)
elif p.sampler_name == "Extended Reverse SDE":
self.get_values_and_apply(p, {consts.ER_MAX_STAGE: max_stage})
section = ("exs", "Extra Samplers")
def on_settings():
opts.add_option(
consts.AP_EULER_A_END,
OptionInfo(
0.35,
"Euler A End",
component=gr.Slider,
component_args={"minimum": 0.0, "maximum": 1.0, "step": 0.05},
section=section,
),
)
opts.add_option(
consts.AP_DPM_2M_END,
OptionInfo(
0.75,
"DPM++ 2M End",
component=gr.Slider,
component_args={"minimum": 0.0, "maximum": 1.0, "step": 0.05},
section=section,
),
)
opts.add_option(
consts.AP_ANCESTRAL_ETA,
OptionInfo(
0.4,
"Adaptive Progressive Eta",
component=gr.Slider,
component_args={"minimum": 0.0, "maximum": 1.0, "step": 0.01},
section=section,
),
)
opts.add_option(
consts.AP_DETAIL_STRENGTH,
OptionInfo(
1.5,
"Adaptive Progressive Detail Strength",
component=gr.Slider,
component_args={"minimum": 0.0, "maximum": 3.0, "step": 0.01},
section=section,
),
)
opts.add_option(
consts.LANGEVIN_STRENGTH,
OptionInfo(
0.1,
"Langevin Strength",
component=gr.Slider,
component_args={"minimum": 0.0, "maximum": 1.0, "step": 0.01},
section=section,
),
)
opts.add_option(
consts.ER_MAX_STAGE,
OptionInfo(
3,
"Extended Reverse Time Max Stage",
component=gr.Slider,
component_args={"minimum": 1, "maximum": 3, "step": 1},
section=section,
),
)
opts.add_option(
consts.GE_GAMMA,
OptionInfo(
consts.GE_DEFAULT_GAMMA,
"Gradient Estimation Gamma",
component=gr.Slider,
component_args={"minimum": consts.GE_MIN_GAMMA, "maximum": consts.GE_MAX_GAMMA, "step": 0.1},
section=section,
),
)
opts.add_option(
consts.GE_GAMMA_OFFSET,
OptionInfo(
consts.GE_DEFAULT_GAMMA_OFFSET,
"Gradient Estimation Gamma Offset",
component=gr.Slider,
component_args={"minimum": consts.GE_MIN_GAMMA_OFFSET, "maximum": consts.GE_MAX_GAMMA_OFFSET, "step": 0.1},
section=section,
),
)
opts.add_option(
consts.GE_USE_ADAPTIVE_STEPS,
OptionInfo(
False,
"Use Adaptive Steps",
component=gr.Checkbox,
section=section,
),
)
opts.add_option(
consts.GE_USE_TIMESTEP_ADAPTIVE_GAMMA,
OptionInfo(
False,
"Use Timestep Adaptive Gamma",
component=gr.Checkbox,
section=section,
),
)
on_ui_settings(on_settings)
|