<?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>Kv-Cache on Tarragon</title><link>https://tarrragon.github.io/blog/tags/kv-cache/</link><description>Recent content in Kv-Cache 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/kv-cache/index.xml" rel="self" type="application/rss+xml"/><item><title>Grouped-Query Attention</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/grouped-query-attention/</link><pubDate>Thu, 14 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/grouped-query-attention/</guid><description>&lt;p>Grouped-query attention（GQA）的核心概念是「&lt;strong>多個 query head 共用較少的 key/value head&lt;/strong>」。它介於 Multi-Head Attention 與 Multi-Query Attention 之間，用較小的品質代價換取更小的 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> 與更好的長 context serving 效率。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>GQA 是 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/multi-head-attention/" data-link-title="Multi-Head Attention" data-link-desc="把 attention 切成多個 head 並行計算、讓模型能同時注意多種模式">multi-head attention&lt;/a> 的推論友善變體。MHA 是每個 query head 都有自己的 K/V；MQA 是所有 query head 共用一組 K/V；GQA 則把 query head 分組，每組共用 K/V。&lt;/p>
&lt;h2 id="可觀察訊號與例子">可觀察訊號與例子&lt;/h2>
&lt;p>在 model config 裡看到 &lt;code>num_attention_heads: 32&lt;/code>、&lt;code>num_key_value_heads: 8&lt;/code>，代表 32 個 Q head 共用 8 組 K/V head，group size 是 4。這會讓 KV cache 約縮到 MHA 的四分之一，長 context 與高併發更友善。&lt;/p>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>選模型或估算 serving 成本時，要看 &lt;code>num_key_value_heads&lt;/code>，而不是只看總參數。GQA 對本地推論特別重要，因為 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/context-window/" data-link-title="Context Window" data-link-desc="模型一次能處理的最大 token 數量：prompt 加生成的總和上限">context window&lt;/a> 與併發數常被 KV cache 卡住。&lt;/p></description><content:encoded><![CDATA[<p>Grouped-query attention（GQA）的核心概念是「<strong>多個 query head 共用較少的 key/value head</strong>」。它介於 Multi-Head Attention 與 Multi-Query Attention 之間，用較小的品質代價換取更小的 <a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> 與更好的長 context serving 效率。</p>
<h2 id="概念位置">概念位置</h2>
<p>GQA 是 <a href="/blog/llm/knowledge-cards/multi-head-attention/" data-link-title="Multi-Head Attention" data-link-desc="把 attention 切成多個 head 並行計算、讓模型能同時注意多種模式">multi-head attention</a> 的推論友善變體。MHA 是每個 query head 都有自己的 K/V；MQA 是所有 query head 共用一組 K/V；GQA 則把 query head 分組，每組共用 K/V。</p>
<h2 id="可觀察訊號與例子">可觀察訊號與例子</h2>
<p>在 model config 裡看到 <code>num_attention_heads: 32</code>、<code>num_key_value_heads: 8</code>，代表 32 個 Q head 共用 8 組 K/V head，group size 是 4。這會讓 KV cache 約縮到 MHA 的四分之一，長 context 與高併發更友善。</p>
<h2 id="設計責任">設計責任</h2>
<p>選模型或估算 serving 成本時，要看 <code>num_key_value_heads</code>，而不是只看總參數。GQA 對本地推論特別重要，因為 <a href="/blog/llm/knowledge-cards/context-window/" data-link-title="Context Window" data-link-desc="模型一次能處理的最大 token 數量：prompt 加生成的總和上限">context window</a> 與併發數常被 KV cache 卡住。</p>
]]></content:encoded></item><item><title>Prefix Cache</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/prefix-cache/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/prefix-cache/</guid><description>&lt;p>Prefix Cache 的核心概念是「當多個請求共用相同的前綴 prompt（如同一 system prompt、同一 few-shot 範例）、把該前綴的 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> 算一次、後續請求共用、省下重複 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prefill/" data-link-title="Prefill" data-link-desc="Prompt 首次處理時的計算階段：把整段輸入跑過模型、產生 KV cache">prefill&lt;/a> 算力」。是 production LLM 服務的常見優化、能大幅降低 latency 跟成本；但在多租戶場景下、跨租戶共用 prefix cache 是直接的隱私洩漏面。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>Prefix Cache 在推論流程中的角色：&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">傳統推論：
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> Request A：system prompt + user A → 完整 prefill → 生成
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl"> Request B：system prompt + user B → 完整 prefill → 生成
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> ↑ 重複算 system prompt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">開啟 Prefix Cache：
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl"> Request A：system prompt + user A → prefill 整段、cache 共用 prefix
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl"> Request B：system prompt + user B → 重用 cache 的 system prefix + 只 prefill user B → 生成
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">9&lt;/span>&lt;span class="cl"> ↑ 省下 system prompt 的 prefill 算力&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>效益對應的場景：&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>同 system prompt、不同 user message&lt;/td>
 &lt;td>prefill 算力大幅省&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>同 few-shot 例子、不同 query&lt;/td>
 &lt;td>prefill 算力大幅省&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>長 RAG context 共用、不同問題&lt;/td>
 &lt;td>prefill 算力大幅省&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>完全獨立的請求（無共用前綴）&lt;/td>
 &lt;td>無效益&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>主流推論引擎的支援度（依版本變化）：vLLM、SGLang、llama.cpp 等都有 prefix cache 機制、命名各異。&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>事實查核註&lt;/strong>：prefix cache 的命名、設定方式、tenant 隔離預設行為依推論引擎跟版本差異大、引用前以對應引擎的官方文件為準（如 &lt;a href="https://docs.vllm.ai/">vLLM Automatic Prefix Caching&lt;/a>、SGLang RadixAttention 等）。&lt;/p>&lt;/blockquote>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>理解 prefix cache 後可以解釋兩個現象：為什麼 production LLM 服務的 latency 在啟用 prefix cache 後大幅下降（system prompt 不再每次重算）、為什麼 prefix cache 在多租戶場景是隱私風險（A 租戶的 prefix 可能被 B 看到、見 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">llm-multi-tenant-isolation&lt;/a>）。&lt;/p>
