SD3.zh
Stable Diffusion 3¶
在本示例中,我们将使用 SimpleTuner 工具包训练一个 Stable Diffusion 3 模型,并使用 lora 模型类型。
前提条件¶
确保您已安装 Python;SimpleTuner 在 3.10 到 3.12 版本上运行良好。
您可以运行以下命令检查:
如果您的 Ubuntu 系统未安装 Python 3.12,可以尝试以下命令:
容器镜像依赖¶
对于 Vast、RunPod 和 TensorDock(以及其他平台),在 CUDA 12.2-12.8 镜像上可以使用以下命令启用 CUDA 扩展编译:
安装¶
通过 pip 安装 SimpleTuner:
pip install 'simpletuner[cuda]'
# CUDA 13 / Blackwell users (NVIDIA B-series GPUs)
pip install 'simpletuner[cuda13]' --extra-index-url https://download.pytorch.org/whl/cu130
如需手动安装或开发环境设置,请参阅安装文档。
AMD ROCm 后续步骤¶
要使 AMD MI300X 可用,必须执行以下操作:
apt install amd-smi-lib
pushd /opt/rocm/share/amd_smi
python3 -m pip install --upgrade pip
python3 -m pip install .
popd
设置环境¶
要运行 SimpleTuner,您需要设置配置文件、数据集和模型目录,以及数据加载器配置文件。
配置文件¶
一个实验性脚本 configure.py 可以通过交互式的逐步配置完全跳过本节。它包含一些安全功能,有助于避免常见陷阱。
**注意:**这不会配置您的数据加载器。您稍后仍需手动配置。
运行方式:
⚠️ 对于位于 Hugging Face Hub 访问受限国家的用户,您应该根据系统使用的
$SHELL将HF_ENDPOINT=https://hf-mirror.com添加到~/.bashrc或~/.zshrc中。
如果您更喜欢手动配置:
将 config/config.json.example 复制为 config/config.json:
接下来需要修改以下变量:
查看示例配置
{
"model_type": "lora",
"model_family": "sd3",
"pretrained_model_name_or_path": "stabilityai/stable-diffusion-3.5-large",
"output_dir": "/home/user/outputs/models",
"validation_resolution": "1024x1024,1280x768",
"validation_guidance": 3.0,
"validation_prompt": "your main test prompt here",
"user_prompt_library": "config/user_prompt_library.json"
}
pretrained_model_name_or_path- 设置为stabilityai/stable-diffusion-3.5-large。注意需要登录 Huggingface 并获得访问权限才能下载该模型。稍后我们会说明登录 Huggingface 的步骤。- 如果您更想训练较旧的 SD3.0 Medium(2B),请改用
stabilityai/stable-diffusion-3-medium-diffusers。 MODEL_TYPE- 设置为lora。MODEL_FAMILY- 设置为sd3。OUTPUT_DIR- 设置为您想要存储检查点和验证图像的目录。建议使用完整路径。VALIDATION_RESOLUTION- 由于 SD3 是 1024px 模型,可设置为1024x1024。- SD3 还在多宽高比桶上进行了微调,可用逗号分隔指定其他分辨率:
1024x1024,1280x768 VALIDATION_GUIDANCE- SD3 适合非常低的值。设置为3.0。
如果使用 Mac M 系列机器,还有一些额外设置:
mixed_precision应设置为no。
高级实验功能¶
显示高级实验详情
SimpleTuner 包含可显著提高训练稳定性和性能的实验功能。 * **[计划采样(Rollout)](../experimental/SCHEDULED_SAMPLING.md):**通过让模型在训练期间生成自己的输入来减少曝光偏差并提高输出质量。 > ⚠️ 这些功能会增加训练的计算开销。 #### 量化模型训练 在 Apple 和 NVIDIA 系统上测试过,Hugging Face Optimum-Quanto 可将精度和 VRAM 要求降低到远低于基础 SDXL 训练的水平。 > ⚠️ 如果使用 JSON 配置文件,请在 `config.json` 中使用以下格式,而不是 `config.env`:{
"base_model_precision": "int8-quanto",
"text_encoder_1_precision": "no_change",
"text_encoder_2_precision": "no_change",
"text_encoder_3_precision": "no_change",
"optimizer": "adamw_bf16"
}
</details>
# choices: int8-quanto, int4-quanto, int2-quanto, fp8-quanto
# int8-quanto was tested with a single subject dreambooth LoRA.
# fp8-quanto does not work on Apple systems. you must use int levels.
# int2-quanto is pretty extreme and gets the whole rank-1 LoRA down to about 13.9GB VRAM.
# may the gods have mercy on your soul, should you push things Too Far.
export TRAINER_EXTRA_ARGS="--base_model_precision=int8-quanto"
# Maybe you want the text encoders to remain full precision so your text embeds are cake.
# We unload the text encoders before training, so, that's not an issue during training time - only during pre-caching.
# Alternatively, you can go ham on quantisation here and run them in int4 or int8 mode, because no one can stop you.
export TRAINER_EXTRA_ARGS="${TRAINER_EXTRA_ARGS} --text_encoder_1_precision=no_change --text_encoder_2_precision=no_change"
# When you're quantising the model, --base_model_default_dtype is set to bf16 by default. This setup requires adamw_bf16, but saves the most memory.
# adamw_bf16 only supports bf16 training, but any other optimiser will support both bf16 or fp32 training precision.
export OPTIMIZER="adamw_bf16"
查看示例配置
[
{
"id": "pseudo-camera-10k-sd3",
"type": "local",
"crop": true,
"crop_aspect": "square",
"crop_style": "center",
"resolution": 1024,
"minimum_image_size": 0,
"maximum_image_size": 1024,
"target_downsample_size": 1024,
"resolution_type": "pixel_area",
"cache_dir_vae": "/home/user/simpletuner/output/cache/vae/sd3/pseudo-camera-10k",
"instance_data_dir": "/home/user/simpletuner/datasets/pseudo-camera-10k",
"disabled": false,
"skip_file_discovery": "",
"caption_strategy": "filename",
"metadata_backend": "discovery"
},
{
"id": "text-embeds",
"type": "local",
"dataset_type": "text_embeds",
"default": true,
"cache_dir": "cache/text/sd3/pseudo-camera-10k",
"disabled": false,
"write_batch_size": 128
}
]
mkdir -p datasets
pushd datasets
huggingface-cli download --repo-type=dataset bghira/pseudo-camera-10k --local-dir=pseudo-camera-10k
popd