<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Decoding on Tarragon</title><link>https://tarrragon.github.io/blog/tags/decoding/</link><description>Recent content in Decoding on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Thu, 14 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/tags/decoding/index.xml" rel="self" type="application/rss+xml"/><item><title>Sampling Constraint</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/sampling-constraint/</link><pubDate>Thu, 14 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/sampling-constraint/</guid><description>&lt;p>Sampling constraint（sampling 約束）的核心概念是「&lt;strong>在模型選下一個 token 時，限制哪些 token 可以被選到&lt;/strong>」。模型 forward pass 產生每個 token 的 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/logit/" data-link-title="Logit" data-link-desc="softmax 之前的原始實數分數、每個 vocab token 一個值、可正可負">logit&lt;/a>，sampling 約束在取樣前調整候選集合或機率，讓輸出符合格式、選項或安全邊界。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>Sampling 約束屬於推論階段，不修改模型權重，也不等於模型真的理解規則。常見控制手段有 temperature、top-p / top-k、logit bias、grammar mask、JSON mode 與 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/constrained-decoding/" data-link-title="Constrained Decoding" data-link-desc="推論時用 grammar 強制 LLM 輸出符合特定格式（JSON / regex / CFG）的 sampling 機制、把不合法 token 的機率歸零">constrained decoding&lt;/a>；其中 grammar mask 是 structured output 最關鍵的一類。&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">prompt → model forward pass → logits
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> ↓
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">sampling constraint：調整候選 token / logit / 機率
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> ↓
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">sample next token → append → 下一輪&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="可觀察訊號與例子">可觀察訊號與例子&lt;/h2>
&lt;p>看到「低 temperature 讓答案更穩」「top-p 過濾長尾 token」「logit bias 禁止某個 token」「grammar mask 只允許合法 JSON token」就是 sampling 約束。例子是 enum 分類：如果合法答案只有 &lt;code>billing&lt;/code>、&lt;code>technical&lt;/code>、&lt;code>other&lt;/code>，推論伺服器可以在輸出欄位值的位置只允許這幾組 token 的路徑。&lt;/p>
&lt;p>Sampling 約束的風險是把模型逼到錯誤但合法的輸出。當 grammar 太窄、enum 缺少 &lt;code>unknown&lt;/code>、schema 沒有容納例外狀態時，模型可能輸出看似可解析但語意不可信的值；這時要加 fallback、confidence 或人工覆核路由。&lt;/p>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>Sampling 約束適合處理格式合法性與候選空間控制，不適合單獨承擔事實正確性。設計時先問三件事：合法 token 集合能否完整表示業務狀態、約束失敗時要 retry 還是回退、下游 validator 如何分辨「格式合法但語意可疑」。下一步路由是 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/structured-output/" data-link-title="Structured Output" data-link-desc="讓 LLM 輸出可被 parser 穩定消費的推論階段設計：JSON mode、schema-guided decoding、grammar 約束都屬於這一層">Structured Output&lt;/a> 與 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">Top-K / Top-P / Min-P Sampling&lt;/a>。&lt;/p></description><content:encoded><![CDATA[<p>Sampling constraint（sampling 約束）的核心概念是「<strong>在模型選下一個 token 時，限制哪些 token 可以被選到</strong>」。模型 forward pass 產生每個 token 的 <a href="/blog/llm/knowledge-cards/logit/" data-link-title="Logit" data-link-desc="softmax 之前的原始實數分數、每個 vocab token 一個值、可正可負">logit</a>，sampling 約束在取樣前調整候選集合或機率，讓輸出符合格式、選項或安全邊界。</p>
<h2 id="概念位置">概念位置</h2>
<p>Sampling 約束屬於推論階段，不修改模型權重，也不等於模型真的理解規則。常見控制手段有 temperature、top-p / top-k、logit bias、grammar mask、JSON mode 與 <a href="/blog/llm/knowledge-cards/constrained-decoding/" data-link-title="Constrained Decoding" data-link-desc="推論時用 grammar 強制 LLM 輸出符合特定格式（JSON / regex / CFG）的 sampling 機制、把不合法 token 的機率歸零">constrained decoding</a>；其中 grammar mask 是 structured output 最關鍵的一類。</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">prompt → model forward pass → logits
</span></span><span class="line"><span class="ln">2</span><span class="cl">  ↓
</span></span><span class="line"><span class="ln">3</span><span class="cl">sampling constraint：調整候選 token / logit / 機率
</span></span><span class="line"><span class="ln">4</span><span class="cl">  ↓
</span></span><span class="line"><span class="ln">5</span><span class="cl">sample next token → append → 下一輪</span></span></code></pre></div><h2 id="可觀察訊號與例子">可觀察訊號與例子</h2>
<p>看到「低 temperature 讓答案更穩」「top-p 過濾長尾 token」「logit bias 禁止某個 token」「grammar mask 只允許合法 JSON token」就是 sampling 約束。例子是 enum 分類：如果合法答案只有 <code>billing</code>、<code>technical</code>、<code>other</code>，推論伺服器可以在輸出欄位值的位置只允許這幾組 token 的路徑。</p>
<p>Sampling 約束的風險是把模型逼到錯誤但合法的輸出。當 grammar 太窄、enum 缺少 <code>unknown</code>、schema 沒有容納例外狀態時，模型可能輸出看似可解析但語意不可信的值；這時要加 fallback、confidence 或人工覆核路由。</p>
<h2 id="設計責任">設計責任</h2>
<p>Sampling 約束適合處理格式合法性與候選空間控制，不適合單獨承擔事實正確性。設計時先問三件事：合法 token 集合能否完整表示業務狀態、約束失敗時要 retry 還是回退、下游 validator 如何分辨「格式合法但語意可疑」。下一步路由是 <a href="/blog/llm/knowledge-cards/structured-output/" data-link-title="Structured Output" data-link-desc="讓 LLM 輸出可被 parser 穩定消費的推論階段設計：JSON mode、schema-guided decoding、grammar 約束都屬於這一層">Structured Output</a> 與 <a href="/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">Top-K / Top-P / Min-P Sampling</a>。</p>
]]></content:encoded></item><item><title>Beam Search</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/beam-search/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/beam-search/</guid><description>&lt;p>Beam search 的核心概念是「&lt;strong>每步同時保留 K 條最有機率的候選 sequence（beam width = K）、最終挑一條總機率最高的當輸出&lt;/strong>」。相比 greedy decoding 只保一條、beam search 能探索更多可能、避免「貪心一時、累積失誤」；但對話 / coding 場景常出現副作用、是 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">top-p sampling&lt;/a> 取代它的原因。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>Beam search 跟其他 decoding 策略的對比：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>策略&lt;/th>
 &lt;th>機制&lt;/th>
 &lt;th>適合場景&lt;/th>
 &lt;th>LLM 常見性&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Greedy&lt;/td>
 &lt;td>每步選機率最大的 token&lt;/td>
 &lt;td>確定性任務、debugging&lt;/td>
 &lt;td>高&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;strong>Beam search (K)&lt;/strong>&lt;/td>
 &lt;td>維護 K 條候選、最後挑總機率最高的&lt;/td>
 &lt;td>機器翻譯、summarization、有「正確答案」的任務&lt;/td>
 &lt;td>中（傳統 NLP 主流）&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Top-k / top-p / min-p&lt;/td>
 &lt;td>從機率分佈隨機取樣（限制候選範圍）&lt;/td>
 &lt;td>對話、寫作、coding、創意輸出&lt;/td>
 &lt;td>高（LLM 主流）&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>Beam search 的算法直覺：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">beam_width = 3
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">Step 1：從機率分佈挑前 3 個 token、得到 3 條 partial sequence
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">Step 2：每條 partial 各自展開所有可能下個 token、組合機率排序、保留前 3
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">Step 3：重複 Step 2、直到所有 beam 都遇到 EOS 或達到 max_length
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">Final：選總 log-probability 最高的 beam 當輸出&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Beam search 在 LLM chat / coding 場景的副作用：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>輸出偏 boilerplate&lt;/strong>：K 個 beam 容易收斂到同樣的高頻開頭（「Sure!」「That&amp;rsquo;s a great question」）、各 beam 平均化掉原本該有的多樣性。&lt;/li>
&lt;li>&lt;strong>缺乏隨機性&lt;/strong>：給同 prompt 永遠生同輸出、缺乏寫作 / 創意任務需要的變化。&lt;/li>
&lt;li>&lt;strong>計算貴&lt;/strong>：K 倍記憶體 + K 倍 forward pass。&lt;/li>
&lt;/ol>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>讀 inference framework 看到 &lt;code>num_beams: 1&lt;/code> 預設值就是用 greedy/sampling、&lt;code>num_beams: 5&lt;/code> 才會開 beam search。寫 code 場景的判讀：日常用 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">top-p sampling&lt;/a> 為主、需要確定性測試用 greedy、需要「在多個候選中挑最好的」用 best-of-N（每個獨立 sample、再選 reward 最高）而非 beam search。Beam search 在現代 LLM chat 場景已經少用、但在 translation / structured output 等「有正確答案」場景仍見。&lt;/p></description><content:encoded><![CDATA[<p>Beam search 的核心概念是「<strong>每步同時保留 K 條最有機率的候選 sequence（beam width = K）、最終挑一條總機率最高的當輸出</strong>」。相比 greedy decoding 只保一條、beam search 能探索更多可能、避免「貪心一時、累積失誤」；但對話 / coding 場景常出現副作用、是 <a href="/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">top-p sampling</a> 取代它的原因。</p>
<h2 id="概念位置">概念位置</h2>
<p>Beam search 跟其他 decoding 策略的對比：</p>
<table>
  <thead>
      <tr>
          <th>策略</th>
          <th>機制</th>
          <th>適合場景</th>
          <th>LLM 常見性</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Greedy</td>
          <td>每步選機率最大的 token</td>
          <td>確定性任務、debugging</td>
          <td>高</td>
      </tr>
      <tr>
          <td><strong>Beam search (K)</strong></td>
          <td>維護 K 條候選、最後挑總機率最高的</td>
          <td>機器翻譯、summarization、有「正確答案」的任務</td>
          <td>中（傳統 NLP 主流）</td>
      </tr>
      <tr>
          <td>Top-k / top-p / min-p</td>
          <td>從機率分佈隨機取樣（限制候選範圍）</td>
          <td>對話、寫作、coding、創意輸出</td>
          <td>高（LLM 主流）</td>
      </tr>
  </tbody>