&lt;p>production 設計時、prefix cache 應該按 tenant 分桶、同 tenant 內可共用、跨 tenant 必須隔離。隔離邊界對齊 &lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/tenant-boundary/" data-link-title="Tenant Boundary" data-link-desc="說明多租戶系統如何隔離不同客戶或組織的資料與資源">tenant-boundary&lt;/a> 卡片的設計。&lt;/p></description><content:encoded><![CDATA[<p>Prefix Cache 的核心概念是「當多個請求共用相同的前綴 prompt（如同一 system prompt、同一 few-shot 範例）、把該前綴的 <a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> 算一次、後續請求共用、省下重複 <a href="/blog/llm/knowledge-cards/prefill/" data-link-title="Prefill" data-link-desc="Prompt 首次處理時的計算階段：把整段輸入跑過模型、產生 KV cache">prefill</a> 算力」。是 production LLM 服務的常見優化、能大幅降低 latency 跟成本；但在多租戶場景下、跨租戶共用 prefix cache 是直接的隱私洩漏面。</p>
<h2 id="概念位置">概念位置</h2>
<p>Prefix Cache 在推論流程中的角色：</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">傳統推論：
</span></span><span class="line"><span class="ln">2</span><span class="cl">  Request A：system prompt + user A → 完整 prefill → 生成
</span></span><span class="line"><span class="ln">3</span><span class="cl">  Request B：system prompt + user B → 完整 prefill → 生成
</span></span><span class="line"><span class="ln">4</span><span class="cl">                                       ↑ 重複算 system prompt
</span></span><span class="line"><span class="ln">5</span><span class="cl">
</span></span><span class="line"><span class="ln">6</span><span class="cl">開啟 Prefix Cache：
</span></span><span class="line"><span class="ln">7</span><span class="cl">  Request A：system prompt + user A → prefill 整段、cache 共用 prefix
</span></span><span class="line"><span class="ln">8</span><span class="cl">  Request B：system prompt + user B → 重用 cache 的 system prefix + 只 prefill user B → 生成
</span></span><span class="line"><span class="ln">9</span><span class="cl">                                       ↑ 省下 system prompt 的 prefill 算力</span></span></code></pre></div><p>效益對應的場景：</p>
<table>
  <thead>
      <tr>
          <th>場景</th>
          <th>效益</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>同 system prompt、不同 user message</td>
          <td>prefill 算力大幅省</td>
      </tr>
      <tr>
          <td>同 few-shot 例子、不同 query</td>
          <td>prefill 算力大幅省</td>
      </tr>
      <tr>
          <td>長 RAG context 共用、不同問題</td>
          <td>prefill 算力大幅省</td>
      </tr>
      <tr>
          <td>完全獨立的請求（無共用前綴）</td>
          <td>無效益</td>
      </tr>
  </tbody>
