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M Quantization
|
| 8 |
+
|
| 9 |
+
**HPC-Quantize V2** is a pure-Python/NumPy ultra-low-bit quantization engine and GGUF re-quantizer (with an integrated desktop GUI in `gui.py`) designed for **2-bit class (`Q2_K`, 2.625 bpw)** compression of large language models.
|
| 10 |
+
|
| 11 |
+
> **Core thesis:** At 2 bits per weight (only 4 levels $q \in \{0,1,2,3\}$ and 4-bit sub-block scales $Ls, Lm \in \{0..15\}$), quantization is not an independent rounding problem. It is a **coupled bilinear factorization and global sequence-selection problem** best solved by extracting discrete lattice coordinates from **geometry-preserving weight analogs**, projecting them onto the true weights in an orthogonalized affine basis, and optimizing cross-block state trajectories via a **sequential Sieve and 36-state Viterbi dynamic program**.
|
| 12 |
+
|
| 13 |
+
HPC-Quantize V2 (`hpc_quantize_v2/` + `gui.py`) has **zero C or `ctypes` dependencies**, reads and writes standard `llama.cpp`-compatible `.gguf` models and `.gguf`/`.dat` importance matrices (`iMatrix`), and requires no custom inference kernels.
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## Key Highlights in V2
|
| 18 |
+
|
| 19 |
+
1. **Geometry-Preserving Weight Analogs (`hpc_quantize_v2/analog.py`)**:
|
| 20 |
+
Constructs a manifold of surrogate weight blocks $\tilde{x}$ that preserve exact sub-block first and second importance-weighted moments ($\mu_j, \sigma_j^2$), rank order, total block energy, and the $2\times 2$ D6/Vesica fold Gram matrix while regularizing heavy-tailed outliers toward the 4-level `Q2_K` lattice.
|
| 21 |
+
2. **Coupled Orthogonal-Basis Bilinear `Q2_K` Factorization (`hpc_quantize_v2/factored.py`)**:
|
| 22 |
+
Replaces independent 1-D scale/offset heuristics with an orthogonalized sub-block decomposition $\hat{x}_{j,k} = s_j(q_{j,k} - \bar{q}_j) + (s_j\bar{q}_j - t_j)$, coupled offset compensation $t_j^{\text{coupled}} = \max(0, (d \cdot Ls_j \cdot S_{wq,j} - S_{x,j})/S_{w,j})$, an exact superblock $2\times 2$ bilinear $(d, d_{\min})$ solve, and a vectorized $16\times 16$ joint $(Ls_j, Lm_j)$ sub-block scan.
|
| 23 |
+
3. **Full HPC Machinery in Vectorized NumPy**:
|
| 24 |
+
Preserves kurtosis-gated outlier clamping, batched GGML seeds, the 6-state sequential Sieve with bounded parity back-action, the 36-state Viterbi sequence optimizer, D6/Vesica + DC fold-pyramid error shaping, row-aware rolling DC residual carry, and monotone local-safety floors.
|
| 25 |
+
4. **Direct GGUF-to-GGUF + iMatrix Pipeline & GUI (`hpc_quantize_v2/gguf.py`, `gui.py`)**:
|
| 26 |
+
Ingests `F32`, `F16`, `BF16`, `Q8_0`, `Q4_0`, and `Q2_K` source GGUFs plus `llama.cpp` iMatrix files, copies metadata/tokenizer KVs and non-quantized tensors byte-for-byte, and emits a stock `llama.cpp`-compatible `Q2_K` GGUF.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## Why Standard `Q2_K` Quantization Breaks Down
|
| 31 |
+
|
| 32 |
+
A `Q2_K` superblock encodes 256 weights in 84 bytes (16 sub-blocks of 16 weights each):
|
| 33 |
+
|
| 34 |
+
$$
|
| 35 |
+
\hat{x}_{16j + k} = d \cdot Ls_j \cdot q_{16j + k} - d_{\min} \cdot Lm_j,
|
| 36 |
+
\qquad
|
| 37 |
+
q \in \{0,1,2,3\}, \quad Ls_j, Lm_j \in \{0,\ldots,15\}, \quad d, d_{\min} \in \mathbb{F}_{16}.
|
| 38 |
+
$$
|
| 39 |
+
|
| 40 |
+
When factoring raw LLM weights directly into this bilinear form, three structural failure modes occur:
|
| 41 |
+
|
| 42 |
+
1. **Intra-sub-block outlier collapse**: A single outlier weight in a 16-weight sub-block stretches the span $x_{\max} - x_{\min}$, forcing 13β15 of the 16 weights into a single bin ($q=1$ or $q=2$) and reducing effective precision below 1 bit.
|
| 43 |
+
2. **Inter-sub-block scale crushing**: A single high-dispersion sub-block inflates the shared superblock scale $d$, forcing $Ls_j \in \{1, 2\}$ across the remaining 15 sub-blocks.
|
| 44 |
+
3. **Scale-offset cross-term neglect**: Because $q_{j,k} \in \{0,1,2,3\}$ is non-negative, its importance-weighted mean $\bar{q}_j = \frac{\sum_k w_{j,k} q_{j,k}}{\sum_k w_{j,k}}$ is strictly positive. Rounding the scale $s_j \to d \cdot Ls_j$ shifts the sub-block mean by $(d \cdot Ls_j - s_j)\bar{q}_j$. Factoring $s_j \approx d \cdot Ls_j$ and $t_j \approx d_{\min} \cdot Lm_j$ independently leaves this mean shift uncompensated.
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
## Mathematical Architecture of HPC-Quantize V2
|
| 49 |
+
|
| 50 |
+
### 1. Geometry-Preserving Weight Analogs
|
| 51 |
+
|
| 52 |
+
To prevent outliers from collapsing the discrete code assignment $q \in \{0,1,2,3\}$ while respecting the true linear-layer geometry, V2 constructs a portfolio of **geometry-preserving analogs** $\tilde{x}$ of each eligible weight block before extracting candidate lattice coordinates:
|
| 53 |
+
|
| 54 |
+
#### A. Sub-block Iso-Moment Quantile-Companded Analog (`subblock_isomoment_analog`)
|
| 55 |
+
For each 16-weight sub-block $j$ with importance weights $w_{j,k}$:
|
| 56 |
+
|
| 57 |
+
$$
|
| 58 |
+
\mu_j = \frac{\sum_k w_{j,k} x_{j,k}}{\sum_k w_{j,k}},
|
| 59 |
+
\qquad
|
| 60 |
+
\sigma_j^2 = \frac{\sum_k w_{j,k}(x_{j,k} - \mu_j)^2}{\sum_k w_{j,k}},
|
| 61 |
+
\qquad
|
| 62 |
+
z_{j,k} = \frac{x_{j,k} - \mu_j}{\sigma_j}.
|
| 63 |
+
$$
|
| 64 |
+
|
| 65 |
+
Standardized coordinates are mapped through an importance-weighted hyperbolic transport blended with ideal 4-level quantiles $q^{\text{ideal}}_r \in [-1.35, +1.35]$:
|
| 66 |
+
|
| 67 |
+
$$
|
| 68 |
+
\tilde{z}_{j,k} = (1 - \alpha)\,\tau \tanh\!\left(\frac{(w_{j,k}/\bar{w}_j)^\beta z_{j,k}}{\tau}\right) + \alpha\, q^{\text{ideal}}_{\text{rank}(z_{j,k})},
|
| 69 |
+
$$
|
| 70 |
+
|
| 71 |
+
and then **orthogonally projected back onto the exact sub-block sphere**:
|
| 72 |
+
|
| 73 |
+
$$
|
| 74 |
+
\tilde{x}_{j,k} = \mu_j + \sigma_j \frac{\tilde{z}_{j,k} - \bar{z}_j}{\sqrt{\frac{1}{W_j}\sum_k w_{j,k}(\tilde{z}_{j,k} - \bar{z}_j)^2}}.
|
| 75 |
+
$$
|
| 76 |
+
|
| 77 |
+
This preserves the exact sub-block weighted mean $\mu_j$, weighted variance $\sigma_j^2$, and rank ordering while equalizing spacing across the 4 quantization levels.
|
| 78 |
+
|
| 79 |
+
#### B. D6 / Vesica Gram-Preserving Fold Analog (`vesica_gram_analog`)
|
| 80 |
+
Decomposes the 256-weight superblock into symmetric (vesica) and antisymmetric (wave) fold modes:
|
| 81 |
+
|
| 82 |
+
$$
|
| 83 |
+
v_p = \frac{1}{2}(x_p + x_{p+128}),
|
| 84 |
+
\qquad
|
| 85 |
+
u_p = \frac{1}{2}(x_p - x_{p+128}),
|
| 86 |
+
\qquad p \in \{0,\ldots,127\}.
|
| 87 |
+
$$
|
| 88 |
+
|
| 89 |
+
Let $G \in \mathbb{R}^{2\times 2}$ be the importance-weighted covariance (Gram) matrix of $(v, u)$. V2 whitens $(v, u)$ via $G^{-1/2}$, applies smooth hyperbolic saturation along the principal axes, re-orthogonalizes, and colors back via $G^{1/2}$ so that:
|
| 90 |
+
|
| 91 |
+
$$
|
| 92 |
+
\operatorname{Gram}_w(\tilde{v}, \tilde{u}) = \operatorname{Gram}_w(v, u),
|
| 93 |
+
\qquad
|
| 94 |
+
\mathbb{E}_w[\tilde{v}] = \mathbb{E}_w[v],
|
| 95 |
+
\qquad
|
| 96 |
+
\mathbb{E}_w[\tilde{u}] = \mathbb{E}_w[u].
|
| 97 |
+
$$
|
| 98 |
+
|
| 99 |
+
#### C. Cross-Sub-block Scale-Lattice Analog (`subscale_lattice_analog`)
|
| 100 |
+
Tempers extreme sub-block standard deviations $\sigma_j \mapsto \sigma_j^\gamma \sigma_{\text{block}}^{1-\gamma}$ so all 16 sub-blocks fit cleanly into the shared 4-bit $\{0..15\}$ scale lattice while preserving every sub-block's unit direction $\hat{u}_j$ ($\cos\theta = 1$), mean $\mu_j$, and total superblock weighted energy.
|
| 101 |
+
|
| 102 |
+
---
|
| 103 |
+
|
| 104 |
+
### 2. Coupled Orthogonal-Basis Bilinear `Q2_K` Factorization
|
| 105 |
+
|
| 106 |
+
Given candidate discrete codes $q_{j,k} \in \{0,1,2,3\}$ (from raw starts and the analog portfolio), V2 projects the continuous and 4-bit scale parameters directly onto the **true weights** $x$:
|
| 107 |
+
|
| 108 |
+
1. **Orthogonalized Sub-block Basis**:
|
| 109 |
+
Rewriting $\hat{x}_{j,k} = s_j(q_{j,k} - \bar{q}_j) + (s_j\bar{q}_j - t_j)$ diagonalizes the $2\times 2$ sub-block normal equations. The true sensitivity of sub-block $j$ to scale error $(s_j - d \cdot Ls_j)^2$ is the centered variance weight:
|
| 110 |
+
$$
|
| 111 |
+
V_{qq,j} = \sum_{k=0}^{15} w_{j,k}(q_{j,k} - \bar{q}_j)^2 = S_{qq,j} - S_{wq,j}\bar{q}_j.
|
| 112 |
+
$$
|
| 113 |
+
2. **Monotone 1-D Factorization (`factor_1d`)**:
|
| 114 |
+
Factors $s_j \approx d \cdot Ls_j$ under weights $V_{qq,j}$ using multi-start Lloyd iterations followed by an exact vectorized coordinate sweep over integer codes $\{0..15\}$.
|
| 115 |
+
3. **Coupled Offset Target**:
|
| 116 |
+
Once $(d, Ls_j)$ are chosen, the optimal offset for sub-block $j$ is shifted to absorb the scale rounding residual:
|
| 117 |
+
$$
|
| 118 |
+
t_j^{\text{coupled}} = \max\!\left(0,\; \frac{d \cdot Ls_j \cdot S_{wq,j} - S_{x,j}}{S_{w,j}}\right)
|
| 119 |
+
= \max\!\left(0,\; t_j + \bar{q}_j(d \cdot Ls_j - s_j)\right),
|
| 120 |
+
$$
|
| 121 |
+
which is then factored into $d_{\min} \cdot Lm_j$ under weights $S_{w,j}$.
|
| 122 |
+
4. **Global $2\times 2$ Bilinear $(d, d_{\min})$ Solve & Joint $16\times 16$ Sub-scale Scan**:
|
| 123 |
+
Solves the exact $2\times 2$ linear system for $(d, d_{\min})$ across all 256 weights (augmented by the rank-1 DC spectral penalty), rounds to $\mathbb{F}_{16}$, and performs a vectorized $16\times 16$ joint $(Ls_j, Lm_j)$ minimization per sub-block, followed by $\mathbb{F}_{16}$ ULP neighborhood polishing and D6/Vesica Hadamard code descent.
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
### 3. Global HPC Pipeline
|
| 128 |
+
|
| 129 |
+
```text
|
| 130 |
+
Source Weights + Optional iMatrix
|
| 131 |
+
β
|
| 132 |
+
βΌ
|
| 133 |
+
Kurtosis-Gated Clamp & Batched GGML Seeds (make_qkx2_batch / make_qp_batch)
|
| 134 |
+
β
|
| 135 |
+
βΌ
|
| 136 |
+
Vectorized Superblock WLS + Factored Candidate Field (6Γ6 / 24Γ24 Lattice)
|
| 137 |
+
β
|
| 138 |
+
βΌ
|
| 139 |
+
6-State Sequential Sieve (Bounded Neighbor Parity Back-Action + Log-Slack)
|
| 140 |
+
β
|
| 141 |
+
βΌ
|
| 142 |
+
36-State Viterbi Global Sequence Optimization (Free-Energy Bins + Manhattan Prior)
|
| 143 |
+
β
|
| 144 |
+
βΌ
|
| 145 |
+
Sub-block Sieve Assembly + ULP Polish + D6/Vesica Spectral Shaping + Rolling DC Carry
|
| 146 |
+
β
|
| 147 |
+
βΌ
|
| 148 |
+
Monotone Local-Safety Candidate & Analog-Factored Floors
|
| 149 |
+
β
|
| 150 |
+
βΌ
|
| 151 |
+
Standard Q2_K GGUF Output
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
---
|
| 155 |
+
|
| 156 |
+
## Measured Improvements over the C Reference Solver
|
| 157 |
+
|
| 158 |
+
Evaluated across 48 synthetic blocks (4 distributions) and a full row (47 superblocks = 12,032 weights) of `blk.0.ffn_down.weight` from `Raon.gguf` weighted by `imatrix.gguf`:
|
| 159 |
+
|
| 160 |
+
| Benchmark Suite | Mean Objective Reduction | Median Reduction | Min Reduction | Worse Cases |
|
| 161 |
+
| :--- | :---: | :---: | :---: | :---: |
|
| 162 |
+
| **Gaussian** ($\mathcal{N}(0, 0.02^2)$) | **+8.89%** | +8.23% | +3.15% | `0 / 12` |
|
| 163 |
+
| **Heavy-Tailed** ($\text{Student-}t_3$) | **+10.47%** | +9.26% | +4.97% | `0 / 12` |
|
| 164 |
+
| **Offset-Sparse** (50% density + DC shift) | **+14.46%** | +14.83% | +3.67% | `0 / 12` |
|
| 165 |
+
| **Sparse** (10% density) | **+36.05%** | +34.05% | +16.91% | `0 / 12` |
|
| 166 |
+
| **Real `Raon.gguf` (`blk.0.ffn_down.weight` + `iMatrix`)** | **+15.96%** | β | β | `0 / 47` |
|
| 167 |
+
|
| 168 |
+
Reproduce at any time with:
|
| 169 |
+
|
| 170 |
+
```bash
|
| 171 |
+
python3 -m hpc_quantize_v2 --bench --bench-per-kind 12
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
---
|
| 175 |
+
|
| 176 |
+
## Quickstart
|
| 177 |
+
|
| 178 |
+
### Requirements
|
| 179 |
+
Only **Python 3.10+** and **NumPy** (`tkinter` is included with standard Python installations for the GUI). No C compiler or shared libraries are needed.
|
| 180 |
+
|
| 181 |
+
```bash
|
| 182 |
+
pip install numpy
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
### 1. Desktop GUI (`gui.py`)
|
| 186 |
+
|
| 187 |
+
Launch the graphical interface to browse a source `.gguf`, attach an `.imatrix`/`.gguf` importance file, inspect eligible tensors, tune analog/HPC parameters, copy the equivalent CLI command, or run quantization with a live progress bar:
|
| 188 |
+
|
| 189 |
+
```bash
|
| 190 |
+
python3 gui.py
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
Or pre-populate paths on launch:
|
| 194 |
+
|
| 195 |
+
```bash
|
| 196 |
+
python3 gui.py --gguf Raon.gguf --imatrix imatrix.gguf --output Raon-Q2K.gguf
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
### 2. Command-Line Interface (`hpc_quantize_v2`)
|
| 200 |
+
|
| 201 |
+
**Quantize a full GGUF model to a `Q2_K` GGUF with an iMatrix:**
|
| 202 |
+
|
| 203 |
+
```bash
|
| 204 |
+
python3 -m hpc_quantize_v2 \
|
| 205 |
+
--gguf Raon.gguf \
|
| 206 |
+
--all-q2k \
|
| 207 |
+
--imatrix-gguf imatrix.gguf \
|
| 208 |
+
--output Raon-Q2K.gguf \
|
| 209 |
+
--verbose
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
**Inspect tensors in a GGUF file:**
|
| 213 |
+
|
| 214 |
+
```bash
|
| 215 |
+
python3 -m hpc_quantize_v2 --gguf Raon.gguf --list-tensors
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
**Smoke-test a single tensor into a valid GGUF (copies remaining tensors verbatim):**
|
| 219 |
+
|
| 220 |
+
```bash
|
| 221 |
+
python3 -m hpc_quantize_v2 \
|
| 222 |
+
--gguf Raon.gguf \
|
| 223 |
+
--tensor blk.0.ssm_beta.weight \
|
| 224 |
+
--imatrix-gguf imatrix.gguf \
|
| 225 |
+
--output Raon-smoke.gguf
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
**Quantize a NumPy `.npy` weight array to `.npz`:**
|
| 229 |
+
|
| 230 |
+
```bash
|
| 231 |
+
python3 -m hpc_quantize_v2 --input weights.npy --output weights_q2k.npz --verbose
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
### 3. Python API
|
| 235 |
+
|
| 236 |
+
```python
|
| 237 |
+
import numpy as np
|
| 238 |
+
from hpc_quantize_v2 import (
|
| 239 |
+
HPCConfig,
|
| 240 |
+
Q2KQuantizer,
|
| 241 |
+
factor_only_solve,
|
| 242 |
+
read_imatrix,
|
| 243 |
+
)
|
| 244 |
+
from hpc_quantize_v2.gguf import write_quantized_gguf
|
| 245 |
+
|
| 246 |
+
# 1. Quantize an in-memory tensor (shape must be a multiple of 256)
|
| 247 |
+
cfg = HPCConfig(
|
| 248 |
+
n_cand_d=6,
|
| 249 |
+
n_cand_m=6,
|
| 250 |
+
use_weight_analogs=True,
|
| 251 |
+
factor_coupled_q2k=True,
|
| 252 |
+
factor_joint_subscales=True,
|
| 253 |
+
use_spectral=True,
|
| 254 |
+
)
|
| 255 |
+
quantizer = Q2KQuantizer(cfg)
|
| 256 |
+
|
| 257 |
+
weights = np.random.default_rng(0).normal(0, 0.02, size=(16, 256)).astype(np.float32)
|
| 258 |
+
result = quantizer.quantize(weights, row_width=256)
|
| 259 |
+
print("RMSE:", result.stats.rmse)
|
| 260 |
+
reconstructed = result.dequantize()
|
| 261 |
+
|
| 262 |
+
# 2. Convert a source GGUF + iMatrix into a quantized Q2_K GGUF
|
| 263 |
+
imat = read_imatrix("imatrix.gguf")
|
| 264 |
+
stats = write_quantized_gguf(
|
| 265 |
+
"Raon.gguf",
|
| 266 |
+
"Raon-Q2K.gguf",
|
| 267 |
+
quantizer,
|
| 268 |
+
imat=imat,
|
| 269 |
+
quantize_all=True,
|
| 270 |
+
)
|
| 271 |
+
print(stats)
|
| 272 |
+
```
|
| 273 |
+
|
| 274 |
+
---
|
| 275 |
+
|
| 276 |
+
## Package Structure
|
| 277 |
+
|
| 278 |
+
```text
|
| 279 |
+
HPC-Quantize/
|
| 280 |
+
βββ gui.py # Standalone Tkinter/TTK GUI wrapper
|
| 281 |
+
βββ hpc_quantize_v2/ # Pure-Python V2 package (zero C dependencies)
|
| 282 |
+
βββ __init__.py # Public API exports
|
| 283 |
+
βββ __main__.py # CLI entry point (--gguf, --input, --demo, --bench)
|
| 284 |
+
βββ config.py # Validated HPCConfig dataclass
|
| 285 |
+
βββ fp16.py # Bit-exact GGML nearest_int & IEEE fp16 ULP primitives
|
| 286 |
+
βββ q2k.py # Q2_K 84-byte block packing, unpacking & dequantization
|
| 287 |
+
βββ analog.py # Geometry-preserving weight analogs (Iso-Moment, Vesica Gram, Subscale)
|
| 288 |
+
βββ factored.py # Coupled orthogonal-basis bilinear Q2_K block & candidate solver
|
| 289 |
+
βββ seeds.py # Kurtosis-gated outlier clamping & batched GGML seed solvers
|
| 290 |
+
βββ spectral.py # Folded error pyramid, DC/Vesica penalty & Hadamard code descent
|
| 291 |
+
βββ sieve.py # 6-state sequential Sieve with neighbor parity back-action
|
| 292 |
+
βββ viterbi.py # Vectorized 36-state Viterbi sequence optimizer
|
| 293 |
+
βββ pipeline.py # End-to-end Q2KQuantizer tensor pipeline
|
| 294 |
+
βββ reference.py # Faithful C-reference solver port for A/B benchmarking
|
| 295 |
+
βββ tests/
|
| 296 |
+
βββ test_v2.py # 19 unit tests covering primitives, analogs, solver, GGUF & pipeline
|
| 297 |
+
```
|
| 298 |
+
|
| 299 |
+
## Running the Test Suite
|
| 300 |
+
|
| 301 |
+
```bash
|
| 302 |
+
python3 -m unittest discover -s hpc_quantize_v2/tests -v
|
| 303 |
+
```
|
| 304 |
+
|
| 305 |
+
---
|