Spaces:
Paused
A newer version of the Gradio SDK is available: 6.24.0
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.--sample_sampler=\"...\": Specifies the sampler (scheduler) for sample image generation.euler_a,dpm++_2m_karras, etc., are common. See--helpfor 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.
--nNegative prompt up to the next option. Ignored when CFG scale is1.0.--wSpecifies the width of the generated image.--hSpecifies the height of the generated image.--dSpecifies the seed of the generated image.--lSpecifies the CFG scale of the generated image. For FLUX.1 models, the default is1.0, which means no CFG. For Chroma models, set to around4.0to enable CFG.--gSpecifies the embedded guidance scale for the models with embedded guidance (FLUX.1), the default is3.5. Set to0.0for Chroma models.--sSpecifies 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 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 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, 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. --initial_epoch=<integer>â Sets the initial epoch number.1means first epoch (same as not specifying). Note:initial_epoch/initial_stepdoesn'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.0means first step (same as not specifying). Overwritesinitial_epoch.--skip_until_initial_stepâ Skips training untilinitial_stepis reached.
1.11. Console and Logging / ã³ã³ãœãŒã«ãšãã°
--console_log_level: Sets the logging level for the console output. Choose fromDEBUG,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. Usuallymodel.--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 (publicorprivate).--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,5will 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 andsdpaorxformersare generally recommended.--xformers: Use xformers library for memory-efficient attention. Requirespip 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 of1.0is a good starting point.--disable_mmap_load_safetensors: Disables memory-mapped loading for.safetensorsfiles. 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 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åŠç¿ã¹ã¯ãªãã
train_network.pyã®äœ¿ãæ¹ - SDXL LoRAåŠç¿ã¹ã¯ãªãã
sdxl_train_network.pyã®äœ¿ãæ¹
ãã®ã¬ã€ãã¯ãåºæ¬çãª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) ãæå®ããŸãã
- ãµã³ãã«ç»åçæã«äœ¿çšããããã³ãããèšè¿°ãããã¡ã€ã« (
--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)ãå ã ã®æè¡è§£èª¬ã¯ãã¡ãã
--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ã®èšå® (
--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ã®èšå®ãäžé©åãããŒã¿ã»ããã®åé¡ãªã©ãèããããŸãã
- NaN Loss: åŠç¿çãé«ããããæ··å粟床ã®èšå®ãäžé©å (
3. ãããã«
sdxl_train_network.py ã¯éåžžã«å€ãã®ãªãã·ã§ã³ãæäŸããŠãããSDXL LoRAåŠç¿ã®æ§ã
ãªåŽé¢ãã«ã¹ã¿ãã€ãºã§ããŸãããã®ããã¥ã¡ã³ãããããé«åºŠãªèšå®ããã¥ãŒãã³ã°ãè¡ãéã®å©ããšãªãã°å¹žãã§ãã
äžæãªç¹ã詳现ã«ã€ããŠã¯ãåã¹ã¯ãªããã® --help ãªãã·ã§ã³ãããªããžããªå
ã®ä»ã®ããã¥ã¡ã³ããå®è£
ã³ãŒãèªäœãåç
§ããŠãã ããã