File size: 25,638 Bytes
fb83c5b |
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
import gradio as gr
from typing import Tuple
from .common_gui import (
get_folder_path,
get_any_file_path,
list_files,
list_dirs,
create_refresh_button,
document_symbol,
)
class AdvancedTraining:
"""
This class configures and initializes the advanced training settings for a machine learning model,
including options for headless operation, fine-tuning, training type selection, and default directory paths.
Attributes:
headless (bool): If True, run without the Gradio interface.
finetuning (bool): If True, enables fine-tuning of the model.
training_type (str): Specifies the type of training to perform.
no_token_padding (gr.Checkbox): Checkbox to disable token padding.
gradient_accumulation_steps (gr.Slider): Slider to set the number of gradient accumulation steps.
weighted_captions (gr.Checkbox): Checkbox to enable weighted captions.
"""
def __init__(
self,
headless: bool = False,
finetuning: bool = False,
training_type: str = "",
config: dict = {},
) -> None:
"""
Initializes the AdvancedTraining class with given settings.
Parameters:
headless (bool): Run in headless mode without GUI.
finetuning (bool): Enable model fine-tuning.
training_type (str): The type of training to be performed.
config (dict): Configuration options for the training process.
"""
self.headless = headless
self.finetuning = finetuning
self.training_type = training_type
self.config = config
# Determine the current directories for VAE and output, falling back to defaults if not specified.
self.current_vae_dir = self.config.get("advanced.vae_dir", "./models/vae")
self.current_state_dir = self.config.get("advanced.state_dir", "./outputs")
self.current_log_tracker_config_dir = self.config.get(
"advanced.log_tracker_config_dir", "./logs"
)
# Define the behavior for changing noise offset type.
def noise_offset_type_change(
noise_offset_type: str,
) -> Tuple[gr.Group, gr.Group]:
"""
Returns a tuple of Gradio Groups with visibility set based on the noise offset type.
Parameters:
noise_offset_type (str): The selected noise offset type.
Returns:
Tuple[gr.Group, gr.Group]: A tuple containing two Gradio Group elements with their visibility set.
"""
if noise_offset_type == "Original":
return (gr.Group(visible=True), gr.Group(visible=False))
else:
return (gr.Group(visible=False), gr.Group(visible=True))
# GUI elements are only visible when not fine-tuning.
with gr.Row(visible=not finetuning):
# Exclude token padding option for LoRA training type.
if training_type != "lora":
self.no_token_padding = gr.Checkbox(
label="No token padding",
value=self.config.get("advanced.no_token_padding", False),
)
self.gradient_accumulation_steps = gr.Slider(
label="Gradient accumulate steps",
info="Number of updates steps to accumulate before performing a backward/update pass",
value=self.config.get("advanced.gradient_accumulation_steps", 1),
minimum=1,
maximum=120,
step=1,
)
self.weighted_captions = gr.Checkbox(
label="Weighted captions",
value=self.config.get("advanced.weighted_captions", False),
)
with gr.Group(), gr.Row(visible=not finetuning):
self.prior_loss_weight = gr.Number(
label="Prior loss weight",
value=self.config.get("advanced.prior_loss_weight", 1.0),
)
def list_vae_files(path):
self.current_vae_dir = path if not path == "" else "."
return list(list_files(path, exts=[".ckpt", ".safetensors"], all=True))
self.vae = gr.Dropdown(
label="VAE (Optional: Path to checkpoint of vae for training)",
interactive=True,
choices=[self.config.get("advanced.vae_dir", "")]
+ list_vae_files(self.current_vae_dir),
value=self.config.get("advanced.vae_dir", ""),
allow_custom_value=True,
)
create_refresh_button(
self.vae,
lambda: None,
lambda: {
"choices": [self.config.get("advanced.vae_dir", "")]
+ list_vae_files(self.current_vae_dir)
},
"open_folder_small",
)
self.vae_button = gr.Button(
"π", elem_id="open_folder_small", visible=(not headless)
)
self.vae_button.click(
get_any_file_path,
outputs=self.vae,
show_progress=False,
)
self.vae.change(
fn=lambda path: gr.Dropdown(
choices=[self.config.get("advanced.vae_dir", "")]
+ list_vae_files(path)
),
inputs=self.vae,
outputs=self.vae,
show_progress=False,
)
with gr.Row():
self.additional_parameters = gr.Textbox(
label="Additional parameters",
placeholder='(Optional) Use to provide additional parameters not handled by the GUI. Eg: --some_parameters "value"',
value=self.config.get("advanced.additional_parameters", ""),
)
with gr.Accordion("Scheduled Huber Loss", open=False):
with gr.Row():
self.loss_type = gr.Dropdown(
label="Loss type",
choices=["huber", "smooth_l1", "l2"],
value=self.config.get("advanced.loss_type", "l2"),
info="The type of loss to use and whether it's scheduled based on the timestep",
)
self.huber_schedule = gr.Dropdown(
label="Huber schedule",
choices=[
"constant",
"exponential",
"snr",
],
value=self.config.get("advanced.huber_schedule", "snr"),
info="The type of loss to use and whether it's scheduled based on the timestep",
)
self.huber_c = gr.Number(
label="Huber C",
value=self.config.get("advanced.huber_c", 0.1),
minimum=0.0,
maximum=1.0,
step=0.01,
info="The huber loss parameter. Only used if one of the huber loss modes (huber or smooth l1) is selected with loss_type",
)
with gr.Row():
self.save_every_n_steps = gr.Number(
label="Save every N steps",
value=self.config.get("advanced.save_every_n_steps", 0),
precision=0,
info="(Optional) The model is saved every specified steps",
)
self.save_last_n_steps = gr.Number(
label="Save last N steps",
value=self.config.get("advanced.save_last_n_steps", 0),
precision=0,
info="(Optional) Save only the specified number of models (old models will be deleted)",
)
self.save_last_n_steps_state = gr.Number(
label="Save last N steps state",
value=self.config.get("advanced.save_last_n_steps_state", 0),
precision=0,
info="(Optional) Save only the specified number of states (old models will be deleted)",
)
with gr.Row():
def full_options_update(full_fp16, full_bf16):
full_fp16_active = True
full_bf16_active = True
if full_fp16:
full_bf16_active = False
if full_bf16:
full_fp16_active = False
return gr.Checkbox(
interactive=full_fp16_active,
), gr.Checkbox(interactive=full_bf16_active)
self.keep_tokens = gr.Slider(
label="Keep n tokens",
value=self.config.get("advanced.keep_tokens", 0),
minimum=0,
maximum=32,
step=1,
)
self.clip_skip = gr.Slider(
label="Clip skip",
value=self.config.get("advanced.clip_skip", 1),
minimum=0,
maximum=12,
step=1,
)
self.max_token_length = gr.Dropdown(
label="Max Token Length",
choices=[
75,
150,
225,
],
info="max token length of text encoder",
value=self.config.get("advanced.max_token_length", 75),
)
with gr.Row():
if training_type == "lora":
self.fp8_base = gr.Checkbox(
label="fp8 base training (experimental)",
info="U-Net and Text Encoder can be trained with fp8 (experimental)",
value=self.config.get("advanced.fp8_base", False),
)
self.full_fp16 = gr.Checkbox(
label="Full fp16 training (experimental)",
value=self.config.get("advanced.full_fp16", False),
)
self.full_bf16 = gr.Checkbox(
label="Full bf16 training (experimental)",
value=self.config.get("advanced.full_bf16", False),
info="Required bitsandbytes >= 0.36.0",
)
self.full_fp16.change(
full_options_update,
inputs=[self.full_fp16, self.full_bf16],
outputs=[self.full_fp16, self.full_bf16],
)
self.full_bf16.change(
full_options_update,
inputs=[self.full_fp16, self.full_bf16],
outputs=[self.full_fp16, self.full_bf16],
)
with gr.Row():
self.gradient_checkpointing = gr.Checkbox(
label="Gradient checkpointing",
value=self.config.get("advanced.gradient_checkpointing", False),
)
self.shuffle_caption = gr.Checkbox(
label="Shuffle caption",
value=self.config.get("advanced.shuffle_caption", False),
)
self.persistent_data_loader_workers = gr.Checkbox(
label="Persistent data loader",
value=self.config.get("advanced.persistent_data_loader_workers", False),
)
self.mem_eff_attn = gr.Checkbox(
label="Memory efficient attention",
value=self.config.get("advanced.mem_eff_attn", False),
)
with gr.Row():
self.xformers = gr.Dropdown(
label="CrossAttention",
choices=["none", "sdpa", "xformers"],
value=self.config.get("advanced.xformers", "xformers"),
)
self.color_aug = gr.Checkbox(
label="Color augmentation",
value=self.config.get("advanced.color_aug", False),
info="Enable weak color augmentation",
)
self.flip_aug = gr.Checkbox(
label="Flip augmentation",
value=getattr(self.config, "advanced.flip_aug", False),
info="Enable horizontal flip augmentation",
)
self.masked_loss = gr.Checkbox(
label="Masked loss",
value=self.config.get("advanced.masked_loss", False),
info="Apply mask for calculating loss. conditioning_data_dir is required for dataset",
)
with gr.Row():
self.scale_v_pred_loss_like_noise_pred = gr.Checkbox(
label="Scale v prediction loss",
value=self.config.get(
"advanced.scale_v_pred_loss_like_noise_pred", False
),
info="Only for SD v2 models. By scaling the loss according to the time step, the weights of global noise prediction and local noise prediction become the same, and the improvement of details may be expected.",
)
self.min_snr_gamma = gr.Slider(
label="Min SNR gamma",
value=self.config.get("advanced.min_snr_gamma", 0),
minimum=0,
maximum=20,
step=1,
info="Recommended value of 5 when used",
)
self.debiased_estimation_loss = gr.Checkbox(
label="Debiased Estimation loss",
value=self.config.get("advanced.debiased_estimation_loss", False),
info="Automates the processing of noise, allowing for faster model fitting, as well as balancing out color issues. Do not use if Min SNR gamma is specified.",
)
with gr.Row():
# self.sdpa = gr.Checkbox(label='Use sdpa', value=False, info='Use sdpa for CrossAttention')
self.bucket_no_upscale = gr.Checkbox(
label="Don't upscale bucket resolution",
value=self.config.get("advanced.bucket_no_upscale", True),
)
self.bucket_reso_steps = gr.Slider(
label="Bucket resolution steps",
value=self.config.get("advanced.bucket_reso_steps", 64),
minimum=1,
maximum=128,
)
self.random_crop = gr.Checkbox(
label="Random crop instead of center crop",
value=self.config.get("advanced.random_crop", False),
)
self.v_pred_like_loss = gr.Slider(
label="V Pred like loss",
value=self.config.get("advanced.v_pred_like_loss", 0),
minimum=0,
maximum=1,
step=0.01,
info="Recommended value of 0.5 when used",
)
with gr.Row():
self.min_timestep = gr.Slider(
label="Min Timestep",
value=self.config.get("advanced.min_timestep", 0),
step=1,
minimum=0,
maximum=1000,
info="Values greater than 0 will make the model more img2img focussed. 0 = image only",
)
self.max_timestep = gr.Slider(
label="Max Timestep",
value=self.config.get("advanced.max_timestep", 1000),
step=1,
minimum=0,
maximum=1000,
info="Values lower than 1000 will make the model more img2img focussed. 1000 = noise only",
)
with gr.Row():
self.noise_offset_type = gr.Dropdown(
label="Noise offset type",
choices=[
"Original",
"Multires",
],
value=self.config.get("advanced.noise_offset_type", "Original"),
scale=1,
)
with gr.Row(visible=True) as self.noise_offset_original:
self.noise_offset = gr.Slider(
label="Noise offset",
value=self.config.get("advanced.noise_offset", 0),
minimum=0,
maximum=1,
step=0.01,
info="Recommended values are 0.05 - 0.15",
)
self.noise_offset_random_strength = gr.Checkbox(
label="Noise offset random strength",
value=self.config.get(
"advanced.noise_offset_random_strength", False
),
info="Use random strength between 0~noise_offset for noise offset",
)
self.adaptive_noise_scale = gr.Slider(
label="Adaptive noise scale",
value=self.config.get("advanced.adaptive_noise_scale", 0),
minimum=-1,
maximum=1,
step=0.001,
info="Add `latent mean absolute value * this value` to noise_offset",
)
with gr.Row(visible=False) as self.noise_offset_multires:
self.multires_noise_iterations = gr.Slider(
label="Multires noise iterations",
value=self.config.get("advanced.multires_noise_iterations", 0),
minimum=0,
maximum=64,
step=1,
info="Enable multires noise (recommended values are 6-10)",
)
self.multires_noise_discount = gr.Slider(
label="Multires noise discount",
value=self.config.get("advanced.multires_noise_discount", 0.3),
minimum=0,
maximum=1,
step=0.01,
info="Recommended values are 0.8. For LoRAs with small datasets, 0.1-0.3",
)
with gr.Row(visible=True):
self.ip_noise_gamma = gr.Slider(
label="IP noise gamma",
value=self.config.get("advanced.ip_noise_gamma", 0),
minimum=0,
maximum=1,
step=0.01,
info="enable input perturbation noise. used for regularization. recommended value: around 0.1",
)
self.ip_noise_gamma_random_strength = gr.Checkbox(
label="IP noise gamma random strength",
value=self.config.get(
"advanced.ip_noise_gamma_random_strength", False
),
info="Use random strength between 0~ip_noise_gamma for input perturbation noise",
)
self.noise_offset_type.change(
noise_offset_type_change,
inputs=[self.noise_offset_type],
outputs=[
self.noise_offset_original,
self.noise_offset_multires,
],
)
with gr.Row():
self.caption_dropout_every_n_epochs = gr.Number(
label="Dropout caption every n epochs",
value=self.config.get("advanced.caption_dropout_every_n_epochs", 0),
)
self.caption_dropout_rate = gr.Slider(
label="Rate of caption dropout",
value=self.config.get("advanced.caption_dropout_rate", 0),
minimum=0,
maximum=1,
)
self.vae_batch_size = gr.Slider(
label="VAE batch size",
minimum=0,
maximum=32,
value=self.config.get("advanced.vae_batch_size", 0),
step=1,
)
with gr.Group(), gr.Row():
self.save_state = gr.Checkbox(
label="Save training state",
value=self.config.get("advanced.save_state", False),
info="Save training state (including optimizer states etc.) when saving models"
)
self.save_state_on_train_end = gr.Checkbox(
label="Save training state at end of training",
value=self.config.get("advanced.save_state_on_train_end", False),
info="Save training state (including optimizer states etc.) on train end"
)
def list_state_dirs(path):
self.current_state_dir = path if not path == "" else "."
return list(list_dirs(path))
self.resume = gr.Dropdown(
label='Resume from saved training state (path to "last-state" state folder)',
choices=[self.config.get("advanced.state_dir", "")]
+ list_state_dirs(self.current_state_dir),
value=self.config.get("advanced.state_dir", ""),
interactive=True,
allow_custom_value=True,
info="Saved state to resume training from"
)
create_refresh_button(
self.resume,
lambda: None,
lambda: {
"choices": [self.config.get("advanced.state_dir", "")]
+ list_state_dirs(self.current_state_dir)
},
"open_folder_small",
)
self.resume_button = gr.Button(
"π", elem_id="open_folder_small", visible=(not headless)
)
self.resume_button.click(
get_folder_path,
outputs=self.resume,
show_progress=False,
)
self.resume.change(
fn=lambda path: gr.Dropdown(
choices=[self.config.get("advanced.state_dir", "")]
+ list_state_dirs(path)
),
inputs=self.resume,
outputs=self.resume,
show_progress=False,
)
self.max_data_loader_n_workers = gr.Number(
label="Max num workers for DataLoader",
info="Override number of epoch. Default: 0",
step=1,
minimum=0,
value=self.config.get("advanced.max_data_loader_n_workers", 0),
)
with gr.Row():
self.log_with = gr.Dropdown(
label="Logging",
choices=["","wandb", "tensorboard","all"],
value="",
info="Loggers to use, tensorboard will be used as the default.",
)
self.wandb_api_key = gr.Textbox(
label="WANDB API Key",
value=self.config.get("advanced.wandb_api_key", ""),
placeholder="(Optional)",
info="Users can obtain and/or generate an api key in the their user settings on the website: https://wandb.ai/login",
)
self.wandb_run_name = gr.Textbox(
label="WANDB run name",
value=self.config.get("advanced.wandb_run_name", ""),
placeholder="(Optional)",
info="The name of the specific wandb session",
)
with gr.Group(), gr.Row():
def list_log_tracker_config_files(path):
self.current_log_tracker_config_dir = path if not path == "" else "."
return list(list_files(path, exts=[".json"], all=True))
self.log_tracker_name = gr.Textbox(
label="Log tracker name",
value=self.config.get("advanced.log_tracker_name", ""),
placeholder="(Optional)",
info="Name of tracker to use for logging, default is script-specific default name",
)
self.log_tracker_config = gr.Dropdown(
label="Log tracker config",
choices=[self.config.get("log_tracker_config_dir", "")]
+ list_log_tracker_config_files(self.current_log_tracker_config_dir),
value=self.config.get("log_tracker_config_dir", ""),
info="Path to tracker config file to use for logging",
interactive=True,
allow_custom_value=True,
)
create_refresh_button(
self.log_tracker_config,
lambda: None,
lambda: {
"choices": [self.config.get("log_tracker_config_dir", "")]
+ list_log_tracker_config_files(self.current_log_tracker_config_dir)
},
"open_folder_small",
)
self.log_tracker_config_button = gr.Button(
document_symbol, elem_id="open_folder_small", visible=(not headless)
)
self.log_tracker_config_button.click(
get_any_file_path,
outputs=self.log_tracker_config,
show_progress=False,
)
self.log_tracker_config.change(
fn=lambda path: gr.Dropdown(
choices=[self.config.get("log_tracker_config_dir", "")]
+ list_log_tracker_config_files(path)
),
inputs=self.log_tracker_config,
outputs=self.log_tracker_config,
show_progress=False,
)
|