</table>
<p>主流推論引擎的支援度（依版本變化）：vLLM、SGLang、llama.cpp 等都有 prefix cache 機制、命名各異。</p>
<blockquote>
<p><strong>事實查核註</strong>：prefix cache 的命名、設定方式、tenant 隔離預設行為依推論引擎跟版本差異大、引用前以對應引擎的官方文件為準（如 <a href="https://docs.vllm.ai/">vLLM Automatic Prefix Caching</a>、SGLang RadixAttention 等）。</p></blockquote>
<h2 id="設計責任">設計責任</h2>
<p>理解 prefix cache 後可以解釋兩個現象：為什麼 production LLM 服務的 latency 在啟用 prefix cache 後大幅下降（system prompt 不再每次重算）、為什麼 prefix cache 在多租戶場景是隱私風險（A 租戶的 prefix 可能被 B 看到、見 <a href="/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/" data-link-title="LLM 多租戶推論隔離" data-link-desc="production LLM 服務的多租戶隔離：KV cache 不共享、log / model artifact 隔離、跨用戶 prompt 洩漏面">llm-multi-tenant-isolation</a>）。</p>
<p>production 設計時、prefix cache 應該按 tenant 分桶、同 tenant 內可共用、跨 tenant 必須隔離。隔離邊界對齊 <a href="/blog/backend/knowledge-cards/tenant-boundary/" data-link-title="Tenant Boundary" data-link-desc="說明多租戶系統如何隔離不同客戶或組織的資料與資源">tenant-boundary</a> 卡片的設計。</p>
]]></content:encoded></item><item><title>5.2 KV cache 量化策略</title><link>https://tarrragon.github.io/blog/llm/05-discrete-gpu/kv-cache-quantization-strategy/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/05-discrete-gpu/kv-cache-quantization-strategy/</guid><description>&lt;p>KV cache 量化是 PC 場景開大 context 或提高併發數的常用工程選項：把 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> 從 fp16 壓到 Q8 或 Q4、體積大幅縮減、騰出的 VRAM 拿去開長 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/context-window/" data-link-title="Context Window" data-link-desc="模型一次能處理的最大 token 數量：prompt 加生成的總和上限">context&lt;/a>、加併發、或載入更大模型。本章不重複&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">卡片定義&lt;/a>、改處理「實際要不要量化、量化到哪一級」的判讀。卡片視角的 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/quantization/" data-link-title="Quantization" data-link-desc="用較少 bits 表示模型權重：壓縮記憶體佔用、加快生字速度，代價是少量品質衰減">量化&lt;/a> 跟本章的 KV cache 量化是兩個方向：前者壓模型權重、後者壓推論時的 attention 暫存。&lt;/p>
&lt;p>讀完本章後、你應該能對自己的工作流回答：KV cache 量化的好處能換到什麼、品質代價落在什麼範圍、K 跟 V 為什麼建議不同等級、跟 context 長度跟併發數怎麼搭配。&lt;/p>
&lt;h2 id="本章目標">本章目標&lt;/h2>
&lt;ol>
&lt;li>理解 KV cache 為什麼會隨 context 線性膨脹、為什麼 PC 場景常需要量化。&lt;/li>
&lt;li>區分 K 跟 V 在 attention 計算中的角色、解釋為何兩者對量化的容忍度不同。&lt;/li>
&lt;li>判讀「該不該量化 KV cache」的工作流類型。&lt;/li>
&lt;li>認識 llama.cpp 的 &lt;code>--cache-type-k&lt;/code> / &lt;code>--cache-type-v&lt;/code> 旗標與相關限制（如 flash attention 要求）。&lt;/li>
&lt;li>知道調參時的觀察訊號跟取捨方向。&lt;/li>
&lt;/ol>
&lt;h2 id="kv-cache-為什麼會膨脹">KV cache 為什麼會膨脹&lt;/h2>
&lt;p>LLM 推論時、每處理一個 token 都會把該 token 的 key 跟 value 向量算出來、暫存進 KV cache、供後續 token 的 attention 計算複用（不重算）。KV cache 的體積跟下面幾個變數線性相關：&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">KV cache 體積 ≈ 2 × n_layers × n_heads × head_dim × bytes_per_value × context_長度 × batch&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>2：分別是 K cache 跟 V cache&lt;/li>
&lt;li>n_layers / n_heads / head_dim：模型結構參數&lt;/li>
&lt;li>bytes_per_value：fp16 是 2 bytes、Q8_0 約 1 byte、Q4_0 約 0.5 byte&lt;/li>
&lt;li>context_長度：context 開多大、KV cache 就放多大&lt;/li>
&lt;li>batch：併發處理多少 sequence&lt;/li>
&lt;/ul>
&lt;p>實際 KV cache 體積依模型 attention 變體（MHA / GQA / MLA）、head 數設計、量化方式而變。比起背公式、更實用的做法是看 llama.cpp 啟動時的 log、它會列出實際 KV cache 配置的記憶體：&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">llm_load_print_meta: n_layer = 48
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">llm_load_print_meta: n_head = 32
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">llama_kv_cache_init: KV self size = 2048.00 MiB, K (q8_0): 1024.00 MiB, V (q8_0): 1024.00 MiB&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>&lt;strong>事實查核註&lt;/strong>：上面的 log 格式跟欄位名稱依 llama.cpp 版本變動、實際輸出以執行時為準。常見模型的 KV cache 估算工具可參考 &lt;a href="https://github.com/ggml-org/llama.cpp">llama.cpp 官方文件&lt;/a> 或社群維護的 calculator。&lt;/p></description><content:encoded><![CDATA[<p>KV cache 量化是 PC 場景開大 context 或提高併發數的常用工程選項：把 <a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> 從 fp16 壓到 Q8 或 Q4、體積大幅縮減、騰出的 VRAM 拿去開長 <a href="/blog/llm/knowledge-cards/context-window/" data-link-title="Context Window" data-link-desc="模型一次能處理的最大 token 數量：prompt 加生成的總和上限">context</a>、加併發、或載入更大模型。本章不重複<a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">卡片定義</a>、改處理「實際要不要量化、量化到哪一級」的判讀。卡片視角的 <a href="/blog/llm/knowledge-cards/quantization/" data-link-title="Quantization" data-link-desc="用較少 bits 表示模型權重：壓縮記憶體佔用、加快生字速度，代價是少量品質衰減">量化</a> 跟本章的 KV cache 量化是兩個方向：前者壓模型權重、後者壓推論時的 attention 暫存。</p>
<p>讀完本章後、你應該能對自己的工作流回答：KV cache 量化的好處能換到什麼、品質代價落在什麼範圍、K 跟 V 為什麼建議不同等級、跟 context 長度跟併發數怎麼搭配。</p>
<h2 id="本章目標">本章目標</h2>
<ol>
<li>理解 KV cache 為什麼會隨 context 線性膨脹、為什麼 PC 場景常需要量化。</li>
<li>區分 K 跟 V 在 attention 計算中的角色、解釋為何兩者對量化的容忍度不同。</li>
<li>判讀「該不該量化 KV cache」的工作流類型。</li>
<li>認識 llama.cpp 的 <code>--cache-type-k</code> / <code>--cache-type-v</code> 旗標與相關限制（如 flash attention 要求）。</li>
<li>知道調參時的觀察訊號跟取捨方向。</li>
</ol>
<h2 id="kv-cache-為什麼會膨脹">KV cache 為什麼會膨脹</h2>
<p>LLM 推論時、每處理一個 token 都會把該 token 的 key 跟 value 向量算出來、暫存進 KV cache、供後續 token 的 attention 計算複用（不重算）。KV cache 的體積跟下面幾個變數線性相關：</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">KV cache 體積 ≈ 2 × n_layers × n_heads × head_dim × bytes_per_value × context_長度 × batch</span></span></code></pre></div><ul>
<li>2：分別是 K cache 跟 V cache</li>
<li>n_layers / n_heads / head_dim：模型結構參數</li>
<li>bytes_per_value：fp16 是 2 bytes、Q8_0 約 1 byte、Q4_0 約 0.5 byte</li>
<li>context_長度：context 開多大、KV cache 就放多大</li>
<li>batch：併發處理多少 sequence</li>
</ul>
<p>實際 KV cache 體積依模型 attention 變體（MHA / GQA / MLA）、head 數設計、量化方式而變。比起背公式、更實用的做法是看 llama.cpp 啟動時的 log、它會列出實際 KV cache 配置的記憶體：</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">llm_load_print_meta: n_layer    = 48
</span></span><span class="line"><span class="ln">2</span><span class="cl">llm_load_print_meta: n_head     = 32
</span></span><span class="line"><span class="ln">3</span><span class="cl">llama_kv_cache_init: KV self size = 2048.00 MiB, K (q8_0): 1024.00 MiB, V (q8_0): 1024.00 MiB</span></span></code></pre></div><blockquote>
<p><strong>事實查核註</strong>：上面的 log 格式跟欄位名稱依 llama.cpp 版本變動、實際輸出以執行時為準。常見模型的 KV cache 估算工具可參考 <a href="https://github.com/ggml-org/llama.cpp">llama.cpp 官方文件</a> 或社群維護的 calculator。</p></blockquote>
<h2 id="k-跟-v-為什麼適合用不同量化等級">K 跟 V 為什麼適合用不同量化等級</h2>
<p>K 跟 V 在 <a href="/blog/llm/knowledge-cards/attention/" data-link-title="Attention" data-link-desc="Transformer 內部讓每個 token 對其他 token 加權平均的核心機制、形成 KV cache 跟 context window 的計算基礎">attention</a> 計算中扮演不同角色、對量化的容忍度也不同。K 參與內積比較（量化容忍度通常較高）、V 是被加權平均的輸出內容（量化誤差會線性累積）、社群常見做法是 K 用較激進的量化、V 保留較高精度。</p>
<p>attention 的計算流程簡化為：</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">attention(Q, K, V) = softmax(Q · K^T / √d) · V</span></span></code></pre></div><p>K 跟 V 在這個流程中的角色差異：</p>
<ol>
<li><strong>K（key）</strong>：用來跟 Q 算內積、產生 attention score。內積本質是「相對量級的比較」、量化造成的微小誤差容易在 softmax 後被吸收。</li>
<li><strong>V（value）</strong>：是被 softmax 加權平均後直接輸出的內容、量化誤差會線性累積進輸出。</li>
</ol>
<p>社群多數回報指出：</p>
<ul>
<li><strong>K 用 Q8_0 或 Q4_0 對品質影響相對小</strong>：因為 softmax 對輸入量級的敏感度集中在最大值附近、其他位置的小幅誤差會被指數壓縮。</li>
<li><strong>V 用 Q4_0 在長 context 末尾較易出現品質下降</strong>：因為 V 是被加權平均的內容、累積誤差會在輸出中可見。</li>
</ul>
<blockquote>
<p><strong>事實查核註</strong>：K 跟 V 對量化敏感度不同的論述、來自社群常見回報跟若干針對 KV cache 量化的論文（如 KIVI、KVQuant 等）。具體影響因模型架構、量化方法（symmetric / asymmetric、per-head / per-channel scale 等）變化、不同模型的表現可能不一致；建議用自己工作流的任務跟自己選定的量化版本實測校準。</p></blockquote>
<h2 id="kv-cache-量化等級對照">KV cache 量化等級對照</h2>
<p>llama.cpp 支援的常見 KV cache 量化等級：</p>
<table>
  <thead>
      <tr>
          <th>量化等級</th>
          <th>bytes/value（約）</th>
          <th>相對 fp16 體積</th>
          <th>社群常見用途</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>fp16</code></td>
          <td>2</td>
          <td>100%</td>
          <td>預設、品質基準</td>
      </tr>
      <tr>
          <td><code>q8_0</code></td>
          <td>1</td>
          <td>50%</td>
          <td>K 的常見起點、品質衰減社群回報為小幅</td>
      </tr>
      <tr>
          <td><code>q5_1</code></td>
          <td>~0.7</td>
          <td>~35%</td>
          <td>中間選項</td>
      </tr>
      <tr>
          <td><code>q5_0</code></td>
          <td>~0.7</td>
          <td>~35%</td>
          <td>中間選項</td>
      </tr>
      <tr>
          <td><code>q4_1</code></td>
          <td>~0.5</td>
          <td>~25%</td>
          <td>V 的常見極限</td>
      </tr>
      <tr>
          <td><code>q4_0</code></td>
          <td>~0.5</td>
          <td>~25%</td>
          <td>V 的常見起點、品質衰減較 Q5 略大</td>
      </tr>
  </tbody>
