<?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>Capacity-Planning on vishctl</title><link>https://vishctl.dev/tags/capacity-planning/</link><description>Recent content in Capacity-Planning on vishctl</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 15 Nov 2026 09:00:00 +0800</lastBuildDate><atom:link href="https://vishctl.dev/tags/capacity-planning/index.xml" rel="self" type="application/rss+xml"/><item><title>How Much GPU Memory Does an LLM Need? Weights, KV Cache, and Headroom</title><link>https://vishctl.dev/posts/llm-gpu-memory-sizing-weights-kv-cache/</link><pubDate>Sun, 15 Nov 2026 09:00:00 +0800</pubDate><guid>https://vishctl.dev/posts/llm-gpu-memory-sizing-weights-kv-cache/</guid><category>gpu</category><category>inference</category><category>kv-cache</category><category>vllm</category><category>capacity-planning</category><category>ai-infra</category><description>A simple, diagram-first guide to sizing a GPU for LLM serving: how big the weights are, how much KV cache each token costs, how many concurrent users fit, and a small calculator script to check your own model.</description><content:encoded><![CDATA[<p>&ldquo;Will this model fit on my GPU?&rdquo; sounds like one question. It is really two, and most sizing mistakes come from answering only the first.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Weights  = a fixed cost. Paid once, the same for 1 user or 100.
</span></span><span class="line"><span class="cl">KV cache = a variable cost. Grows with every token of every active request.
</span></span></code></pre></div><figure class="mermaid-figure">
  <div class="mermaid">flowchart TD
    subgraph ONE["One user"]
        direction LR
        A1["Weights<br/>16 GB"] a1@--> A2["KV cache<br/>0.5 GB"]
        A2 a2@--> A3["Total<br/>about 17 GB"]
    end

    subgraph MANY["Fifty users"]
        direction LR
        B1["Weights<br/>16 GB"] b1@--> B2["KV cache<br/>26 GB"]
        B2 b2@--> B3["Total<br/>about 42 GB"]
    end

    ONE ~~~ MANY
    a1@{ animate: true }
    a2@{ animate: true }
    b1@{ animate: true }
    b2@{ animate: true }

    classDef tree fill:#281d48,stroke:#a78bfa,color:#f1edff,stroke-width:2px;
    classDef gpu fill:#173f59,stroke:#22d3ee,color:#ecfeff,stroke-width:2px;
    classDef warn fill:#302410,stroke:#fbbf24,color:#fff3cf,stroke-width:2px;
    class A1,B1 tree;
    class A2,B2 gpu;
    class A3,B3 warn;</div>
  <figcaption>Top: with one user, the weights dominate. Bottom: with fifty users, the same weights are still there, but the KV cache is now the bigger number.</figcaption>
</figure>

<p>This post gives you two small formulas, one worked example, and a calculator script. By the end you can estimate how many concurrent requests a GPU (or a <a href="https://vishctl.dev/posts/kubernetes-gpu-sharing-time-slicing-mps-mig/">MIG slice</a>) can hold before you rent it.</p>
<p>For what the KV cache actually is, see <a href="https://vishctl.dev/posts/llm-basics-visual-guide/#5-context-is-the-input-kv-cache-saves-work">What Happens When You Ask an LLM a Question</a>.</p>
<h2 id="1-weights-the-fixed-cost">1. Weights: the fixed cost</h2>
<p>The weights are the model itself. Their size is one multiplication:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Weight memory = number of parameters x bytes per parameter
</span></span></code></pre></div><p>Bytes per parameter depends on the precision the model is stored in:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Precision</th>
            <th scope="col">Bytes per parameter</th>
            <th scope="col">8B model</th>
            <th scope="col">70B model</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>FP32</td>
            <td>4</td>
            <td>32 GB</td>
            <td>280 GB</td>
        </tr>
        <tr>
            <td>FP16 or BF16</td>
            <td>2</td>
            <td>16 GB</td>
            <td>140 GB</td>
        </tr>
        <tr>
            <td>FP8 or INT8</td>
            <td>1</td>
            <td>8 GB</td>
            <td>70 GB</td>
        </tr>
        <tr>
            <td>4-bit</td>
            <td>0.5</td>
            <td>4 GB</td>
            <td>35 GB</td>
        </tr>
    </tbody>
  </table>
