This file provides the overview and guidance for developers working with the codebase, including setup instructions, architecture details, and common commands.
The codebase is built around a strategy pattern architecture that supports multiple diffusion model families:
library/strategy_base.py: Base classes for tokenization, text encoding, latent caching, and training strategieslibrary/strategy_*.py: Model-specific implementations for SD, SDXL, SD3, FLUX, etc.library/train_util.py: Core training utilities shared across all model typeslibrary/config_util.py: Configuration management with TOML support
Each supported model family has a consistent structure:
- Training script:
{model}_train.py(full fine-tuning),{model}_train_network.py(LoRA/network training) - Model utilities:
library/{model}_models.py,library/{model}_train_utils.py,library/{model}_utils.py - Networks:
networks/lora_{model}.py,networks/oft_{model}.pyfor adapter training
- Stable Diffusion 1.x:
train*.py,library/train_util.py,train_db.py(for DreamBooth) - SDXL:
sdxl_train*.py,library/sdxl_* - SD3:
sd3_train*.py,library/sd3_* - FLUX.1:
flux_train*.py,library/flux_*
- Block swapping: CPU-GPU memory optimization via
--blocks_to_swapparameter, works with custom offloading. Only available for models with transformer architectures like SD3 and FLUX.1. - Custom offloading:
library/custom_offloading_utils.pyfor advanced memory management - Gradient checkpointing: Memory reduction during training
- LoRA training: Low-rank adaptation networks in
networks/lora*.py - ControlNet training: Conditional generation control
- Textual Inversion: Custom embedding training
- Multi-resolution training: Bucket-based aspect ratio handling
- Validation loss: Real-time training monitoring, only for LoRA training
Dataset configuration uses TOML files with structured validation:
[datasets.sample_dataset]
resolution = 1024
batch_size = 2
[[datasets.sample_dataset.subsets]]
image_dir = "path/to/images"
caption_extension = ".txt"All training scripts follow this general pattern:
accelerate launch --mixed_precision bf16 {script_name}.py \
--pretrained_model_name_or_path model.safetensors \
--dataset_config config.toml \
--output_dir output \
--output_name model_name \
[model-specific options]For low VRAM environments, use block swapping:
# Add to any training command for memory reduction
--blocks_to_swap 10 # Swap 10 blocks to CPU (adjust number as needed)Located in tools/ directory:
tools/merge_lora.py: Merge LoRA weights into base modelstools/cache_latents.py: Pre-cache VAE latents for faster trainingtools/cache_text_encoder_outputs.py: Pre-cache text encoder outputs
When adding support for new models, implement the four core strategies:
TokenizeStrategy: Text tokenization handlingTextEncodingStrategy: Text encoder forward passLatentsCachingStrategy: VAE encoding/cachingTextEncoderOutputsCachingStrategy: Text encoder output caching
- Unit tests focus on utility functions and model loading
- Integration tests validate training script syntax and basic execution
- Most tests use mocks to avoid requiring actual model files
- Add tests for new model support in
tests/test_{model}_*.py
- Use
config_util.pydataclasses for type-safe configuration - Support both command-line arguments and TOML file configuration
- Validate configuration early in training scripts to prevent runtime errors
- Always consider VRAM limitations when implementing features
- Use gradient checkpointing for large models
- Implement block swapping for models with transformer architectures
- Cache intermediate results (latents, text embeddings) when possible