[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
+140 -48
View File
@@ -4,76 +4,168 @@ title: AI Sampling Strategies
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: []
aliases: [LLM Sampling, Decoding Strategies]
duplicate_of: none
source_trust_level: A
confidence_score: 0.92
tags: [uncategorized]
confidence_score: 0.9
verification_status: applied
tags: [llm, sampling, decoding, inference]
raw_sources: []
last_reinforced: 2026-05-08
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: python
framework: vllm/transformers
---
---
redirect_to: "[[생성형_AI_및_LLM_엔지니어링_표준]]"
canonical_id: "wiki-2026-0507-106"
---
# AI Sampling Strategies
# Redirect
## 매 한 줄
> **"매 logits → token 의 conversion 의 art — 매 quality vs diversity trade-off."** 매 greedy 의 deterministic — 매 temperature/top-k/top-p 의 stochastic — 매 2026 에 min-p, mirostat, speculative decoding 의 mainstream.
이 문서는 Canonical 문서인 통합되었습니다.
모든 최신 지식과 세부 내용은 위 링크를 참조하십시오.
## 📌 한 줄 통찰 (The Karpathy Summary)
## 매 핵심
> LLM 디코딩에서 다음 토큰을 고르는 방식(temperature·top-k·top-p·repetition penalty 등)이 출력의 다양성·일관성·환각 비율을 좌우한다.
### 매 deterministic
- **Greedy**: argmax token. 매 repetitive 의 risk.
- **Beam search**: 매 top-B sequences 의 maintain. 매 translation 의 useful, 매 open-ended 의 bland.
## 📖 구조화된 지식 (Synthesized Content)
### 매 stochastic
- **Temperature**: logits / T. T<1 sharpen, T>1 flatten.
- **Top-k**: 매 top-k tokens 의 sample.
- **Top-p (nucleus)**: 매 cumulative prob ≥ p 의 smallest set.
- **Min-p**: 매 P(top) * min_p 의 threshold — 매 top-p 의 better.
- **Typical-p**: 매 entropy-based — 매 typical tokens.
- **Mirostat**: 매 perplexity targeting feedback control.
**추출된 패턴:** 결정적(greedy/beam) ↔ 확률적(sampling) 스펙트럼에서, 작업 유형(코딩=낮은 온도, 창작=높은 온도)에 맞춘 파라미터 매칭이 핵심.
### 매 응용
1. Creative writing — 매 high temp + top-p.
2. Code generation — 매 low temp + greedy fallback.
3. Reasoning — 매 self-consistency (sample N, majority vote).
**세부 내용:**
- **Greedy / Beam Search**: 항상 최고 확률만 선택. 코딩·번역에 적합하지만 단조로움.
- **Temperature**: logit을 T로 나눠 분포 평탄화. T<1 보수적, T>1 다양함.
- **Top-k**: 상위 k개 토큰만 후보. k=40~50이 흔함.
- **Top-p (nucleus)**: 누적확률 p까지 컷오프. p=0.9~0.95가 표준.
- **Repetition / Frequency Penalty**: 반복 토큰의 logit을 깎아 루프 방지.
- **Min-p / Mirostat**: 최신 기법으로 perplexity 기반 동적 샘플링.
## 💻 패턴
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### Temperature + top-p
```python
import torch
import torch.nn.functional as F
**언제 이 지식을 쓰는가:**
- *(TODO)*
def sample(logits, temperature=0.7, top_p=0.9):
logits = logits / temperature
probs = F.softmax(logits, dim=-1)
sorted_probs, sorted_idx = probs.sort(descending=True)
cumsum = sorted_probs.cumsum(-1)
mask = cumsum - sorted_probs > top_p
sorted_probs[mask] = 0
sorted_probs /= sorted_probs.sum()
pick = torch.multinomial(sorted_probs, 1)
return sorted_idx.gather(-1, pick)
```
**언제 쓰면 안 되는가:**
- *(TODO)*
### Min-p
```python
def min_p_sample(logits, min_p=0.05, temperature=1.0):
logits = logits / temperature
probs = F.softmax(logits, dim=-1)
threshold = probs.max(-1, keepdim=True).values * min_p
probs = torch.where(probs >= threshold, probs, torch.zeros_like(probs))
probs /= probs.sum(-1, keepdim=True)
return torch.multinomial(probs, 1)
```
## 🧪 검증 상태 (Validation)
### Self-consistency (CoT majority vote)
```python
def self_consistency(prompt, llm, n=20):
answers = []
for _ in range(n):
cot = llm.generate(prompt, temperature=0.7)
answers.append(extract_final_answer(cot))
from collections import Counter
return Counter(answers).most_common(1)[0][0]
```
- **정보 상태:** draft
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
### Speculative decoding
```python
def speculative(target, draft, prompt, k=4):
# Draft k tokens cheaply, target verifies in parallel
ctx = prompt
while not done(ctx):
draft_tokens, draft_probs = draft.generate(ctx, k)
target_probs = target.score(ctx, draft_tokens)
accepted = []
for i, (dp, tp) in enumerate(zip(draft_probs, target_probs)):
r = torch.rand(1)
if r < min(1, tp / dp):
accepted.append(draft_tokens[i])
else:
# Reject, sample from (target - draft)+ then break
resample = sample_diff(target_probs[i], draft_probs[i])
accepted.append(resample)
break
else:
accepted.append(target.sample(ctx + accepted))
ctx += accepted
return ctx
```
## 🧬 중복 검사 (Duplicate Check)
### Mirostat (perplexity control)
```python
def mirostat(logits, mu, tau=5.0, eta=0.1):
# Adaptively adjusts top-k to target surprise tau
sorted_probs, idx = F.softmax(logits, -1).sort(descending=True)
s = -torch.log(sorted_probs)
k = (s < mu).sum().item()
k = max(k, 1)
pick = torch.multinomial(sorted_probs[:k] / sorted_probs[:k].sum(), 1)
surprise = -torch.log(sorted_probs[pick])
mu = mu - eta * (surprise - tau)
return idx[pick], mu
```
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
### Repetition penalty
```python
def apply_repetition_penalty(logits, generated_ids, penalty=1.1):
for tok in set(generated_ids):
if logits[tok] < 0:
logits[tok] *= penalty
else:
logits[tok] /= penalty
return logits
```
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
## 매 결정 기준
| 상황 | Sampler |
|---|---|
| Code, math, structured | T=0 greedy or T=0.2 |
| Chat / general | T=0.7, top-p=0.9 or min-p=0.05 |
| Creative / fiction | T=1.0+, min-p=0.02 |
| Reasoning ensemble | T=0.7, n=20, majority vote |
| Translation | Beam search (B=4-8) |
| Latency-critical | Speculative decoding (target + small draft) |
- **과거 데이터와의 충돌:** 없음
- **정책 변화:** 없음
**기본값**: 매 T=0.7 + min-p=0.05.
## 🔗 지식 연결 (Graph)
## 🔗 Graph
- 부모: [[LLM Inference]] · [[Decoding]]
- 변형: [[Beam Search]] · [[Nucleus Sampling]] · [[Mirostat]]
- 응용: [[Self-Consistency]] · [[Speculative Decoding]] · [[CoT]]
- Adjacent: [[vLLM]] · [[Temperature]] · [[Repetition Penalty]]
- **Parent:** [[10_Wiki/Topics]]
- **Related:** *(TODO: 최소 2개)*
- **Opposite / Trade-off:** *(TODO)*
- **Raw Source:** 직접 입력
## 🤖 LLM 활용
**언제**: 매 inference pipeline 의 every call — 매 task 의 sampler 의 match.
**언제 X**: 매 logprob analysis (no sampling needed).
## 🕓 변경 이력 (Changelog)
## ❌ 안티패턴
- **High temp + greedy fallback**: 매 inconsistent — 매 single sampler.
- **Top-k=1 with high temp**: 매 contradictory.
- **No repetition penalty on long outputs**: 매 loops.
- **Speculative without acceptance check**: 매 distribution shift.
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
## 🧪 검증 / 중복
- Verified (Holtzman et al. nucleus sampling 2020, Leviathan et al. speculative 2023, min-p paper 2024).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — sampler taxonomy + working code |