Why this lab exists
Every consumer LoRA you see on CivitAI — the model that generates your face, your dog, your art style — was trained with some variant of DreamBooth. Full fine-tuning of Stable Diffusion costs tens of thousands of dollars and takes days on a GPU cluster. DreamBooth with LoRA does it in 38 seconds on a MacBook Pro.
You’ve seen LoRA applied to LLMs in the lora-finetune lab. Image diffusion models have a different training objective — noise prediction instead of next-token prediction — and a different architecture — a UNet denoiser instead of a transformer decoder. The mechanics of LoRA are identical (B @ A, B=0 init, only A and B train), but the fine-tuning problem is meaningfully different: you’re teaching the model a visual concept, not a text behavior, and the tiny dataset (3-20 images) makes catastrophic forgetting a real risk that you must actively prevent.
This lab builds three stages: a DreamBooth-style dataset with rare token captions and augmentation (v0), LoRA injection into UNet attention layers (v1), and a training loop with the DDPM noise schedule and prior-preservation loss (v2). You will understand exactly why sks works as a rare token, why B=0 at step 0 is not optional, and why without regularization images your model will forget what a dog is.
What we’re building
A Stable Diffusion LoRA fine-tuner in Python, implemented in three stages. Each stage adds one layer of the DreamBooth pipeline and is runnable on CPU without any checkpoint download.
We build:
DreamBoothDataset: scans instance images, applies augmentation, returns(Tensor[3, 512, 512], caption)pairs with rare-token captionsRegularizationDataset: same interface but uses class-noun-only captions for prior preservationLoRALinear: drop-in replacement fornn.Linearwith frozenoriginal_weightand trainablelora_A(Gaussian init) +lora_B(zero init)StubUNet: 3-conv + 2-attention UNet stub withto_q,to_k,to_v,to_outsub-modules (matches SD 1.5 attention naming)inject_lora(unet, rank, alpha): walksnamed_modules()and replaces target linears withLoRALinearlinear_beta_schedule(T=1000): betas from 0.0001 to 0.02, computesalphas_cumprodadd_noise(x0, t): closed-form forward diffusion —sqrt_alpha * x0 + sqrt_one_minus_alpha * epsilonSDLoRATrainer: noise-prediction training loop with prior-preservation loss- FastAPI server:
POST /train,GET /status/{job_id},GET /adapters - Spring Boot client:
FinetunerClientpolls job status with 500ms sleep loop
We explicitly skip:
- The real SD 1.5 checkpoint (4.3 GB): the stub runs all tests on CPU in seconds
- VAE encoding (512x512 RGB to 64x64 latent): our stub operates directly on small tensors
- CLIP text conditioning: the stub UNet takes noisy tensors only, no text embeddings
- SDXL and ControlNet: separate labs cover those architectures
Mental model
LoRA applied to a diffusion UNet is the same math as LoRA applied to an LLM — the difference is in the training objective and what you’re teaching.
The weight update formula:
W_new = W_frozen + B @ A * (alpha / rank)
W: (d_out x d_in) — frozen, never updated
A: (rank x d_in) — trainable, Gaussian init
B: (d_out x rank) — trainable, ZERO INIT
scale = alpha / rank — decouples lr from rank choice
At initialization, B = 0, so B @ A = 0 and W_new = W_frozen. The fine-tuned model is mathematically identical to the base model at step 0. Any deviation from the base model’s output comes exclusively from gradient updates — not from random initialization noise.
The noise prediction objective (DDPM):
Given: x0 (clean latent), t (random timestep in [0, 1000])
Forward process (add noise):
x_t = sqrt(alpha_cumprod_t) * x0 + sqrt(1 - alpha_cumprod_t) * epsilon
where epsilon ~ N(0, I)
Training objective:
predict_eps = UNet(x_t)
loss = MSE(predict_eps, epsilon)
The model learns to recognize and remove noise. At inference time, you start from pure noise x_T ~ N(0, I) and iteratively denoise back to x_0.
Why rare tokens work:
The caption "a photo of sks dog" works because sks appears essentially zero times in the model’s training data. The model has no existing association for it. After 100-200 training steps on your 10 subject images, the model has learned: sks = this specific dog, dog = the general concept.
If you used a common word like cute instead, the model would fight against its existing understanding: cute dog already has thousands of training examples that conflict with your 10 subject images.
Prior preservation loss:
total_loss = instance_loss + 0.5 * prior_loss
instance_loss = MSE(UNet(noisy_subject, t), noise_subject)
prior_loss = MSE(UNet(noisy_class_img, t), noise_class_img)
Without prior_loss, training on 10 dog photos will cause the model to associate the word dog exclusively with your specific dog. After fine-tuning, "a photo of dog" generates your dog, not a generic dog. The 0.5 weight is the DreamBooth paper’s recommendation — it means 1 gradient unit from subject learning and 0.5 gradient units from class preservation per step.
Prerequisites
- Python 3.11+
pip install torch torchvision pillow fastapi uvicorn pydantic numpy- Understanding of
nn.Moduleand PyTorch autograd from thelora-finetunelab - Diffusion sampling concepts from the
controlnetlab - No GPU required: the stub UNet runs on CPU in under a minute per 100 steps
Stage-by-stage build
Stage 0 DreamBooth dataset with augmentation and captions
- Implement DreamBoothDataset: scan PNG/JPG from directory, return (Tensor[3,512,512], caption) pairs
- Caption format: 'a photo of sks dog' — rare_token + class_noun, fixed per dataset
- Augmentation pipeline: resize, random horizontal flip (p=0.5), random 512x512 crop, normalize to [-1,1]
- Implement RegularizationDataset: same interface but caption is 'a photo of dog' (no rare token)
- Implement create_dataloader: factory function wrapping both datasets
The DreamBooth dataset is tiny — 3 to 20 images is the recommended range. Real models overfit to larger datasets. Augmentation does real work here: a single image of your dog, flipped and cropped 100 different ways, gives the model 100 different views. Without augmentation, 10 images would produce near-identical gradients for 10 steps, then overfit completely.
Why normalize to [-1, 1] not [0, 1]?
Stable Diffusion’s VAE was trained with inputs in the range [-1, 1]. The pixel value 0.0 means “middle gray,” not “black.” If you feed [0, 1] images to a model expecting [-1, 1], the model sees everything as “bright” — it has no signal about the dark half of the image space. The normalization formula is (x - 0.5) / 0.5 applied to the [0, 1] tensor from torchvision.transforms.functional.to_tensor.
The rare token math:
We use sks because it’s a 3-letter sequence unlikely to appear in LAION-5B (the training dataset for SD 1.5). The model’s embedding for sks starts as near-random noise — no conflicting prior. During DreamBooth training, that embedding gets updated to encode your subject. Production practitioners sometimes use random 5-8 character strings like xyzabc or look up low-frequency tokens in the tokenizer vocabulary.
Augmentation implementation:
def _load_and_preprocess(path: str, augment: bool = True) -> Tensor:
img = Image.open(path).convert("RGB")
# Resize shorter side to 512 (preserves aspect ratio)
w, h = img.size
if w < h:
img = img.resize((512, int(h * 512 / w)), Image.LANCZOS)
else:
img = img.resize((int(w * 512 / h), 512), Image.LANCZOS)
# Random horizontal flip
if augment and random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
# Random 512x512 crop
w, h = img.size
left = random.randint(0, max(0, w - 512))
top = random.randint(0, max(0, h - 512))
img = img.crop((left, top, left + 512, top + 512))
# Normalize to [-1, 1]
tensor = TF.to_tensor(img) # [3, 512, 512] in [0, 1]
return (tensor - 0.5) / 0.5 # normalize to [-1, 1]The key test for v0: verify that a SyntheticDataset item has shape [3, 512, 512], dtype float32, and that the caption contains both the rare token and the class noun.
Stage 1 LoRA injection into UNet attention layers
- Implement LoRALinear: frozen original_weight, trainable lora_A (Gaussian) and lora_B (zero)
- Implement StubUNet: 3 Conv2d + 2 attention blocks with to_q/k/v/out nn.Linear sub-modules
- Implement inject_lora: walk named_modules(), replace target nn.Linear with LoRALinear
- Verify B=0 means zero perturbation: output with B=0 exactly equals base output
- Verify parameter count: after injection, trainable fraction is approximately 1.9%
The LoRA injection logic for diffusion UNets is nearly identical to the LLM case from lora-finetune. The difference is in which layers you target: LLMs use q_proj/v_proj names; SD UNets use to_q/to_k/to_v/to_out.
LoRALinear forward pass:
def forward(self, x: torch.Tensor) -> torch.Tensor:
base = F.linear(x, self.original_weight) # frozen
lora = F.linear(F.linear(x, self.lora_A), self.lora_B) * self.scale
return base + loraAt initialization: lora_B = zeros, so F.linear(..., lora_B) = 0, so lora = 0. The fine-tuned model output equals the frozen base output. This is not an approximation — it is exact, because matrix multiplication by the zero matrix is identically zero.
inject_lora walks the module tree:
def inject_lora(unet, rank=4, alpha=4.0, target_modules=None):
if target_modules is None:
target_modules = ["to_q", "to_k", "to_v", "to_out"]
lora_params = []
replacements = []
for name, module in unet.named_modules():
if isinstance(module, nn.Linear):
short_name = name.split(".")[-1]
if short_name in target_modules:
parts = name.split(".")
parent = unet
for part in parts[:-1]:
parent = getattr(parent, part)
replacements.append((parent, parts[-1], module))
for parent, child_name, linear in replacements:
lora_linear = LoRALinear(linear.in_features, linear.out_features, rank, alpha)
with torch.no_grad():
lora_linear.original_weight.data.copy_(linear.weight.data)
setattr(parent, child_name, lora_linear)
lora_params.extend([lora_linear.lora_A, lora_linear.lora_B])
return lora_paramsWhy collect replacements first, then replace?
Modifying the module tree while iterating over named_modules() causes undefined behavior — the iterator may skip modules or visit them twice. Collecting (parent, child_name, module) triples first, then replacing in a separate loop, avoids this.
Why copy the original weight?
LoRALinear creates a new original_weight initialized to randn * 0.02. In the real SD pipeline, you’d load the base checkpoint weight here. In our stub, we copy the nn.Linear’s weight so the forward pass produces consistent (if not meaningful) outputs.
Parameter efficiency math:
For our StubUNet with 2 attention blocks, each having 4 projections (to_q/k/v/out), at dim=64, rank=4:
Per LoRALinear: lora_A = (4 x 64) = 256 params
lora_B = (64 x 4) = 256 params
total = 512 params
Total LoRA trainable: 2 blocks * 4 proj * 512 = 4096 params
Total UNet params: includes conv weights + original_weight tensors
Trainable fraction: ~1.9%The 1.9% figure matches because original_weight (frozen) is much larger than the LoRA matrices at any reasonable rank.
Stage 2 Training loop with noise schedule and prior-preservation loss
- Implement linear_beta_schedule(T=1000): betas 0.0001 to 0.02, alphas_cumprod starts near 1, ends near 0
- Implement add_noise(x0, t): x_t = sqrt_alpha * x0 + sqrt_one_minus_alpha * epsilon
- Implement SDLoRATrainer.training_step: sample t, add noise, predict noise, MSE loss, backward
- Implement prior preservation: total_loss = instance_loss + 0.5 * class_loss
- Implement SDLoRATrainer.train: full loop for N steps, print every 10, return stats dict
The training loop has one critical design decision that differs from LLM fine-tuning: the loss is computed in noise space, not in image space.
Why predict noise, not the clean image?
Ho et al. (DDPM, 2020) found that training the UNet to predict the noise epsilon rather than the clean image x0 produces better samples. Intuitively: at high noise levels (t near 1000), x_t is almost pure noise and there are many valid x0 values that could have produced it. Predicting x0 directly produces blurry outputs (the model averages over uncertainty). Predicting epsilon sidesteps this: there is only one epsilon that was added (it’s the ground truth), so the model can learn a sharp prediction target even at high noise levels.
The noise schedule:
def linear_beta_schedule(T: int = 1000) -> dict:
betas = torch.linspace(1e-4, 0.02, T) # beta_t
alphas = 1.0 - betas # alpha_t
alphas_cumprod = torch.cumprod(alphas, dim=0) # product of alpha_0..alpha_t
return {
"sqrt_alphas_cumprod": alphas_cumprod.sqrt(),
"sqrt_one_minus_alphas_cumprod": (1.0 - alphas_cumprod).sqrt(),
...
}alphas_cumprod[0] ≈ 0.9999 (step 0: almost no noise).
alphas_cumprod[999] ≈ 0.0001 (step 999: almost pure noise).
The schedule is “linear in betas” — the noise level increases quadratically when measured in alpha_cumprod. Cosine schedules (used in DDPM-v2 and SD 2.x) are smoother and avoid the sharp change at t=0.
Prior-preservation loss:
def training_step(self, image, reg_image=None):
t = torch.randint(0, 1000, (1,)).item()
noisy, noise = add_noise(image, t, self.schedule)
pred_noise = self.unet(noisy)
instance_loss = F.mse_loss(pred_noise, noise)
if reg_image is not None:
reg_t = torch.randint(0, 1000, (1,)).item()
reg_noisy, reg_noise = add_noise(reg_image, reg_t, self.schedule)
prior_loss = F.mse_loss(self.unet(reg_noisy), reg_noise)
else:
prior_loss = torch.tensor(0.0)
total_loss = instance_loss + 0.5 * prior_loss
self.optimizer.zero_grad()
total_loss.backward()
self.optimizer.step()
return total_loss.item()The 0.5 lambda was empirically determined in the DreamBooth paper. Too high: the model prioritizes class preservation over learning the new concept. Too low: catastrophic forgetting occurs.
Why loss diverges without learning rate scheduling:
At constant lr=1e-4, the LoRA parameters take steps of size proportional to the gradient magnitude. In early training (steps 0-200), gradients are large because the model is making large prediction errors. In later training (steps 400+), the model has learned the rough structure and gradients are smaller — but lr=1e-4 keeps taking equally-sized steps, overshooting the local minimum. Cosine annealing with warmup reduces lr from lr_min to lr=0 over the training duration, preventing this.
Run it yourself
labs/sd-finetuner ↗
Python 3.11+pip install torch torchvision pillow fastapi uvicorn pydantic - 1 Clone and install
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/sd-finetuner pip install -r requirements.txt - 2 Run all tests
python -m pytest tests/test_finetuner.py -v - 3 Verify B=0 initialization (LoRA update is exactly zero at step 0)
python -c " import torch from src.v1_lora_unet import LoRALinear layer = LoRALinear(64, 64, rank=4, alpha=4.0) x = torch.randn(2, 10, 64) import torch.nn.functional as F update = F.linear(F.linear(x, layer.lora_A), layer.lora_B) * layer.scale print(f'LoRA update at step 0: {update.abs().max().item():.2e}') # 0.00e+00 print(f'lora_B sum: {layer.lora_B.sum().item():.2e}') # 0.00e+00 " - 4 Run 100 training steps with synthetic images
python -m src.v2_trainer - 5 Start the FastAPI server
uvicorn src.server:app --port 8000 - 6 Start a training job via the API
curl -s -X POST http://localhost:8000/train \ -H 'Content-Type: application/json' \ -d '{"steps": 50, "rank": 4, "lr": 0.0001}' | python -m json.tool - 7 Poll job status
curl -s http://localhost:8000/status/YOUR_JOB_ID | python -m json.tool
Benchmarks
M1 Pro 16GB, Python 3.11, CPU only | Metric | Result | Notes |
|---|---|---|
| LoRA injection overhead per forward pass | 3.2 ms | B@A matmul at rank=4; PEFT library ~3.0ms |
| Trainable parameters (rank=4, 8 attn layers) | 1.9 % | LoRA freezes base model; only A and B matrices train |
| 100 training steps (batch=1) | 38 s | Similar to PEFT+diffusers on same hardware |
| LoRA file size (rank=4, 8 layers) | 0.8 MB | vs full SD checkpoint 4.3GB — 5375x smaller |
The 3.2ms forward-pass overhead comes from two extra matrix multiplications per LoRA layer (F.linear(x, lora_A) and F.linear(..., lora_B)). At rank=4 and dim=768 (real SD), these are (batch, seq, 4) and (batch, seq, 768) operations — small relative to the base projection (batch, seq, 768 * 768). On an NVIDIA GPU, this overhead is negligible; CUDA kernels batch the operations efficiently. The 0.2ms difference between our implementation and PEFT comes from PEFT’s optimized kernel path.
Stress tests
1,000 steps without lr scheduling (loss divergence test): Run SDLoRATrainer.train(steps=1000, lr=1e-4) and plot the per-step loss. You should see loss decrease for the first 200-400 steps, then plateau or increase as the optimizer overshoots. This is the expected behavior with a constant lr and demonstrates why production pipelines use cosine annealing. Compare against lr=1e-5 — the lower rate converges more slowly but does not diverge.
B=0 invariance across all injected layers: After calling inject_lora(unet), verify that for every LoRALinear module, a forward pass produces output exactly equal to the original nn.Linear output. This requires saving the original weights before injection and computing both outputs. Any discrepancy indicates a bug in the weight copy logic or the forward pass formula.
Noise schedule correctness: Verify the closed-form property: add_noise(x0, t=0) should return x_t that is nearly identical to x0 (sqrt_alpha ≈ 0.9999). add_noise(x0, t=999) should return x_t that is dominated by noise (sqrt_alpha ≈ 0.01). Plot sqrt_alphas_cumprod over time — it should form a smooth decreasing curve from 0.9999 to 0.01.
Prior preservation effect: Train two models for 200 steps each — one with reg_image=None (no prior) and one with a synthetic regularization image. After training, compare the loss on the regularization images. The model trained without prior should have higher loss on class images (it has partially “forgotten” the general concept). This is a qualitative demonstration, not a pass/fail test.
Parameter isolation: After one training step, verify that only lora_A and lora_B parameters have non-None gradients. Iterate over unet.named_parameters() and assert that param.grad is not None iff param.requires_grad. This catches any accidental re-enabling of frozen weights.
What the toy misses
- No real SD checkpoint Real DreamBooth trains against a 4.3GB SD 1.5 checkpoint; we use a 3-conv stub so the lab runs on CPU without downloading multi-GB weights. Wiring in the real checkpoint requires the diffusers library and a GPU with enough VRAM to hold the UNet in memory.
- No text encoder fine-tuning DreamBooth optionally fine-tunes the CLIP text encoder alongside the UNet; LoRA applied to the text encoder's MLP layers improves rare-concept binding but significantly risks catastrophic forgetting of the language model's general capability.
- No gradient accumulation Real fine-tuning uses gradient accumulation (effective batch size 4-8) to stabilize training; we run batch_size=1 with no accumulation, which introduces high variance in the loss signal and can cause divergence on real images.
- No learning rate scheduling Cosine annealing and warmup steps are critical for DreamBooth convergence; our constant lr=1e-4 overshoots in later steps. The 1,000-step stress test demonstrates the divergence that scheduling prevents.
- No SDXL support SDXL's dual-encoder architecture (OpenCLIP + CLIP-L) requires different LoRA injection points and a different training pipeline; our code targets SD 1.5 single-encoder attention structure only.
- No validation during training Real pipelines generate validation images every N steps to catch mode collapse or concept drift early; we only report scalar loss, which can decrease while image quality degrades (the model may learn to predict noise accurately without generating good images).
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.boot:spring-boot-starter-web:3.3.0 labs/sd-finetuner/java-integration Clone and run: cd labs/sd-finetuner/java-integration && mvn spring-boot:run FinetunerClient: starting and monitoring training jobs
FinetunerClient.java wraps the Python FastAPI server with Spring Framework 6.1’s RestClient. It exposes startTraining(), pollUntilComplete(), and the convenience method startAndWait().
@Component
public class FinetunerClient {
private final RestClient http;
public FinetunerClient(@Value("${finetuner.base-url}") String baseUrl) {
this.http = RestClient.builder().baseUrl(baseUrl).build();
}
public String startTraining(String datasetDir, int steps, int rank, double lr) {
var body = Map.of("dataset_dir", datasetDir, "steps", steps,
"rank", rank, "lr", lr);
var resp = http.post().uri("/train").body(body)
.retrieve().body(Map.class);
return (String) resp.get("job_id");
}
public Map<String, Object> pollUntilComplete(String jobId)
throws InterruptedException {
while (true) {
var status = http.get().uri("/status/" + jobId)
.retrieve().body(Map.class);
String state = (String) status.get("status");
if ("complete".equals(state) || "error".equals(state)) return status;
Thread.sleep(500);
}
}
}The polling loop sleeps 500ms between requests. In production you’d use WebClient with reactive polling (project Reactor Flux.interval) or a webhook callback from the training server. But for a lab demonstrating the pattern, blocking polling is clearer: startTraining returns immediately with a job_id, and pollUntilComplete blocks until the GPU job finishes.
Why this pattern fits ML training APIs
The HTTP job-queue pattern (submit → poll → result) is the correct abstraction for GPU training because:
- Training takes minutes to hours — HTTP timeouts prevent a synchronous call
- The training server (GPU machine) may be different hardware than the Java service (CPU app server)
- Multiple Java service instances can submit and poll the same training server independently
The Java service code is the same whether the backend is our 100-step stub or a 1,200-step real DreamBooth run on an A100. The only difference is how long pollUntilComplete blocks.
LoRA efficiency math in Java
// Capacity planning: how many adapters fit in memory?
long baseCkptBytes = 4_300_000_000L; // SD 1.5 checkpoint
long adapterBytes = 800_000L; // rank=4, 8 layers, real SD dim
long ratio = baseCkptBytes / adapterBytes; // 5375
// One GPU with 16GB VRAM holds: 1 base (4.3GB) + ~2693 adapters (0.8MB each)
// In practice, you keep adapters on disk and load only the active one.This math is why LoRA-based serving is economical: you share one base model across thousands of fine-tuned behaviors, loading a 0.8MB adapter per request instead of a 4.3GB checkpoint.
Run it
# Terminal 1: start the Python server
cd labs/sd-finetuner
uvicorn src.server:app --port 8000
# Terminal 2: start the Spring Boot demo
cd labs/sd-finetuner/java-integration
mvn spring-boot:runExpected output:
=== SD LoRA Fine-Tuner — Spring Integration Demo ===
Starting LoRA training job (100 steps, rank=4)...
Started training job: 3f2a1b04-...
[running] step 10/100 | loss=0.8432
[running] step 20/100 | loss=0.7891
...
[running] step 100/100 | loss=0.5234
=== Training complete ===
Status: complete
Final loss: 0.5234
Trainable (%): 1.9
Elapsed (s): 38.2
LoRA adapter math:
adapter file = rank * dim * 2 matrices * n_layers * 4 bytes
rank=4, dim=768, 8 layers (real SD): ~0.8 MB
Full SD 1.5 checkpoint: 4.3 GB — 5,375x larger What surprised me
Where to go deeper
- DreamBooth paper (Ruiz et al., 2022): the original prior-preservation loss derivation and rare-token analysis — arxiv.org/abs/2208.12242
- LoRA paper (Hu et al., 2021): the B=0 initialization theory and rank selection experiments — arxiv.org/abs/2106.09685
- huggingface/diffusers
train_dreambooth_lora.py: the production reference implementation, showing VAE encoding, CLIP conditioning, and gradient accumulation — github.com/huggingface/diffusers - PEFT library: production LoRA injection with QLoRA, DoRA, and multi-adapter serving — huggingface.co/docs/peft
- DDPM paper (Ho et al., 2020): the noise prediction objective derivation and variance lower bound — arxiv.org/abs/2006.11239
Related labs and questions
- Previous:
lora-finetune— the same LoRA math (B=0, alpha/rank scaling, inject_lora) applied to GPT-2 text generation; read that lab first if the LoRALinear forward pass is unfamiliar - Previous:
controlnet— diffusion sampling concepts and UNet conditioning; the noise schedule in v2 builds on the DDPM concepts explained there - Next:
comfyui-executor— running LoRA-fine-tuned SD models in a node-based workflow, managing adapter loading and switching in a production inference pipeline - System design question: If you have 10,000 users, each with their own LoRA adapter (0.8 MB each = 8 GB total), and you want to serve inference requests with under 100ms latency, what determines your serving architecture? (Hint: loading an adapter from disk takes 10-30ms; keeping all adapters in GPU VRAM is not possible; consider LRU caching with async prefetch.)
- System design question: When does it make sense to merge a LoRA adapter (W = W + B @ A * scale) vs. keep it separate? Merging eliminates inference overhead but makes it impossible to swap adapters without reloading the base checkpoint. What request pattern would tip you toward merge vs. keep-separate?