Files
2nd/10_Wiki/Topics/Domain_Programming/AI_and_ML/CFG 스케일(Classifier-Free Guidance Scale).md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +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