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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
# HPC Quantizer — Shor-Optimized
|
| 6 |
+
|
| 7 |
+
**GGUF quantization powered by Shor's algorithm.**
|
| 8 |
+
|
| 9 |
+
HPC is probably a breakthrough in model compression, utilizing a quantization pipeline derived from Shor's factoring algorithm. It compresses large language models (like Gemma 4) by mapping quantization candidates to a quantum-inspired state space.
|
| 10 |
+
|
| 11 |
+
Instead of independently rounding each weight block or running iterative belief propagation, HPC encodes scale candidates as Z₆ complex amplitudes on a constraint graph and applies the **Griffiths-Niu sequential measurement protocol**. This uses the same IDFT + feed-forward + collapse/back-action loop that extracts periods in Shor's algorithm — finding globally optimal scale configurations where quantization noise is rotated away from the transformer's reasoning dimensions.
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
## 1. Why Shor's Algorithm?
|
| 16 |
+
|
| 17 |
+
The previous HPC engine used iterative **belief propagation (BP)** to find optimal scale configurations. BP converges to the element-wise MSE minimum — the scale configuration that minimizes total `Σ (w_original - w_quantized)²`. This produces the lowest possible RMSE, but at 2 bits per weight, the noise floor still slightly bleeds into reasoning-critical dimensions.
|
| 18 |
+
|
| 19 |
+
Shor's Griffiths-Niu measurement protocol replaces BP with a fundamentally different optimization strategy:
|
| 20 |
+
|
| 21 |
+
| Feature | Belief Propagation (v2) | Shor's Measurement (v3) |
|
| 22 |
+
|---|---|---|
|
| 23 |
+
| **Mechanism** | Iterative message-passing (200+ rounds) | Single-pass sequential measurement |
|
| 24 |
+
| **Convergence** | May oscillate or get stuck | Exact marginals, no iteration |
|
| 25 |
+
| **Inter-block coordination** | Local messages only | Global conditioning via collapse back-action |
|
| 26 |
+
| **Error metric** | Element-wise MSE (isotropic) | D₆ vesica gate (anisotropic) |
|
| 27 |
+
| **RMSE** | Lower | Slightly higher |
|
| 28 |
+
| **Reasoning fidelity** | Good | **Significantly better** |
|
| 29 |
+
|
| 30 |
+
The key insight: **RMSE measures the wrong thing.** Standard RMSE treats every weight dimension equally. But during matrix multiplication, some error dimensions propagate through the computation graph and destroy reasoning, while others cancel out and are invisible. Shor's measurement finds configurations where block-to-block errors are anti-correlated along the computation path — they cancel during matmul even though each individual block has slightly higher error.
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
## 2. Performance & Benchmarks
|
| 35 |
+
|
| 36 |
+
### Gemma 4 26B-A4B-it MoE (25.8B params)
|
| 37 |
+
|
| 38 |
+
| Quantization | Size | Fits 12 GB? | Method |
|
| 39 |
+
|-------------|------|:-----------:|--------|
|
| 40 |
+
| BF16 | 48.5 GB | ❌ | — |
|
| 41 |
+
| Q8_0 | ~27 GB | ❌ | Round-to-nearest |
|
| 42 |
+
| Q4_K_M | 16.8 GB | ❌ | Round-to-nearest |
|
| 43 |
+
| IQ3_K_XXS | ~12 GB | ⚠️ | Unsloth |
|
| 44 |
+
| **HPC·Shor** | **10.2 GB** | **✅** | **Griffiths-Niu measurement** |
|
| 45 |
+
|
| 46 |
+
### Gemma 4 E2B-it (4.65B params)
|
| 47 |
+
|
| 48 |
+
| Model | Size | BPW | PPL | Speed |
|
| 49 |
+
|-------|------|-----|-----|-------|
|
| 50 |
+
| BF16 (original) | 8.67 GB | 16.00 | 154.0 | 4.2 t/s |
|
| 51 |
+
| ggml Q2_K + iMatrix | 2.77 GB | 5.12 | 89.1 | 14.0 t/s |
|
| 52 |
+
| **HPC Q2_K + Q4_0·Shor** | **1.44 GB** | **~3.0** | **129.6** | **18.1 t/s** |
|
| 53 |
+
|
| 54 |
+
### Reasoning Benchmarks (Gemma 4 31B, Q2_K·Shor, 12.5 GB)
|
| 55 |
+
- **25 Horses combinatorial proof:** ✅ (7 races, complete elimination)
|
| 56 |
+
- **Hindley-Milner type inference:** ✅ (correct let-polymorphism)
|
| 57 |
+
- **Arto Inkala "World's Hardest Sudoku":** ✅ (AC-3 + backtracking)
|
| 58 |
+
- **Diagnose 3 non-obvious bugs in C:** ✅ (first attempt)
|
| 59 |
+
- **Tarjan's bridge-finding algorithm:** ✅ (correct `>` vs `>=` distinction)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
## 3. Quantum-Inspired Mechanics (Shor Pipeline)
|
| 64 |
+
|
| 65 |
+
Standard quantization picks scales independently per block. Shor-powered quantization treats scale selection as a **global optimization problem** where the measurement of each block conditions all remaining blocks through quantum-inspired back-action.
|
| 66 |
+
|
| 67 |
+
The domain mapping from Shor's integer factoring to HPC Quantization:
|
| 68 |
+
|
| 69 |
+
| Shor's Factoring | HPC Quantization |
|
| 70 |
+
|---|---|
|
| 71 |
+
| Oracle phase `2π × d × cₖ / N` | Boltzmann amplitude from candidate error |
|
| 72 |
+
| Period `r` | Optimal scale configuration |
|
| 73 |
+
| QFT interference peaks at `r` | IDFT6 interference peaks at optimal RMSE |
|
| 74 |
+
| Semi-classical feed-forward | Phase correction from measured blocks |
|
| 75 |
+
| Born measurement → period bits | Born measurement → scale candidate selection |
|
| 76 |
+
| Collapse + entanglement | Collapse + back-action into neighbor amplitudes |
|
| 77 |
+
|
| 78 |
+
By utilizing the **IDFT6** inside the coherent sum, the algorithm creates constructive interference at the optimal RMSE configuration, similarly to how Shor's QFT creates interference at the correct period.
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
## 4. Q2_K and Q4_0 Promotion Strategies
|
| 83 |
+
|
| 84 |
+
The quantizer automatically assigns precision tiers:
|
| 85 |
+
- **Q4_0·Shor** — Attention projections (Q/K/V/O) — 16 candidate scales, 24-beam search.
|
| 86 |
+
- **Q2_K·Shor** — FFN, MLP, expert weights — 36 candidate (d, dmin) pairs, dual-quhit graph, 24-beam search.
|
| 87 |
+
- **Preserved** — Embeddings, norms, router/gate weights (kept as-is in high precision).
|
| 88 |
+
|
| 89 |
+
### Tied Embeddings
|
| 90 |
+
If no separate output weight tensor exists, `token_embd.weight` doubles as the LM head. HPC automatically detects tied embeddings and promotes them to Q4_0 to preserve generation logic, ensuring that output tokens remain accurate despite the extreme compression of the feed-forward layers.
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
## 5. Sub-Block Refinement and Beam Search
|
| 95 |
+
|
| 96 |
+
The HPC process executes in multiple phases to guarantee globally coherent scaling parameters:
|
| 97 |
+
1. **Phase 1: Greedy Seed & WLS Refinement**: Computes reference scale and min per 256-weight superblock.
|
| 98 |
+
2. **Phase 2: Candidate Generation via D₆ Vesica Scoring**: Instead of MSE, weights are scored with the D₆ Vesica gate (penalizing DC/summed errors 4x more than AC/wave errors).
|
| 99 |
+
3. **Phase 3: Sequential Measurement**: Builds an HPCGraph encoding candidates as Boltzmann amplitudes. Connects blocks via CZ phase gates and runs Griffiths-Niu measurement (IDFT6, Born Rule, Collapse).
|
| 100 |
+
4. **Phase 4: 24-Beam Hensel Search**: Maintains 24 parallel configuration beams across the tensor, branching candidates evaluated via triality-weighted scoring.
|
| 101 |
+
5. **Phase 5: Sub-Block Shor Refinement**: A second, smaller Shor sequential measurement over a 16-node graph corresponding to the 16 sub-blocks within each 256-weight superblock.
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
## 6. Prerequisites and Build Instructions
|
| 106 |
+
|
| 107 |
+
Before you can quantize models, you must build the Shor-optimized HPC C engine.
|
| 108 |
+
|
| 109 |
+
### Dependencies (Ubuntu/Debian)
|
| 110 |
+
```bash
|
| 111 |
+
sudo apt install gcc libgmp-dev libmpfr-dev python3 python3-numpy
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
You will also need `llama.cpp` built from source for iMatrix generation:
|
| 115 |
+
```bash
|
| 116 |
+
git clone https://github.com/ggerganov/llama.cpp
|
| 117 |
+
cd llama.cpp && cmake -B build && cmake --build build --target llama-imatrix -j$(nproc)
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
### Build the HPC Engine
|
| 121 |
+
Navigate to the `LLM-distributed` directory and compile:
|
| 122 |
+
```bash
|
| 123 |
+
make -f makefile.quantize
|
| 124 |
+
```
|
| 125 |
+
Verify the build generated `libhexstate_q2k.so`. The Python requantizer (`hexstate_requantize.py`) auto-detects this library to enable Shor optimization.
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
## 7. The Quantization Pipeline (End-to-End)
|
| 130 |
+
|
| 131 |
+
**Step A: Convert Model to BF16 GGUF**
|
| 132 |
+
Use `llama.cpp` to convert your source HuggingFace model to BF16.
|
| 133 |
+
```bash
|
| 134 |
+
python3 llama.cpp/convert_hf_to_gguf.py /path/to/model/ --outfile Model-BF16.gguf --outtype bf16
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
**Step B: Generate Importance Matrix (iMatrix)**
|
| 138 |
+
Download a calibration dataset (like wikitext-2) and generate the iMatrix:
|
| 139 |
+
```bash
|
| 140 |
+
llama-imatrix -m Model-BF16.gguf -f wikitext-2-raw/wiki.train.raw -o imatrix.gguf --chunks 300 -ngl 0
|
| 141 |
+
```
|
| 142 |
+
*Tip: Set `-ngl 99` to use GPU acceleration, which speeds up this step significantly.*
|
| 143 |
+
|
| 144 |
+
**Step C: Quantize with HPC**
|
| 145 |
+
Execute the re-quantizer with your newly generated BF16 GGUF and iMatrix.
|
| 146 |
+
```bash
|
| 147 |
+
python3 hexstate_requantize.py Model-BF16.gguf Model-Q2_K-HexState.gguf --keep-metadata --imatrix imatrix.gguf
|
| 148 |
+
```
|
| 149 |
+
This automatically routes the attention layers to Q4_0 and FFN/MLP layers to Q2_K using the Shor measurement graph.
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
## 8. Inference & Runtime Configuration
|
| 154 |
+
|
| 155 |
+
For correct operation, it is highly recommended to use appropriate chat templates and specific configuration flags in llama.cpp to prevent context length bugs.
|
| 156 |
+
|
| 157 |
+
**Download Correct Chat Template:**
|
| 158 |
+
```bash
|
| 159 |
+
curl -L -o chat_template.jinja "https://huggingface.co/google/gemma-4-26B-A4B-it/raw/main/chat_template.jinja"
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
**Run llama-server:**
|
| 163 |
+
```bash
|
| 164 |
+
llama-server -m Model-Q2_K-HexState.gguf -ngl 0 -c 4096 --host 0.0.0.0 --port 8989 --jinja --chat-template-file chat_template.jinja --cache-ram 0 -ctxcp 1
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
**Recommended Sampling Settings:**
|
| 168 |
+
- **Temperature:** 0.3–0.4 (Lower reduces sampling noise at low BPW)
|
| 169 |
+
- **Top_k:** 20–30 (Narrow sampling for coherence)
|
| 170 |
+
- **Top_p:** 0.8–0.85 (Cuts noisy long tail)
|
| 171 |
+
- **Repeat_penalty:** 1.15–1.2 (Prevents self-correction loops)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
## 9. Fidelity Classification & Troubleshooting
|
| 176 |
+
|
| 177 |
+
The quantizer reports a fidelity rating based on total RMSE across all quantized tensors:
|
| 178 |
+
|
| 179 |
+
| Rating | RMSE Threshold | Icon |
|
| 180 |
+
|--------|:-:|:-:|
|
| 181 |
+
| ULTRA | ≤ 1e-04 | ★★★★ |
|
| 182 |
+
| HIGH | ≤ 3e-04 | ★★★☆ |
|
| 183 |
+
| GOOD | ≤ 1e-03 | ★★☆☆ |
|
| 184 |
+
| STANDARD | > 1e-03 | ★☆☆☆ |
|
| 185 |
+
|
| 186 |
+
*Note: Due to anisotropic error shaping, a "GOOD" Shor-quantized model will typically outperform a "HIGH" BP-quantized model on reasoning tasks.*
|
| 187 |
+
|
| 188 |
+
### Common Issues
|
| 189 |
+
- **Arabic/Korean characters in output:** The embedded chat template is broken. Use `--chat-template-file` with the correct Jinja template.
|
| 190 |
+
- **RAM usage keeps growing:** Use `--cache-ram 0 -ctxcp 1` with llama.cpp to manage sliding window attention.
|
| 191 |
+
- **RMSE is higher than standard Q2_K:** This is intentional. The D₆ vesica gate trades total RMSE for computation-aligned error minimization.
|
| 192 |
+
- **libhexstate_q2k.so not found:** Make sure to compile the C engine using `make -f makefile.quantize`.
|
| 193 |
+
|
| 194 |
+
## 10. License
|
| 195 |
+
The quantizer code is part of the HPC project (MIT). Quantized models inherit the license of the base model (e.g., Gemma Terms of Use).
|