CompressedGemma commited on
Commit
613160c
Β·
verified Β·
1 Parent(s): ce39be0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +148 -32
README.md CHANGED
@@ -33,9 +33,11 @@ HPC-Quantize V2 (`hpc_quantize_v2/` + `gui.py`) has **zero C or `ctypes` depende
33
  3. **Algebraic Coupled Phase-4 Block Assembly (`hpc_quantize_v2/pipeline.py`)**:
34
  Replaces legacy brute-force $16\times 16$ sub-block scans, $9\times 9$ weight-level ULP loops, and 256-pass greedy code searches with closed-form sufficient-statistic projections (`1,200–1,475 blocks/sec` in Phase 4, achieving **+2.64% lower objective error** than brute-force Phase 4 in **1/9th the time**).
35
  4. **Bit-Identical Multi-Core Execution (`--workers 0`)**:
36
- Distributes block-independent candidate and analog solves across all CPU cores via zero-copy `fork` shared memory while keeping cross-block Sieve, 36-state Viterbi, and row-aware DC carry strictly sequential β€” producing **100% bit-for-bit identical** GGUF output regardless of worker count.
37
  5. **Real-Time Telemetry GUI (`gui.py`) & Direct GGUF-to-GGUF Pipeline (`hpc_quantize_v2/gguf.py`)**:
38
- Ingests `F32`, `F16`, `BF16`, `Q8_0`, `Q4_0`, and `Q2_K` source GGUFs plus `llama.cpp` iMatrix files, displays live intra-tensor stage/block progress, dual **Tensor ETA** and block-weighted **Whole-Model ETA**, live `RMSE`/`RMSE/Οƒ`, Sieve temperature, and floor hits, and emits a stock `llama.cpp` `Q2_K` GGUF.
 
 
39
 
40
  ---
41
 
@@ -154,7 +156,85 @@ Instead of brute-force $16\times 16$ sub-block scans and $9\times 9$ weight-leve
154
 
155
  ---
156
 
157
- ### 4. End-to-End HPC Pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  ```text
160
  Source Weights + Optional iMatrix
@@ -175,7 +255,13 @@ Vectorized Superblock WLS + Multi-Core Analog-Guided Factored Candidate Field
175
  Batched Algebraic Phase-4 Coupled Assembly + Quadratic ULP Polish + Closed-Form Code Projection
176
  β”‚
177
  β–Ό
178
- Row-Aware Sequential DC Residual Nudge & Monotone Local-Safety Floors
 
 
 
 
 
 
179
  β”‚
180
  β–Ό
181
  Standard Q2_K GGUF Output
@@ -236,6 +322,7 @@ python3 -m hpc_quantize_v2 \
236
  --all-q2k \
237
  --imatrix-gguf imatrix.gguf \
238
  --output Raon-Q2K.gguf \
 
239
  --workers 0 \
240
  --verbose
241
  ```
@@ -262,6 +349,60 @@ python3 -m hpc_quantize_v2 \
262
  python3 -m hpc_quantize_v2 --input weights.npy --output weights_q2k.npz --verbose
263
  ```
264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  ### 3. Python API
266
 
267
  ```python
@@ -281,6 +422,9 @@ cfg = HPCConfig(
281
  factor_coupled_q2k=True,
282
  factor_joint_subscales=True,
283
  use_spectral=True,
 
 
 
284
  workers=0,
285
  )
286
  quantizer = Q2KQuantizer(cfg)
@@ -303,31 +447,3 @@ print(stats)
303
  ```
304
 
305
  ---