</table>
<p>常見組合（社群回報、需自行校準）：</p>
<ul>
<li><strong>保守（品質優先）</strong>：K=fp16、V=fp16。完全不量化、VRAM 用量最大。</li>
<li><strong>平衡起點</strong>：K=Q8_0、V=Q8_0。體積約一半、品質衰減社群多數回報為小幅或不明顯。</li>
<li><strong>激進（context 優先）</strong>：K=Q8_0、V=Q4_0。體積約 fp16 的 35%、社群回報短 prompt 影響小、長 prompt 末尾可能出現品質下降。</li>
<li><strong>極限</strong>：K=Q4_0、V=Q4_0。體積約 fp16 的 25%、用於開超大 context 或極高併發、品質風險最高。</li>
</ul>
<h2 id="何時值得量化何時不該量化">何時值得量化、何時不該量化</h2>
<p>KV cache 量化的主要用途是「VRAM 不足以同時放下模型權重 + 目標 context 長度 + 目標併發數」的場景。當 VRAM 已有充裕餘量、量化省下的 VRAM 沒有對應的用途時、保留 fp16 通常較合適。下表整理常見的判讀情境：</p>
<table>
  <thead>
      <tr>
          <th>場景</th>
          <th>是否值得量化</th>
          <th>主要考量</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>寫 code、補完、跨檔案重構</td>
          <td>值得（K=Q8/V=Q4）</td>
          <td>程式碼合法性約束會過濾小幅誤差、社群回報品質影響小</td>
      </tr>
      <tr>
          <td>RAG（大型 codebase 索引、長文件摘要）</td>
          <td>值得</td>
          <td>context 通常很長、KV cache 是 VRAM 主要瓶頸</td>
      </tr>
      <tr>
          <td>自由創作（小說、長對話、詩）</td>
          <td>評估、可能不適合</td>
          <td>V 量化的累積誤差較易在創作品質上感知</td>
      </tr>
      <tr>
          <td>數學 / 邏輯推理（chain-of-thought）</td>
          <td>從保守起點</td>
          <td>推理鏈累積誤差較敏感、建議從 K=Q8 / V=Q8 起步、再依任務評估</td>
      </tr>
      <tr>
          <td>短 prompt 短回答（&lt; 4K context）</td>
          <td>不必要</td>
          <td>KV cache 體積本來就小、量化省下的 VRAM 不多</td>
      </tr>
      <tr>
          <td>對品質高度敏感的研究或產品任務</td>
          <td>從保守起點</td>
          <td>先用 fp16 建立品質基準、再依需求逐步量化、確認品質可接受</td>
      </tr>
  </tbody>
</table>
<p>判讀原則：<strong>先確認瓶頸是「VRAM 不夠」還是「品質不夠」</strong>。前者量化是解法、後者量化通常會惡化問題。</p>
<h2 id="跟-context-長度併發數的協調">跟 context 長度、併發數的協調</h2>
<p>KV cache 量化的好處要跟其他 VRAM 用量一起評估。常見的取捨方向：</p>
<ol>
<li><strong>量化 → 開更大 context</strong>：把省下的 VRAM 用在加大 <code>-c</code>、能開長 prompt（如 RAG、長對話、跨檔案分析）。</li>
<li><strong>量化 → 加併發</strong>：把省下的 VRAM 用在加 <code>--parallel</code>、能同時服務多個 client（如多個編輯器視窗、多 agent）。</li>
<li><strong>量化 → 載入更大模型</strong>：把省下的 VRAM 用在降 <code>--n-cpu-moe</code>、減少卸載、提升生字速度。</li>
</ol>
<p>三者通常不能同時極大化、需要依工作流挑主軸。</p>
<p>實務上的常見搭配（社群回報、需校準）：</p>
<table>
  <thead>
      <tr>
          <th>工作流</th>
          <th>建議搭配</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>單人寫 code、補完為主</td>
          <td>K=Q8 / V=Q4、context 32K ~ 128K、<code>--parallel 1 ~ 2</code></td>
      </tr>
      <tr>
          <td>RAG 大型 codebase</td>
          <td>K=Q8 / V=Q4、context 128K ~ 256K、<code>--parallel 1</code></td>
      </tr>
      <tr>
          <td>多 agent / 多視窗並用</td>
          <td>K=Q8 / V=Q4 或更激進、context 32K、<code>--parallel 4 ~ 8</code></td>
      </tr>
      <tr>
          <td>對話品質敏感、純創作</td>
          <td>K=Q8 / V=Q8 起步、context 適中、依品質確認再決定是否加量化</td>
      </tr>
  </tbody>
