<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Quantization on kenji.blog</title><link>http://kenji.blog/en/tags/quantization/</link><description>Recent content in Quantization on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>kenjinote</copyright><lastBuildDate>Fri, 11 Sep 2026 00:00:00 +0900</lastBuildDate><atom:link href="http://kenji.blog/en/tags/quantization/index.xml" rel="self" type="application/rss+xml"/><item><title>An Explanation of How llama.cpp Quantization Technology (GGUF) Works</title><link>http://kenji.blog/en/p/llama-cpp-quantization-gguf/</link><pubDate>Fri, 11 Sep 2026 00:00:00 +0900</pubDate><guid>http://kenji.blog/en/p/llama-cpp-quantization-gguf/</guid><description>&lt;img src="http://kenji.blog/p/llama-cpp-quantization-gguf/img/eyecatch.jpg" alt="Featured image of post An Explanation of How llama.cpp Quantization Technology (GGUF) Works" />&lt;h2 id="1-introduction-why-do-llms-need-quantization">1. Introduction: Why Do LLMs Need Quantization?
&lt;/h2>&lt;p>The recent evolution of Large Language Models (LLMs) has been remarkable, but behind the scenes, serious problems of &amp;ldquo;exhaustion of computational resources&amp;rdquo; and &amp;ldquo;memory bandwidth bottlenecks&amp;rdquo; have emerged. For example, if a 70B (70 billion) parameter model like Llama 3 is loaded into memory in standard 16-bit floating-point (FP16), the parameters alone consume about 140GB of VRAM/RAM. When the context during inference (KV cache) is added to this, it will not run unless multiple high-end GPUs for data centers (such as NVIDIA A100 80GB or H100 80GB) are clustered together.&lt;/p>
&lt;p>To save individual developers and edge devices (like MacBooks and standard gaming PCs) wanting to run LLMs, &lt;strong>llama.cpp&lt;/strong> and its core &lt;strong>Quantization&lt;/strong> technology appeared as a savior. In particular, the &lt;strong>GGUF (GPT-Generated Unified Format)&lt;/strong> file format and the advanced block-wise quantization algorithm called &lt;strong>k-quants&lt;/strong> are revolutionary methods that compress the model size to a fraction while minimizing the degradation of model accuracy (Perplexity).&lt;/p>
&lt;p>This article thoroughly explains the mathematical background of quantization in llama.cpp, its differences from the GGML format, the detailed structure of the GGUF format, and the internal mechanisms of k-quants.&lt;/p>
&lt;hr>
&lt;h2 id="2-mathematical-foundations-of-quantization">2. Mathematical Foundations of Quantization
&lt;/h2>&lt;p>Quantization in the context of LLMs refers to the operation of mapping continuous values (or high-precision floating-point numbers) to discrete values with a smaller number of bits (INT8, INT4, INT3, etc.).&lt;/p>
&lt;h3 id="21-basic-formulas-of-linear-quantization">2.1. Basic Formulas of Linear Quantization
&lt;/h3>&lt;p>The simplest approach is linear quantization (Min-Max quantization). Let $W$ be the original high-precision weight tensor, and $W_q$ be the quantized integer tensor.&lt;/p>
$$ W_q = \text{round}\left( \frac{W}{S} \right) + Z $$
&lt;p>Here,&lt;/p>
&lt;ul>
&lt;li>$S$ is the &lt;strong>Scale Factor&lt;/strong>, which determines the step size (resolution) of the quantization.&lt;/li>
&lt;li>$Z$ is the &lt;strong>Zero-point&lt;/strong>, which is a bias value used to shift what integer value the real number $0.0$ corresponds to after quantization.&lt;/li>
&lt;li>$\text{round}(\cdot)$ is a function that rounds to the nearest integer.&lt;/li>
&lt;/ul>
&lt;p>By dequantization, the approximate real weights $\tilde{W}$ are restored during inference.&lt;/p>
$$ \tilde{W} = S \times (W_q - Z) $$
&lt;h3 id="22-symmetric-quantization-vs-asymmetric-quantization">2.2. Symmetric Quantization vs. Asymmetric Quantization
&lt;/h3>&lt;p>Depending on the treatment of the zero-point $Z$, it is broadly divided into two methods.&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Asymmetric Quantization&lt;/strong>
It maps using the minimum value $W_{\min}$ and the maximum value $W_{\max}$ of the data.
&lt;/p>
$$ S = \frac{W_{\max} - W_{\min}}{2^b - 1}, \quad Z = \text{round}\left(-\frac{W_{\min}}{S}\right) $$
&lt;p>
Here $b$ is the number of quantization bits (e.g., for 4 bits, $2^4-1 = 15$). Because it is necessary to hold $Z$, the computation and memory overhead slightly increase.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Symmetric Quantization&lt;/strong>
It uses the maximum absolute value of the data and maps around zero ($Z=0$).
&lt;/p>
$$ S = \frac{\max(|W_{\max}|, |W_{\min}|)}{2^{b-1} - 1}, \quad Z = 0 $$
&lt;p>
Early quantization in llama.cpp (like the legacy Q4_0) adopted symmetric quantization, and since there is no $Z$ term, it has the advantage of significantly speeding up dot product calculations with SIMD instructions.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="3-the-evolution-from-ggml-to-gguf-and-file-structure">3. The Evolution from GGML to GGUF and File Structure
&lt;/h2>&lt;p>When talking about llama.cpp, the tensor math library &lt;strong>GGML&lt;/strong> written in C++, and its derived file format &lt;strong>GGUF&lt;/strong>, are indispensable.&lt;/p>
&lt;h3 id="31-issues-with-ggml">3.1. Issues with GGML
&lt;/h3>&lt;p>Early llama.cpp used the &lt;code>ggml&lt;/code> format (and variants like &lt;code>ggjt&lt;/code>). However, these had the following problems:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Lack of extensibility:&lt;/strong> Magic numbers and hyperparameters were hardcoded in fixed lengths and a fixed order, causing breaking changes every time a new model architecture (e.g., Llama, Falcon, Mixtral, etc.) or a new tokenizer was added.&lt;/li>
&lt;li>&lt;strong>Loss of backward compatibility:&lt;/strong> The format was frequently updated, leading to many situations where old model files could not be read by the latest llama.cpp.&lt;/li>
&lt;/ul>
&lt;h3 id="32-the-birth-of-the-gguf-format">3.2. The Birth of the GGUF Format
&lt;/h3>&lt;p>&lt;strong>GGUF&lt;/strong>, introduced in August 2023, is a highly versatile format designed to solve these problems. Its most significant feature is the adoption of a &lt;strong>Key-Value-based metadata structure&lt;/strong>.&lt;/p>
&lt;p>The Mermaid diagram below abstracts the file structure of GGUF.&lt;/p>
&lt;div class="mermaid">graph TD
A["GGUF File"] --> B["Header (Magic, Version)"]
A --> C["Metadata (Key-Value Pairs)"]
A --> D["Tensor Info (Name, Shape, Offset)"]
A --> E["Tensor Data (Binary payload)"]
C --> C1["general.architecture: llama"]
C --> C2["llama.context_length: 4096"]
C --> C3["tokenizer.ggml.tokens: [...]"]
E --> E1["Layer 0 Weights"]
E --> E2["Layer 1 Weights"]
E --> E3["..."]&lt;/div>
&lt;p>&lt;strong>Main Advantages of GGUF:&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Flexibility:&lt;/strong> All model hyperparameters, RoPE (Rotary Positional Embedding) settings, tokenizer vocabulary data, etc., are stored as named Key-Value pairs. Unknown keys are ignored, making it easy to add new features.&lt;/li>
&lt;li>&lt;strong>Endian-independent:&lt;/strong> GGUF adopts little-endian by default, but it explicitly holds a flag, making it safely portable across different architectures.&lt;/li>
&lt;li>&lt;strong>Optimized for mmap (Memory Mapping):&lt;/strong> Tensor data is aligned (padded) to specific boundaries and can be mapped directly from the disk into the memory space using the OS&amp;rsquo;s &lt;code>mmap()&lt;/code> system call. As a result, the initialization time for loading the model becomes virtually zero.&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="4-the-depths-of-k-quants-advanced-block-wise-quantization">4. The Depths of k-quants: Advanced Block-wise Quantization
&lt;/h2>&lt;p>The true value of the GGUF format lies in the mechanism called &lt;strong>k-quants (K-quantization)&lt;/strong>, which is responsible for compressing model weights.&lt;/p>
&lt;p>Normally, the weights of a neural network take a shape close to a normal distribution when looking at the entire layer, but Outliers exist locally. If the weights of an entire layer are quantized with a uniform scale factor $S$, the information of small weights will be completely lost due to being dragged by outliers.&lt;/p>
&lt;p>To prevent this, llama.cpp performs &lt;strong>Block-wise Quantization&lt;/strong>. The weight tensor is divided into small blocks (e.g., 32 elements or 256 elements), and each block is given its own unique scale factor (and zero-point).&lt;/p>
&lt;h3 id="41-limitations-of-legacy-quantization-q4_0-q4_1">4.1. Limitations of Legacy Quantization (Q4_0, Q4_1)
&lt;/h3>&lt;p>The early &lt;code>Q4_0&lt;/code> treated 32 FP16 weights as one block and shared one FP16 scale factor.&lt;/p>
&lt;ul>
&lt;li>Block size: 32&lt;/li>
&lt;li>Memory: 1 scale (16-bit) + 32 4-bit weights (128-bit) = 144-bit&lt;/li>
&lt;li>Effective bits per weight (bpw): $144 / 32 = 4.5$ bpw&lt;/li>
&lt;/ul>
&lt;p>Even this was excellent enough, but the limits of accuracy and compression ratio became apparent. Thus, &lt;strong>k-quants&lt;/strong> emerged, featuring a more complex and sophisticated hierarchical structure.&lt;/p>
&lt;h3 id="42-hierarchical-structure-of-super-blocks-and-sub-blocks-example-of-q4_k_m">4.2. Hierarchical Structure of Super-blocks and Sub-blocks (Example of Q4_K_M)
&lt;/h3>&lt;p>k-quants has a hierarchical structure consisting of large &amp;ldquo;Super-blocks&amp;rdquo; and small &amp;ldquo;Sub-blocks&amp;rdquo; contained within them. This allows the metadata itself (such as scale values) to be quantized, maintaining accuracy while reducing bpw to the absolute limit.&lt;/p>
&lt;p>Let&amp;rsquo;s look at the structure of &lt;strong>Q4_K_M&lt;/strong>, which is the most popular configuration. Q4_K_M uses a super-block of 256 elements.&lt;/p>
&lt;div class="mermaid">graph TD
A["Super-block (256 weights)"] --> B["Scale metadata (FP16/INT8)"]
A --> C["Sub-block 0 (32 weights, 4-bit)"]
A --> D["Sub-block 1 (32 weights, 4-bit)"]
A --> E["..."]
A --> F["Sub-block 7 (32 weights, 4-bit)"]
B --> B1["Super-scale (FP16)"]
B --> B2["Sub-scales (8 x 6-bit)"]
B --> B3["Sub-mins (8 x 6-bit)"]&lt;/div>
&lt;p>The actual structure in C++ (GGML) is defined conceptually as follows:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;span class="lnt">5
&lt;/span>&lt;span class="lnt">6
&lt;/span>&lt;span class="lnt">7
&lt;/span>&lt;span class="lnt">8
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-cpp" data-lang="cpp">&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Conceptual structure of block_q4_K in llama.cpp
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="cp">#define QK_K 256
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">struct&lt;/span> &lt;span class="nc">block_q4_K&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kt">uint8_t&lt;/span> &lt;span class="n">d&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">];&lt;/span> &lt;span class="c1">// Super-scale for the entire super-block (e.g., FP16 x 2)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kt">uint8_t&lt;/span> &lt;span class="n">scales&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="mi">12&lt;/span>&lt;span class="p">];&lt;/span> &lt;span class="c1">// Packed data of 6-bit scales and 6-bit minimums (zero-points) for 8 sub-blocks (32 elements each)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="kt">uint8_t&lt;/span> &lt;span class="n">qs&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">QK_K&lt;/span>&lt;span class="o">/&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">];&lt;/span> &lt;span class="c1">// Weight data quantized in 4-bit (256 elements / 2 = 128 bytes)
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;strong>Mathematical Dequantization Process:&lt;/strong>&lt;/p>
&lt;p>The approximate real value $\tilde{W}_{i, j}$ of element $j$ ($0 \le j &lt; 32$) within sub-block $i$ ($0 \le i &lt; 8$) is calculated as follows:&lt;/p>
$$ \tilde{W}_{i, j} = S_{\text{super}} \times s_i \times (w_{i, j} - m_i) $$
&lt;ul>
&lt;li>$S_{\text{super}}$: Floating-point scale for the entire super-block&lt;/li>
&lt;li>$s_i$: 6-bit scale quantized for sub-block $i$&lt;/li>
&lt;li>$m_i$: 6-bit minimum (zero-point) quantized for sub-block $i$&lt;/li>
&lt;li>$w_{i, j}$: 4-bit quantized weight ($0 \dots 15$)&lt;/li>
&lt;/ul>
&lt;p>This hierarchical structure drastically reduces the memory footprint occupied by the scale factors themselves while maintaining adaptability to outliers. Q4_K_M achieves around &lt;strong>4.8 bpw&lt;/strong> overall.&lt;/p>
&lt;h3 id="43-diverse-k-quants-options">4.3. Diverse k-quants Options
&lt;/h3>&lt;p>llama.cpp provides a number of variations depending on the purpose. The suffixes after &amp;ldquo;K&amp;rdquo; (S, M, L) represent the relative size.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left">Format&lt;/th>
&lt;th style="text-align:center">BPW (Bits per Weight)&lt;/th>
&lt;th style="text-align:left">Overview and Features&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q2_K&lt;/strong>&lt;/td>
&lt;td style="text-align:center">2.5～3.3&lt;/td>
&lt;td style="text-align:left">Extreme compression. Accuracy drops significantly, but meant for environments with extremely low VRAM.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q3_K_M&lt;/strong>&lt;/td>
&lt;td style="text-align:center">3.3&lt;/td>
&lt;td style="text-align:left">Standard for 3-bit quantization. It degrades more than Q4 but often stays within acceptable limits.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q4_K_M&lt;/strong>&lt;/td>
&lt;td style="text-align:center">4.8&lt;/td>
&lt;td style="text-align:left">&lt;strong>Recommended sweet spot&lt;/strong>. Achieves both a halving of model size and maintenance of accuracy.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q5_K_M&lt;/strong>&lt;/td>
&lt;td style="text-align:center">5.5&lt;/td>
&lt;td style="text-align:left">When higher accuracy is required. Positioned halfway between Q4 and FP16.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q6_K&lt;/strong>&lt;/td>
&lt;td style="text-align:center">6.6&lt;/td>
&lt;td style="text-align:left">Maintains Perplexity almost equivalent to FP16, but the file size is relatively large.&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Q8_0&lt;/strong>&lt;/td>
&lt;td style="text-align:center">8.5&lt;/td>
&lt;td style="text-align:left">Equivalent to INT8. Mainly used for intermediate tensors for computation during inference, or only in the final layer.&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;ul>
&lt;li>Note: The actual BPW is averaged over the entire model, as Mixed Quantization is performed depending on the model&amp;rsquo;s tensors (for example, whether it is a Q/K/V projection in Attention, or FFN weights). Optimizations such as quantizing important tensors with Q6 and others with Q4 are performed internally.&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="5-performance-optimization-during-inference-simd-and-cuda-architectures">5. Performance Optimization During Inference: SIMD and CUDA Architectures
&lt;/h2>&lt;p>Merely loading a GGUF model into memory does not speed up inference. The majority of LLM inference is &amp;ldquo;Matrix Multiplication&amp;rdquo; (Matrix-Vector Multiplication, abbreviated as GEMV, or Matrix-Matrix, GEMM). The key is how to speed up the multiply-accumulate operations between quantized weights and activations (input data) held in FP16 (or FP32).&lt;/p>
&lt;h3 id="51-utilizing-simd-instructions-in-cpu-environments">5.1. Utilizing SIMD Instructions in CPU Environments
&lt;/h3>&lt;p>The reason llama.cpp boasts tremendous speed in CPU inference lies in &lt;strong>SIMD (Single Instruction, Multiple Data)&lt;/strong> optimization at the assembly level.
For example, it fully utilizes the &lt;strong>AVX2&lt;/strong> or &lt;strong>AVX-512&lt;/strong> instruction sets on Intel/AMD CPUs, and &lt;strong>ARM NEON&lt;/strong> on Apple Silicon.&lt;/p>
&lt;p>During inference, it does not bother to revert (dequantize) $W_q$ to FP32 before multiplying.
The activation side is also dynamically quantized in blocks (Dynamic Quantization, typically to INT8), and integer operations of &lt;strong>INT8 $\times$ INT4&lt;/strong> are computed at once using special SIMD dot-product instructions (e.g., &lt;code>vdpaddd&lt;/code> or &lt;code>_mm256_madd_epi16&lt;/code>). By converting it back to FP32 in the final accumulator and multiplying by the scale factor, it achieves incredible throughput.&lt;/p>
&lt;h3 id="52-offloading-in-gpu-environments-cublas--cuda">5.2. Offloading in GPU Environments (cuBLAS / CUDA)
&lt;/h3>&lt;p>Recent versions of llama.cpp have strong support for NVIDIA GPUs (CUBLAS / CUDA) as well as CPUs.
It is possible to offload some or all layers of a GGUF file to VRAM (using the &lt;code>--n-gpu-layers&lt;/code> option).&lt;/p>
&lt;div class="mermaid">sequenceDiagram
participant User
participant CPU_RAM as CPU &amp; RAM (mmap)
participant VRAM as GPU VRAM
participant Compute as Tensor Cores
User->>CPU_RAM: Load GGUF (mmap)
CPU_RAM->>VRAM: Offload Layers (e.g. 30/32 layers)
Note over CPU_RAM, VRAM: Data remains quantized in VRAM
User->>Compute: Forward Pass (Input Tokens)
Compute->>VRAM: Fetch Quantized Weights
Compute->>Compute: Dequantize to FP16 on-the-fly in SRAM
Compute->>Compute: Matrix Multiplication (cuBLAS / Custom Kernels)
Compute->>User: Output Logits&lt;/div>
&lt;p>When computing on a GPU, the Memory Bandwidth of VRAM becomes the biggest bottleneck. Because the weights are compressed with k-quants, the amount of data transferred from VRAM to the GPU&amp;rsquo;s compute units (SM: Streaming Multiprocessors or Tensor Cores) is reduced to 1/3 to 1/4. The moment the weights reach the compute units, they are dequantized to FP16 on-the-fly, and matrix multiplication is executed ultra-fast using Tensor Cores.
In other words, quantization is performed &lt;strong>not to &amp;ldquo;reduce the amount of computation,&amp;rdquo; but to &amp;ldquo;reduce the amount of memory transfer&amp;rdquo;&lt;/strong>.&lt;/p>
&lt;hr>
&lt;h2 id="6-specific-examples-of-memory-usage-vs-performance-trade-offs">6. Specific Examples of Memory Usage vs. Performance Trade-offs
&lt;/h2>&lt;p>Here, let&amp;rsquo;s look at the required specifications for each quantization level of GGUF, taking the Llama 3 8B model as an example. (The numbers are rough estimates.)&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left">Model / Quantization&lt;/th>
&lt;th style="text-align:left">File Size&lt;/th>
&lt;th style="text-align:left">Required VRAM/RAM&lt;/th>
&lt;th style="text-align:left">Inference Speed (Est.)&lt;/th>
&lt;th style="text-align:left">Perplexity Degradation&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (FP16)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 16 GB&lt;/td>
&lt;td style="text-align:left">18 GB or more&lt;/td>
&lt;td style="text-align:left">Baseline&lt;/td>
&lt;td style="text-align:left">None (Base)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (Q8_0)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 8.5 GB&lt;/td>
&lt;td style="text-align:left">10 GB or more&lt;/td>
&lt;td style="text-align:left">Fast&lt;/td>
&lt;td style="text-align:left">Near zero&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (Q6_K)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 6.6 GB&lt;/td>
&lt;td style="text-align:left">8 GB or more&lt;/td>
&lt;td style="text-align:left">Very Fast&lt;/td>
&lt;td style="text-align:left">Minimal&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (Q4_K_M)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 4.9 GB&lt;/td>
&lt;td style="text-align:left">6.5 GB or more&lt;/td>
&lt;td style="text-align:left">Fastest / Optimal&lt;/td>
&lt;td style="text-align:left">Acceptable / Slight&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (Q3_K_M)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 3.9 GB&lt;/td>
&lt;td style="text-align:left">5.5 GB or more&lt;/td>
&lt;td style="text-align:left">Fastest&lt;/td>
&lt;td style="text-align:left">Somewhat noticeable&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">&lt;strong>Llama-3-8B (Q2_K)&lt;/strong>&lt;/td>
&lt;td style="text-align:left">Approx. 3.0 GB&lt;/td>
&lt;td style="text-align:left">4.5 GB or more&lt;/td>
&lt;td style="text-align:left">Fast&lt;/td>
&lt;td style="text-align:left">Obvious degradation&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Important Note (Impact of KV Cache):&lt;/strong>
In LLM inference, as the context length (number of prompt tokens) becomes longer, not only the model weights but also the memory consumption of the &lt;strong>KV Cache&lt;/strong>, which stores past Attention states, increases explosively.
For example, if the context is 8192 tokens, the KV cache alone consumes several GBs. Therefore, in actual operation, it is necessary to secure a margin (Headroom) of &lt;code>Model File Size + Approx. 1.5GB to 3GB&lt;/code>. The reason Q4_K_M is recommended is because it perfectly strikes a balance, safely running on standard GPUs with 8GB VRAM (like RTX 3060 / 4060) even with this KV cache reserved.&lt;/p>
&lt;p>In recent versions of llama.cpp, a &lt;strong>feature to quantize the KV cache itself to Q8_0 or Q4_0&lt;/strong> has also been added, and continuous efforts are being made to further extend the context length.&lt;/p>
&lt;hr>
&lt;h2 id="7-conclusion">7. Conclusion
&lt;/h2>&lt;p>In this article, we delved deep into and explained the internal structure of the GGUF format and k-quants quantization technology, which are the heart of llama.cpp.&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Flexibility of GGUF:&lt;/strong> With a Key-Value-based metadata structure, it has built a robust ecosystem capable of following the rapid evolution of LLMs (the emergence of new model architectures) without breaking changes.&lt;/li>
&lt;li>&lt;strong>Extreme Compression with k-quants:&lt;/strong> By managing hierarchical scale factors with super-blocks and sub-blocks, it achieved incredible compression of an average of 4.8 bits per weight (Q4_K_M) while preserving outlier information.&lt;/li>
&lt;li>&lt;strong>Elimination of Memory Bandwidth Bottleneck:&lt;/strong> Through advanced kernel implementations in SIMD and CUDA, performing computations while dequantizing on-the-fly reduces VRAM transfer volumes and dramatically improves inference speed.&lt;/li>
&lt;/ol>
&lt;p>The technological prowess of llama.cpp, which advances the democratization of AI, is no exaggeration to call one of the peaks of modern software engineering, transcending the boundaries of a mere tool. By understanding the quantization algorithms and the mechanics of the GGUF format, you will be able to choose the most suitable model for your environment and perform performance tuning more accurately.&lt;/p>
&lt;h3 id="reference-links">Reference Links
&lt;/h3>&lt;ul>
&lt;li>&lt;a class="link" href="https://github.com/ggerganov/llama.cpp" target="_blank" rel="noopener"
>llama.cpp GitHub Repository&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://github.com/ggerganov/ggml/blob/master/docs/gguf.md" target="_blank" rel="noopener"
>GGUF Format Specification&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://github.com/ggerganov/llama.cpp/pull/1684" target="_blank" rel="noopener"
>K-quants Implementation PR&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>(End)&lt;/p></description></item></channel></rss>