Files
2nd/10_Wiki/Topics/AI_and_ML/CFG 스케일(Classifier-Free Guidance Scale).md
T
koriweb d8a80f6272 chore(wiki): dangling 링크 canonical 정규화 (768파일/1200건)
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해
끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은
과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업.
도구: Datacollect/scripts/link_reconcile_apply.mjs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:24:15 +09:00

7.0 KiB

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

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)

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)

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

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

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)

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

# 매 좋은 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

🤖 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.

🧪 검증 / 중복

🕓 Changelog

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