</div>
<p>Most models are published in 16-bit, so <strong>&ldquo;parameters in billions, times two&rdquo; gives you gigabytes.</strong> An 8B model is about 16 GB. A 70B model is about 140 GB.</p>
<p>Two things to remember:</p>
<ul>
<li><strong>Real 4-bit files run a little larger</strong> than the table says, because some layers are kept at higher precision. Budget closer to 0.6 bytes per parameter.</li>
<li><strong>Weights do not grow with traffic.</strong> One user or a hundred, the bill is the same.</li>
</ul>
<h2 id="2-kv-cache-the-cost-per-token">2. KV cache: the cost per token</h2>
<p>Every token a request holds in context (prompt and answer) stores a key and a value in every layer. That is the KV cache, and it has its own formula:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">KV bytes per token = 2 x layers x KV heads x head dimension x bytes per number
</span></span></code></pre></div><figure class="mermaid-figure">
  <div class="mermaid">flowchart LR
    T["1 token"] k1@--> L["32 layers"]
    L k2@--> H["8 KV heads<br/>per layer"]
    H k3@--> D["128 numbers<br/>per head<br/>key and value"]
    D k4@--> R["About<br/>0.13 MB<br/>per token"]

    k1@{ animate: true }
    k2@{ animate: true }
    k3@{ animate: true }
    k4@{ animate: true }

    classDef input fill:#13264a,stroke:#38bdf8,color:#e6f6ff,stroke-width:2px;
    classDef tree fill:#281d48,stroke:#a78bfa,color:#f1edff,stroke-width:2px;
    classDef gpu fill:#173f59,stroke:#22d3ee,color:#ecfeff,stroke-width:3px;
    class T input;
    class L,H,D tree;
    class R gpu;</div>
  <figcaption>Where the per-token cost comes from. One token leaves a key and a value in every KV head of every layer.</figcaption>
</figure>

<h3 id="worked-example-llama-31-8b">Worked example: Llama 3.1 8B</h3>
<p>The four numbers come from the model&rsquo;s <code>config.json</code>:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Setting</th>
            <th scope="col">Field in config.json</th>
            <th scope="col">Value</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Layers</td>
            <td><code>num_hidden_layers</code></td>
            <td>32</td>
        </tr>
        <tr>
            <td>KV heads</td>
            <td><code>num_key_value_heads</code></td>
            <td>8</td>
        </tr>
        <tr>
            <td>Head dimension</td>
            <td><code>hidden_size / num_attention_heads</code></td>
            <td>4096 / 32 = 128</td>
        </tr>
        <tr>
            <td>Bytes per number</td>
            <td>2 for FP16</td>
            <td>2</td>
        </tr>
    </tbody>
  </table>
</div>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">2 x 32 x 8 x 128 x 2 = 131,072 bytes = about 0.13 MB per token
</span></span></code></pre></div><p>One eighth of a megabyte sounds tiny. Now multiply it by real context lengths:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Tokens held in context</th>
            <th scope="col">KV cache needed</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1,000 (a short chat)</td>
            <td>0.13 GB</td>
        </tr>
        <tr>
            <td>8,000 (a long chat or small RAG prompt)</td>
            <td>About 1 GB</td>
        </tr>
        <tr>
            <td>32,000 (a big document)</td>
            <td>About 4 GB</td>
        </tr>
        <tr>
            <td>128,000 (the full context window)</td>
            <td>About 17 GB</td>
        </tr>
    </tbody>
  </table>