306
-
307
- ## Package Structure
308
-
309
- ```text
310
- HPC-Quantize/
311
- β”œβ”€β”€ gui.py # Standalone Tkinter/TTK GUI with live telemetry dashboard
312
- └── hpc_quantize_v2/ # Pure-Python V2 package (zero C dependencies)
313
- β”œβ”€β”€ __init__.py # Public API exports
314
- β”œβ”€β”€ __main__.py # CLI entry point (--gguf, --input, --demo, --bench, --workers)
315
- β”œβ”€β”€ config.py # Validated HPCConfig dataclass
316
- β”œβ”€β”€ fp16.py # Bit-exact GGML nearest_int & IEEE fp16 ULP primitives
317
- β”œβ”€β”€ q2k.py # Q2_K 84-byte block packing, unpacking & batched dequantization
318
- β”œβ”€β”€ analog.py # Geometry-preserving weight analogs (Iso-Moment, Vesica Gram, Subscale)
319
- β”œβ”€β”€ factored.py # Coupled orthogonal-basis bilinear Q2_K block & vectorized candidate solver
320
- β”œβ”€β”€ seeds.py # Kurtosis-gated outlier clamping & batched GGML seed solvers
321
- β”œβ”€β”€ spectral.py # Folded error pyramid, DC/Vesica penalty & Hadamard code descent
322
- β”œβ”€β”€ sieve.py # 6-state sequential Sieve with neighbor parity back-action
323
- β”œβ”€β”€ viterbi.py # Vectorized 36-state Viterbi sequence optimizer
324
- β”œβ”€β”€ pipeline.py # End-to-end Q2KQuantizer with multi-core Phase 2b & algebraic Phase 4
325
- β”œβ”€β”€ gguf.py # GGUF reader, iMatrix loader & GGUF-to-GGUF streaming writer
326
- β”œβ”€β”€ reference.py # Faithful C-reference solver port for A/B benchmarking
327
- ```
328
-
329
- ## Running the Test Suite
330
-
331
- ```bash
332
- python3 -m unittest discover -s hpc_quantize_v2/tests -v
333
- ```
 
33
  3. **Algebraic Coupled Phase-4 Block Assembly (`hpc_quantize_v2/pipeline.py`)**:
34
  Replaces legacy brute-force $16\times 16$ sub-block scans, $9\times 9$ weight-level ULP loops, and 256-pass greedy code searches with closed-form sufficient-statistic projections (`1,200–1,475 blocks/sec` in Phase 4, achieving **+2.64% lower objective error** than brute-force Phase 4 in **1/9th the time**).
35
  4. **Bit-Identical Multi-Core Execution (`--workers 0`)**:
36
+ Distributes block-independent candidate and analog solves across all CPU cores via zero-copy `fork` shared memory while keeping cross-block Sieve, 36-state Viterbi, and row-aware DC carry strictly sequential β€” producing **100% bit-for-bit identical** GGUF output regardless of worker count.
37
  5. **Real-Time Telemetry GUI (`gui.py`) & Direct GGUF-to-GGUF Pipeline (`hpc_quantize_v2/gguf.py`)**:
38
+ Ingests `F32`, `F16`, `BF16`, `Q8_0`, `Q4_0`, and `Q2_K` source GGUFs plus `llama.cpp` iMatrix files, displays live intra-tensor stage/block progress, dual **Tensor ETA** and block-weighted **Whole-Model ETA**, live `RMSE`/`RMSE/Οƒ`, Sieve temperature, and floor hits, and emits a stock `llama.cpp` `Q2_K` GGUF.
39
+ 6. **The Coherent-Residual Drain & the `Q2_K` Free Lunch (`spectral.py`, `anneal.py`, `q2k.py`)**:
40
+ A four-stage drain of the *coherent* error channel β€” in-objective DC/vesica shaping, an opt-in thermal `Ξ»`-tilt drain, a row-aware sequential DC carry nudge, and the closed-form **Zero-DC Pin** (Phase 4.7): a pure $O(n)$ re-solve of the superblock scale pair on the exact `Ξ£e = 0` line that **drops the block-DC residual by ~94% while slightly *lowering* RMSE** β€” for free, with zero SSE budget.
41
 
42
  ---
43
 
 
156
 
157
  ---
158
 
