42 KiB
Status: under review
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 and How to Use the SDXL LoRA Training Script sdxl_train_network.py.
This guide targets experienced users who want to fine tune settings in detail.
Prerequisites:
- You have cloned the
sd-scriptsrepository and prepared a Python environment. - A training dataset and its
.tomlconfiguration are ready (see the dataset configuration guide). - 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.safetensorsfile.--vae=\"<VAE path>\": optionally use a different VAE. Specify when using a VAE other than the one included in the SDXL model. Can specify.ckptor.safetensorsfiles.--no_half_vae: keep the VAE in float32 even with fp16/bf16 training. The VAE for SDXL can become unstable withfloat16, so it is recommended to enable this whenfp16is specified. Usually unnecessary forbf16.--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.tomldataset config. High resolution data and aspect ratio buckets (specifyenable_bucket = truein.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.tomlfiles, refer to the Dataset Configuration Guide.
1.3. Output and Saving
Options match train_network.py:
--output_dir,--output_name(both required)--save_model_as(recommendedsafetensors),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--resumeoption.--save_last_n_epochs_state=M,--save_last_n_steps_state=M: Limits the number of savedstatefiles to M. Overrides the--save_last_n_epochs/stepsspecification.--no_metadata: Does not save metadata to the output model.--save_state_to_huggingfaceand 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 ofnetwork_dimor the same value asnetwork_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 inkey=valueformat. For LoRA, the following advanced settings are available:- Block-wise dimensions/alphas:
- Allows specifying different
dimandalphafor 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_alphaor--conv_dim/--conv_alpha(if they exist). - For details, refer to Block-wise learning rate for LoRA (in train_network.md, applicable to SDXL) and the implementation (lora.py).
- Allows specifying different
- 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 and the implementation (lora.py).
- Block-wise dimensions/alphas:
--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--resumeis that this option only loads LoRA module weights, while--resumealso 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_dimbecomes 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 like1e-3or1e-4are 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 includeAdamW8bit(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).AdamW8bitorPagedAdamW8bit(requiresbitsandbytes) are common.Adafactoris memory-efficient but slightly complex to configure (relative step (relative_step=True) recommended,adafactorlearning rate scheduler recommended).DAdaptation,Prodigyhave automatic learning rate adjustment but cannot be used with LoRA+. Specify a learning rate around1.0. For details, see theget_optimizerfunction in train_util.py.--optimizer_args ...: Specifies additional arguments to the optimizer inkey=valueformat (e.g.,\"weight_decay=0.01\"\"betas=0.9,0.999\").--lr_scheduler=\"...\": Specifies the learning rate scheduler. Options includeconstant(no change),cosine(cosine curve),linear(linear decay),constant_with_warmup(constant with warmup),cosine_with_restarts, etc.constant,cosine, andconstant_with_warmupare commonly used. Some schedulers require additional arguments (see--lr_scheduler_args). If using optimizers with auto LR adjustment likeDAdaptationorProdigy, a scheduler is not needed (constantshould 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, usingbf16(if GPU supports it) orfp16is 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 totrain_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.0disables 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 supportsAdafactoroptimizer. 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_latentsto 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_outputsto 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. Format follows gen_img_diffusers.py. See documentation for details.--sample_sampler=\"...\": Specifies the sampler (scheduler) for sample image generation.euler_a,dpm++_2m_karras, etc., are common. See--helpfor choices.
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 usingwandb,pip install wandbis required.--log_prefix=\"<prefix>\": Specifies the prefix for subdirectory names created withinlogging_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.--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.--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 withrandom_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=5etc. 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.Nspecifies 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 isl2(MSE).huberandsmooth_l1are robust to outliers.--huber_schedule=\"constant\"/\"exponential\"/\"snr\": Scheduling method when usinghuberorsmooth_l1loss.snris recommended.--huber_c=C/--huber_scale=S: Parameters forhuberorsmooth_l1loss.--masked_loss: Limits loss calculation area based on a mask image. Requires specifying mask images (black and white) inconditioning_data_dirin dataset settings. See About Masked Loss for details.
1.10. Distributed Training and Others
--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, typically75(default),150, or225. 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.--lowramis for environments like Colab where RAM < VRAM,--highvramis 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.tomlfile 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.
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 around1e-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_vaenot specified withfp16), 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.
- NaN Loss: Learning rate might be too high, mixed precision settings incorrect (e.g.,
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.
日本語
高度な設定: SDXL LoRA学習スクリプト sdxl_train_network.py 詳細ガイド
このドキュメントでは、sd-scripts リポジトリに含まれる sdxl_train_network.py を使用した、SDXL (Stable Diffusion XL) モデルに対する LoRA (Low-Rank Adaptation) モデル学習の高度な設定オプションについて解説します。
基本的な使い方については、以下のドキュメントを参照してください。
このガイドは、基本的なLoRA学習の経験があり、より詳細な設定や高度な機能を試したい熟練した利用者を対象としています。
前提条件:
sd-scriptsリポジトリのクローンと Python 環境のセットアップが完了していること。- 学習用データセットの準備と設定(
.tomlファイル)が完了していること。(データセット設定ガイド参照) - 基本的なLoRA学習のコマンドライン実行経験があること。
1. コマンドライン引数 詳細解説
sdxl_train_network.py は train_network.py の機能を継承しつつ、SDXL特有の機能を追加しています。ここでは、SDXL LoRA学習に関連する主要なコマンドライン引数について、機能別に分類して詳細に解説します。
基本的な引数については、LoRA学習スクリプト train_network.py の使い方 および SDXL LoRA学習スクリプト sdxl_train_network.py の使い方 を参照してください。
1.1. モデル読み込み関連
--pretrained_model_name_or_path="<モデルパス>"[必須]- 学習のベースとなる SDXLモデル を指定します。Hugging Face HubのモデルID、ローカルのDiffusers形式モデルディレクトリ、または
.safetensorsファイルを指定できます。 - 詳細は基本ガイドを参照してください。
- 学習のベースとなる SDXLモデル を指定します。Hugging Face HubのモデルID、ローカルのDiffusers形式モデルディレクトリ、または
--vae="<VAEパス>"- オプションで、学習に使用するVAEを指定します。SDXLモデルに含まれるVAE以外を使用する場合に指定します。
.ckptまたは.safetensorsファイルを指定できます。
- オプションで、学習に使用するVAEを指定します。SDXLモデルに含まれるVAE以外を使用する場合に指定します。
--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ファイルの書き方の詳細はデータセット設定ガイドを参照してください。- アスペクト比バケツの解像度ステップ(
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オプションでの学習再開に必要です。
- モデル保存時/学習終了時に、Optimizerの状態などを含む学習状態(
--save_last_n_epochs_state=M/--save_last_n_steps_state=Mstateの保存数を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。
- LoRAのアルファ値。
--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内、SDXLでも同様に適用可能) や実装 (lora.py) を参照してください。
- U-Netの各ブロックごとに異なる
- 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や実装 (lora.py) を参照してください。
- 階層別 (Block-wise) 次元数/アルファ:
- ネットワークモジュールへの追加引数を
--network_train_unet_only- U-NetのLoRAモジュールのみを学習します。Text Encoderの学習を行わない場合に指定します。
--cache_text_encoder_outputsを使用する場合は必須です。
- U-NetのLoRAモジュールのみを学習します。Text Encoderの学習を行わない場合に指定します。
--network_train_text_encoder_only- Text EncoderのLoRAモジュールのみを学習します。U-Netの学習を行わない場合に指定します。
--network_weights="<重みファイル>"- 学習済みのLoRA重みを読み込んで学習を開始します。ファインチューニングや学習再開に使用します。
--resumeとの違いは、このオプションはLoRAモジュールの重みのみを読み込み、--resumeはOptimizerの状態や学習ステップ数なども復元します。
- 学習済みのLoRA重みを読み込んで学習を開始します。ファインチューニングや学習再開に使用します。
--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 1 (OpenCLIP ViT-G/14) のLoRAモジュールの学習率。通常、U-Netより小さい値 (例:
--text_encoder_lr2=LR_TE2- Text Encoder 2 (CLIP ViT-L/14) のLoRAモジュールの学習率。通常、U-Netより小さい値 (例:
1e-5,2e-5) が推奨されます。
- Text Encoder 2 (CLIP ViT-L/14) のLoRAモジュールの学習率。通常、U-Netより小さい値 (例:
--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の
get_optimizer関数を参照してください。
- 使用するOptimizerを指定します。
--optimizer_args ...- Optimizerへの追加引数を
key=value形式で指定します (例:"weight_decay=0.01""betas=0.9,0.999").
- Optimizerへの追加引数を
--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使用量を削減し、学習速度を向上させます。
- 混合精度学習の設定。SDXLでは
--full_fp16/--full_bf16- 勾配計算も含めて完全に半精度/bf16で行います。VRAM使用量をさらに削減できますが、学習の安定性に影響する可能性があります。VRAMがどうしても足りない場合に使用します。
--gradient_accumulation_steps=N- 勾配をNステップ分蓄積してからOptimizerを更新します。実質的なバッチサイズを
train_batch_size * Nに増やし、少ないVRAMで大きなバッチサイズ相当の効果を得られます。デフォルトは1。
- 勾配をNステップ分蓄積してからOptimizerを更新します。実質的なバッチサイズを
--max_grad_norm=N- 勾配クリッピングの閾値。勾配のノルムがNを超える場合にクリッピングします。デフォルトは1.0。
0で無効。
- 勾配クリッピングの閾値。勾配のノルムがNを超える場合にクリッピングします。デフォルトは1.0。
--gradient_checkpointing- メモリ使用量を大幅に削減しますが、学習速度は若干低下します。SDXLではメモリ消費が大きいため、有効にすることが推奨されます。
--fused_backward_pass- 実験的機能: 勾配計算とOptimizerのステップを融合し、VRAM使用量を削減します。SDXLで利用可能です。現在
AdafactorOptimizerのみ対応。Gradient Accumulationとは併用できません。
- 実験的機能: 勾配計算とOptimizerのステップを融合し、VRAM使用量を削減します。SDXLで利用可能です。現在
--resume="<stateディレクトリ>"--save_stateで保存された学習状態から学習を再開します。Optimizerの状態や学習ステップ数などが復元されます。
1.6. キャッシュ機能関連
SDXLは計算コストが高いため、キャッシュ機能が効果的です。
--cache_latents- VAEの出力(Latent)をメモリにキャッシュします。VAEの計算を省略でき、VRAM使用量を削減し、学習を高速化します。注意: 画像に対するAugmentation (
color_aug,flip_aug,random_crop等) は無効になります。
- VAEの出力(Latent)をメモリにキャッシュします。VAEの計算を省略でき、VRAM使用量を削減し、学習を高速化します。注意: 画像に対するAugmentation (
--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の指定が必須です)。
- Text Encoderの出力をメモリにキャッシュします。Text Encoderの計算を省略でき、VRAM使用量を削減し、学習を高速化します。注意: キャプションに対するAugmentation (
--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) を指定します。書式はgen_img_diffusers.pyに準じます。詳細はドキュメントを参照してください。
- サンプル画像生成に使用するプロンプトを記述したファイル (
--sample_sampler="..."- サンプル画像生成時のサンプラー(スケジューラ)を指定します。
euler_a,dpm++_2m_karrasなどが一般的です。選択肢は--helpを参照してください。
- サンプル画像生成時のサンプラー(スケジューラ)を指定します。
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)。元々の技術解説はこちら。
--noise_offset_random_strength- ノイズオフセットの強度を0から指定値の間でランダムに変動させます。
--adaptive_noise_scale=N- Latentの平均絶対値に応じてノイズオフセットを調整します。
--noise_offsetと併用します。
- Latentの平均絶対値に応じてノイズオフセットを調整します。
--multires_noise_iterations=N/--multires_noise_discount=D- 複数解像度ノイズを有効にします。異なる周波数成分のノイズを加えることで、ディテールの再現性を向上させる効果が期待できます。イテレーション回数N (6-10程度) と割引率D (0.3程度) を指定します。技術解説はこちら。
--ip_noise_gamma=G/--ip_noise_gamma_random_strength- Input Perturbation Noiseを有効にします。入力(Latent)に微小なノイズを加えて正則化を行います。Gamma値 (0.1程度) を指定します。
random_strengthで強度をランダム化できます。
- Input Perturbation Noiseを有効にします。入力(Latent)に微小なノイズを加えて正則化を行います。Gamma値 (0.1程度) を指定します。
--min_snr_gamma=N- Min-SNR Weighting Strategy を適用します。学習初期のノイズが大きいタイムステップでのLossの重みを調整し、学習を安定させます。
N=5などが使用されます。
- Min-SNR Weighting Strategy を適用します。学習初期のノイズが大きいタイムステップでのLossの重みを調整し、学習を安定させます。
--scale_v_pred_loss_like_noise_pred- v-predictionモデルにおいて、vの予測ロスをノイズ予測ロスと同様のスケールに調整します。SDXLはv-predictionではないため、通常は使用しません。
--v_pred_like_loss=N- ノイズ予測モデルにv予測ライクなロスを追加します。
Nでその重みを指定します。SDXLでは通常は使用しません。
- ノイズ予測モデルにv予測ライクなロスを追加します。
--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=Shuberまたはsmooth_l1損失のパラメータ。
--masked_loss- マスク画像に基づいてLoss計算領域を限定します。データセット設定で
conditioning_data_dirにマスク画像(白黒)を指定する必要があります。詳細はマスクロスについてを参照してください。
- マスク画像に基づいてLoss計算領域を限定します。データセット設定で
1.10. 分散学習・その他
--seed=N- 乱数シードを指定します。学習の再現性を確保したい場合に設定します。
--max_token_length=N(75,150,225)- Text Encoderが処理するトークンの最大長。SDXLでは通常
75(デフォルト) または150,225。長くするとより複雑なプロンプトを扱えますが、VRAM使用量が増加します。
- Text Encoderが処理するトークンの最大長。SDXLでは通常
--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を使用する場合は、別途設定が必要です。
- 分散学習時の詳細設定。通常はAccelerateの設定 (
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の設定が不適切、データセットの問題などが考えられます。
- NaN Loss: 学習率が高すぎる、混合精度の設定が不適切 (
3. おわりに
sdxl_train_network.py は非常に多くのオプションを提供しており、SDXL LoRA学習の様々な側面をカスタマイズできます。このドキュメントが、より高度な設定やチューニングを行う際の助けとなれば幸いです。
不明な点や詳細については、各スクリプトの --help オプションや、リポジトリ内の他のドキュメント、実装コード自体を参照してください。