</table>
<p>Beam search 的算法直覺：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">beam_width = 3
</span></span><span class="line"><span class="ln">2</span><span class="cl">Step 1：從機率分佈挑前 3 個 token、得到 3 條 partial sequence
</span></span><span class="line"><span class="ln">3</span><span class="cl">Step 2：每條 partial 各自展開所有可能下個 token、組合機率排序、保留前 3
</span></span><span class="line"><span class="ln">4</span><span class="cl">Step 3：重複 Step 2、直到所有 beam 都遇到 EOS 或達到 max_length
</span></span><span class="line"><span class="ln">5</span><span class="cl">Final：選總 log-probability 最高的 beam 當輸出</span></span></code></pre></div><p>Beam search 在 LLM chat / coding 場景的副作用：</p>
<ol>
<li><strong>輸出偏 boilerplate</strong>：K 個 beam 容易收斂到同樣的高頻開頭（「Sure!」「That&rsquo;s a great question」）、各 beam 平均化掉原本該有的多樣性。</li>
<li><strong>缺乏隨機性</strong>：給同 prompt 永遠生同輸出、缺乏寫作 / 創意任務需要的變化。</li>
<li><strong>計算貴</strong>：K 倍記憶體 + K 倍 forward pass。</li>
</ol>
<h2 id="設計責任">設計責任</h2>
<p>讀 inference framework 看到 <code>num_beams: 1</code> 預設值就是用 greedy/sampling、<code>num_beams: 5</code> 才會開 beam search。寫 code 場景的判讀：日常用 <a href="/blog/llm/knowledge-cards/top-p-sampling/" data-link-title="Top-K / Top-P / Min-P Sampling" data-link-desc="從機率分佈取樣前先過濾低機率 token 的三種策略、現代 LLM 推論主流">top-p sampling</a> 為主、需要確定性測試用 greedy、需要「在多個候選中挑最好的」用 best-of-N（每個獨立 sample、再選 reward 最高）而非 beam search。Beam search 在現代 LLM chat 場景已經少用、但在 translation / structured output 等「有正確答案」場景仍見。</p>
]]></content:encoded></item><item><title>Top-K / Top-P / Min-P Sampling</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/top-p-sampling/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/top-p-sampling/</guid><description>&lt;p>Top-K、Top-P（nucleus sampling）、Min-P 的核心概念是「&lt;strong>從 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/softmax/" data-link-title="Softmax" data-link-desc="把任意實數向量正規化成「總和為 1、每個分量 ∈ [0,1]」的機率分佈">softmax&lt;/a> 出來的機率分佈中、先過濾掉低機率 token、再從剩餘候選隨機取樣&lt;/strong>」。三者是 LLM 對話 / 寫 code 場景的主流 sampling 策略、跟 greedy 對比保留隨機多樣性、跟 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/beam-search/" data-link-title="Beam Search" data-link-desc="同時保留 K 條候選 sequence 的 decoding 策略、機器翻譯主流、chat / coding 場景慎用">beam search&lt;/a> 對比計算成本低。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>三種策略的篩選方式：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>策略&lt;/th>
 &lt;th>機制&lt;/th>
 &lt;th>直覺&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>&lt;strong>Top-K&lt;/strong>&lt;/td>
 &lt;td>只保留機率前 K 個 token、其餘設 0&lt;/td>
 &lt;td>固定候選數量、簡單&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;strong>Top-P&lt;/strong>&lt;/td>
 &lt;td>把 token 依機率排序、保留「累積機率達到 P」的最小集合&lt;/td>
 &lt;td>動態候選數量、適應分佈尖銳度&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;strong>Min-P&lt;/strong>&lt;/td>
 &lt;td>只保留機率 ≥ (P × max_probability) 的 token&lt;/td>
 &lt;td>相對閾值、避免低品質 token&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>範例（vocab 前 10 個 token 的機率）：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">token: A B C D E F G H I J
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">prob: 0.45 0.30 0.12 0.05 0.03 0.02 0.01 0.01 0.005 0.005
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">Top-K=3：保留 A、B、C（前 3 個）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">Top-P=0.9：累積機率達 0.9、保留 A、B、C、D（0.45+0.30+0.12+0.05 = 0.92）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">Min-P=0.1：max=0.45、閾值=0.045、保留 A、B、C、D（≥ 0.045）&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>三者實務上常組合使用（如 &lt;code>top_k=40, top_p=0.9, temperature=0.7&lt;/code>）、各自處理不同形狀的分佈。&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>參數情境&lt;/th>
 &lt;th>適合策略&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>分佈非常尖（模型很確定）&lt;/td>
 &lt;td>Top-P / Min-P 動態縮小、Top-K 可能太大&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>分佈平（模型不確定）&lt;/td>
 &lt;td>Top-K 限制最大候選、避免取到極低品質 token&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>寫 code / 嚴謹任務&lt;/td>
 &lt;td>低 temperature (0.2 ~ 0.5) + 較緊的 Top-P (0.8 ~ 0.9)&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>創意 / 多樣寫作&lt;/td>
 &lt;td>高 temperature (0.7 ~ 1.0) + 寬鬆的 Top-P (0.95+)&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>讀 inference config / Continue.dev 設定看到 &lt;code>top_k&lt;/code>、&lt;code>top_p&lt;/code>、&lt;code>min_p&lt;/code>、&lt;code>temperature&lt;/code> 就是這組參數。寫 code 場景的判讀：嚴謹任務（code generation、structured output）用低 temperature + 緊 Top-P 取「最可能對的少數 token」；創意 / 對話用高 temperature + 寬 Top-P 取多樣性。Min-P 是 2023 後流行的新策略、實務上比 Top-P 更穩、避免「分佈很尖時 Top-P 仍納入長尾低品質 token」的問題。&lt;/p></description><content:encoded><![CDATA[<p>Top-K、Top-P（nucleus sampling）、Min-P 的核心概念是「<strong>從 <a href="/blog/llm/knowledge-cards/softmax/" data-link-title="Softmax" data-link-desc="把任意實數向量正規化成「總和為 1、每個分量 ∈ [0,1]」的機率分佈">softmax</a> 出來的機率分佈中、先過濾掉低機率 token、再從剩餘候選隨機取樣</strong>」。三者是 LLM 對話 / 寫 code 場景的主流 sampling 策略、跟 greedy 對比保留隨機多樣性、跟 <a href="/blog/llm/knowledge-cards/beam-search/" data-link-title="Beam Search" data-link-desc="同時保留 K 條候選 sequence 的 decoding 策略、機器翻譯主流、chat / coding 場景慎用">beam search</a> 對比計算成本低。</p>
<h2 id="概念位置">概念位置</h2>
<p>三種策略的篩選方式：</p>
<table>
  <thead>
      <tr>
          <th>策略</th>
          <th>機制</th>
          <th>直覺</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>Top-K</strong></td>
          <td>只保留機率前 K 個 token、其餘設 0</td>
          <td>固定候選數量、簡單</td>
      </tr>
      <tr>
          <td><strong>Top-P</strong></td>
          <td>把 token 依機率排序、保留「累積機率達到 P」的最小集合</td>
          <td>動態候選數量、適應分佈尖銳度</td>
      </tr>
      <tr>
          <td><strong>Min-P</strong></td>
          <td>只保留機率 ≥ (P × max_probability) 的 token</td>
          <td>相對閾值、避免低品質 token</td>
      </tr>
  </tbody>
</table>
<p>範例（vocab 前 10 個 token 的機率）：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">token:     A     B     C     D     E     F     G     H     I     J
</span></span><span class="line"><span class="ln">2</span><span class="cl">prob:    0.45  0.30  0.12  0.05  0.03  0.02  0.01  0.01  0.005 0.005
</span></span><span class="line"><span class="ln">3</span><span class="cl">
</span></span><span class="line"><span class="ln">4</span><span class="cl">Top-K=3：保留 A、B、C（前 3 個）
</span></span><span class="line"><span class="ln">5</span><span class="cl">Top-P=0.9：累積機率達 0.9、保留 A、B、C、D（0.45+0.30+0.12+0.05 = 0.92）
</span></span><span class="line"><span class="ln">6</span><span class="cl">Min-P=0.1：max=0.45、閾值=0.045、保留 A、B、C、D（≥ 0.045）</span></span></code></pre></div><p>三者實務上常組合使用（如 <code>top_k=40, top_p=0.9, temperature=0.7</code>）、各自處理不同形狀的分佈。</p>
<table>
  <thead>
      <tr>
          <th>參數情境</th>
          <th>適合策略</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>分佈非常尖（模型很確定）</td>
          <td>Top-P / Min-P 動態縮小、Top-K 可能太大</td>
      </tr>
      <tr>
          <td>分佈平（模型不確定）</td>
          <td>Top-K 限制最大候選、避免取到極低品質 token</td>
      </tr>
      <tr>
          <td>寫 code / 嚴謹任務</td>
          <td>低 temperature (0.2 ~ 0.5) + 較緊的 Top-P (0.8 ~ 0.9)</td>
      </tr>
      <tr>
          <td>創意 / 多樣寫作</td>
          <td>高 temperature (0.7 ~ 1.0) + 寬鬆的 Top-P (0.95+)</td>
      </tr>
  </tbody>
</table>
<h2 id="設計責任">設計責任</h2>
<p>讀 inference config / Continue.dev 設定看到 <code>top_k</code>、<code>top_p</code>、<code>min_p</code>、<code>temperature</code> 就是這組參數。寫 code 場景的判讀：嚴謹任務（code generation、structured output）用低 temperature + 緊 Top-P 取「最可能對的少數 token」；創意 / 對話用高 temperature + 寬 Top-P 取多樣性。Min-P 是 2023 後流行的新策略、實務上比 Top-P 更穩、避免「分佈很尖時 Top-P 仍納入長尾低品質 token」的問題。</p>
]]></content:encoded></item></channel></rss>