←Back to Science Coding Hub/Part K/K3
K3Easy★ Core Essential · Industrial BedrockPart K · Generative Models & Diffusion

Classifier-Free Guidance (CFG)

Industrial-grade implementation and mathematical foundations of Classifier-Free Guidance (CFG).

⏱️ Time Complexity: O(B * C * H * W) 前向计算量翻倍
💾 Space Complexity: 显存批次翻倍
💡

Core Mental Anchor / Mnemonic

Master Classifier-Free Guidance (CFG): enforce numerical stability, check tensor shapes, and eliminate redundant memory allocations.

📐 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)
### Mathematical Derivation & Theoretical Principles
Detailed first-principles formulation and architectural mechanics for Classifier-Free Guidance (CFG).

Refer to the LaTeX equation above for the core operator definition. The operator is designed to ensure strict numerical bounds, avoiding floating-point overflows and gradient anomalies.

🔄 Tensor Dimensions & Shape Flow

noise_uncond, noise_cond -> 差值 * scale -> 叠加回 uncond -> 引导后最终预测噪声

🛡️ Industrial Numerical Stability & Pitfalls

  • Ensure proper multi-dimensional tensor broadcasting and keepdims retention.
  • Enforce numerical guards (eps clamping and overflow thresholds) during exponentiation and division.
  • Verify train versus eval mode behavioral distinctions (e.g. frozen running statistics and dropout bypass).

💻 Industrial 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 to verify correctness:

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 Architecture Follow-up Q&A

Q1:What are the key trade-offs and memory bottlenecks when deploying Classifier-Free Guidance (CFG) in high-throughput inference?
Memory bandwidth (HBM to SRAM I/O) is the primary latency factor. Fusing element-wise operations and avoiding intermediate tensor materialization significantly outperforms naive implementations.
Q2:How does Classifier-Free Guidance (CFG) handle extreme numerical boundaries or precision reduction (FP16/BF16/INT8)?
Under low precision, operations must be upcasted to FP32 during accumulation to prevent underflow/overflow, followed by proper scaling and clamping before converting back to the target format.
←Prev: DDPM Reverse Denoising Sampling StepAll 69 KernelsNext: Flow Matching Velocity Field Objective→