---
id: wiki-2026-0508-cfg-scale-classifier-free-guidance
title: CFG Scale (Classifier-Free Guidance)
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [CFG, classifier-free guidance, guidance scale, prompt strength, negative prompt, conditioning strength]
duplicate_of: none
source_trust_level: A
confidence_score: 0.93
verification_status: applied
tags: [diffusion, stable-diffusion, cfg, guidance, sampling, prompt-engineering, dpm-solver, conditioning]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: Python
  framework: Diffusers / ComfyUI / Automatic1111
---

# CFG Scale (Classifier-Free Guidance)

## 📌 한 줄 통찰
> **"매 prompt 의 strict 의 dial"**. 매 diffusion 의 generation 의 매 conditioned (prompt) ↔ 매 unconditional 의 trade-off. 매 high CFG = 매 prompt 의 strict 가, 매 over-saturation. 매 sweet spot 7-9 (SDXL) / 3.5-7 (Flux).

## 📖 핵심

### 매 formula
$$\epsilon_{\text{guided}} = \epsilon_{\text{uncond}} + s \cdot (\epsilon_{\text{cond}} - \epsilon_{\text{uncond}})$$

- 매 s = CFG scale.
- 매 s = 1 → 매 unconditional (prompt 무시).
- 매 s = 7 → 매 typical.
- 매 s > 15 → 매 over-cooked.
- 매 negative prompt = 매 conditional 의 두 번째 (with -1 coefficient).

### 매 effect

| CFG | 결과 |
|---|---|
| < 1 | 매 random / blank |
| 1-3 | 매 loose, 매 creative |
| 5-7 | 매 balanced (default) |
| 7-12 | 매 prompt-strict |
| 13-20 | 매 over-saturated, 매 burned |
| > 20 | 매 garbage |

### 매 modern alternative

#### Flux (Black Forest Labs)
- 매 distilled CFG (CFG=1 가능).
- 매 inference 의 fast (no double pass).

#### Negative prompt
- 매 unconditional 의 noise 의 swap.
- 매 explicit avoidance.

#### Dynamic CFG
- 매 step 의 따라 변동.
- 매 early high → 매 late low.

#### Adaptive CFG (CFG++)
- 매 adaptive scale.
- 매 over-saturation 회피.

### 매 sampler 와 의 interaction
- 매 DPM++ 2M Karras: 20 step + CFG 7.
- 매 DPM++ SDE: 30 step + CFG 5.
- 매 Euler ancestral: 매 stochastic.
- 매 Flux: CFG=1 + 4-step.

### 매 prompt quality 와 의 관계
- 매 좋은 prompt + CFG 7 = 매 best.
- 매 나쁜 prompt + CFG ↑ = 매 더 나쁘게 (confident garbage).
- 매 negative prompt 의 keyword 매 wrong → 매 오히려 push.

→ **CFG ↑ ≠ 매 quality ↑**. 매 prompt quality 가 base.

### 매 typical setup
| 모델 | CFG | Steps | Sampler |
|---|---|---|---|
| SD 1.5 | 7-12 | 20-30 | DPM++ 2M Karras |
| SDXL | 7-9 | 20-30 | DPM++ 2M Karras |
| SDXL Turbo | 1 | 1-4 | Euler |
| Flux Dev | 3.5 | 20-50 | Euler |
| Flux Schnell | 1 | 4 | Euler |

## 💻 패턴

### Diffusers (basic)
```python
from diffusers import StableDiffusionXLPipeline
import torch

pipe = StableDiffusionXLPipeline.from_pretrained(
    'stabilityai/stable-diffusion-xl-base-1.0',
    torch_dtype=torch.float16,
).to('cuda')

image = pipe(
    prompt='a cat with a hat, oil painting, vivid colors',
    negative_prompt='blurry, low quality, watermark',
    guidance_scale=7.0,  # 매 CFG
    num_inference_steps=30,
).images[0]
```

### CFG sweep (find sweet spot)
```python
import matplotlib.pyplot as plt

cfg_values = [1, 3, 5, 7, 9, 12, 15, 20]
fig, axes = plt.subplots(2, 4, figsize=(20, 10))

for ax, cfg in zip(axes.flat, cfg_values):
    image = pipe(prompt=prompt, guidance_scale=cfg, generator=torch.manual_seed(42)).images[0]
    ax.imshow(image)
    ax.set_title(f'CFG={cfg}')
    ax.axis('off')
plt.show()
```

