K3Easy★ Core Essential · Industrial BedrockPart K · Generative Models & Diffusion

Classifier-Free Guidance (CFG)

⚡Flashcard Drill➔

Extrapolate conditional vs. unconditional noise predictions by guidance scale w to boost prompt alignment.

⏱️ Time: O(B * C * H * W) 前向计算量翻倍
💾 Space: 显存批次翻倍
🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
def apply_classifier_free_guidance(
noise_pred_uncond: np.ndarray, # (B, C, H, W) 无条件输出
noise_pred_cond: np.ndarray, # (B, C, H, W) 文本条件输出
guidance_scale: float = 7.5
) -> np.ndarray:
"""
CFG 线性外插公式: uncond + scale * (cond - uncond)
"""
return
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: CFG doubles forward inference compute per step. How do production pipelines reduce this cost?
Next: Flow Matching Velocity Field Objective →
💡

Core Mental Anchor / Mnemonic

Take unconditional noise as anchor and extrapolate along (cond - uncond) scaled by guidance w.

📐 Mathematical Derivation & Core Formula

ϵ^θ(xt,c)=ϵθ(xt,∅)+w⋅(ϵθ(xt,c)−ϵθ(xt,∅))\hat{\epsilon}_\theta(x_t, c) = \epsilon_\theta(x_t, \emptyset) + w \cdot \left(\epsilon_\theta(x_t, c) - \epsilon_\theta(x_t, \emptyset)\right)
### Conditioning Extrapolation Without an External Classifier
Classifier-Free Guidance (Ho & Salimans, 2021) randomly drops conditioning prompts (c→∅c \to \emptyset) during training (10–20%) so one model learns both conditional and unconditional scores.
During sampling, it extrapolates along the conditional direction with guidance scale w>1w > 1:
ϵ^=ϵuncond+w⋅(ϵcond−ϵuncond)\hat{\epsilon} = \epsilon_{\text{uncond}} + w \cdot (\epsilon_{\text{cond}} - \epsilon_{\text{uncond}})
This improves prompt adherence and sample fidelity at the cost of two evaluations per step.

🔄 Tensor Dimensions & Shape Flow

noise_uncond, noise_cond -> uncond + w * (cond - uncond) -> guided noise estimate

🛡️ Numerical Stability & Pitfalls Checklist

  • Excessive guidance_scale (> 15) causes color clipping/oversaturation; dynamic thresholding mitigates extremes.
  • Batch [uncond, cond] together along dim 0 to execute both predictions in a single GPU forward pass.

💻 Reference Code Implementation

import numpy as np

def apply_classifier_free_guidance(
    noise_pred_uncond: np.ndarray,  # (B, C, H, W) 无条件输出
    noise_pred_cond: np.ndarray,    # (B, C, H, W) 文本条件输出
    guidance_scale: float = 7.5
) -> np.ndarray:
    """
    CFG 线性外插公式: uncond + scale * (cond - uncond)
    """
    return noise_pred_uncond + guidance_scale * (noise_pred_cond - noise_pred_uncond)

🧪 Runnable Assertions & Validation

Copy and run directly in Python / Jupyter:

import numpy as np
uncond = np.array([1.0, 2.0])
cond = np.array([1.5, 3.0])
# diff = [0.5, 1.0], scale=7.5 -> 1.0 + 3.75 = 4.75, 2.0 + 7.5 = 9.5
cfg_out = apply_classifier_free_guidance(uncond, cond, guidance_scale=7.5)
assert np.allclose(cfg_out, [4.75, 9.5])
print("✓ CFG 引导计算自测通过")

🎯 Core Follow-up Inquiries & Reference Answers

Q1:CFG doubles forward inference compute per step. How do production pipelines reduce this cost?
Techniques include scheduled CFG (applying CFG only at mid/high-noise structure-forming steps), single-pass attention perturbations, and step/CFG distillation (such as LCM or Turbo models).
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
← Prev: DDPM Reverse Denoising Sampling StepBack to Part K & All 105Next: Flow Matching Velocity Field Objective →