Phase 1: Diffusion Model Fundamentals · 50 min · Python · Hugging Face Diffusers · Civitai
The Concept
Text conditioning: CLIP and T5
Text conditioning is how the model "understands" your prompt. The text is encoded into embeddings that guide the diffusion process.
CLIP (Contrastive Language-Image Pre-training)
Used in Stable Diffusion 1.5 and SDXL.
- SD 1.5: CLIP ViT-L/14 (768-dim embeddings)
- SDXL: Dual CLIP — ViT-L/14 + ViT-bigG/14 (1280-dim embeddings, open_clip)
# SDXL uses two CLIP text encoders
from transformers import CLIPTextModel, CLIPTokenizer
tokenizer1 = CLIPTokenizer.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="tokenizer")
text_encoder1 = CLIPTextModel.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="text_encoder")
tokenizer2 = CLIPTokenizer.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="tokenizer_2")
text_encoder2 = CLIPTextModel.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="text_encoder_2")
# Encode prompt
tokens = tokenizer1("a cat on a windowsill", return_tensors="pt", padding="max_length", max_length=77)
embeddings = text_encoder1(tokens.input_ids).last_hidden_state
# embeddings shape: (1, 77, 768) — 77 tokens, 768-dim each
T5 (Text-to-Text Transformer)
Used in FLUX, Imagen, and Stable Diffusion 3.
- FLUX.1: T5-XXL (4096-dim embeddings, 256 tokens)
- SD 3.5: T5-XXL + CLIP (triple encoder)
T5 provides much longer context (256 tokens vs CLIP's 77) and better understanding of complex prompts. This is why FLUX handles detailed prompts better than SDXL.
Why does FLUX handle complex, detailed prompts better than SDXL?
FLUX uses T5-XXL for text encoding, which provides 4096-dim embeddings and 256 token context length. SDXL uses CLIP with 768/1280-dim embeddings and only 77 tokens. T5 is a language model trained on text-to-text tasks, giving it much better understanding of complex prompts. The 256 token limit means FLUX can process ~3x longer prompts than SDXL.
Image conditioning: ControlNet and IP-adapter
Text isn't the only way to control generation. Image conditioning provides structural control.
ControlNet
ControlNet adds spatial conditioning — you provide a structural image (depth map, pose, edges, canny) and the model follows that structure.
from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
# Load ControlNet for depth conditioning
controlnet = ControlNetModel.from_pretrained("diffusers/controlnet-depth-sdxl-1.0")
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
controlnet=controlnet,
)
# Generate with depth map control
image = pipe(
prompt="a modern living room",
image=depth_map_image, # Structural control
controlnet_conditioning_scale=0.8, # How strongly to follow the structure
).images[0]
ControlNet types:
- Depth: 3D depth maps — maintains spatial structure
- Canny: Edge detection — maintains outlines
- OpenPose: Human pose skeletons — maintains body positions
- Scribble: Rough sketches — guides composition
- Tile: Low-res image — maintains layout while allowing style changes
IP-Adapter
IP-Adapter provides image-prompt conditioning — you provide a reference image and the model generates in a similar style.
from diffusers import StableDiffusionXLPipeline
from ip_adapter import IPAdapterXL
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
ip_adapter = IPAdapterXL(pipe, image_encoder_path, ip_adapter_path, device="cuda")
# Generate with style reference
image = ip_adapter.generate(
prompt="a cat sitting on a chair",
ip_adapter_image=reference_style_image,
scale=0.7, # How strongly to follow the reference style
)
Model file formats
Checkpoints (.safetensors)
The full model — UNet/DiT + VAE + text encoders bundled together.
SDXL checkpoint (~6.4GB):
├── UNet (~5.6GB) — the noise prediction network
├── VAE (~335MB) — the image encoder/decoder
└── Text Encoders (~1.5GB) — CLIP ViT-L + ViT-bigG
# Loading a checkpoint
from diffusers import StableDiffusionXLPipeline
pipe = StableDiffusionXLPipeline.from_single_file("sd_xl_base_1.0.safetensors")
LoRA (Low-Rank Adaptation)
A small adapter (~50-300MB) that modifies the base model's behavior. LoRAs contain low-rank matrices that are added to the base model's weights.
LoRA file (~150MB):
└── Low-rank matrices for UNet attention layers
(rank=16, typically 0.1-1% of base model parameters)
# Loading a LoRA on top of base model
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
pipe.load_lora_weights("path/to/style_lora.safetensors")
# Control LoRA strength
image = pipe(
prompt="a cat in watercolor style",
cross_attention_kwargs={"scale": 0.8}, # LoRA strength (0-1)
).images[0]
VAE (Variational Autoencoder)
The image compression model — can be swapped independently.
# Swapping VAE
from diffusers import AutoencoderKL
custom_vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix")
pipe.vae = custom_vae
Text Encoders
Can be swapped or fine-tuned, though this is less common.
Component combination order
The correct loading order matters:
# 1. Load base model (checkpoint)
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
# 2. Load LoRA adapter(s) on top
pipe.load_lora_weights("civitai/lora/watercolor_style.safetensors")
pipe.load_lora_weights("civitai/lora/detail_enhancer.safetensors") # Can stack multiple
# 3. Swap VAE if needed
pipe.vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix")
# 4. Add ControlNet if needed
pipe.controlnet = ControlNetModel.from_pretrained("diffusers/controlnet-depth-sdxl-1.0")
# 5. Generate
image = pipe(prompt="...", cross_attention_kwargs={"scale": 0.8}).images[0]
File format summary
| Component | Format | Typical Size | Role |
|---|
| Checkpoint | .safetensors | 2-7GB | Full model (UNet + VAE + encoders) |
| LoRA | .safetensors | 50-300MB | Style/behavior adapter |
| VAE | .safetensors / folder | 50-350MB | Image compression |
| Text Encoder | .bin / .safetensors | 250MB-5GB | Prompt understanding |
| ControlNet | .safetensors / folder | 1-5GB | Structural conditioning |
| IP-Adapter | .safetensors | 50-100MB | Image style reference |