Lab 04: CFG Code Reading
Annotated code reading lab. Running code is optional.
CFG Code Reading
This lab maps directly to the handbook section. Read the related handbook section first, then use the lab page and starter file to connect the concept to concrete variables, shapes, APIs, and interview-ready explanations.
CFG Code Reading
Read classifier-free guidance as a linear combination of unconditional and conditional predictions.
Mechanism to keep in mind
- `uncond` gives the baseline denoising direction.
- `cond - uncond` is the prompt direction.
- `guidance_scale` amplifies that direction.
What the CFG line means
guided = uncond + scale * (cond - uncond)
uncondis the prediction without text conditioning; in Stable Diffusion-style pipelines, a negative prompt often changes this branch.condis the prediction with prompt conditioning.- Higher
scalecan improve prompt adherence but may reduce diversity or create artifacts. - The exact tensors being combined depend on prediction target and scheduler.
Starter preview
Excerpt from code/lab-04-cfg-code-reading/classifier_free_guidance.py. The linked starter file is the source of truth.
# CFG Code Reading
# Annotated reading material. Running this file is optional.
# Source-of-truth focus: Read classifier-free guidance as a linear combination of unconditional and conditional predictions.
uncond = "epsilon_without_prompt"
cond = "epsilon_with_prompt"
guidance_scale = 7.5
noise_pred = f"{uncond} + {guidance_scale} * ({cond} - {uncond})"
# What to explain while reading:
# - uncond gives the baseline denoising direction.
# - cond - uncond is the prompt direction.
# - guidance_scale amplifies that direction.
#
# Common traps:
# - Classifier-free guidance does not require an external classifier.
# - CFG changes the denoiser prediction, not the scheduler formula itself.
What each block is doing
- Setup / contract
- `uncond` gives the baseline denoising direction.
- Main transition
- `cond - uncond` is the prompt direction.
- Interview hook
- `guidance_scale` amplifies that direction.
Reading checkpoints
- CFG is usually inference-time guidance.
- Higher scale is not always better.
- Negative prompts modify the unconditional branch in many pipelines.
What this lab prevents
- Classifier-free guidance does not require an external classifier.
- CFG changes the denoiser prediction, not the scheduler formula itself.
How to say it out loud
Read classifier-free guidance as a linear combination of unconditional and conditional predictions. Then explain the code by naming the state being transformed, the axis or shape that matters, and the tradeoff that would appear in a real system.
Additional intuition
- Use official docs and papers for API behavior and factual claims; use blogs only to improve the mental picture.
- If support matrices, performance behavior or backend choices are version-sensitive, check current docs before repeating them.
- A strong interview answer names the state object, the shape or axis it changes, and the tradeoff it creates.