</div>
<p>Look at the last row. <strong>One request using the full context window needs as much memory as the entire model.</strong></p>
<h3 id="read-the-numbers-for-your-own-model">Read the numbers for your own model</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">curl -sL https://huggingface.co/&lt;org&gt;/&lt;model&gt;/resolve/main/config.json <span class="se">\
</span></span></span><span class="line"><span class="cl">  <span class="p">|</span> jq <span class="s1">&#39;{num_hidden_layers, num_attention_heads, num_key_value_heads, hidden_size}&#39;</span>
</span></span></code></pre></div><p>Gated models need a Hugging Face token for this. You can also open <code>config.json</code> on the model page.</p>
<p>If <code>num_key_value_heads</code> is smaller than <code>num_attention_heads</code>, the model uses <strong>grouped-query attention</strong>. That is good news: Llama 3.1 8B has 32 attention heads but only 8 KV heads, so its cache is four times smaller than it would otherwise be.</p>
<h2 id="3-put-it-together-how-many-requests-fit">3. Put it together: how many requests fit?</h2>
<p>Take one H100 with 80 GB and serve Llama 3.1 8B in FP16 with <a href="https://vishctl.dev/posts/why-vllm-is-dominating-ai-inference-market/">vLLM</a>.</p>
<figure class="mermaid-figure">
  <div class="mermaid">flowchart TD
    G["H100<br/>80 GB"] m1@--> B["vLLM budget at 0.9<br/>72 GB"]
    B m2@--> W["Weights<br/>16 GB"]
    B m3@--> O["Runtime overhead<br/>about 2 GB"]
    B m4@--> K["KV cache pool<br/>about 54 GB"]
    K m5@--> T["Room for about<br/>410,000 tokens"]

    m1@{ animate: true }
    m2@{ animate: true }
    m3@{ animate: true }
    m4@{ animate: true }
    m5@{ animate: true }

    classDef input fill:#13264a,stroke:#38bdf8,color:#e6f6ff,stroke-width:2px;
    classDef tree fill:#281d48,stroke:#a78bfa,color:#f1edff,stroke-width:2px;
    classDef idle fill:#101e27,stroke:#324950,color:#8aa0a8;
    classDef gpu fill:#173f59,stroke:#22d3ee,color:#ecfeff,stroke-width:3px;
    class G,B input;
    class W tree;
    class O idle;
    class K,T gpu;</div>
  <figcaption>The memory budget from top to bottom. Whatever is left after the weights and overhead becomes the KV cache pool, and the pool decides how many requests fit.</figcaption>
</figure>

<p>Step by step:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Step</th>
            <th scope="col">Calculation</th>
            <th scope="col">Result</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1. Budget</td>
            <td>80 GB x 0.9 (vLLM&rsquo;s default <code>--gpu-memory-utilization</code>)</td>
            <td>72 GB</td>
        </tr>
        <tr>
            <td>2. Subtract weights</td>
            <td>72 - 16</td>
            <td>56 GB</td>
        </tr>
        <tr>
            <td>3. Subtract overhead</td>
            <td>56 - 2 (an estimate for runtime buffers)</td>
            <td>54 GB</td>
        </tr>
        <tr>
            <td>4. Convert to tokens</td>
            <td>54 GB / 0.13 MB per token</td>
            <td>About 410,000 tokens</td>
        </tr>
    </tbody>
  </table>
</div>
<p>That pool of 410,000 tokens is shared by every request in flight. So the answer to &ldquo;how many users?&rdquo; depends entirely on how long their requests are:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Average tokens per request</th>
            <th scope="col">Requests that fit at once</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>2,000 (short chat)</td>
            <td>About 205</td>
        </tr>
        <tr>
            <td>8,000 (long chat, small RAG)</td>
            <td>About 51</td>
        </tr>
        <tr>
            <td>32,000 (document analysis)</td>
            <td>About 12</td>
        </tr>
        <tr>
            <td>128,000 (full context)</td>
            <td>3</td>
        </tr>
    </tbody>
  </table>
</div>
<p>Same GPU, same model. <strong>The workload moves the answer from 205 to 3.</strong></p>
<p>This is a memory ceiling, not a speed promise. Long before 205 requests, latency will tell you to stop. Use the load-curve method in the <a href="https://vishctl.dev/posts/llm-inference-metrics-ttft-tpot-throughput-goodput/">inference metrics guide</a> to find where that happens.</p>
<h2 id="4-sizing-a-shared-gpu-slice">4. Sizing a shared GPU slice</h2>
<p>The same math tells you which <a href="https://vishctl.dev/posts/kubernetes-gpu-sharing-time-slicing-mps-mig/">MIG profile</a> to ask for. Here is the 8B model again, on three slices of an H100, with the same 0.9 budget and 2 GB of overhead:</p>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Slice</th>
            <th scope="col">Weights</th>
            <th scope="col">KV pool left</th>
            <th scope="col">Tokens</th>
            <th scope="col">4,000-token requests</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><code>1g.10gb</code></td>
            <td>FP16, 16 GB</td>
            <td>Does not fit</td>
            <td>0</td>
            <td>0</td>
        </tr>
        <tr>
            <td><code>1g.10gb</code></td>
            <td>4-bit, about 5 GB</td>
            <td>About 2 GB</td>
            <td>About 17,000</td>
            <td>4</td>
        </tr>
        <tr>
            <td><code>2g.20gb</code></td>
            <td>FP16, 16 GB</td>
            <td>Nothing left</td>
            <td>0</td>
            <td>0</td>
        </tr>
        <tr>
            <td><code>2g.20gb</code></td>
            <td>4-bit, about 5 GB</td>
            <td>About 11 GB</td>
            <td>About 85,000</td>
            <td>21</td>
        </tr>
        <tr>
            <td><code>3g.40gb</code></td>
            <td>FP16, 16 GB</td>
            <td>18 GB</td>
            <td>About 137,000</td>
            <td>34</td>
        </tr>
    </tbody>
  </table>