159
+ ### 4. The Coherent-Residual Drain: DC & Vesica Error Handling (`spectral.py`, `anneal.py`, `q2k.py`)
160
+
161
+ #### A. Residual anatomy: why "SSE-optimal" is not enough
162
+
163
+ For a finished `Q2_K` superblock, the per-element error is
164
+
165
+ $$
166
+ e_i = x_i - \big(d \cdot Ls_{\lfloor i/16 \rfloor}\, q_i - d_{\min} \cdot Lm_{\lfloor i/16 \rfloor}\big),
167
+ \qquad i \in \{0,\ldots,255\}.
168
+ $$
169
+
170
+ SSE-optimal per-element quantizers are unbiased only *on average over huge ensembles*. At 2 bits per weight, with the `(d, d_min)` pair stored in `fp16`, the residual of a single superblock has two coherent components with scale far above the white-noise floor:
171
+
172
+ 1. **Block DC** $D = \sum_{i=0}^{255} e_i$ β€” the systematic per-superblock offset error.
173
+ 2. **Vesica pairs** $v_p = e_p + e_{p+128}$, $p \in \{0,\ldots,127\}$ β€” the first fold level of the dyadic/Hadamard error pyramid, i.e. the dominant low-frequency coherent residual shared by the symmetric halves of a superblock.
174
+
175
+ Measured on this stack: the *unpinned* pipeline leaves block-DC RMS $\approx 2.7\times 10^{-2}$ against a total RMSE of $\approx 4.1\times 10^{-3}$ β€” the coherent channel is **~7Γ— louder than the entire random error**, and because it is coherent it *accumulates across layers* in inference instead of averaging out. V2 therefore treats draining $D$ and $v_p$ as a first-class problem, with four independent, composable mechanisms:
176
+
177
+ #### B. Mechanism 1 β€” In-objective spectral shaping (Phases 1–4, always on)
178
+
179
+ The block objective augments importance-weighted SSE with the DC/vesica fold penalty
180
+
181
+ $$
182
+ E = \sum_i w_i e_i^2 \;+\; \frac{\lambda_{\text{vw}}}{n} \sum_{p=0}^{127} v_p^2 \;+\; \frac{\lambda_{\text{dc}}}{n} \,(D + \text{carry})^2 ,
183
+ $$
184
+
185
+ (`dc_lambda`, `vw_lambda`; the full vectorized fold pyramid generalizes this to every dyadic level). All three algebraic Phase-4 solvers β€” the coupled sub-scale projection, the $2\times 2$ CramΓ©r scale/offset solve, and the 1-D quadratic code descent β€” minimize this extended form exactly, so the *lattice solution space itself* is biased toward low coherent residual before any separate drain pass runs.
186
+
187
+ #### C. Mechanism 2 β€” Thermal `Ξ»`-tilt drain (Phase 4.5, opt-in, `--thermal` + `--thermal-tilt`)
188
+
189
+ With `therm_passes > 0`, the exact heat-bath Gibbs chain sweeps the code lattice with the *extended* energy. Every accepted move is exact on the quantization lattice, so multiplying the spectral lambdas by the zero-residual tilt (`therm_lambda_tilt` β‰₯ 1) makes the chain deliberately **spend weighted SSE to buy coherent-residual drainage** β€” it walks $(\text{SSE}, D, v_p)$ along the exact Pareto frontier of the block. Endpoint behavior at large tilts: the coherent residual is driven to the lattice floor and the remaining error is (almost) spectrally white β€” measured on this model's `ssm_out`: **DC RMS βˆ’97% at tilt $10^3$ for ~8Γ— SSE**. The pass is monotone per block (extended energy never regresses) and seeded/deterministic.
190
+
191
+ #### D. Mechanism 3 β€” Row-aware sequential DC carry nudge (Phase 4.8, on by default)
192
+
193
+ `_apply_dc_nudge` sweeps superblocks strictly left-to-right (row topology of the weight matrix, `row_width` blocks per row). Each block's codes are re-projected by `dc_nudge_codes` β€” a bounded-SSE-budget greedy descent that cancels the *incoming* DC carry `dc_decay Β· rolling` in exchange for as little objective cost as the `sse_budget` cap allows; whatever DC the block cannot cheaply cancel becomes part of the *rolling* carry handed to the next superblock (reset at row boundaries; `carry_cumulative` selects reset-to-current vs accumulate). Net effect: **the residual DC of the whole tensor gets funneled into whichever blocks have the cheapest 2-bit codes to absorb it**, instead of staying distributed uniformly. Cost is exactly bounded by construction; the sweep is deterministic and single-pass.
194
+
195
+ #### E. Mechanism 4 β€” The Zero-DC Pin, a.k.a. the "`Q2_K` Free Lunch" (Phase 4.7, opt-in, `pin_dc`)
196
+
197
+ This is a **pure closed-form bonus**: with codes, $Ls$, $Lm$ frozen at their final refined state, the remaining freedom $(d, m)$ (with $m \equiv d_{\min}$, the fp16 minimum field) is re-solved *subject to exact zero DC*. Two degrees of freedom, two constraints ($\min \sum e_i^2$, $\sum e_i = 0$) β†’ an exact solution, no lattice search, no budget.
198
+
199
+ Write $a_i = Ls_{\lfloor i/16\rfloor} c_i$ and $b_i = Lm_{\lfloor i/16\rfloor}$ so $e_i = x_i - d\,a_i + m\,b_i$, and let $S_x = \sum_i x_i$, $S_a = \sum_i a_i$, $S_b = \sum_i b_i$. The constraint $\sum_i e_i = 0$ pins $m$ onto a line parameterized by $d$:
200
+
201
+ $$
202
+ m(d) = \frac{d\,S_a - S_x}{S_b}
203
+ \quad\Rightarrow\quad
204
+ e_i(d) = u_i - d\,v_i,
205
+ \quad
206
+ u_i = x_i - b_i \tfrac{S_x}{S_b},\;
207
+ v_i = a_i - b_i \tfrac{S_a}{S_b}.
208
+ $$
209
+
210
+ Along that line the block energy
211
+
212
+ $$
213
+ \sum_i e_i^2(d) = \|u\|^2 \;-\; 2d\,\langle u, v\rangle \;+\; d^2 \|v\|^2
214
+ $$
215
+
216
+ is a convex 1-D quadratic with exact minimizer
217
+
218
+ $$
219
+ d^* = \frac{\langle u, v\rangle}{\langle v, v\rangle},
220
+ \qquad
221
+ m^* = \frac{d^* S_a - S_x}{S_b}.
222
+ $$
223
+
224
+ **Geometric meaning:** $v$ is the component of the code vector $a$ orthogonal to the all-ones direction (the DC direction), and $u$ is the analogous component of the data β€” so $d^*$ is the exact OLS scale of orthogonally-projected data against orthogonally-projected codes. The pin is self-dominating: moving from the input $(d, m)$ to $(d^*, m^*)$ travels *along* the zero-DC line, so it is at worst the zero move β€” **SSE is non-increasing, $|D|$ is non-increasing, only the fp16 storage rounding of the `(d, m)` fields limits the final DC**. Blocks where the translation degree of freedom degenerates ($S_b = 0$ or $\langle v, v\rangle = 0$) keep their input parameters; the map is fp16-idempotent (re-pinning an already-pinned state is a no-op).
225
+
226
+ Its practical effect (live `blk.0.ssm_out.weight`, 8,192 superblocks, RTX 3060 path):
227
+
228
+ | Configuration | RMSE | Block-DC RMS | mean $\lvert D \rvert$ |
229
+ | :--- | :---: | :---: | :---: |
230
+ | Base pipeline (Phases 1–4 + carry nudge) | $4.068\times 10^{-3}$ | $2.72\times 10^{-2}$ | $1.96\times 10^{-2}$ |
231
+ | Base + Zero-DC Pin (Phase 4.7) | $4.045\times 10^{-3}$ | $1.70\times 10^{-3}$ | $1.36\times 10^{-3}$ |
232
+
233
+ i.e. **DC RMS βˆ’93.8%, RMSE βˆ’0.6%, at <10 ms for the whole tensor** (a handful of $O(n_B \cdot 256)$ reductions). The weighted iMatrix-objective does rise slightly (~5% on `wSSE` for the pinned sample) β€” that is the point: it is an *exchange*, trading part of the importance-weighted objective for a near-zero coherent channel, and it composes safely with the drain stages of Mechanisms 1–3 (recommended order: thermal `Ξ»`-tilt drain β†’ pin β†’ row-aware carry nudge, which is exactly the pipeline's Stage order 4.5 β†’ 4.7 β†’ 4.8).
234
+
235
+ ---
236
+
237
+ ### 5. End-to-End HPC Pipeline
238
 
239
  ```text
240
  Source Weights + Optional iMatrix
 
255
  Batched Algebraic Phase-4 Coupled Assembly + Quadratic ULP Polish + Closed-Form Code Projection
256
  β”‚
257
  β–Ό
258
+ Opt-In Thermal Drain (Exact Heat-Bath Sweep, Zero-Residual Ξ»-Tilt β€” Phase 4.5)
259
+ β”‚
260
+ β–Ό
261
+ Zero-DC Pin / "Q2_K Free Lunch" (Closed-Form dc=0 Re-Solve of (d, m) β€” Phase 4.7)
262
+ β”‚
263
+ β–Ό
264
+ Row-Aware Sequential DC Residual Nudge & Monotone Local-Safety Floors (Phase 4.8)
265
  β”‚
266
  β–Ό
267
  Standard Q2_K GGUF Output
 
322
  --all-q2k \
323
  --imatrix-gguf imatrix.gguf \
324
  --output Raon-Q2K.gguf \
325
+ --pin-dc \
326
  --workers 0 \
327
  --verbose
328
  ```
 
349
  python3 -m hpc_quantize_v2 --input weights.npy --output weights_q2k.npz --verbose
350
  ```
351
 
352
+ **Optional thermal refinery (opt-in, seeded, deterministic per `--seed`):**
353
+
354
+ ```bash
355
+ python3 -m hpc_quantize_v2 --gguf model.gguf --all-q2k --thermal 4 --verbose
356
+ ```
357
+
358
+ `--thermal N` runs `N` heat-bath annealing passes over each block's code
359
+ lattice after Phase 4 (Phase 4.5), trading a tiny amount of weighted SSE for
360
+ large reductions in the rolling row-wise DC residual (measured on real GGUF
361
+ tensors: extended objective ~1.3% better, DC penalty ~40% lower at ~4% time
362
+ cost per 4 passes). `0` (default) keeps the deterministic pipeline output
363
+ bit-for-bit. The full knob set (`therm_passes`, `therm_sweeps`,
364
+ `therm_t0`, `therm_t_decay`, `therm_seed`) lives on `HPCConfig`; the pass is
365
+ monotone per block and never regresses a block's extended energy.
366
+
367
+ The refinery also covers the format quantizers: with `therm_passes > 0` the
368
+ finished Q4_0 / Q1_0 / Q3_K blocks are re-swept on their own 16/2/8-level
369
+ lattices (signed fp16 scales included) with extended-nonincreasing
370
+ acceptance under the `sse_budget` cap β€” on CPU and CUDA alike, and with the
371
+ same bit-exact seeded behavior.
372
+
373
+ **Zero-residual ("0-error") anneal (`therm_lambda_tilt`, GUI "Zero-Residual
374
+ Tilt", CLI `--thermal-tilt`):** a multiplier (β‰₯1, 1=off) applied to
375
+ `dc_lambda`/`vw_lambda` while annealing. Because every Gibbs move is exact
376
+ on the quantization lattice, tilting the spectral lambdas makes the chain
377
+ spend weighted SSE to drain the *coherent* residual β€” row DC and vesica
378
+ pairs β€” toward the lattice floor (measured on this model's `ssm_out`:
379
+ DC RMS βˆ’97% at tilt 1e3 for ~8Γ— SSE). The endpoint is a weight matrix
380
+ whose remaining error is (almost) spectrally white: no residual systematic
381
+ error left to accumulate across layers.
382
+
383
+ **Q2_K Free Lunch β€” Zero-DC Pin (`pin_dc`, GUI "Pin DC residual to 0 (Q2_K
384
+ free lunch)", CLI `--pin-dc`, API `pin_dc=True`):** after Phase 4 (and
385
+ thermal, when enabled) the pin re-solves each superblock's `(d, d_min)`
386
+ pair in closed form on the exact `Ξ£e = 0` line β€” codes and 4-bit scales
387
+ frozen β€” so the block-DC residual collapses to the `fp16` storage floor
388
+ (measured: **DC RMS βˆ’94%, RMSE βˆ’0.6%**, block SSE non-increasing,
389
+ <10 ms per tensor). It spends **no** SSE budget (the pin can only lower
390
+ unweighted block SSE) and runs identically on the CPU and CUDA paths.
391
+
392
+ **DC/vesica drain knob map (and where they act):**
393
+
394
+ | Knob | Config field | Stage | Cost model |
395
+ | :--- | :--- | :--- | :--- |
396
+ | Spectral shaping | `dc_lambda`, `vw_lambda` (Ξ»_dc=Ξ»_vw=1.0 default) | Phases 1–4 (in-objective) | arises inside the weighted objective itself |
397
+ | Thermal Ξ»-tilt drain | `therm_passes`, `therm_lambda_tilt` | Phase 4.5 (opt-in) | spends SSE under `sse_budget`, seeded & deterministic |
398
+ | Zero-DC Pin | `pin_dc` | Phase 4.7 (opt-in) | **exact & free** β€” SSE non-increasing, DC β†’ fp16 floor |
399
+ | Row-aware carry nudge | `apply_dc_carry`, `dc_decay`, `sse_budget`, `carry_cumulative` | Phase 4.8 (on by default) | exactly bounded per-block budget, residual funneled downstream |
400
+
401
+ For maximal coherent-residual drainage enable thermal (with tilt) + the
402
+ pin and keep the carry nudge on (the default); for a bit-exact
403
+ "pure-pipeline + free lunch" result, leave `pin_dc=True` with
404
+ `therm_passes=0` and disable `apply_dc_carry`.
405
+
406
  ### 3. Python API
407
 
408
  ```python
 
422
  factor_coupled_q2k=True,
423
  factor_joint_subscales=True,
424
  use_spectral=True,
425
+ therm_passes=0,
426
+ pin_dc=True, # Q2_K free lunch: exact dc=0 re-solve of (d, m)
427
+ apply_dc_carry=True, # row-aware DC carry drain (Phase 4.8)
428
  workers=0,
429
  )
430
  quantizer = Q2KQuantizer(cfg)
 
447
  ```
448
 
449
  ---