LoRA Fine-Tuning (Apple Silicon)
Train custom LoRA adapters for Stable Diffusion 1.5 on Mac hardware. Tested on M4 24GB — produces 3.1MB weight files in ~15 minutes at 500 steps.
Hardware Requirements
| Config | Model | Resolution | VRAM |
|---|
| M4 24GB | SD 1.5 | 512×512 | ✅ Works |
| M4 24GB |
SDXL | 512×512 | ⚠️ Tight, may OOM |
| M4 24GB | FLUX.1-schnell | Any | ❌ OOMs |
| M4 Pro 48GB | SDXL | 1024×1024 | ✅ Estimated |
Training Pipeline
- 1. Prepare dataset: 15-25 images in consistent style, 512×512, with text captions
- Train LoRA: 500 steps, learning rate 1e-4, rank 4
- Evaluate: Generate test images, compare base vs LoRA vs reference (Gemini/DALL-E)
- Score: LLM-as-judge rates each on style consistency, quality, prompt adherence
Quick Start
CODEBLOCK0
Evaluation with LLM-as-Judge
CODEBLOCK1
Key Lessons
- - float32 required on MPS — float16 silently produces NaN on Apple Silicon for SD pipelines
- mflux is faster than PyTorch MPS for FLUX (~105s vs ~90min) but doesn't support LoRA training
- SD 1.5 is the ceiling for 24GB — FLUX LoRA OOMs even with gradient checkpointing
- 15-25 images is the sweet spot — fewer undertrain, more doesn't help proportionally
- Gemini (Imagen 4.0) beats fine-tuned SD 1.5 with zero training — use commercial APIs for production, LoRA for experimentation and offline use
Files
- -
scripts/train_lora.py — Training script with Apple Silicon MPS support - INLINECODE1 — LLM-as-judge evaluation comparing base vs LoRA vs reference
技能名称:lora-finetune
详细描述:
LoRA 微调(Apple Silicon)
在 Mac 硬件上为 Stable Diffusion 1.5 训练自定义 LoRA 适配器。已在 M4 24GB 上测试——500 步训练可在约 15 分钟内生成 3.1MB 的权重文件。
硬件要求
| 配置 | 模型 | 分辨率 | 显存 |
|---|
| M4 24GB | SD 1.5 | 512×512 | ✅ 可用 |
| M4 24GB |
SDXL | 512×512 | ⚠️ 紧张,可能内存溢出 |
| M4 24GB | FLUX.1-schnell | 任意 | ❌ 内存溢出 |
| M4 Pro 48GB | SDXL | 1024×1024 | ✅ 预估可用 |
训练流程
- 1. 准备数据集: 15-25 张风格一致的图像,512×512 分辨率,附带文本描述
- 训练 LoRA: 500 步,学习率 1e-4,秩为 4
- 评估: 生成测试图像,对比基础模型、LoRA 与参考模型(Gemini/DALL-E)
- 评分: 以 LLM 作为评判者,对风格一致性、图像质量、提示遵循度进行评分
快速开始
bash
在文件夹中准备训练图像
ls training_data/
image001.png image001.txt image002.png image002.txt ...
训练(完整选项见 scripts/train_lora.py)
python3 scripts/train_lora.py \
--data
dir ./trainingdata \
--output
dir ./loraweights \
--steps 500 \
--lr 1e-4 \
--rank 4
以 LLM 作为评判者的评估
python
对比基础模型、LoRA 与商业模型(Gemini/DALL-E)
Pixtral Large 对每张图像按 1-10 分进行评分:
- 与训练数据的风格一致性
- 图像质量与连贯性
- 提示遵循度
我们的结果:基础模型 6.8 → LoRA 9.0 → Gemini 9.5
经验:Gemini 无需训练即胜出,但 LoRA 显著缩小了差距
关键经验
- - MPS 上必须使用 float32 — 在 Apple Silicon 上,float16 会在 SD 流程中静默产生 NaN
- mflux 比 PyTorch MPS 更快处理 FLUX(约 105 秒 vs 约 90 分钟),但不支持 LoRA 训练
- SD 1.5 是 24GB 显存的上限 — 即使使用梯度检查点,FLUX LoRA 仍会内存溢出
- 15-25 张图像是最佳数量 — 更少会导致训练不足,更多则不会成比例提升效果
- Gemini(Imagen 4.0)无需训练即可超越微调后的 SD 1.5 — 生产环境使用商业 API,实验和离线使用 LoRA
文件
- - scripts/trainlora.py — 支持 Apple Silicon MPS 的训练脚本
- scripts/comparemodels.py — 以 LLM 作为评判者的评估脚本,对比基础模型、LoRA 与参考模型