</div>
<p>Two lessons hide in that table:</p>
<ul>
<li><strong>&ldquo;The weights fit&rdquo; is not enough.</strong> On <code>2g.20gb</code> the FP16 weights squeeze in on paper (16 GB into 20 GB), but 0.9 of 20 GB is 18 GB, and after overhead there is nothing left for the cache. The server either refuses to start or handles almost no context.</li>
<li><strong>Quantizing the weights frees room for cache.</strong> The cache itself usually stays in FP16 even when the weights are 4-bit, so the per-token cost does not shrink.</li>
</ul>
<p>If the cache is your bottleneck, vLLM can also store it in 8-bit with <code>--kv-cache-dtype fp8</code>, which roughly halves the per-token cost. Test output quality before relying on it.</p>
<h2 id="try-it-a-sizing-calculator">Try it: a sizing calculator</h2>
<p>Want to test models, MIG slices, prefix caching, and multi-GPU TP interactively in your browser? Open the <a href="https://vishctl.dev/vram-calculator/">Interactive LLM VRAM &amp; KV-Cache Calculator</a>.</p>
<p>If you prefer a local CLI, this script does the whole calculation using only the Python standard library.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="ch">#!/usr/bin/env python3</span>
</span></span><span class="line"><span class="cl"><span class="s2">&#34;&#34;&#34;llm_fit.py: estimate weights, KV cache, and concurrency for one GPU.&#34;&#34;&#34;</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">argparse</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">p</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--params-b&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&#34;parameters, in billions&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--bytes-per-param&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">2.0</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&#34;2 = FP16, 1 = 8-bit, 0.6 = 4-bit&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--layers&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--kv-heads&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--head-dim&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--kv-bytes&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">2.0</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&#34;2 = FP16 cache, 1 = FP8 cache&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--gpu-gb&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--utilization&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">0.9</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--overhead-gb&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">2.0</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">p</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s2">&#34;--tokens-per-request&#34;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">4000</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">a</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">GB</span> <span class="o">=</span> <span class="mf">1e9</span>  <span class="c1"># decimal gigabytes, to match how model and GPU sizes are quoted</span>
</span></span><span class="line"><span class="cl"><span class="n">weights_gb</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">params_b</span> <span class="o">*</span> <span class="mf">1e9</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">bytes_per_param</span> <span class="o">/</span> <span class="n">GB</span>
</span></span><span class="line"><span class="cl"><span class="n">kv_per_token</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">layers</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">kv_heads</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">head_dim</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">kv_bytes</span>
</span></span><span class="line"><span class="cl"><span class="n">budget_gb</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">gpu_gb</span> <span class="o">*</span> <span class="n">a</span><span class="o">.</span><span class="n">utilization</span>
</span></span><span class="line"><span class="cl"><span class="n">pool_gb</span> <span class="o">=</span> <span class="n">budget_gb</span> <span class="o">-</span> <span class="n">weights_gb</span> <span class="o">-</span> <span class="n">a</span><span class="o">.</span><span class="n">overhead_gb</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Weights            : </span><span class="si">{</span><span class="n">weights_gb</span><span class="si">:</span><span class="s2">7.1f</span><span class="si">}</span><span class="s2"> GB&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;KV cache per token : </span><span class="si">{</span><span class="n">kv_per_token</span> <span class="o">/</span> <span class="mf">1e6</span><span class="si">:</span><span class="s2">7.3f</span><span class="si">}</span><span class="s2"> MB&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Usable budget      : </span><span class="si">{</span><span class="n">budget_gb</span><span class="si">:</span><span class="s2">7.1f</span><span class="si">}</span><span class="s2"> GB&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="n">pool_gb</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="k">raise</span> <span class="ne">SystemExit</span><span class="p">(</span><span class="s2">&#34;Does not fit: weights plus overhead exceed the budget.&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">tokens</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">pool_gb</span> <span class="o">*</span> <span class="n">GB</span> <span class="o">/</span> <span class="n">kv_per_token</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;KV cache pool      : </span><span class="si">{</span><span class="n">pool_gb</span><span class="si">:</span><span class="s2">7.1f</span><span class="si">}</span><span class="s2"> GB&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Tokens that fit    : </span><span class="si">{</span><span class="n">tokens</span><span class="si">:</span><span class="s2">,</span><span class="si">}</span><span class="s2">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Concurrent requests: </span><span class="si">{</span><span class="n">tokens</span> <span class="o">//</span> <span class="n">a</span><span class="o">.</span><span class="n">tokens_per_request</span><span class="si">:</span><span class="s2">,</span><span class="si">}</span><span class="s2"> at </span><span class="si">{</span><span class="n">a</span><span class="o">.</span><span class="n">tokens_per_request</span><span class="si">:</span><span class="s2">,</span><span class="si">}</span><span class="s2"> tokens each&#34;</span><span class="p">)</span>
</span></span></code></pre></div><p>Save it as <code>llm_fit.py</code>. The example from this post:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">python3 llm_fit.py --params-b <span class="m">8</span> --layers <span class="m">32</span> --kv-heads <span class="m">8</span> --head-dim <span class="m">128</span> --gpu-gb <span class="m">80</span>
</span></span></code></pre></div><p>The same model as a 4-bit file on a 20 GB MIG slice:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">python3 llm_fit.py --params-b <span class="m">8</span> --bytes-per-param 0.6 <span class="se">\
</span></span></span><span class="line"><span class="cl">  --layers <span class="m">32</span> --kv-heads <span class="m">8</span> --head-dim <span class="m">128</span> --gpu-gb <span class="m">20</span>
</span></span></code></pre></div><p>A 70B-class model (80 layers, 8 KV heads, head dimension 128) on one 80 GB card:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">python3 llm_fit.py --params-b <span class="m">70</span> --layers <span class="m">80</span> --kv-heads <span class="m">8</span> --head-dim <span class="m">128</span> --gpu-gb <span class="m">80</span>
</span></span></code></pre></div><p>That last one reports that it does not fit. A 70B model in FP16 needs about 140 GB for weights alone, so it needs two or more GPUs, or a quantized copy.</p>
<h2 id="check-the-estimate-against-reality">Check the estimate against reality</h2>
<p>A formula is a starting point. The server knows the real answer.</p>
<p>When vLLM starts, it logs how much memory it gave to the KV cache and the maximum concurrency for full-length requests. Look for it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">kubectl logs deploy/vllm <span class="p">|</span> grep -i -E <span class="s2">&#34;kv cache|maximum concurrency&#34;</span>
</span></span></code></pre></div><p>While it runs, the live fill level is a Prometheus metric:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">curl -s http://localhost:8000/metrics <span class="p">|</span> grep kv_cache_usage_perc
</span></span></code></pre></div><p>If that gauge sits near 1.0 under normal load, the pool is too small for your traffic. Requests will start to queue or be preempted, and it is time to shorten context, cap concurrency, or <a href="https://vishctl.dev/posts/autoscaling-llm-inference-kubernetes-keda/">add replicas</a>.</p>
<h2 id="what-the-estimate-leaves-out">What the estimate leaves out</h2>
<div class="table-scroll">
  <table>
    <thead>
        <tr>
            <th scope="col">Not counted</th>
            <th scope="col">Why it matters</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Prefill working memory</td>
            <td>A very long prompt needs temporary buffers while it is being read</td>
        </tr>
        <tr>
            <td>Vision or audio encoders</td>
            <td>Multimodal models carry extra weights beyond the language model</td>
        </tr>
        <tr>
            <td>LoRA adapters</td>
            <td>Each loaded adapter adds a little weight memory</td>
        </tr>
        <tr>
            <td>Mixture of experts</td>
            <td>All experts are loaded, even though only a few run per token</td>
        </tr>
        <tr>
            <td>Multiple GPUs</td>
            <td>Tensor parallelism splits weights and cache, but adds per-GPU overhead</td>
        </tr>
        <tr>
            <td>Other tenants</td>
            <td>On a time-sliced GPU, your neighbor&rsquo;s memory comes out of the same card</td>
        </tr>
    </tbody>
  </table>
</div>
<p>For a first estimate, the 2 GB overhead in the script covers the small items. For a purchase decision, load the model and read the startup log.</p>
<h2 id="what-to-do-when-it-does-not-fit">What to do when it does not fit</h2>
<figure class="mermaid-figure">
  <div class="mermaid">flowchart TD
    Q1["Do the weights fit<br/>in the budget?"] f1@-->|"No"| W["Quantize weights<br/>or use a bigger GPU<br/>or split across GPUs"]
    Q1 f2@-->|"Yes"| Q2["Is the KV pool big enough<br/>for your traffic?"]
    Q2 f3@-->|"No"| K["Shorten context<br/>cap concurrency<br/>FP8 cache or add replicas"]
    Q2 f4@-->|"Yes"| OK["It fits<br/>now measure latency"]

    f1@{ animate: true }
    f2@{ animate: true }
    f3@{ animate: true }
    f4@{ animate: true }

    classDef question fill:#281d48,stroke:#a78bfa,color:#f1edff,stroke-width:2px;
    classDef warn fill:#302410,stroke:#fbbf24,color:#fff3cf,stroke-width:2px;
    classDef gpu fill:#173f59,stroke:#22d3ee,color:#ecfeff,stroke-width:3px;
    class Q1,Q2 question;
    class W,K warn;
    class OK gpu;</div>
  <figcaption>Two different problems with two different sets of fixes. First check whether the weights fit, then whether the cache pool is big enough.</figcaption>
</figure>

<p>One more lever sits outside the GPU. If many requests share a long prefix, prefix caching stores that part once instead of once per request. That is the idea behind <a href="https://vishctl.dev/posts/why-lmcache-is-needed/">LMCache</a> and cache-aware routing in <a href="https://vishctl.dev/posts/llm-d-kubernetes-inference-routing/">llm-d</a>.</p>
<h2 id="cheat-sheet">Cheat sheet</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Weight memory           = parameters x bytes per parameter; fixed, independent of traffic
</span></span><span class="line"><span class="cl">FP16 rule of thumb      = parameters in billions x 2 = gigabytes
</span></span><span class="line"><span class="cl">KV cache per token      = 2 x layers x KV heads x head dimension x bytes per number
</span></span><span class="line"><span class="cl">Llama 3.1 8B            = about 0.13 MB of KV cache per token in FP16
</span></span><span class="line"><span class="cl">KV cache pool           = GPU budget minus weights minus overhead
</span></span><span class="line"><span class="cl">Tokens that fit         = KV cache pool / KV bytes per token
</span></span><span class="line"><span class="cl">Concurrency ceiling     = tokens that fit / average tokens per request
</span></span><span class="line"><span class="cl">gpu-memory-utilization  = vLLM&#39;s share of the GPU, 0.9 by default
</span></span><span class="line"><span class="cl">Grouped-query attention = fewer KV heads than attention heads, so a smaller cache
</span></span><span class="line"><span class="cl">kv-cache-dtype fp8      = store the cache in 8-bit to roughly halve the per-token cost
</span></span><span class="line"><span class="cl">Rule of thumb           = weights decide if it starts, KV cache decides how many users it serves
</span></span></code></pre></div><p>Next steps: pick a slice size with the <a href="https://vishctl.dev/posts/kubernetes-gpu-sharing-time-slicing-mps-mig/">GPU sharing guide</a>, find the real limit with the <a href="https://vishctl.dev/posts/llm-inference-metrics-ttft-tpot-throughput-goodput/">inference metrics guide</a>, and grow past one GPU with the <a href="https://vishctl.dev/posts/autoscaling-llm-inference-kubernetes-keda/">autoscaling guide</a>.</p>
]]></content:encoded></item></channel></rss>