InfraLens

A clear starting point for learning AI infrastructure.

Overview

Lab 04: CFG Code Reading

Annotated code reading lab. Running code is optional.

Related handbook section

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.

Concept Goal

CFG Code Reading

Read classifier-free guidance as a linear combination of unconditional and conditional predictions.

Mental Model

Mechanism to keep in mind

  • `uncond` gives the baseline denoising direction.
  • `cond - uncond` is the prompt direction.
  • `guidance_scale` amplifies that direction.
Formula bridge

What the CFG line means

Classifier-free guidance

guided = uncond + scale * (cond - uncond)

  • uncond is the prediction without text conditioning; in Stable Diffusion-style pipelines, a negative prompt often changes this branch.
  • cond is the prediction with prompt conditioning.
  • Higher scale can improve prompt adherence but may reduce diversity or create artifacts.
  • The exact tensors being combined depend on prediction target and scheduler.
Annotated Code Preview

Starter preview

Excerpt from code/lab-04-cfg-code-reading/classifier_free_guidance.py. The linked starter file is the source of truth.

Open starter file
# 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.
Line-by-line Explanation

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.
What to Notice

Reading checkpoints

  • CFG is usually inference-time guidance.
  • Higher scale is not always better.
  • Negative prompts modify the unconditional branch in many pipelines.
Common Misunderstandings

What this lab prevents

  • Classifier-free guidance does not require an external classifier.
  • CFG changes the denoiser prediction, not the scheduler formula itself.
Interview Explanation

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.

External intuition notes

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.
Further Reading

Official, paper and practical references