### Flux (CFG=1, distilled)
```python
from diffusers import FluxPipeline

pipe = FluxPipeline.from_pretrained('black-forest-labs/FLUX.1-dev', torch_dtype=torch.bfloat16).to('cuda')

image = pipe(
    prompt='a cat with a hat',
    guidance_scale=3.5,  # Flux dev
    num_inference_steps=50,
).images[0]

# Schnell (4-step, distilled)
pipe_schnell = FluxPipeline.from_pretrained('black-forest-labs/FLUX.1-schnell', torch_dtype=torch.bfloat16).to('cuda')
image = pipe_schnell(
    prompt=prompt,
    guidance_scale=0,
    num_inference_steps=4,
).images[0]
```

### Dynamic CFG
```python
def dynamic_cfg_callback(pipe, step, timestep, callback_kwargs):
    """매 early step 의 high CFG, 매 late 의 low."""
    progress = step / pipe.num_inference_steps
    cfg = 12 - 7 * progress  # 12 → 5
    callback_kwargs['guidance_scale'] = cfg
    return callback_kwargs

pipe(prompt=prompt, callback_on_step_end=dynamic_cfg_callback)
```

### Custom CFG implementation
```python
def classifier_free_guidance(model, x_t, t, prompt_emb, neg_prompt_emb, scale):
    # 매 batched: cond + uncond
    emb_combined = torch.cat([neg_prompt_emb, prompt_emb])
    x_t_combined = torch.cat([x_t, x_t])
    
    eps_combined = model(x_t_combined, t, emb_combined)
    eps_uncond, eps_cond = eps_combined.chunk(2)
    
    # 매 guided
    return eps_uncond + scale * (eps_cond - eps_uncond)
```

### CFG++ (adaptive)
```python
def cfg_pp(eps_cond, eps_uncond, scale, x_t, alpha_t):
    """CFG++ — 매 over-saturation 회피."""
    cfg_basic = eps_uncond + scale * (eps_cond - eps_uncond)
    # 매 sample-space adjustment
    delta = (cfg_basic - eps_uncond) * (1 - alpha_t)
    return eps_uncond + scale * (eps_cond - eps_uncond) - delta
```

### Negative prompt strategy
```python
# 매 좋은 negative prompt
negative_prompts = {
    'photorealistic': 'cartoon, anime, painting, drawing',
    'illustration': 'photo, photograph, photographic',
    'quality': 'blurry, low quality, jpeg artifacts, watermark, signature, deformed, ugly',
    'anatomy': 'extra limbs, deformed hands, missing fingers, distorted face',
}

prompt = 'a portrait of a woman'
style = 'photorealistic'
neg = negative_prompts['quality'] + ', ' + negative_prompts[style]

image = pipe(prompt=prompt, negative_prompt=neg, guidance_scale=7).images[0]
```

## 🤔 결정 기준
| 상황 | CFG |
|---|---|
| Photorealistic | 7-9 |
| Stylized art | 8-12 |
| Creative / loose | 3-5 |
| Strict prompt | 10-15 |
| Flux Dev | 3.5 |
| Flux Schnell / SDXL Turbo | 1 |
| Burning / over-saturated | < 7 |

**기본값**: SDXL = 7, Flux = 3.5, Schnell = 1.

## 🔗 Graph
- 부모: [[Diffusion-Models]] · [[AI 이미지 생성 (AI Image Generation)|Image-Generation]]
- 응용: [[Stable-Diffusion]] · [[Flux]] · [[ComfyUI]]
- Adjacent: [[Negative-Prompt]]

## 🤖 LLM 활용
**언제**: 매 image generation tuning. 매 SD / Flux pipeline. 매 quality vs prompt-fidelity trade-off.
**언제 X**: 매 distilled model (CFG=1). 매 deterministic 매 sampler.

## ❌ 안티패턴
- **CFG 의 high 의 mean fix**: 매 over-saturation.
- **Negative prompt 의 wrong word + high CFG**: 매 confident garbage.
- **Same CFG 의 모든 model**: 매 distilled vs base 의 다름.
- **Sampler 의 mismatch**: 매 sampler 별 의 sweet spot.
- **CFG = 1 가 prompt 무시**: 매 unconditional.

## 🧪 검증 / 중복
- Verified (Ho & Salimans CFG paper, Flux docs, Diffusers).
- 신뢰도 A.
- Related: [[Stable-Diffusion]] · [[Flux]] · [[Negative-Prompt]] · [[DPM-Solver]] · [[AI 이미지 생성 (AI Image Generation)]].

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — formula + dynamic / Flux / sweep + 매 diffusers code |