</table>
<h2 id="llamacpp-的相關旗標">llama.cpp 的相關旗標</h2>
<p>跑 KV cache 量化時、常用的旗標：</p>
<table>
  <thead>
      <tr>
          <th>旗標</th>
          <th>作用</th>
          <th>備註</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>--cache-type-k &lt;type&gt;</code></td>
          <td>K cache 量化（如 <code>f16</code>、<code>q8_0</code>、<code>q4_0</code>）</td>
          <td>預設 f16</td>
      </tr>
      <tr>
          <td><code>--cache-type-v &lt;type&gt;</code></td>
          <td>V cache 量化</td>
          <td>預設 f16</td>
      </tr>
      <tr>
          <td><code>-fa</code> / <code>--flash-attn</code></td>
          <td>啟用 flash attention</td>
          <td>部分量化組合需要 flash attention 才能啟用、見下方說明</td>
      </tr>
      <tr>
          <td><code>-c &lt;N&gt;</code></td>
          <td>context window 大小</td>
          <td>KV cache 體積跟此線性相關</td>
      </tr>
      <tr>
          <td><code>--parallel &lt;N&gt;</code></td>
          <td>併發處理數</td>
          <td>KV cache 體積跟此線性相關</td>
      </tr>
      <tr>
          <td><code>-ctk &lt;type&gt;</code> / <code>-ctv &lt;type&gt;</code></td>
          <td><code>--cache-type-k</code> / <code>--cache-type-v</code> 的短旗標</td>
          <td>同義、版本依 llama.cpp 變動</td>
      </tr>
  </tbody>
