Spaces:
Paused
Paused
File size: 54,018 Bytes
48cba3f | 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 | # Advanced Settings: Detailed Guide for SDXL LoRA Training Script `sdxl_train_network.py` / é«åºŠãªèšå®: SDXL LoRAåŠç¿ã¹ã¯ãªãã `sdxl_train_network.py` 詳现ã¬ã€ã
This document describes the advanced options available when training LoRA models for SDXL (Stable Diffusion XL) with `sdxl_train_network.py` in the `sd-scripts` repository. For the basics, please read [How to Use the LoRA Training Script `train_network.py`](train_network.md) and [How to Use the SDXL LoRA Training Script `sdxl_train_network.py`](sdxl_train_network.md).
This guide targets experienced users who want to fine tune settings in detail.
**Prerequisites:**
* You have cloned the `sd-scripts` repository and prepared a Python environment.
* A training dataset and its `.toml` configuration are ready (see the [Dataset Configuration Guide](./config_README-en.md)).
* You are familiar with running basic LoRA training commands.
## 1. Command Line Options / ã³ãã³ãã©ã€ã³åŒæ° 詳现解説
`sdxl_train_network.py` inherits the functionality of `train_network.py` and adds SDXL-specific features. Major options are grouped and explained below. For common arguments, see the other guides mentioned above.
### 1.1. Model Loading
* `--pretrained_model_name_or_path=\"<model path>\"` **[Required]**: specify the base SDXL model. Supports a Hugging Face model ID, a local Diffusers directory or a `.safetensors` file.
* `--vae=\"<VAE path>\"`: optionally use a different VAE. Specify when using a VAE other than the one included in the SDXL model. Can specify `.ckpt` or `.safetensors` files.
* `--no_half_vae`: keep the VAE in float32 even with fp16/bf16 training. The VAE for SDXL can become unstable with `float16`, so it is recommended to enable this when `fp16` is specified. Usually unnecessary for `bf16`.
* `--fp8_base` / `--fp8_base_unet`: **Experimental**: load the base model (U-Net, Text Encoder) or just the U-Net in FP8 to reduce VRAM (requires PyTorch 2.1+). For details, refer to the relevant section in TODO add document later (this is an SD3 explanation but also applies to SDXL).
### 1.2. Dataset Settings
* `--dataset_config=\"<path to config>\"`: specify a `.toml` dataset config. High resolution data and aspect ratio buckets (specify `enable_bucket = true` in `.toml`) are common for SDXL. The resolution steps for aspect ratio buckets (`bucket_reso_steps`) must be multiples of 32 for SDXL. For details on writing `.toml` files, refer to the [Dataset Configuration Guide](./config_README-en.md).
### 1.3. Output and Saving
Options match `train_network.py`:
* `--output_dir`, `--output_name` (both required)
* `--save_model_as` (recommended `safetensors`), `ckpt`, `pt`, `diffusers`, `diffusers_safetensors`
* `--save_precision=\"fp16\"`, `\"bf16\"`, `\"float\"`: Specifies the precision for saving the model. If not specified, the model is saved with the training precision (`fp16`, `bf16`, etc.).
* `--save_every_n_epochs=N`, `--save_every_n_steps=N`: Saves the model every N epochs/steps.
* `--save_last_n_epochs=M`, `--save_last_n_steps=M`: When saving at every epoch/step, only the latest M files are kept, and older ones are deleted.
* `--save_state`, `--save_state_on_train_end`: Saves the training state (`state`), including Optimizer status, etc., when saving the model or at the end of training. Required for resuming training with the `--resume` option.
* `--save_last_n_epochs_state=M`, `--save_last_n_steps_state=M`: Limits the number of saved `state` files to M. Overrides the `--save_last_n_epochs/steps` specification.
* `--no_metadata`: Does not save metadata to the output model.
* `--save_state_to_huggingface` and related options (e.g., `--huggingface_repo_id`): Options related to uploading models and states to Hugging Face Hub. See TODO add document for details.
### 1.4. Network Parameters (LoRA)
* `--network_module=networks.lora` **[Required]**
* `--network_dim=N` **[Required]**: Specifies the rank (dimensionality) of LoRA. For SDXL, values like 32 or 64 are often tried, but adjustment is necessary depending on the dataset and purpose.
* `--network_alpha=M`: LoRA alpha value. Generally around half of `network_dim` or the same value as `network_dim`. Default is 1.
* `--network_dropout=P`: Dropout rate (0.0-1.0) within LoRA modules. Can be effective in suppressing overfitting. Default is None (no dropout).
* `--network_args ...`: Allows advanced settings by specifying additional arguments to the network module in `key=value` format. For LoRA, the following advanced settings are available:
* **Block-wise dimensions/alphas:**
* Allows specifying different `dim` and `alpha` for each block of the U-Net. This enables adjustments to strengthen or weaken the influence of specific layers.
* `block_dims`: Comma-separated dims for Linear and Conv2d 1x1 layers in U-Net (23 values for SDXL).
* `block_alphas`: Comma-separated alpha values corresponding to the above.
* `conv_block_dims`: Comma-separated dims for Conv2d 3x3 layers in U-Net.
* `conv_block_alphas`: Comma-separated alpha values corresponding to the above.
* Blocks not specified will use values from `--network_dim`/`--network_alpha` or `--conv_dim`/`--conv_alpha` (if they exist).
* For details, refer to [Block-wise learning rate for LoRA](train_network.md#lora-ã®éå±€å¥åŠç¿ç) (in train_network.md, applicable to SDXL) and the implementation ([lora.py](lora.py)).
* **LoRA+:**
* `loraplus_lr_ratio=R`: Sets the learning rate of LoRA's upward weights (UP) to R times the learning rate of downward weights (DOWN). Expected to improve learning speed. Paper recommends 16.
* `loraplus_unet_lr_ratio=RU`: Specifies the LoRA+ learning rate ratio for the U-Net part individually.
* `loraplus_text_encoder_lr_ratio=RT`: Specifies the LoRA+ learning rate ratio for the Text Encoder part individually (multiplied by the learning rates specified with `--text_encoder_lr1`, `--text_encoder_lr2`).
* For details, refer to [README](../README.md#jan-17-2025--2025-01-17-version-090) and the implementation ([lora.py](lora.py)).
* `--network_train_unet_only`: Trains only the LoRA modules of the U-Net. Specify this if not training Text Encoders. Required when using `--cache_text_encoder_outputs`.
* `--network_train_text_encoder_only`: Trains only the LoRA modules of the Text Encoders. Specify this if not training the U-Net.
* `--network_weights=\"<weight file>\"`: Starts training by loading pre-trained LoRA weights. Used for fine-tuning or resuming training. The difference from `--resume` is that this option only loads LoRA module weights, while `--resume` also restores Optimizer state, step count, etc.
* `--dim_from_weights`: Automatically reads the LoRA dimension (`dim`) from the weight file specified by `--network_weights`. Specification of `--network_dim` becomes unnecessary.
### 1.5. Training Parameters
* `--learning_rate=LR`: Sets the overall learning rate. This becomes the default value for each module (`unet_lr`, `text_encoder_lr1`, `text_encoder_lr2`). Values like `1e-3` or `1e-4` are often tried.
* `--unet_lr=LR_U`: Learning rate for the LoRA module of the U-Net part.
* `--text_encoder_lr1=LR_TE1`: Learning rate for the LoRA module of Text Encoder 1 (OpenCLIP ViT-G/14). Usually, a smaller value than U-Net (e.g., `1e-5`, `2e-5`) is recommended.
* `--text_encoder_lr2=LR_TE2`: Learning rate for the LoRA module of Text Encoder 2 (CLIP ViT-L/14). Usually, a smaller value than U-Net (e.g., `1e-5`, `2e-5`) is recommended.
* `--optimizer_type=\"...\"`: Specifies the optimizer to use. Options include `AdamW8bit` (memory-efficient, common), `Adafactor` (even more memory-efficient, proven in SDXL full model training), `Lion`, `DAdaptation`, `Prodigy`, etc. Each optimizer may require additional arguments (see `--optimizer_args`). `AdamW8bit` or `PagedAdamW8bit` (requires `bitsandbytes`) are common. `Adafactor` is memory-efficient but slightly complex to configure (relative step (`relative_step=True`) recommended, `adafactor` learning rate scheduler recommended). `DAdaptation`, `Prodigy` have automatic learning rate adjustment but cannot be used with LoRA+. Specify a learning rate around `1.0`. For details, see the `get_optimizer` function in [train_util.py](train_util.py).
* `--optimizer_args ...`: Specifies additional arguments to the optimizer in `key=value` format (e.g., `\"weight_decay=0.01\"` `\"betas=0.9,0.999\"`).
* `--lr_scheduler=\"...\"`: Specifies the learning rate scheduler. Options include `constant` (no change), `cosine` (cosine curve), `linear` (linear decay), `constant_with_warmup` (constant with warmup), `cosine_with_restarts`, etc. `constant`, `cosine`, and `constant_with_warmup` are commonly used. Some schedulers require additional arguments (see `--lr_scheduler_args`). If using optimizers with auto LR adjustment like `DAdaptation` or `Prodigy`, a scheduler is not needed (`constant` should be specified).
* `--lr_warmup_steps=N`: Number of warmup steps for the learning rate scheduler. The learning rate gradually increases during this period at the start of training. If N < 1, it's interpreted as a fraction of total steps.
* `--lr_scheduler_num_cycles=N` / `--lr_scheduler_power=P`: Parameters for specific schedulers (`cosine_with_restarts`, `polynomial`).
* `--max_train_steps=N` / `--max_train_epochs=N`: Specifies the total number of training steps or epochs. Epoch specification takes precedence.
* `--mixed_precision=\"bf16\"` / `\"fp16\"` / `\"no\"`: Mixed precision training settings. For SDXL, using `bf16` (if GPU supports it) or `fp16` is strongly recommended. Reduces VRAM usage and improves training speed.
* `--full_fp16` / `--full_bf16`: Performs gradient calculations entirely in half-precision/bf16. Can further reduce VRAM usage but may affect training stability. Use if VRAM is critically low.
* `--gradient_accumulation_steps=N`: Accumulates gradients for N steps before updating the optimizer. Effectively increases the batch size to `train_batch_size * N`, achieving the effect of a larger batch size with less VRAM. Default is 1.
* `--max_grad_norm=N`: Gradient clipping threshold. Clips gradients if their norm exceeds N. Default is 1.0. `0` disables it.
* `--gradient_checkpointing`: Significantly reduces memory usage but slightly decreases training speed. Recommended for SDXL due to high memory consumption.
* `--fused_backward_pass`: **Experimental**: Fuses gradient calculation and optimizer steps to reduce VRAM usage. Available for SDXL. Currently only supports `Adafactor` optimizer. Cannot be used with Gradient Accumulation.
* `--resume=\"<state directory>\"`: Resumes training from a saved state (saved with `--save_state`). Restores optimizer state, step count, etc.
### 1.6. Caching
Caching is effective for SDXL due to its high computational cost.
* `--cache_latents`: Caches VAE outputs (latents) in memory. Skips VAE computation, reducing VRAM usage and speeding up training. **Note:** Image augmentations (`color_aug`, `flip_aug`, `random_crop`, etc.) will be disabled.
* `--cache_latents_to_disk`: Used with `--cache_latents` to cache to disk. Particularly effective for large datasets or multiple training runs. Caches are generated on disk during the first run and loaded from there on subsequent runs.
* `--cache_text_encoder_outputs`: Caches Text Encoder outputs in memory. Skips Text Encoder computation, reducing VRAM usage and speeding up training. **Note:** Caption augmentations (`shuffle_caption`, `caption_dropout_rate`, etc.) will be disabled. **Also, when using this option, Text Encoder LoRA modules cannot be trained (requires `--network_train_unet_only`).**
* `--cache_text_encoder_outputs_to_disk`: Used with `--cache_text_encoder_outputs` to cache to disk.
* `--skip_cache_check`: Skips validation of cache file contents. File existence is checked, and if not found, caches are generated. Usually not needed unless intentionally re-caching for debugging, etc.
### 1.7. Sample Image Generation
Basic options are common with `train_network.py`.
* `--sample_every_n_steps=N` / `--sample_every_n_epochs=N`: Generates sample images every N steps/epochs.
* `--sample_at_first`: Generates sample images before training starts.
* `--sample_prompts=\"<prompt file>\"`: Specifies a file (`.txt`, `.toml`, `.json`) containing prompts for sample image generation.
* `--sample_sampler=\"...\"`: Specifies the sampler (scheduler) for sample image generation. `euler_a`, `dpm++_2m_karras`, etc., are common. See `--help` for choices.
#### Format of Prompt File
A prompt file can contain multiple prompts with options, for example:
```
# prompt 1
masterpiece, best quality, (1girl), in white shirts, upper body, looking at viewer, simple background --n low quality, worst quality, bad anatomy,bad composition, poor, low effort --w 768 --h 768 --d 1 --l 7.5 --s 28
# prompt 2
masterpiece, best quality, 1boy, in business suit, standing at street, looking back --n (low quality, worst quality), bad anatomy,bad composition, poor, low effort --w 576 --h 832 --d 2 --l 5.5 --s 40
```
Lines beginning with `#` are comments. You can specify options for the generated image with options like `--n` after the prompt. The following can be used.
* `--n` Negative prompt up to the next option. Ignored when CFG scale is `1.0`.
* `--w` Specifies the width of the generated image.
* `--h` Specifies the height of the generated image.
* `--d` Specifies the seed of the generated image.
* `--l` Specifies the CFG scale of the generated image. For FLUX.1 models, the default is `1.0`, which means no CFG. For Chroma models, set to around `4.0` to enable CFG.
* `--g` Specifies the embedded guidance scale for the models with embedded guidance (FLUX.1), the default is `3.5`. Set to `0.0` for Chroma models.
* `--s` Specifies the number of steps in the generation.
The prompt weighting such as `( )` and `[ ]` are working for SD/SDXL models, not working for other models like FLUX.1.
### 1.8. Logging & Tracking
* `--logging_dir=\"<log directory>\"`: Specifies the directory for TensorBoard and other logs. If not specified, logs are not output.
* `--log_with=\"tensorboard\"` / `\"wandb\"` / `\"all\"`: Specifies the logging tool to use. If using `wandb`, `pip install wandb` is required.
* `--log_prefix=\"<prefix>\"`: Specifies the prefix for subdirectory names created within `logging_dir`.
* `--wandb_api_key=\"<API key>\"` / `--wandb_run_name=\"<run name>\"`: Options for Weights & Biases (wandb).
* `--log_tracker_name` / `--log_tracker_config`: Advanced tracker configuration options. Usually not needed.
* `--log_config`: Logs the training configuration used (excluding some sensitive information) at the start of training. Helps ensure reproducibility.
### 1.9. Regularization and Advanced Techniques
* `--noise_offset=N`: Enables noise offset and specifies its value. Expected to improve bias in image brightness and contrast. Recommended to enable as SDXL base models are trained with this (e.g., 0.0357). Original technical explanation [here](https://www.crosslabs.org/blog/diffusion-with-offset-noise).
* `--noise_offset_random_strength`: Randomly varies noise offset strength between 0 and the specified value.
* `--adaptive_noise_scale=N`: Adjusts noise offset based on the mean absolute value of latents. Used with `--noise_offset`.
* `--multires_noise_iterations=N` / `--multires_noise_discount=D`: Enables multi-resolution noise. Adding noise of different frequency components is expected to improve detail reproduction. Specify iteration count N (around 6-10) and discount rate D (around 0.3). Technical explanation [here](https://wandb.ai/johnowhitaker/multires_noise/reports/Multi-Resolution-Noise-for-Diffusion-Model-Training--VmlldzozNjYyOTU2).
* `--ip_noise_gamma=G` / `--ip_noise_gamma_random_strength`: Enables Input Perturbation Noise. Adds small noise to input (latents) for regularization. Specify Gamma value (around 0.1). Strength can be randomized with `random_strength`.
* `--min_snr_gamma=N`: Applies Min-SNR Weighting Strategy. Adjusts loss weights for timesteps with high noise in early training to stabilize learning. `N=5` etc. are used.
* `--scale_v_pred_loss_like_noise_pred`: In v-prediction models, scales v-prediction loss similarly to noise prediction loss. **Not typically used for SDXL** as it's not a v-prediction model.
* `--v_pred_like_loss=N`: Adds v-prediction-like loss to noise prediction models. `N` specifies its weight. **Not typically used for SDXL**.
* `--debiased_estimation_loss`: Calculates loss using Debiased Estimation. Similar purpose to Min-SNR but a different approach.
* `--loss_type=\"l1\"` / `\"l2\"` / `\"huber\"` / `\"smooth_l1\"`: Specifies the loss function. Default is `l2` (MSE). `huber` and `smooth_l1` are robust to outliers.
* `--huber_schedule=\"constant\"` / `\"exponential\"` / `\"snr\"`: Scheduling method when using `huber` or `smooth_l1` loss. `snr` is recommended.
* `--huber_c=C` / `--huber_scale=S`: Parameters for `huber` or `smooth_l1` loss.
* `--masked_loss`: Limits loss calculation area based on a mask image. Requires specifying mask images (black and white) in `conditioning_data_dir` in dataset settings. See [About Masked Loss](masked_loss_README.md) for details.
### 1.10. Distributed Training and Other Training Related Options
* `--seed=N`: Specifies the random seed. Set this to ensure training reproducibility.
* `--max_token_length=N` (`75`, `150`, `225`): Maximum token length processed by Text Encoders. For SDXL, typically `75` (default), `150`, or `225`. Longer lengths can handle more complex prompts but increase VRAM usage.
* `--clip_skip=N`: Uses the output from N layers skipped from the final layer of Text Encoders. **Not typically used for SDXL**.
* `--lowram` / `--highvram`: Options for memory usage optimization. `--lowram` is for environments like Colab where RAM < VRAM, `--highvram` is for environments with ample VRAM.
* `--persistent_data_loader_workers` / `--max_data_loader_n_workers=N`: Settings for DataLoader worker processes. Affects wait time between epochs and memory usage.
* `--config_file="<config file>"` / `--output_config`: Options to use/output a `.toml` file instead of command line arguments.
* **Accelerate/DeepSpeed related:** (`--ddp_timeout`, `--ddp_gradient_as_bucket_view`, `--ddp_static_graph`): Detailed settings for distributed training. Accelerate settings (`accelerate config`) are usually sufficient. DeepSpeed requires separate configuration.
* `--initial_epoch=<integer>` â Sets the initial epoch number. `1` means first epoch (same as not specifying). Note: `initial_epoch`/`initial_step` doesn't affect the lr scheduler, which means lr scheduler will start from 0 without `--resume`.
* `--initial_step=<integer>` â Sets the initial step number including all epochs. `0` means first step (same as not specifying). Overwrites `initial_epoch`.
* `--skip_until_initial_step` â Skips training until `initial_step` is reached.
### 1.11. Console and Logging / ã³ã³ãœãŒã«ãšãã°
* `--console_log_level`: Sets the logging level for the console output. Choose from `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`.
* `--console_log_file`: Redirects console logs to a specified file.
* `--console_log_simple`: Enables a simpler log format.
### 1.12. Hugging Face Hub Integration / Hugging Face Hub 飿º
* `--huggingface_repo_id`: The repository name on Hugging Face Hub to upload the model to (e.g., `your-username/your-model`).
* `--huggingface_repo_type`: The type of repository on Hugging Face Hub. Usually `model`.
* `--huggingface_path_in_repo`: The path within the repository to upload files to.
* `--huggingface_token`: Your Hugging Face Hub authentication token.
* `--huggingface_repo_visibility`: Sets the visibility of the repository (`public` or `private`).
* `--resume_from_huggingface`: Resumes training from a state saved on Hugging Face Hub.
* `--async_upload`: Enables asynchronous uploading of models to the Hub, preventing it from blocking the training process.
* `--save_n_epoch_ratio`: Saves the model at a certain ratio of total epochs. For example, `5` will save at least 5 checkpoints throughout the training.
### 1.13. Advanced Attention Settings / é«åºŠãªAttentionèšå®
* `--mem_eff_attn`: Use memory-efficient attention mechanism. This is an older implementation and `sdpa` or `xformers` are generally recommended.
* `--xformers`: Use xformers library for memory-efficient attention. Requires `pip install xformers`.
### 1.14. Advanced LR Scheduler Settings / é«åºŠãªåŠç¿çã¹ã±ãžã¥ãŒã©èšå®
* `--lr_scheduler_type`: Specifies a custom scheduler module.
* `--lr_scheduler_args`: Provides additional arguments to the custom scheduler (e.g., `"T_max=100"`).
* `--lr_decay_steps`: Sets the number of steps for the learning rate to decay.
* `--lr_scheduler_timescale`: The timescale for the inverse square root scheduler.
* `--lr_scheduler_min_lr_ratio`: Sets the minimum learning rate as a ratio of the initial learning rate for certain schedulers.
### 1.15. Differential Learning with LoRA / LoRAã®å·®ååŠç¿
This technique involves merging a pre-trained LoRA into the base model before starting a new training session. This is useful for fine-tuning an existing LoRA or for learning the 'difference' from it.
* `--base_weights`: Path to one or more LoRA weight files to be merged into the base model before training begins.
* `--base_weights_multiplier`: A multiplier for the weights of the LoRA specified by `--base_weights`. You can specify multiple values if you provide multiple weights.
### 1.16. Other Miscellaneous Options / ãã®ä»ã®ãªãã·ã§ã³
* `--tokenizer_cache_dir`: Specifies a directory to cache the tokenizer, which is useful for offline training.
* `--scale_weight_norms`: Scales the weight norms of the LoRA modules. This can help prevent overfitting by controlling the magnitude of the weights. A value of `1.0` is a good starting point.
* `--disable_mmap_load_safetensors`: Disables memory-mapped loading for `.safetensors` files. This can speed up model loading in some environments like WSL.
## 2. Other Tips / ãã®ä»ã®Tips
* **VRAM Usage:** SDXL LoRA training requires a lot of VRAM. Even with 24GB VRAM, you might run out of memory depending on settings. Reduce VRAM usage with these settings:
* `--mixed_precision=\"bf16\"` or `\"fp16\"` (essential)
* `--gradient_checkpointing` (strongly recommended)
* `--cache_latents` / `--cache_text_encoder_outputs` (highly effective, with limitations)
* `--optimizer_type=\"AdamW8bit\"` or `\"Adafactor\"`
* Increase `--gradient_accumulation_steps` (reduce batch size)
* `--full_fp16` / `--full_bf16` (be mindful of stability)
* `--fp8_base` / `--fp8_base_unet` (experimental)
* `--fused_backward_pass` (Adafactor only, experimental)
* **Learning Rate:** Appropriate learning rates for SDXL LoRA depend on the dataset and `network_dim`/`alpha`. Starting around `1e-4` ~ `4e-5` (U-Net), `1e-5` ~ `2e-5` (Text Encoders) is common.
* **Training Time:** Training takes time due to high-resolution data and the size of the SDXL model. Using caching features and appropriate hardware is important.
* **Troubleshooting:**
* **NaN Loss:** Learning rate might be too high, mixed precision settings incorrect (e.g., `--no_half_vae` not specified with `fp16`), or dataset issues.
* **Out of Memory (OOM):** Try the VRAM reduction measures listed above.
* **Training not progressing:** Learning rate might be too low, optimizer/scheduler settings incorrect, or dataset issues.
## 3. Conclusion / ãããã«
`sdxl_train_network.py` offers many options to customize SDXL LoRA training. Refer to `--help`, other documents and the source code for further details.
<details>
<summary>æ¥æ¬èª</summary>
# é«åºŠãªèšå®: SDXL LoRAåŠç¿ã¹ã¯ãªãã `sdxl_train_network.py` 詳现ã¬ã€ã
ãã®ããã¥ã¡ã³ãã§ã¯ã`sd-scripts` ãªããžããªã«å«ãŸãã `sdxl_train_network.py` ã䜿çšãããSDXL (Stable Diffusion XL) ã¢ãã«ã«å¯Ÿãã LoRA (Low-Rank Adaptation) ã¢ãã«åŠç¿ã®é«åºŠãªèšå®ãªãã·ã§ã³ã«ã€ããŠè§£èª¬ããŸãã
åºæ¬çãªäœ¿ãæ¹ã«ã€ããŠã¯ã以äžã®ããã¥ã¡ã³ããåç
§ããŠãã ããã
* [LoRAåŠç¿ã¹ã¯ãªãã `train_network.py` ã®äœ¿ãæ¹](train_network.md)
* [SDXL LoRAåŠç¿ã¹ã¯ãªãã `sdxl_train_network.py` ã®äœ¿ãæ¹](sdxl_train_network.md)
ãã®ã¬ã€ãã¯ãåºæ¬çãªLoRAåŠç¿ã®çµéšãããããã詳现ãªèšå®ãé«åºŠãªæ©èœã詊ãããçç·Žããå©çšè
ã察象ãšããŠããŸãã
**åææ¡ä»¶:**
* `sd-scripts` ãªããžããªã®ã¯ããŒã³ãš Python ç°å¢ã®ã»ããã¢ãããå®äºããŠããããšã
* åŠç¿çšããŒã¿ã»ããã®æºåãšèšå®ïŒ`.toml`ãã¡ã€ã«ïŒãå®äºããŠããããšãïŒ[ããŒã¿ã»ããèšå®ã¬ã€ã](./config_README-ja.md)åç
§ïŒ
* åºæ¬çãªLoRAåŠç¿ã®ã³ãã³ãã©ã€ã³å®è¡çµéšãããããšã
## 1. ã³ãã³ãã©ã€ã³åŒæ° 詳现解説
`sdxl_train_network.py` 㯠`train_network.py` ã®æ©èœãç¶æ¿ãã€ã€ãSDXLç¹æã®æ©èœã远å ããŠããŸããããã§ã¯ãSDXL LoRAåŠç¿ã«é¢é£ããäž»èŠãªã³ãã³ãã©ã€ã³åŒæ°ã«ã€ããŠãæ©èœå¥ã«åé¡ããŠè©³çްã«è§£èª¬ããŸãã
åºæ¬çãªåŒæ°ã«ã€ããŠã¯ã[LoRAåŠç¿ã¹ã¯ãªãã `train_network.py` ã®äœ¿ãæ¹](train_network.md#31-äž»èŠãªã³ãã³ãã©ã€ã³åŒæ°) ããã³ [SDXL LoRAåŠç¿ã¹ã¯ãªãã `sdxl_train_network.py` ã®äœ¿ãæ¹](sdxl_train_network.md#31-äž»èŠãªã³ãã³ãã©ã€ã³åŒæ°ïŒå·®åïŒ) ãåç
§ããŠãã ããã
### 1.1. ã¢ãã«èªã¿èŸŒã¿é¢é£
* `--pretrained_model_name_or_path="<ã¢ãã«ãã¹>"` **[å¿
é ]**
* åŠç¿ã®ããŒã¹ãšãªã **SDXLã¢ãã«** ãæå®ããŸããHugging Face Hubã®ã¢ãã«IDãããŒã«ã«ã®Diffusers圢åŒã¢ãã«ãã£ã¬ã¯ããªããŸãã¯`.safetensors`ãã¡ã€ã«ãæå®ã§ããŸãã
* 詳现ã¯[åºæ¬ã¬ã€ã](sdxl_train_network.md#ã¢ãã«é¢é£)ãåç
§ããŠãã ããã
* `--vae="<VAEãã¹>"`
* ãªãã·ã§ã³ã§ãåŠç¿ã«äœ¿çšããVAEãæå®ããŸããSDXLã¢ãã«ã«å«ãŸããVAE以å€ã䜿çšããå Žåã«æå®ããŸãã`.ckpt`ãŸãã¯`.safetensors`ãã¡ã€ã«ãæå®ã§ããŸãã
* `--no_half_vae`
* æ··å粟床(`fp16`/`bf16`)äœ¿çšæã§ãVAEã`float32`ã§åäœãããŸããSDXLã®VAEã¯`float16`ã§äžå®å®ã«ãªãããšãããããã`fp16`æå®æã«ã¯æå¹ã«ããããšãæšå¥šãããŸãã`bf16`ã§ã¯éåžžäžèŠã§ãã
* `--fp8_base` / `--fp8_base_unet`
* **å®éšçæ©èœ:** ããŒã¹ã¢ãã«ïŒU-Net, Text EncoderïŒãŸãã¯U-Netã®ã¿ãFP8ã§èªã¿èŸŒã¿ãVRAM䜿çšéãåæžããŸããPyTorch 2.1以äžãå¿
èŠã§ãã詳现㯠TODO åŸã§ããã¥ã¡ã³ãã远å ã®é¢é£ã»ã¯ã·ã§ã³ãåç
§ããŠãã ãã (SD3ã®èª¬æã§ããSDXLã«ãé©çšãããŸã)ã
### 1.2. ããŒã¿ã»ããèšå®é¢é£
* `--dataset_config="<èšå®ãã¡ã€ã«ã®ãã¹>"`
* ããŒã¿ã»ããã®èšå®ãèšè¿°ãã`.toml`ãã¡ã€ã«ãæå®ããŸããSDXLã§ã¯é«è§£å床ããŒã¿ãšãã±ãæ©èœïŒ`.toml` ã§ `enable_bucket = true` ãæå®ïŒã®å©çšãäžè¬çã§ãã
* `.toml`ãã¡ã€ã«ã®æžãæ¹ã®è©³çްã¯[ããŒã¿ã»ããèšå®ã¬ã€ã](./config_README-ja.md)ãåç
§ããŠãã ããã
* ã¢ã¹ãã¯ãæ¯ãã±ãã®è§£å床ã¹ããã(`bucket_reso_steps`)ã¯ãSDXLã§ã¯32ã®åæ°ãšããå¿
èŠããããŸãã
### 1.3. åºåã»ä¿åé¢é£
åºæ¬çãªãªãã·ã§ã³ã¯ `train_network.py` ãšå
±éã§ãã
* `--output_dir="<åºåå
ãã£ã¬ã¯ããª>"` **[å¿
é ]**
* `--output_name="<åºåãã¡ã€ã«å>"` **[å¿
é ]**
* `--save_model_as="safetensors"` (æšå¥š), `ckpt`, `pt`, `diffusers`, `diffusers_safetensors`
* `--save_precision="fp16"`, `"bf16"`, `"float"`
* ã¢ãã«ã®ä¿å粟床ãæå®ããŸããæªæå®æã¯åŠç¿æã®ç²ŸåºŠ(`fp16`, `bf16`ç)ã§ä¿åãããŸãã
* `--save_every_n_epochs=N` / `--save_every_n_steps=N`
* Nãšããã¯/ã¹ãããããšã«ã¢ãã«ãä¿åããŸãã
* `--save_last_n_epochs=M` / `--save_last_n_steps=M`
* ãšããã¯/ã¹ãããããšã«ä¿åããéãææ°ã®Måã®ã¿ãä¿æããå€ããã®ã¯åé€ããŸãã
* `--save_state` / `--save_state_on_train_end`
* ã¢ãã«ä¿åæ/åŠç¿çµäºæã«ãOptimizerã®ç¶æ
ãªã©ãå«ãåŠç¿ç¶æ
(`state`)ãä¿åããŸãã`--resume`ãªãã·ã§ã³ã§ã®åŠç¿åéã«å¿
èŠã§ãã
* `--save_last_n_epochs_state=M` / `--save_last_n_steps_state=M`
* `state`ã®ä¿åæ°ãMåã«å¶éããŸãã`--save_last_n_epochs/steps`ã®æå®ãäžæžãããŸãã
* `--no_metadata`
* åºåã¢ãã«ã«ã¡ã¿ããŒã¿ãä¿åããŸããã
* `--save_state_to_huggingface` / `--huggingface_repo_id` ãªã©
* Hugging Face Hubãžã®ã¢ãã«ãstateã®ã¢ããããŒãé¢é£ãªãã·ã§ã³ã詳现㯠TODO ããã¥ã¡ã³ãã远å ãåç
§ããŠãã ããã
### 1.4. ãããã¯ãŒã¯ãã©ã¡ãŒã¿ (LoRA)
åºæ¬çãªãªãã·ã§ã³ã¯ `train_network.py` ãšå
±éã§ãã
* `--network_module=networks.lora` **[å¿
é ]**
* `--network_dim=N` **[å¿
é ]**
* LoRAã®ã©ã³ã¯ (次å
æ°) ãæå®ããŸããSDXLã§ã¯32ã64ãªã©ã詊ãããããšãå€ãã§ãããããŒã¿ã»ãããç®çã«å¿ããŠèª¿æŽãå¿
èŠã§ãã
* `--network_alpha=M`
* LoRAã®ã¢ã«ãã¡å€ã`network_dim`ã®ååçšåºŠããŸãã¯`network_dim`ãšåãå€ãªã©ãäžè¬çã§ããããã©ã«ãã¯1ã
* `--network_dropout=P`
* LoRAã¢ãžã¥ãŒã«å
ã®ããããã¢ãŠãç (0.0~1.0)ãéåŠç¿æå¶ã®å¹æãæåŸ
ã§ããŸããããã©ã«ãã¯None (ããããã¢ãŠããªã)ã
* `--network_args ...`
* ãããã¯ãŒã¯ã¢ãžã¥ãŒã«ãžã®è¿œå åŒæ°ã `key=value` 圢åŒã§æå®ããŸããLoRAã§ã¯ä»¥äžã®é«åºŠãªèšå®ãå¯èœã§ãã
* **éå±€å¥ (Block-wise) 次å
æ°/ã¢ã«ãã¡:**
* U-Netã®åãããã¯ããšã«ç°ãªã`dim`ãš`alpha`ãæå®ã§ããŸããããã«ãããç¹å®ã®å±€ã®åœ±é¿ã匷ããã匱ããããã調æŽãå¯èœã§ãã
* `block_dims`: U-Netã®Linearå±€ããã³Conv2d 1x1å±€ã«å¯Ÿãããããã¯ããšã®dimãã«ã³ãåºåãã§æå®ããŸã (SDXLã§ã¯23åã®æ°å€)ã
* `block_alphas`: äžèšã«å¯Ÿå¿ããalphaå€ãã«ã³ãåºåãã§æå®ããŸãã
* `conv_block_dims`: U-Netã®Conv2d 3x3å±€ã«å¯Ÿãããããã¯ããšã®dimãã«ã³ãåºåãã§æå®ããŸãã
* `conv_block_alphas`: äžèšã«å¯Ÿå¿ããalphaå€ãã«ã³ãåºåãã§æå®ããŸãã
* æå®ããªããããã¯ã¯ `--network_dim`/`--network_alpha` ãŸã㯠`--conv_dim`/`--conv_alpha` (ååšããå Žå) ã®å€ã䜿çšãããŸãã
* 詳现ã¯[LoRA ã®éå±€å¥åŠç¿ç](train_network.md#lora-ã®éå±€å¥åŠç¿ç) (train\_network.mdå
ãSDXLã§ãåæ§ã«é©çšå¯èœ) ãå®è£
([lora.py](lora.py)) ãåç
§ããŠãã ããã
* **LoRA+:**
* `loraplus_lr_ratio=R`: LoRAã®äžåãéã¿(UP)ã®åŠç¿çããäžåãéã¿(DOWN)ã®åŠç¿çã®Råã«ããŸããåŠç¿é床ã®åäžãæåŸ
ã§ããŸããè«ææšå¥šã¯16ã
* `loraplus_unet_lr_ratio=RU`: U-Netéšåã®LoRA+åŠç¿çæ¯ãåå¥ã«æå®ããŸãã
* `loraplus_text_encoder_lr_ratio=RT`: Text Encoderéšåã®LoRA+åŠç¿çæ¯ãåå¥ã«æå®ããŸãã(`--text_encoder_lr1`, `--text_encoder_lr2`ã§æå®ããåŠç¿çã«ä¹ç®ãããŸã)
* 詳现ã¯[README](../README.md#jan-17-2025--2025-01-17-version-090)ãå®è£
([lora.py](lora.py)) ãåç
§ããŠãã ããã
* `--network_train_unet_only`
* U-Netã®LoRAã¢ãžã¥ãŒã«ã®ã¿ãåŠç¿ããŸããText Encoderã®åŠç¿ãè¡ããªãå Žåã«æå®ããŸãã`--cache_text_encoder_outputs` ã䜿çšããå Žåã¯å¿
é ã§ãã
* `--network_train_text_encoder_only`
* Text Encoderã®LoRAã¢ãžã¥ãŒã«ã®ã¿ãåŠç¿ããŸããU-Netã®åŠç¿ãè¡ããªãå Žåã«æå®ããŸãã
* `--network_weights="<éã¿ãã¡ã€ã«>"`
* åŠç¿æžã¿ã®LoRAéã¿ãèªã¿èŸŒãã§åŠç¿ãéå§ããŸãããã¡ã€ã³ãã¥ãŒãã³ã°ãåŠç¿åéã«äœ¿çšããŸãã`--resume` ãšã®éãã¯ããã®ãªãã·ã§ã³ã¯LoRAã¢ãžã¥ãŒã«ã®éã¿ã®ã¿ãèªã¿èŸŒã¿ã`--resume` ã¯Optimizerã®ç¶æ
ãåŠç¿ã¹ãããæ°ãªã©ã埩å
ããŸãã
* `--dim_from_weights`
* `--network_weights` ã§æå®ããéã¿ãã¡ã€ã«ããLoRAã®æ¬¡å
æ° (`dim`) ãèªåçã«èªã¿èŸŒã¿ãŸãã`--network_dim` ã®æå®ã¯äžèŠã«ãªããŸãã
### 1.5. åŠç¿ãã©ã¡ãŒã¿
* `--learning_rate=LR`
* å
šäœã®åŠç¿çãåã¢ãžã¥ãŒã«(`unet_lr`, `text_encoder_lr1`, `text_encoder_lr2`)ã®ããã©ã«ãå€ãšãªããŸãã`1e-3` ã `1e-4` ãªã©ã詊ãããããšãå€ãã§ãã
* `--unet_lr=LR_U`
* U-Netéšåã®LoRAã¢ãžã¥ãŒã«ã®åŠç¿çã
* `--text_encoder_lr1=LR_TE1`
* Text Encoder 1 (OpenCLIP ViT-G/14) ã®LoRAã¢ãžã¥ãŒã«ã®åŠç¿çãéåžžãU-Netããå°ããå€ (äŸ: `1e-5`, `2e-5`) ãæšå¥šãããŸãã
* `--text_encoder_lr2=LR_TE2`
* Text Encoder 2 (CLIP ViT-L/14) ã®LoRAã¢ãžã¥ãŒã«ã®åŠç¿çãéåžžãU-Netããå°ããå€ (äŸ: `1e-5`, `2e-5`) ãæšå¥šãããŸãã
* `--optimizer_type="..."`
* 䜿çšããOptimizerãæå®ããŸãã`AdamW8bit` (çã¡ã¢ãªãäžè¬ç), `Adafactor` (ããã«çã¡ã¢ãªãSDXLãã«ã¢ãã«åŠç¿ã§å®çžŸãã), `Lion`, `DAdaptation`, `Prodigy`ãªã©ãéžæå¯èœã§ããåOptimizerã«ã¯è¿œå ã®åŒæ°ãå¿
èŠãªå ŽåããããŸã (`--optimizer_args`åç
§)ã
* `AdamW8bit` ã `PagedAdamW8bit` (èŠ `bitsandbytes`) ãäžè¬çã§ãã
* `Adafactor` ã¯ã¡ã¢ãªå¹çãè¯ãã§ãããèšå®ãããè€éã§ã (çžå¯Ÿã¹ããã(`relative_step=True`)æšå¥šãåŠç¿çã¹ã±ãžã¥ãŒã©ã¯`adafactor`æšå¥š)ã
* `DAdaptation`, `Prodigy` ã¯åŠç¿çã®èªåèª¿æŽæ©èœããããŸãããLoRA+ãšã®äœµçšã¯ã§ããŸãããåŠç¿çã¯`1.0`çšåºŠãæå®ããŸãã
* 詳现ã¯[train\_util.py](train_util.py)ã®`get_optimizer`颿°ãåç
§ããŠãã ããã
* `--optimizer_args ...`
* Optimizerãžã®è¿œå åŒæ°ã `key=value` 圢åŒã§æå®ããŸã (äŸ: `"weight_decay=0.01"` `"betas=0.9,0.999"`).
* `--lr_scheduler="..."`
* åŠç¿çã¹ã±ãžã¥ãŒã©ãæå®ããŸãã`constant` (å€åãªã), `cosine` (ã³ãµã€ã³ã«ãŒã), `linear` (ç·åœ¢æžè¡°), `constant_with_warmup` (ãŠã©ãŒã ã¢ããä»ã宿°), `cosine_with_restarts` ãªã©ã`constant` ã `cosine` ã `constant_with_warmup` ããã䜿ãããŸãã
* ã¹ã±ãžã¥ãŒã©ã«ãã£ãŠã¯è¿œå ã®åŒæ°ãå¿
èŠã§ã (`--lr_scheduler_args`åç
§)ã
* `DAdaptation` ã `Prodigy` ãªã©ã®èªå·±åŠç¿çèª¿æŽæ©èœä»ãOptimizerã䜿çšããå Žåãã¹ã±ãžã¥ãŒã©ã¯äžèŠã§ã (`constant` ãæå®)ã
* `--lr_warmup_steps=N`
* åŠç¿çã¹ã±ãžã¥ãŒã©ã®ãŠã©ãŒã ã¢ããã¹ãããæ°ãåŠç¿éå§æã«åŠç¿çãåŸã
ã«äžããŠããæéã§ããN < 1 ã®å Žåã¯å
šã¹ãããæ°ã«å¯Ÿããå²åãšè§£éãããŸãã
* `--lr_scheduler_num_cycles=N` / `--lr_scheduler_power=P`
* ç¹å®ã®ã¹ã±ãžã¥ãŒã© (`cosine_with_restarts`, `polynomial`) ã®ããã®ãã©ã¡ãŒã¿ã
* `--max_train_steps=N` / `--max_train_epochs=N`
* åŠç¿ã®ç·ã¹ãããæ°ãŸãã¯ãšããã¯æ°ãæå®ããŸãããšããã¯æå®ãåªå
ãããŸãã
* `--mixed_precision="bf16"` / `"fp16"` / `"no"`
* æ··å粟床åŠç¿ã®èšå®ãSDXLã§ã¯ `bf16` (察å¿GPUã®å Žå) ãŸã㯠`fp16` ã®äœ¿çšãåŒ·ãæšå¥šãããŸããVRAM䜿çšéãåæžããåŠç¿é床ãåäžãããŸãã
* `--full_fp16` / `--full_bf16`
* åŸé
èšç®ãå«ããŠå®å
šã«å粟床/bf16ã§è¡ããŸããVRAM䜿çšéãããã«åæžã§ããŸãããåŠç¿ã®å®å®æ§ã«åœ±é¿ããå¯èœæ§ããããŸããVRAMãã©ãããŠãè¶³ããªãå Žåã«äœ¿çšããŸãã
* `--gradient_accumulation_steps=N`
* åŸé
ãNã¹ãããåèç©ããŠããOptimizerãæŽæ°ããŸããå®è³ªçãªããããµã€ãºã `train_batch_size * N` ã«å¢ãããå°ãªãVRAMã§å€§ããªããããµã€ãºçžåœã®å¹æãåŸãããŸããããã©ã«ãã¯1ã
* `--max_grad_norm=N`
* åŸé
ã¯ãªããã³ã°ã®éŸå€ãåŸé
ã®ãã«ã ãNãè¶
ããå Žåã«ã¯ãªããã³ã°ããŸããããã©ã«ãã¯1.0ã`0`ã§ç¡å¹ã
* `--gradient_checkpointing`
* ã¡ã¢ãªäœ¿çšéã倧å¹
ã«åæžããŸãããåŠç¿é床ã¯è¥å¹²äœäžããŸããSDXLã§ã¯ã¡ã¢ãªæ¶è²»ã倧ãããããæå¹ã«ããããšãæšå¥šãããŸãã
* `--fused_backward_pass`
* **å®éšçæ©èœ:** åŸé
èšç®ãšOptimizerã®ã¹ããããèåããVRAM䜿çšéãåæžããŸããSDXLã§å©çšå¯èœã§ããçŸåš `Adafactor` Optimizerã®ã¿å¯Ÿå¿ãGradient Accumulationãšã¯äœµçšã§ããŸããã
* `--resume="<stateãã£ã¬ã¯ããª>"`
* `--save_state`ã§ä¿åãããåŠç¿ç¶æ
ããåŠç¿ãåéããŸããOptimizerã®ç¶æ
ãåŠç¿ã¹ãããæ°ãªã©ã埩å
ãããŸãã
### 1.6. ãã£ãã·ã¥æ©èœé¢é£
SDXLã¯èšç®ã³ã¹ããé«ãããããã£ãã·ã¥æ©èœã广çã§ãã
* `--cache_latents`
* VAEã®åºå(Latent)ãã¡ã¢ãªã«ãã£ãã·ã¥ããŸããVAEã®èšç®ãçç¥ã§ããVRAM䜿çšéãåæžããåŠç¿ãé«éåããŸãã**泚æ:** ç»åã«å¯ŸããAugmentation (`color_aug`, `flip_aug`, `random_crop` ç) ã¯ç¡å¹ã«ãªããŸãã
* `--cache_latents_to_disk`
* `--cache_latents` ãšäœµçšãããã£ãã·ã¥å
ããã£ã¹ã¯ã«ããŸãã倧éã®ããŒã¿ã»ãããè€æ°åã®åŠç¿ã§ç¹ã«æå¹ã§ããååå®è¡æã«ãã£ã¹ã¯ã«ãã£ãã·ã¥ãçæããã2åç®ä»¥éã¯ãããèªã¿èŸŒã¿ãŸãã
* `--cache_text_encoder_outputs`
* Text Encoderã®åºåãã¡ã¢ãªã«ãã£ãã·ã¥ããŸããText Encoderã®èšç®ãçç¥ã§ããVRAM䜿çšéãåæžããåŠç¿ãé«éåããŸãã**泚æ:** ãã£ãã·ã§ã³ã«å¯ŸããAugmentation (`shuffle_caption`, `caption_dropout_rate` ç) ã¯ç¡å¹ã«ãªããŸãã**ãŸãããã®ãªãã·ã§ã³ã䜿çšããå ŽåãText Encoderã®LoRAã¢ãžã¥ãŒã«ã¯åŠç¿ã§ããŸãã (`--network_train_unet_only` ã®æå®ãå¿
é ã§ã)ã**
* `--cache_text_encoder_outputs_to_disk`
* `--cache_text_encoder_outputs` ãšäœµçšãããã£ãã·ã¥å
ããã£ã¹ã¯ã«ããŸãã
* `--skip_cache_check`
* ãã£ãã·ã¥ãã¡ã€ã«ã®å
å®¹ã®æ€èšŒãã¹ãããããŸãããã¡ã€ã«ã®ååšç¢ºèªã¯è¡ãããååšããªãå Žåã¯ãã£ãã·ã¥ãçæãããŸãããããã°çã§æå³çã«åãã£ãã·ã¥ãããå Žåãé€ããéåžžã¯æå®äžèŠã§ãã
### 1.7. ãµã³ãã«ç»åçæé¢é£
åºæ¬çãªãªãã·ã§ã³ã¯ `train_network.py` ãšå
±éã§ãã
* `--sample_every_n_steps=N` / `--sample_every_n_epochs=N`
* Nã¹ããã/ãšããã¯ããšã«ãµã³ãã«ç»åãçæããŸãã
* `--sample_at_first`
* åŠç¿éå§åã«ãµã³ãã«ç»åãçæããŸãã
* `--sample_prompts="<ããã³ãããã¡ã€ã«>"`
* ãµã³ãã«ç»åçæã«äœ¿çšããããã³ãããèšè¿°ãããã¡ã€ã« (`.txt`, `.toml`, `.json`) ãæå®ããŸãã
* `--sample_sampler="..."`
* ãµã³ãã«ç»åçææã®ãµã³ãã©ãŒïŒã¹ã±ãžã¥ãŒã©ïŒãæå®ããŸãã`euler_a`, `dpm++_2m_karras` ãªã©ãäžè¬çã§ããéžæè¢ã¯ `--help` ãåç
§ããŠãã ããã
#### ããã³ãããã¡ã€ã«ã®æžåŒ
ããã³ãããã¡ã€ã«ã¯è€æ°ã®ããã³ãããšãªãã·ã§ã³ãå«ããããšãã§ããŸããäŸãã°ïŒ
```
# prompt 1
masterpiece, best quality, (1girl), in white shirts, upper body, looking at viewer, simple background --n low quality, worst quality, bad anatomy,bad composition, poor, low effort --w 768 --h 768 --d 1 --l 7.5 --s 28
# prompt 2
masterpiece, best quality, 1boy, in business suit, standing at street, looking back --n (low quality, worst quality), bad anatomy,bad composition, poor, low effort --w 576 --h 832 --d 2 --l 5.5 --s 40
```
`#`ã§å§ãŸãè¡ã¯ã³ã¡ã³ãã§ããçæç»åã®ãªãã·ã§ã³ã¯ããã³ããã®åŸã« `--n` ã®ããã«æå®ã§ããŸãã以äžã®ãªãã·ã§ã³ã䜿çšå¯èœã§ãã
* `--n` 次ã®ãªãã·ã§ã³ãŸã§ããã¬ãã£ãããã³ããã§ããCFGã¹ã±ãŒã«ã `1.0` ã®å Žåã¯ç¡èŠãããŸãã
* `--w` çæç»åã®å¹
ãæå®ããŸãã
* `--h` çæç»åã®é«ããæå®ããŸãã
* `--d` çæç»åã®ã·ãŒãå€ãæå®ããŸãã
* `--l` çæç»åã®CFGã¹ã±ãŒã«ãæå®ããŸããFLUX.1ã¢ãã«ã§ã¯ãããã©ã«ã㯠`1.0` ã§CFGãªããæå³ããŸããChromaã¢ãã«ã§ã¯ãCFGãæå¹ã«ããããã« `4.0` çšåºŠã«èšå®ããŠãã ããã
* `--g` åã蟌ã¿ã¬ã€ãã³ã¹ä»ãã¢ãã«ïŒFLUX.1ïŒã®åã蟌ã¿ã¬ã€ãã³ã¹ã¹ã±ãŒã«ãæå®ãããã©ã«ã㯠`3.5`ãChromaã¢ãã«ã§ã¯ `0.0` ã«èšå®ããŠãã ããã
* `--s` çææã®ã¹ãããæ°ãæå®ããŸãã
ããã³ããã®éã¿ä»ã `( )` ã `[ ]` ã¯SD/SDXLã¢ãã«ã§åäœããFLUX.1ãªã©ä»ã®ã¢ãã«ã§ã¯åäœããŸããã
### 1.8. Logging & Tracking é¢é£
* `--logging_dir="<ãã°ãã£ã¬ã¯ããª>"`
* TensorBoardãªã©ã®ãã°ãåºåãããã£ã¬ã¯ããªãæå®ããŸããæå®ããªãå Žåããã°ã¯åºåãããŸããã
* `--log_with="tensorboard"` / `"wandb"` / `"all"`
* 䜿çšãããã°ããŒã«ãæå®ããŸãã`wandb`ã䜿çšããå Žåã`pip install wandb`ãå¿
èŠã§ãã
* `--log_prefix="<ãã¬ãã£ãã¯ã¹>"`
* `logging_dir` å
ã«äœæããããµããã£ã¬ã¯ããªåã®æ¥é èŸãæå®ããŸãã
* `--wandb_api_key="<APIããŒ>"` / `--wandb_run_name="<å®è¡å>"`
* Weights & Biases (wandb) äœ¿çšæã®ãªãã·ã§ã³ã
* `--log_tracker_name` / `--log_tracker_config`
* é«åºŠãªãã©ãã«ãŒèšå®çšãªãã·ã§ã³ãéåžžã¯æå®äžèŠã
* `--log_config`
* åŠç¿éå§æã«ã䜿çšãããåŠç¿èšå®ïŒäžéšã®æ©å¯æ
å ±ãé€ãïŒããã°ã«åºåããŸããåçŸæ§ã®ç¢ºä¿ã«åœ¹ç«ã¡ãŸãã
### 1.9. æ£ååã»é«åºŠãªåŠç¿ãã¯ããã¯é¢é£
* `--noise_offset=N`
* ãã€ãºãªãã»ãããæå¹ã«ãããã®å€ãæå®ããŸããç»åã®æãããã³ã³ãã©ã¹ãã®åããæ¹åãã广ãæåŸ
ã§ããŸããSDXLã®ããŒã¹ã¢ãã«ã¯ãã®å€ã§åŠç¿ãããŠãããããæå¹ã«ããããšãæšå¥šãããŸã (äŸ: 0.0357)ãå
ã
ã®æè¡è§£èª¬ã¯[ãã¡ã](https://www.crosslabs.org/blog/diffusion-with-offset-noise)ã
* `--noise_offset_random_strength`
* ãã€ãºãªãã»ããã®åŒ·åºŠã0ããæå®å€ã®éã§ã©ã³ãã ã«å€åãããŸãã
* `--adaptive_noise_scale=N`
* Latentã®å¹³å絶察å€ã«å¿ããŠãã€ãºãªãã»ããã調æŽããŸãã`--noise_offset`ãšäœµçšããŸãã
* `--multires_noise_iterations=N` / `--multires_noise_discount=D`
* è€æ°è§£å床ãã€ãºãæå¹ã«ããŸããç°ãªãåšæ³¢æ°æåã®ãã€ãºãå ããããšã§ããã£ããŒã«ã®åçŸæ§ãåäžããã广ãæåŸ
ã§ããŸããã€ãã¬ãŒã·ã§ã³åæ°N (6-10çšåºŠ) ãšå²åŒçD (0.3çšåºŠ) ãæå®ããŸããæè¡è§£èª¬ã¯[ãã¡ã](https://wandb.ai/johnowhitaker/multires_noise/reports/Multi-Resolution-Noise-for-Diffusion-Model-Training--VmlldzozNjYyOTU2)ã
* `--ip_noise_gamma=G` / `--ip_noise_gamma_random_strength`
* Input Perturbation Noiseãæå¹ã«ããŸããå
¥å(Latent)ã«åŸ®å°ãªãã€ãºãå ããŠæ£ååãè¡ããŸããGammaå€ (0.1çšåºŠ) ãæå®ããŸãã`random_strength`ã§åŒ·åºŠãã©ã³ãã åã§ããŸãã
* `--min_snr_gamma=N`
* Min-SNR Weighting Strategy ãé©çšããŸããåŠç¿åæã®ãã€ãºã倧ããã¿ã€ã ã¹ãããã§ã®Lossã®éã¿ã調æŽããåŠç¿ãå®å®ãããŸãã`N=5` ãªã©ã䜿çšãããŸãã
* `--scale_v_pred_loss_like_noise_pred`
* v-predictionã¢ãã«ã«ãããŠãvã®äºæž¬ãã¹ããã€ãºäºæž¬ãã¹ãšåæ§ã®ã¹ã±ãŒã«ã«èª¿æŽããŸããSDXLã¯v-predictionã§ã¯ãªãããã**éåžžã¯äœ¿çšããŸãã**ã
* `--v_pred_like_loss=N`
* ãã€ãºäºæž¬ã¢ãã«ã«väºæž¬ã©ã€ã¯ãªãã¹ã远å ããŸãã`N`ã§ãã®éã¿ãæå®ããŸããSDXLã§ã¯**éåžžã¯äœ¿çšããŸãã**ã
* `--debiased_estimation_loss`
* Debiased Estimationã«ããLossèšç®ãè¡ããŸããMin-SNRãšé¡äŒŒã®ç®çãæã¡ãŸãããç°ãªãã¢ãããŒãã§ãã
* `--loss_type="l1"` / `"l2"` / `"huber"` / `"smooth_l1"`
* æå€±é¢æ°ãæå®ããŸããããã©ã«ãã¯`l2` (MSE)ã`huber`ã`smooth_l1`ã¯å€ãå€ã«é å¥ãªæå€±é¢æ°ã§ãã
* `--huber_schedule="constant"` / `"exponential"` / `"snr"`
* `huber`ãŸãã¯`smooth_l1`æå€±äœ¿çšæã®ã¹ã±ãžã¥ãŒãªã³ã°æ¹æ³ã`snr`ãæšå¥šãããŠããŸãã
* `--huber_c=C` / `--huber_scale=S`
* `huber`ãŸãã¯`smooth_l1`æå€±ã®ãã©ã¡ãŒã¿ã
* `--masked_loss`
* ãã¹ã¯ç»åã«åºã¥ããŠLossèšç®é åãéå®ããŸããããŒã¿ã»ããèšå®ã§`conditioning_data_dir`ã«ãã¹ã¯ç»åïŒçœé»ïŒãæå®ããå¿
èŠããããŸãã詳现ã¯[ãã¹ã¯ãã¹ã«ã€ããŠ](masked_loss_README.md)ãåç
§ããŠãã ããã
### 1.10. 忣åŠç¿ããã®ä»åŠç¿é¢é£
* `--seed=N`
* ä¹±æ°ã·ãŒããæå®ããŸããåŠç¿ã®åçŸæ§ã確ä¿ãããå Žåã«èšå®ããŸãã
* `--max_token_length=N` (`75`, `150`, `225`)
* Text EncoderãåŠçããããŒã¯ã³ã®æå€§é·ãSDXLã§ã¯éåžž`75` (ããã©ã«ã) ãŸã㯠`150`, `225`ãé·ããããšããè€éãªããã³ãããæ±ããŸãããVRAM䜿çšéãå¢å ããŸãã
* `--clip_skip=N`
* Text Encoderã®æçµå±€ããNå±€ã¹ãããããå±€ã®åºåã䜿çšããŸããSDXLã§ã¯**é垞䜿çšããŸãã**ã
* `--lowram` / `--highvram`
* ã¡ã¢ãªäœ¿çšéã®æé©åã«é¢ãããªãã·ã§ã³ã`--lowram`ã¯Colabãªã©RAM < VRAMç°å¢åãã`--highvram`ã¯VRAM最沢ãªç°å¢åãã
* `--persistent_data_loader_workers` / `--max_data_loader_n_workers=N`
* DataLoaderã®ã¯ãŒã«ããã»ã¹ã«é¢ããèšå®ããšããã¯éã®åŸ
ã¡æéãã¡ã¢ãªäœ¿çšéã«åœ±é¿ããŸãã
* `--config_file="<èšå®ãã¡ã€ã«>"` / `--output_config`
* ã³ãã³ãã©ã€ã³åŒæ°ã®ä»£ããã«`.toml`ãã¡ã€ã«ã䜿çš/åºåãããªãã·ã§ã³ã
* **Accelerate/DeepSpeedé¢é£:** (`--ddp_timeout`, `--ddp_gradient_as_bucket_view`, `--ddp_static_graph`)
* 忣åŠç¿æã®è©³çްèšå®ãéåžžã¯Accelerateã®èšå® (`accelerate config`) ã§ååã§ããDeepSpeedã䜿çšããå Žåã¯ãå¥éèšå®ãå¿
èŠã§ãã
* `--initial_epoch=<integer>` â éå§ãšããã¯çªå·ãèšå®ããŸãã`1`ã§æåã®ãšããã¯ïŒæªæå®æãšåãïŒã泚æïŒ`initial_epoch`/`initial_step`ã¯lr schedulerã«åœ±é¿ããªãããã`--resume`ããªãå Žåã¯lr schedulerã¯0ããå§ãŸããŸãã
* `--initial_step=<integer>` â å
šãšããã¯ãå«ãéå§ã¹ãããçªå·ãèšå®ããŸãã`0`ã§æåã®ã¹ãããïŒæªæå®æãšåãïŒã`initial_epoch`ãäžæžãããŸãã
* `--skip_until_initial_step` â `initial_step`ã«å°éãããŸã§åŠç¿ãã¹ãããããŸãã
### 1.11. ã³ã³ãœãŒã«ãšãã°
* `--console_log_level`: ã³ã³ãœãŒã«åºåã®ãã°ã¬ãã«ãèšå®ããŸãã`DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`ããéžæããŸãã
* `--console_log_file`: ã³ã³ãœãŒã«ã®ãã°ãæå®ããããã¡ã€ã«ã«åºåããŸãã
* `--console_log_simple`: ããã·ã³ãã«ãªãã°ãã©ãŒããããæå¹ã«ããŸãã
### 1.12. Hugging Face Hub 飿º
* `--huggingface_repo_id`: ã¢ãã«ãã¢ããããŒãããHugging Face Hubã®ãªããžããªå (äŸ: `your-username/your-model`)ã
* `--huggingface_repo_type`: Hugging Face Hubã®ãªããžããªã®çš®é¡ãéåžžã¯`model`ã§ãã
* `--huggingface_path_in_repo`: ãªããžããªå
ã§ãã¡ã€ã«ãã¢ããããŒããããã¹ã
* `--huggingface_token`: Hugging Face Hubã®èªèšŒããŒã¯ã³ã
* `--huggingface_repo_visibility`: ãªããžããªã®å
¬éèšå® (`public`ãŸãã¯`private`)ã
* `--resume_from_huggingface`: Hugging Face Hubã«ä¿åãããç¶æ
ããåŠç¿ãåéããŸãã
* `--async_upload`: Hubãžã®ã¢ãã«ã®éåæã¢ããããŒããæå¹ã«ããåŠç¿ããã»ã¹ããããã¯ããªãããã«ããŸãã
* `--save_n_epoch_ratio`: ç·ãšããã¯æ°ã«å¯Ÿããç¹å®ã®æ¯çã§ã¢ãã«ãä¿åããŸããäŸãã°`5`ãæå®ãããšãåŠç¿å
šäœã§å°ãªããšã5ã€ã®ãã§ãã¯ãã€ã³ããä¿åãããŸãã
### 1.13. é«åºŠãªAttentionèšå®
* `--mem_eff_attn`: ã¡ã¢ãªå¹çã®è¯ãAttentionã¡ã«ããºã ã䜿çšããŸããããã¯å€ãå®è£
ã§ãããäžè¬çã«ã¯`sdpa`ã`xformers`ã®äœ¿çšãæšå¥šãããŸãã
* `--xformers`: ã¡ã¢ãªå¹çã®è¯ãAttentionã®ããã«xformersã©ã€ãã©ãªã䜿çšããŸãã`pip install xformers`ãå¿
èŠã§ãã
### 1.14. é«åºŠãªåŠç¿çã¹ã±ãžã¥ãŒã©èšå®
* `--lr_scheduler_type`: ã«ã¹ã¿ã ã¹ã±ãžã¥ãŒã©ã¢ãžã¥ãŒã«ãæå®ããŸãã
* `--lr_scheduler_args`: ã«ã¹ã¿ã ã¹ã±ãžã¥ãŒã©ã«è¿œå ã®åŒæ°ãæž¡ããŸã (äŸ: `"T_max=100"`)ã
* `--lr_decay_steps`: åŠç¿çãæžè¡°ããã¹ãããæ°ãèšå®ããŸãã
* `--lr_scheduler_timescale`: éå¹³æ¹æ ¹ã¹ã±ãžã¥ãŒã©ã®ã¿ã€ã ã¹ã±ãŒã«ã
* `--lr_scheduler_min_lr_ratio`: ç¹å®ã®ã¹ã±ãžã¥ãŒã©ã«ã€ããŠãåæåŠç¿çã«å¯Ÿããæå°åŠç¿çã®æ¯çãèšå®ããŸãã
### 1.15. LoRAã®å·®ååŠç¿
æ¢åã®åŠç¿æžã¿LoRAãããŒã¹ã¢ãã«ã«ããŒãžããŠãããæ°ããªåŠç¿ãéå§ããææ³ã§ããæ¢åLoRAã®ãã¡ã€ã³ãã¥ãŒãã³ã°ããå·®åãåŠç¿ããããå Žåã«æå¹ã§ãã
* `--base_weights`: åŠç¿éå§åã«ããŒã¹ã¢ãã«ã«ããŒãžããLoRAã®éã¿ãã¡ã€ã«ã1ã€ä»¥äžæå®ããŸãã
* `--base_weights_multiplier`: `--base_weights`ã§æå®ããLoRAã®éã¿ã®åçãè€æ°æå®ãå¯èœã§ãã
### 1.16. ãã®ä»ã®ãªãã·ã§ã³
* `--tokenizer_cache_dir`: ãªãã©ã€ã³ã§ã®åŠç¿ã«äŸ¿å©ãªããã«ãtokenizerããã£ãã·ã¥ãããã£ã¬ã¯ããªãæå®ããŸãã
* `--scale_weight_norms`: LoRAã¢ãžã¥ãŒã«ã®éã¿ã®ãã«ã ãã¹ã±ãŒãªã³ã°ããŸããéã¿ã®å€§ãããå¶åŸ¡ããããšã§éåŠç¿ãé²ãå©ãã«ãªããŸãã`1.0`ãè¯ãåºçºç¹ã§ãã
* `--disable_mmap_load_safetensors`: `.safetensors`ãã¡ã€ã«ã®ã¡ã¢ãªããããããŒãã£ã³ã°ãç¡å¹ã«ããŸããWSLãªã©ã®äžéšç°å¢ã§ã¢ãã«ã®èªã¿èŸŒã¿ãé«éåã§ããŸãã
## 2. ãã®ä»ã®Tips
* **VRAM䜿çšé:** SDXL LoRAåŠç¿ã¯å€ãã®VRAMãå¿
èŠãšããŸãã24GB VRAMã§ãèšå®ã«ãã£ãŠã¯ã¡ã¢ãªäžè¶³ã«ãªãããšããããŸãã以äžã®èšå®ã§VRAM䜿çšéãåæžã§ããŸãã
* `--mixed_precision="bf16"` ãŸã㯠`"fp16"` (å¿
é çŽ)
* `--gradient_checkpointing` (åŒ·ãæšå¥š)
* `--cache_latents` / `--cache_text_encoder_outputs` (广倧ãå¶çŽãã)
* `--optimizer_type="AdamW8bit"` ãŸã㯠`"Adafactor"`
* `--gradient_accumulation_steps` ã®å€ãå¢ãã (ããããµã€ãºãå°ãããã)
* `--full_fp16` / `--full_bf16` (å®å®æ§ã«æ³šæ)
* `--fp8_base` / `--fp8_base_unet` (å®éšç)
* `--fused_backward_pass` (Adafactoréå®ãå®éšç)
* **åŠç¿ç:** SDXL LoRAã®é©åãªåŠç¿çã¯ããŒã¿ã»ããã`network_dim`/`alpha`ã«äŸåããŸãã`1e-4` ~ `4e-5` (U-Net), `1e-5` ~ `2e-5` (Text Encoders) ããããã詊ãã®ãäžè¬çã§ãã
* **åŠç¿æé:** é«è§£å床ããŒã¿ãšSDXLã¢ãã«ã®ãµã€ãºã®ãããåŠç¿ã«ã¯æéãããããŸãããã£ãã·ã¥æ©èœãé©åãªããŒããŠã§ã¢ã®å©çšãéèŠã§ãã
* **ãã©ãã«ã·ã¥ãŒãã£ã³ã°:**
* **NaN Loss:** åŠç¿çãé«ããããæ··å粟床ã®èšå®ãäžé©å (`fp16`æã®`--no_half_vae`æªæå®ãªã©)ãããŒã¿ã»ããã®åé¡ãªã©ãèããããŸãã
* **VRAMäžè¶³ (OOM):** äžèšã®VRAMåæžçã詊ããŠãã ããã
* **åŠç¿ãé²ãŸãªã:** åŠç¿çãäœããããOptimizer/Schedulerã®èšå®ãäžé©åãããŒã¿ã»ããã®åé¡ãªã©ãèããããŸãã
## 3. ãããã«
`sdxl_train_network.py` ã¯éåžžã«å€ãã®ãªãã·ã§ã³ãæäŸããŠãããSDXL LoRAåŠç¿ã®æ§ã
ãªåŽé¢ãã«ã¹ã¿ãã€ãºã§ããŸãããã®ããã¥ã¡ã³ãããããé«åºŠãªèšå®ããã¥ãŒãã³ã°ãè¡ãéã®å©ããšãªãã°å¹žãã§ãã
äžæãªç¹ã詳现ã«ã€ããŠã¯ãåã¹ã¯ãªããã® `--help` ãªãã·ã§ã³ãããªããžããªå
ã®ä»ã®ããã¥ã¡ã³ããå®è£
ã³ãŒãèªäœãåç
§ããŠãã ããã
</details>
|