</table>
<h3 id="flash-attention-的關係">flash attention 的關係</h3>
<p>部分 KV cache 量化組合（特別是 V=Q4_0 / Q4_1）在 llama.cpp 上需要同時啟用 <a href="/blog/llm/knowledge-cards/flash-attention/" data-link-title="Flash Attention" data-link-desc="Attention 計算的記憶體友善實作、減少 GPU memory 讀寫、提升長 context 推論吞吐">flash attention</a>（<code>-fa</code>）才能正常運作；沒啟用時可能載入失敗或 fallback 到 fp16。具體要求依 llama.cpp 版本變化、以實際 <code>llama-server --help</code> 跟 <a href="https://github.com/ggml-org/llama.cpp/pulls?q=is%3Amerged&#43;kv&#43;cache&#43;quant">llama.cpp 官方 issue / PR</a> 為準。</p>
<blockquote>
<p><strong>事實查核註</strong>：flash attention 對 KV cache 量化組合的限制、是 llama.cpp 實作層面的演進議題、不是模型本身的限制。新版 llama.cpp 可能放寬或改變要求、引用前以最新版的 release notes 為準。</p></blockquote>
<h2 id="給讀者的調參步驟">給讀者的調參步驟</h2>
<p>實際設定 KV cache 量化時、可以照下面的步驟調：</p>
<ol>
<li><strong>先用 fp16 基準跑一次</strong>：用實際工作流的代表性任務、記錄補完品質、執行時間、VRAM 用量。這是後續比較的基準。</li>
<li><strong>切到 K=Q8 / V=Q8</strong>：跑同樣的任務、比較品質。社群多數回報差異不明顯、但需以自己工作流確認。</li>
<li><strong>進一步切到 V=Q4</strong>：再跑同樣任務、特別注意長 prompt 末尾、推理鏈、複雜邏輯任務的輸出品質。</li>
<li><strong>若品質可接受、評估省下的 VRAM 怎麼用</strong>：加大 <code>-c</code>、提高 <code>--parallel</code>、或減少 <code>--n-cpu-moe</code>。</li>
<li><strong>建立可重複的校準腳本</strong>：把代表性任務寫成 prompt 集、做為日後升級模型或調參時的回歸測試。</li>
</ol>
<h2 id="下一章">下一章</h2>
<p>下一章：<a href="/blog/llm/05-discrete-gpu/llama-cpp-on-pc/" data-link-title="5.3 llama.cpp 在 PC 上" data-link-desc="CUDA / ROCm build 取得、核心旗標地圖、llama-bench 校準、多卡 tensor split 的入門設定">5.3 llama.cpp 在 PC 上</a>、把本章跟 <a href="/blog/llm/05-discrete-gpu/moe-cpu-offload-strategy/" data-link-title="5.1 MoE 模型與 CPU 卸載策略" data-link-desc="PC 場景把 MoE 不活躍專家層留在系統 RAM 的判讀：何時值得卸載、卸幾層、對 prefill 跟生成的影響各自不同">5.1 MoE 卸載</a> 的旗標放進完整的 llama.cpp 調參工作流。</p>
]]></content:encoded></item><item><title>LLM 多租戶推論隔離</title><link>https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-multi-tenant-isolation/</guid><description>&lt;p>本章的責任是把 LLM 推論服務的多租戶隔離問題拆成可操作的判讀節點。LLM 服務的隔離議題在一般 multi-tenant 隔離（compute / network / data、見 &lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/tenant-boundary/" data-link-title="Tenant Boundary" data-link-desc="說明多租戶系統如何隔離不同客戶或組織的資料與資源">tenant-boundary&lt;/a>）之上、多了 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a>（特別是 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache&lt;/a> 重用）、prompt log、model artifact 訪問權三個 LLM-specific 層、本章聚焦這些差異。一般 multi-tenant 隔離原則沿用 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/identity-access-boundary/" data-link-title="7.2 身分與授權邊界" data-link-desc="以問題驅動方式整理身分、授權、會話與供應商身分鏈">7.2 身分授權邊界&lt;/a> 跟 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/supply-chain-integrity-and-artifact-trust/" data-link-title="7.12 供應鏈完整性與 Artifact 信任" data-link-desc="定義 build provenance、artifact 信任與交付鏈風險問題">7.4 供應鏈&lt;/a>。&lt;/p>
&lt;h2 id="本章寫作邊界">本章寫作邊界&lt;/h2>
&lt;p>本章聚焦 production LLM 推論的多租戶 isolation 特殊性。team / 個人 dev 場景的「多人共用本地 server」見 &lt;a href="https://tarrragon.github.io/blog/llm/06-security/routing-to-production-security/" data-link-title="6.5 跨進 production 的 routing 中樞" data-link-desc="個人 dev → 團隊 → production LLM 服務的三層演化、跟 backend/07 對應卡片的 routing 清單">llm/6.5 跨進 production 的 routing 中樞&lt;/a>；通用 IAM / 服務間信任邊界見 7.2。&lt;/p>
&lt;h2 id="本章-threat-scope">本章 threat scope&lt;/h2>
&lt;p>&lt;strong>In-scope&lt;/strong>：KV cache 跨租戶洩漏、prompt log 隔離、模型 artifact 訪問權、batch 推論的順序敏感性、tenant-scoped rate limit、共用 GPU 上的記憶體殘留。&lt;/p>
&lt;p>&lt;strong>Out-of-scope&lt;/strong>（路由到他章）：&lt;/p>
&lt;ul>
&lt;li>通用 IAM / 服務間信任 → &lt;a href="../identity-access-boundary/">7.2 identity-access-boundary&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://tarrragon.github.io/blog/backend/knowledge-cards/workload-identity/" data-link-title="Workload Identity" data-link-desc="用於機器工作負載的身份語意與授權邊界">workload identity&lt;/a> → &lt;a href="../workload-identity-and-federated-trust/">7.7 workload-identity-and-federated-trust&lt;/a>&lt;/li>
&lt;li>log / PII 治理 → &lt;a href="../llm-log-and-pii-governance/">llm-log-and-pii-governance&lt;/a>&lt;/li>
&lt;li>model artifact 供應鏈 → &lt;a href="../llm-deployment-supply-chain/">llm-deployment-supply-chain&lt;/a>&lt;/li>
&lt;li>入口治理 → &lt;a href="../entrypoint-and-server-protection/">7.3 entrypoint-and-server-protection&lt;/a>&lt;/li>
&lt;/ul>
&lt;h2 id="從本章到實作">從本章到實作&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Mechanism&lt;/strong>：問題節點表 → knowledge-card → 看具體機制。&lt;/li>
&lt;li>&lt;strong>Delivery&lt;/strong>：交接路由 → &lt;code>05-deployment-platform / 06-reliability / 08-incident-response&lt;/code>。&lt;/li>
&lt;/ul>
&lt;h2 id="llm-多租戶隔離的三個-llm-specific-層">LLM 多租戶隔離的三個 LLM-specific 層&lt;/h2>
&lt;p>跟一般 service 的多租戶隔離（compute / network / data）相比、LLM 推論服務多了三個層次：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>KV cache 層&lt;/strong>：&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache&lt;/a> 是推論時的 attention 暫存、跨 request 可能重用（prefix cache、shared prefix optimization）；跨租戶共用 cache 是直接的資料洩漏面。&lt;/li>
&lt;li>&lt;strong>prompt log 層&lt;/strong>：production LLM 服務通常會 log prompt + response 用於 debug / billing / abuse detection；log 的隔離與保留期限直接影響跨租戶洩漏風險。&lt;/li>
&lt;li>&lt;strong>model artifact 訪問權&lt;/strong>：production 可能部署多個 fine-tuned 模型（如 customer-specific 模型）、模型本身是 sensitive artifact、訪問權要對齊 IAM。&lt;/li>
&lt;/ol>
&lt;h2 id="分析模型">分析模型&lt;/h2>
&lt;p>production LLM 推論的多租戶隔離依四個層次分析：&lt;/p></description><content:encoded><![CDATA[<p>本章的責任是把 LLM 推論服務的多租戶隔離問題拆成可操作的判讀節點。LLM 服務的隔離議題在一般 multi-tenant 隔離（compute / network / data、見 <a href="/blog/backend/knowledge-cards/tenant-boundary/" data-link-title="Tenant Boundary" data-link-desc="說明多租戶系統如何隔離不同客戶或組織的資料與資源">tenant-boundary</a>）之上、多了 <a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a>（特別是 <a href="/blog/llm/knowledge-cards/prefix-cache/" data-link-title="Prefix Cache" data-link-desc="把多個請求共用的前綴 prompt 的 KV cache 重用、省下重複 prefill 算力的優化、production 多用戶服務的常見設計">prefix cache</a> 重用）、prompt log、model artifact 訪問權三個 LLM-specific 層、本章聚焦這些差異。一般 multi-tenant 隔離原則沿用 <a href="/blog/backend/07-security-data-protection/identity-access-boundary/" data-link-title="7.2 身分與授權邊界" data-link-desc="以問題驅動方式整理身分、授權、會話與供應商身分鏈">7.2 身分授權邊界</a> 跟 <a href="/blog/backend/07-security-data-protection/supply-chain-integrity-and-artifact-trust/" data-link-title="7.12 供應鏈完整性與 Artifact 信任" data-link-desc="定義 build provenance、artifact 信任與交付鏈風險問題">7.4 供應鏈</a>。</p>
<h2 id="本章寫作邊界">本章寫作邊界</h2>
<p>本章聚焦 production LLM 推論的多租戶 isolation 特殊性。team / 個人 dev 場景的「多人共用本地 server」見 <a href="/blog/llm/06-security/routing-to-production-security/" data-link-title="6.5 跨進 production 的 routing 中樞" data-link-desc="個人 dev → 團隊 → production LLM 服務的三層演化、跟 backend/07 對應卡片的 routing 清單">llm/6.5 跨進 production 的 routing 中樞</a>；通用 IAM / 服務間信任邊界見 7.2。</p>
<h2 id="本章-threat-scope">本章 threat scope</h2>
<p><strong>In-scope</strong>：KV cache 跨租戶洩漏、prompt log 隔離、模型 artifact 訪問權、batch 推論的順序敏感性、tenant-scoped rate limit、共用 GPU 上的記憶體殘留。</p>
<p><strong>Out-of-scope</strong>（路由到他章）：</p>
<ul>
<li>通用 IAM / 服務間信任 → <a href="../identity-access-boundary/">7.2 identity-access-boundary</a></li>
<li><a href="/blog/backend/knowledge-cards/workload-identity/" data-link-title="Workload Identity" data-link-desc="用於機器工作負載的身份語意與授權邊界">workload identity</a> → <a href="../workload-identity-and-federated-trust/">7.7 workload-identity-and-federated-trust</a></li>
<li>log / PII 治理 → <a href="../llm-log-and-pii-governance/">llm-log-and-pii-governance</a></li>
<li>model artifact 供應鏈 → <a href="../llm-deployment-supply-chain/">llm-deployment-supply-chain</a></li>
<li>入口治理 → <a href="../entrypoint-and-server-protection/">7.3 entrypoint-and-server-protection</a></li>
</ul>
<h2 id="從本章到實作">從本章到實作</h2>
<ul>
<li><strong>Mechanism</strong>：問題節點表 → knowledge-card → 看具體機制。</li>
<li><strong>Delivery</strong>：交接路由 → <code>05-deployment-platform / 06-reliability / 08-incident-response</code>。</li>
</ul>
<h2 id="llm-多租戶隔離的三個-llm-specific-層">LLM 多租戶隔離的三個 LLM-specific 層</h2>
<p>跟一般 service 的多租戶隔離（compute / network / data）相比、LLM 推論服務多了三個層次：</p>
<ol>
<li><strong>KV cache 層</strong>：<a href="/blog/llm/knowledge-cards/kv-cache/" data-link-title="KV Cache" data-link-desc="已處理 token 的 attention 中間結果暫存：避免重算、加速後續生成">KV cache</a> 是推論時的 attention 暫存、跨 request 可能重用（prefix cache、shared prefix optimization）；跨租戶共用 cache 是直接的資料洩漏面。</li>
<li><strong>prompt log 層</strong>：production LLM 服務通常會 log prompt + response 用於 debug / billing / abuse detection；log 的隔離與保留期限直接影響跨租戶洩漏風險。</li>
<li><strong>model artifact 訪問權</strong>：production 可能部署多個 fine-tuned 模型（如 customer-specific 模型）、模型本身是 sensitive artifact、訪問權要對齊 IAM。</li>
</ol>
<h2 id="分析模型">分析模型</h2>
<p>production LLM 推論的多租戶隔離依四個層次分析：</p>
<ol>
<li><strong>memory 層</strong>：GPU VRAM、CPU RAM 中的 KV cache 跟模型權重、跨 request / 跨租戶的殘留與共享邊界。</li>
<li><strong>storage 層</strong>：模型 artifact、prompt log、context cache 在儲存層的隔離。</li>
<li><strong>identity 層</strong>：tenant identity 怎麼帶到 inference call、rate limit / quota 怎麼按租戶分。</li>
<li><strong>observability 層</strong>：metric / log / trace 中的 tenant tag、跨租戶分析的允許範圍。</li>
</ol>
<h2 id="判讀流程">判讀流程</h2>
<p>判讀流程的責任是把「能服務多個租戶的 LLM 服務」轉成「租戶間資料不互相洩漏的 LLM 服務」。</p>
<ol>
<li>先確認 tenant identity 從 API gateway 到 inference call 的傳遞路徑。</li>
<li>再確認 KV cache、prompt log、model artifact 各自的隔離邊界。</li>
<li>接著確認 GPU 記憶體中的跨 request 殘留是否清理。</li>
<li>最後交接到偵測流程、確認跨租戶異常能被識別。</li>
</ol>
<h2 id="問題節點案例觸發式">問題節點（案例觸發式）</h2>
<table>
  <thead>
      <tr>
          <th>問題節點</th>
          <th>判讀訊號</th>
          <th>風險後果</th>
          <th>前置控制面</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>KV cache 跨租戶共享</td>
          <td>shared prefix optimization 沒按 tenant key 分桶</td>
          <td>租戶 A 的 prompt prefix 被租戶 B 看見</td>
          <td><a href="/blog/backend/07-security-data-protection/data-protection-and-masking-governance/" data-link-title="7.4 資料保護與遮罩治理" data-link-desc="以問題驅動方式整理資料分級、遮罩、匯出與備份治理">data-protection</a></td>
      </tr>
      <tr>
          <td>prompt log 沒分租戶</td>
          <td>集中 log、查詢時 tenant filter 缺失</td>
          <td>abuse detection 跨租戶看 prompt 內容、隱私違規</td>
          <td><a href="/blog/backend/knowledge-cards/audit-log/" data-link-title="Audit Log" data-link-desc="說明高風險操作如何留下可追溯、可稽核的紀錄">audit-log</a></td>
      </tr>
      <tr>
          <td>共用 GPU 上的記憶體殘留</td>
          <td>推論完未清 VRAM、下一個 request 可能 dump 到前一個內容</td>
          <td>同 GPU 上的不同 tenant 之間殘留洩漏</td>
          <td><a href="/blog/backend/knowledge-cards/secret-management/" data-link-title="Secret Management" data-link-desc="說明 token、key、password 與憑證如何保存、輪替與撤銷">secret-management</a></td>
      </tr>
      <tr>
          <td>tenant-scoped rate limit 失效</td>
          <td>同一 API key 限流、租戶被互相 DoS</td>
          <td>大租戶吃光 quota、其他租戶無法用</td>
          <td><a href="/blog/backend/knowledge-cards/rate-limit/" data-link-title="Rate Limit" data-link-desc="說明限流如何保護服務入口、下游依賴與租戶公平性">rate-limit</a></td>
      </tr>
      <tr>
          <td>model artifact 訪問權混亂</td>
          <td>fine-tuned 模型路徑可被其他 tenant 載入</td>
          <td>客戶模型被其他客戶使用、模型權重洩漏</td>
          <td><a href="/blog/backend/07-security-data-protection/identity-access-boundary/" data-link-title="7.2 身分與授權邊界" data-link-desc="以問題驅動方式整理身分、授權、會話與供應商身分鏈">identity-access-boundary</a></td>
      </tr>
      <tr>
          <td>batch 推論的 cross-tenant 順序敏感</td>
          <td>dynamic batching 把不同 tenant 的 request 合批</td>
          <td>一個 tenant 的 OOM / 長 prompt 影響其他 tenant 的 latency</td>
          <td><a href="/blog/backend/knowledge-cards/contract/" data-link-title="Boundary Contract" data-link-desc="說明跨邊界約定如何維持相容與可驗證">contract</a></td>
      </tr>
  </tbody>
</table>
<h2 id="常見風險邊界">常見風險邊界</h2>
<p>風險邊界的責任是界定何時 LLM 多租戶 isolation 已進入高壓狀態。</p>
<ul>
<li>KV cache 共用範圍跨越 tenant 邊界時、代表記憶體層 isolation 失效。</li>
<li>prompt log 沒帶 tenant tag、或 tag 後仍可跨 tenant 查時、代表 log 層 isolation 不足。</li>
<li>模型 artifact 訪問權跟 IAM 解耦時、代表 identity 層 isolation 不足。</li>
<li>推論 batch 對 tenant boundary 不敏感時、代表 batch 層的 noisy-neighbor 風險上升。</li>
</ul>
<h2 id="llm-場景的特殊判讀">LLM 場景的特殊判讀</h2>
<p>LLM 多租戶 isolation 相對一般 multi-tenant 服務的特殊性：</p>
<ol>
<li><strong>KV cache 是有用但敏感的優化</strong>：shared prefix cache（如多 tenant 用同一 system prompt）能省大量 prefill 算力、但跨 tenant 共用就是洩漏。判讀：可以 share 同 tenant 內的 prefix、不能 share 跨 tenant。</li>
<li><strong>prompt log 含豐富使用者意圖</strong>：相比一般 API log 主要記 endpoint / status code、LLM prompt log 記的是「使用者實際在問什麼」、隱私敏感度高得多。</li>
<li><strong>GPU 是稀缺資源、共用比 CPU 多</strong>：production LLM 服務常多 tenant 共用同卡、isolation 比一般 multi-tenant 服務（每 tenant 跑獨立 pod）更難做、需要更細的 batch 跟 memory 管理。</li>
<li><strong>fine-tuned 模型本身是 customer asset</strong>：模型訓練成本高、權重是客戶 IP、訪問權混亂直接是 IP 外洩。</li>
<li><strong>「LLM 記住 cross-tenant 資訊」的疑慮</strong>：使用者常擔心 LLM 把 A tenant 的 prompt「記住」洩漏給 B tenant；對 inference-only 服務（無 fine-tune）這不發生（模型權重 immutable）、有 fine-tune 時要看 training data 隔離。</li>
</ol>
<h2 id="案例觸發參考">案例觸發參考</h2>
<p>LLM 多租戶 isolation 的公開案例累積中、本章先沿用通用 multi-tenant 案例：</p>
<ul>
<li>一般 multi-tenant 隔離案例見 <a href="/blog/backend/07-security-data-protection/identity-access-boundary/" data-link-title="7.2 身分與授權邊界" data-link-desc="以問題驅動方式整理身分、授權、會話與供應商身分鏈">7.2 身分授權邊界</a>。</li>
<li>LLM-specific 案例累積後會補入 <code>red-team/cases/llm-multi-tenant/</code>。</li>
</ul>
<blockquote>
<p><strong>事實查核註</strong>：LLM 多租戶 isolation 的公開事件案例還在早期、社群上有些「LLM A 的 system prompt 被 B 看到」等報告、多數屬 prompt injection 範疇而非 cache 洩漏。建議引用前以最新的 OWASP LLM Top 10 跟具體 vendor 的 incident 公告為準。</p></blockquote>
<h2 id="引用標準">引用標準</h2>
<table>
  <thead>
      <tr>
          <th>標準</th>
          <th>版本 / 年份</th>
          <th>適用場景</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>NIST SP 800-207（Zero Trust Architecture）</td>
          <td>2020</td>
          <td>tenant boundary 零信任模型 reference</td>
      </tr>
      <tr>
          <td>OWASP LLM Top 10</td>
          <td>2025</td>
          <td>LLM application security 通用 reference</td>
      </tr>
      <tr>
          <td>CSA Cloud Controls Matrix</td>
          <td>v4 (2021)</td>
          <td>multi-tenant cloud 控制 reference</td>
      </tr>
  </tbody>
</table>
<p>引用版本與 cadence 規則見 <a href="/blog/report/security-citation-currency-and-precision/" data-link-title="Security 標準引用的時效性與精確度" data-link-desc="資安 citation 跟一般技術引用不同——best practice 時效短（MD5 / SHA-1 / bcrypt 100k / TLS 1.0 都曾是 best practice）、原文常被引用扭曲（conditional → unconditional drift）、版本不標 reader 會套用過時 spec。citation 同時涵蓋外部標準（OWASP / RFC / NIST / CIS）跟內部 citation（knowledge-cards / 跨章引用作為 control-of-record）；後者因無版本號 anchor 反而更易 silent drift / broken。每條 citation 必須附：版本 / 年份、引用句意可回溯、deprecated / superseded 標記、強度參數對應 actor 能力的 review trigger（外部）/ last-checked &#43; sync owner（內部）。">security-citation-currency-and-precision</a>。Last reviewed: 2026-05-12。</p>
<h2 id="下一步路由">下一步路由</h2>
<ul>
<li>身份授權邊界：<a href="/blog/backend/07-security-data-protection/identity-access-boundary/" data-link-title="7.2 身分與授權邊界" data-link-desc="以問題驅動方式整理身分、授權、會話與供應商身分鏈">7.2 identity-access-boundary</a></li>
<li>log 治理：<a href="/blog/backend/07-security-data-protection/llm-log-and-pii-governance/" data-link-title="LLM Log 與 PII 治理" data-link-desc="production LLM 服務的 prompt log 累積、PII 偵測與過濾、保留期限與合規對齊">llm-log-and-pii-governance</a></li>
<li>agent prompt injection 後果：<a href="/blog/backend/07-security-data-protection/llm-prompt-injection-in-agent/" data-link-title="LLM Agent Prompt Injection 後果治理" data-link-desc="production LLM agent 場景的 prompt injection 後果：tool spec 設計、agent loop 限制、review checkpoint、跟 incident workflow 的接合">llm-prompt-injection-in-agent</a></li>
<li>部署平台：<code>05-deployment-platform</code></li>
<li>可靠性：<code>06-reliability</code></li>
</ul>
]]></content:encoded></item></channel></rss>