File size: 17,926 Bytes
6e7d405 a1aff72 6e7d405 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6e7d405 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 1c5db53 6390ee4 1c5db53 6390ee4 1c5db53 6390ee4 1c5db53 6390ee4 1c5db53 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 6390ee4 a1aff72 1c5db53 a1aff72 1c5db53 a1aff72 6390ee4 1c5db53 a1aff72 1c5db53 a1aff72 1c5db53 a1aff72 1c5db53 a1aff72 6390ee4 1c5db53 a1aff72 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | ---
language:
- ru
- en
license: apache-2.0
library_name: pytorch
tags:
- quantization
- progressive-quantization
- mixed-precision
- dynamic-bitwidth
- ternary
- bitnet
- xnli
- nli
datasets:
- facebook/xnli
- MoritzLaurer/multilingual-NLI-26lang-2mil7
- stanfordnlp/snli
metrics:
- accuracy
---
# Oscillation-Driven Precision Expansion (ODPE)
**Author:** Левон Гиносян (Levon Ginosyan / `Logn001`)
**Bottom-Up Progressive Quantization (`1-bit → FP32`) with Neuron Block Reallocation, Causal Rollback Penalty, Two-Stage Calibration, and `10M → 100M` Scaling Proof**
[](https://pytorch.org/)
[](LICENSE)
[](https://huggingface.co/datasets/facebook/xnli)
## Overview
Conventional neural network quantization trains in high precision (`FP32`/`BF16`) and compresses *downward*. **Oscillation-Driven Precision Expansion (ODPE)** initializes low-bit weights (`1-bit` FFN / `1.585–2-bit` Attention / `4-bit` Head) and grows precision *upward* only for oscillating critical neurons, proving cleanly that compression ratio and accuracy **improve together as scale increases from `10M` to `100M` parameters**:
1. **Adaptive V1–V5 (`10.85M`, 50k `facebook/xnli`):** Establishes oscillation routing (`PrecisionRouter`), homogeneous neuron block reallocation (`reallocate_blocks`), causal rollback penalties, attention/head protection, strict verify rollback (`enforce_rollback_in_verify`), Stage-1 best restoration, and Stage-2 EMA calibration (`9.44×–12.27×` compression).
2. **Adaptive V5.1–V5.8 (`10.85M → 10.67M`, 50k `facebook/xnli`):** Systematic refinements introducing learnable tier scales (`log_tier_scales`), salience-gated consolidation (`consolidate_down`), `group_size=64` sub-channel scaling, `RMSNorm + RoPE + QK-Norm + SwiGLU`, BitNet `1.58-bit` ternary rung `(1.0 → 1.585 → 2 → 4 → 8 → 16 → 32)`, Stage-2 Sharpness-Aware Minimization (`SAM`), and asymmetric embedding regularization — hitting **`1.94` bits (`16.48×`)** with **0% FP32** and **`52.04%`** Ep. 8 Stress accuracy.
3. **Adaptive V6 (`100M` / `99.17M`, 200k Multi-Source NLI):** Transfers the exact `V5.8` stack onto `100M` scale (`d_model=768`, `n_layers=12`, `n_heads=12`, `d_ff=2240`), reaching **`1.23` weight bits (`26.07×` pure weight compression; `1.48b / 21.66×` with g64 FP16 scales)**, **`0%` weights in `16b/FP32`**, **`54.51%`** unseen Stress 20k Ep. 4 accuracy (`+4.12` pp over `Classical FP32 100.51M`), and **`31.30 MiB`** standalone sub-byte `.safetensors` checkpoints.
See [`PAPER.md`](PAPER.md) for the full scientific manuscript and [`RESEARCH_NOTES.md`](RESEARCH_NOTES.md) for the chronological research log.
---
## Datasets & Splits Used for Training and Evaluation
This repository hosts model weights, shared tokenizer ([`artifacts/xnli_bpe_tokenizer.json`](artifacts/xnli_bpe_tokenizer.json)), training/quantization code, and evaluation telemetry. The datasets themselves are loaded cleanly from Hugging Face via [`data_xnli.py`](data_xnli.py) and [`experiment_100m.py`](experiment_100m.py) without redistributing raw corpora (all evaluation sets remain strictly disjoint from training):
| Benchmark Scale | Training Corpus & Exact Source Breakdown | Validation (`4,980`) | Test (`10,020`) | Unseen Stress (`20,000`) |
| --- | --- | :---: | :---: | :---: |
| **`10M` Models (`V1–V5`, `V5.1–V5.8`)** | **`50,000` pairs** (`facebook/xnli`: `25,000` RU + `25,000` EN train `[0..25k)` per language) | `facebook/xnli` val (`2,490` RU + `2,490` EN) | `facebook/xnli` test (`5,010` RU + `5,010` EN) | `facebook/xnli` disjoint `[50k..60k)` (`10k` RU + `10k` EN) |
| **`10M` Independent Fold (`seed=123`)** | **`50,000` pairs** (`facebook/xnli` disjoint `[25k..50k)` per language: `25k` RU + `25k` EN) | `facebook/xnli` val (`4,980`) | `facebook/xnli` test (`10,020`) | `facebook/xnli` disjoint `[50k..60k)` (`20,000`) |
| **`100M` Models (`FP32`, `V5`, `V6`)** | **`200,000` pairs**: [`facebook/xnli`](https://huggingface.co/datasets/facebook/xnli) (`60k` RU + `60k` EN non-stress) + [`MoritzLaurer/multilingual-NLI-26lang-2mil7`](https://huggingface.co/datasets/MoritzLaurer/multilingual-NLI-26lang-2mil7) (`40k` RU + `15k` EN disjoint ANLI/FEVER/LingNLI/WANLI) + [`stanfordnlp/snli`](https://huggingface.co/datasets/stanfordnlp/snli) (`25k` EN) | `facebook/xnli` val (`4,980`) | `facebook/xnli` test (`10,020`) | `facebook/xnli` disjoint `[50k..60k)` (`20,000`) |
---
## Stage 1 · Compact `10M` Foundation (`10.85M`, `50k` XNLI)
### 1.1 6-Way Controlled Experiment (`Classical FP32` vs. `Adaptive V1–V5`)
All six baseline runs share the exact same `BilingualNLITransformer` architecture (`10,846,723` total parameters; `7,578,560` adaptive linear parameters across `38` layers; `6` Pre-LN layers, `d_model=320`, `n_heads=8`, `d_ff=1280`, shared `10,000`-token BPE tokenizer), identical initial `state_dict` (`seed=42`), `50,000` training pairs (`25k ru + 25k en`), and `8` epochs (`batch_size=256`, **NVIDIA GeForce RTX 5070 Ti**):
| Metric | Classical FP32 | Adaptive V1 | Adaptive V2 | Adaptive V3 (Ep. 8 / Best Ep. 5) | Adaptive V4 (Ep. 8 / Best Ep. 7) | Adaptive V5 (Ep. 8 / Best Ep. 6) |
| --- | :---: | :---: | :---: | :---: | :---: | :---: |
| Mean Linear Bit-Width | 32.00 bits | 15.71 bits | 5.82 bits | 8.15 bits | 8.57 bits | **3.39 bits** |
| Linear Weight Memory | 28.91 MB | 14.19 MB (`2.04×`) | 5.26 MB (`5.50×`) | 7.37 MB (`3.92×`) | 7.74 MB (`3.73×`) | **3.06 MB (`9.44×`)** |
| Test Accuracy (`10,020` pairs) | 46.85% | 45.12% | 45.87% | 46.75% / 47.95% | 46.83% / 47.24% | **48.11%** / **48.76%** |
| Test RU / EN (`Ep. 8`) | 46.19% / 47.50% | 44.07% / 46.17% | 45.37% / 46.37% | 45.71% / 47.78% | 45.99% / 47.66% | **46.99% / 49.24%** |
| Test RU / EN (`Best Ckpt`) | — | — | — | 47.09% / 48.82% | 46.45% / 48.02% | **47.68% / 49.84%** |
| Test Cross-Entropy Loss | 2.0297 | 1.8084 | 1.6734 | 1.1599 / **1.0561** | 1.3855 / 1.3095 | **1.1441** / 1.0790 |
| Seen-Train Probe (`2,000`) | 94.60% | 88.65% | 87.20% | 71.20% / 63.70% | 83.50% / 80.50% | 75.50% / 70.75% |
| Peak Validation Accuracy | **49.88%** (Ep. 4) | 46.77% (Ep. 2) | 47.81% (Ep. 5) | 47.89% (Ep. 4) | 47.95% (Ep. 2) | **49.88%** (Ep. 6 EMA) |
| Total Training Time | **198.8s** | 682.1s | 646.8s | 535.2s (`46.8s`/ep S2) | 589.7s (`~46.2s`/ep S2) | 547.5s (`~48.4s`/ep S2) |
| Peak VRAM | **4,662 MB** | 5,049 MB | 5,053 MB | 5,178 MB | 5,262 MB | 5,310 MB |
#### Final Learned Bit-Width Allocation (`7,578,560` Linear Parameters, `10.85M` `V1–V5`)
| Precision Tier | Adaptive V1 | Adaptive V2 | Adaptive V3 | Adaptive V4 | Adaptive V5 |
| --- | :---: | :---: | :---: | :---: | :---: |
| `1-bit` | `16.47%` (`1.25M`) | `33.06%` (`2.51M`) | `28.51%` (`2.16M`) | `24.86%` (`1.88M`) | **`38.87%` (`2.95M`)** |
| `2-bit` | `15.89%` (`1.20M`) | `25.45%` (`1.93M`) | `21.85%` (`1.66M`) | `23.27%` (`1.76M`) | **`31.31%` (`2.37M`)** |
| `4-bit` | `12.46%` (`0.94M`) | `17.21%` (`1.30M`) | `16.46%` (`1.25M`) | `16.61%` (`1.26M`) | **`17.40%` (`1.32M`)** |
| `8-bit` | `8.11%` (`0.61M`) | `10.21%` (`0.77M`) | `11.06%` (`0.84M`) | `11.86%` (`0.90M`) | `7.50%` (`0.57M`) |
| `16-bit` | `6.15%` (`0.47M`) | `6.43%` (`0.49M`) | `7.44%` (`0.56M`) | `7.78%` (`0.59M`) | `3.10%` (`0.23M`) |
| `FP32` (`32-bit`) | `40.92%` (`3.10M`) | `7.64%` (`0.58M`) | `14.69%` (`1.11M`) | `15.62%` (`1.18M`) | **`1.82%` (`0.14M`)** |


### 1.2 Independent Disjoint-Fold & Calibration Validation (`10.85M`, `seed=123`)
Trained from a fresh initialization (`seed=123`) on `50,000` non-overlapping `facebook/xnli` pairs (`[25k..50k]` per language) and evaluated on `Test (10,020)` and the untouched **`20,000`-pair Stress Split** (`[50k..60k]` per language):
| Metric | Classical FP32 (`Ep. 8 / Best@3`) | Adaptive V5 (`Ep. 8 / Best@4`) |
| --- | :---: | :---: |
| Mean Linear Bit-Width / Memory | `32.00` bits / `28.91 MB` (`1.00×`) | **`2.61` bits / `2.36 MB` (`12.27×`)** (`1.25%` FP32) |
| XNLI Test Accuracy (`10,020`) | `47.48%` / **`49.14%`** | **`47.69%`** / `48.98%` |
| XNLI Test Macro-F1 | `0.4708` / `0.4770` | **`0.4752` / `0.4881`** |
| Unseen Stress Accuracy (`20,000`) | `45.96%` / **`48.76%`** | **`46.04%`** / `47.33%` |
| Stress Cross-Entropy Loss (`Ep. 8 / Best`) | `1.8998` / **`1.0300`** | **`1.3014`** / `1.1192` |
| Stress `10-bin ECE` (`Ep. 8 / Best`) | `0.3871` / **`0.1018`** | **`0.2623`** / `0.1645` |
| Wrong-Answer Confidence (`Stress Ep. 8`) | `82.75%` | **`69.87%`** |

---
## Stage 2 · Compact `10M` Evolution: `V5.1 → V5.8` (50k XNLI)
[`adaptive_v5_series.py`](adaptive_v5_series.py) systematically refined quantization precision, architecture, and Stage-2 calibration at `10M` (`seed=42`, `50k` train, `10,020` test, `20,000` unseen stress, `8` epochs, `bsz=256`):
| Variant | Key Mechanisms | Params | Test (`Ep.8 / BestVal`) | Stress 20k (`Ep.8 / BestVal`) | ECE Best (`T / S`) | Mean Bits (Comp.) |
| --- | --- | :---: | :---: | :---: | :---: | :---: |
| `FP32` | Baseline Pre-LN Transformer | 10.85M | 46.56% / 48.99% | 46.36% / 47.66% | 0.2014 / 0.2198 | 32.00b (1.00×) |
| `V5 Base` | Protected `2b/4b` + Strict Verify | 10.85M | 48.61% / 48.64% | 47.06% / 47.40% | 0.0681 / 0.0800 | 2.62b (12.21×) |
| `V5.1` | Weighted bit-cost + unit-fraction penalty | 10.85M | 48.33% / 49.76% | 46.92% / 48.99% | 0.0805 / 0.0856 | 2.50b (12.80×) |
| `V5.2` | Loss-guarded val score + tuned EMA LR | 10.85M | 48.80% / 49.37% | 47.71% / 48.84% | 0.0707 / 0.0746 | 2.56b (12.49×) |
| `V5.3` | Tier Costs (`0.35/1.15/0.25`) + LSQ `log_tier_scales` + STE clip | 10.85M | 49.44% / 50.09% | 48.47% / 48.87% | 0.0509 / 0.0642 | 2.19b (14.61×) |
| `V5.4` | V5.3 + Salience Gating + `consolidate_down` | 10.85M | 49.38% / 50.10% | 48.60% / 47.46% | **0.0183 / 0.0493** | 2.10b (15.21×) |
| `V5.5` | V5.4 + Stage-2 EMA Self-Distillation | 10.85M | 48.58% / 50.00% | 48.23% / 49.36% | 0.0703 / 0.0779 | 2.24b (14.29×) |
| `V5.6` | `Group64` + `RMSNorm` + `RoPE` + `QK-Norm` + `SwiGLU` | 10.67M | 50.52% / 52.48% | 49.51% / 51.14% | 0.0887 / 0.1017 | 3.22b (9.95×) |
| `V5.7` | V5.6 + `1.58b` Ternary + Cosine Curriculum + 2-Batch Verify | 10.67M | 50.74% / **53.14%** | 50.07% / **51.67%** | 0.0746 / 0.0874 | 2.58b (12.41×) |
| **`V5.8`** | **V5.7 + `8b/16b` Cap + Stage-2 `SAM` + Emb Reg + S1 Smooth** | **10.67M** | **51.93%** / 52.99% | **52.04%** / 51.64% | 0.0522 / 0.0568 | **1.94b (16.48×)** |
*(For `V5.6–V5.8`, `group_size=64` FP16 scale metadata adds `+0.25b`, making `V5.8` `2.19b / 14.60×` end-to-end; all `10M` runs are saved in [`results/adaptive_v5_series_results.json`](results/adaptive_v5_series_results.json).)*

---
## Stage 3 · Large `100M` Scalability Proof: `Adaptive V6` (`26.07×` Compression)
Scaling raw `Adaptive V5` from `10M` to `100.51M` initially stalled at `1.38b` (`48.89%` test vs. `50.34%` `Classical FP32`) because wide per-row scaling (`d_ff=3424`) lacked sub-channel granularity and ternary headroom. Transferring the **`Adaptive V5.8`** stack onto **`Adaptive V6 100M` (`99.17M` params: `d_model=768`, `n_layers=12`, `n_heads=12`, `d_ff=2240`)** on `200,000` multi-source pairs (`bsz=64`, 4 epochs, `seed=42`) demonstrates that **as model scale increases from `10M` to `100M`, ODPE achieves BOTH stronger compression (`16.48× → 26.07×`) AND higher unseen generalization (`52.04% → 54.51%` Stress 20k) without losing quality**:
| Metric (`100M`, `200k` Multi-Source, `seed=42`) | Classical FP32 (`100.51M`) | Adaptive V5 (`100.51M`) | **Adaptive V6 (`99.17M`, V5.8 Stack)** |
| --- | :---: | :---: | :---: |
| Mean Linear Bit-Width | `32.00` bits | `1.38` bits | **`1.23` bits** (`1.48b` incl. g64 FP16 scales) |
| Weight Compression Ratio | `1.00×` (`353.26 MB`) | `23.11×` (`15.29 MB`) | **`26.07×`** (**`13.38 MB`** vs `348.76 MB`, **`21.66×`** w/ scales) |
| Standalone Packed `.safetensors` Checkpoint | ~402 MB (FP32) | ~402 MB (unpacked) | **`31.30 MiB`** (`32.8 MB` total w/ FP16 emb + scales) |
| `FP32` (`32b`) / `16b` Linear Occupancy | `100%` | `0.003%` / `0.04%` | **`0.00%` / `0.00%`** (`66.50%` 1b, `30.73%` 1.585b, `2.10%` 2b, `0.50%` 4b, `0.17%` 8b) |
| Peak Validation Accuracy (`4,980`) | `50.20%` (Ep. 3) | `48.43%` (Ep. 3) | **`53.41%`** (Ep. 3, `val_loss = 0.9419`) |
| Test Accuracy (`10,020`, `Final / BestVal`) | `50.34% / 49.70%` | `48.38% / 48.89%` | **`52.10% / 52.61%`** (**`+2.91` pp BestVal**, `RU 51.40% / EN 53.83%`) |
| Unseen Stress `20,000` (`Final / BestVal`) | `50.39% / 50.31%` | `50.83% / 50.03%` | **`54.51% / 54.22%`** (**`+4.12` pp Ep.4**, `RU 53.81% / EN 55.20%`) |
| Macro-F1 (`Test Best / Stress Final`) | `0.4858 / 0.5054` | `0.4755 / 0.5096` | **`0.5224 / 0.5444`** |
| `10-bin ECE` (`Test Ep.4 / Test BestVal`) | `0.0278 / 0.0216` | `0.0372 / 0.0243` | **`0.0173 / 0.0116`** |
**`Adaptive V6 100M` Layer-Tier Bit Breakdown (`0%` `16-bit` or `FP32`):**
- **Attention (`28.31M` linear params):** **`1.67` bits** (`92.31%` at `1.585b` BitNet ternary).
- **FFN SwiGLU (`61.93M` linear params):** **`1.02` bits** (`98.16%` at `1.0b`, `1.41%` at `1.585b`).
- **Classifier Head (`1.18M` linear params):** **`1.69` bits** (`92.40%` at `1.585b`).
**`10M → 100M` Scaling Trajectory Summary:**
- `Classical FP32`: Test BestVal `48.99% → 49.70%` (`+0.71` pp), Stress Ep. 8/4 `46.36% → 50.39%` at `32.00b`.
- `ODPE (V5.8 → V6)`: Compression sharpens **`16.48× → 26.07×`** (`1.94b → 1.23b`), `Test ECE` improves **`0.0522 → 0.0116`**, and `Stress` accuracy climbs **`52.04% → 54.51%`** (`+4.12` pp above `FP32 100M`).


---
## Repository Structure
- [`progressive_quant.py`](progressive_quant.py) — `ProgressiveLinear`, `ProgressiveConv2d`, fused block execution (`reallocate_blocks`), sub-channel `group_size` scaling, BitNet `1.58-bit` ternary rung, `PrecisionRouter`, strict loss-gain bit-cost verification, and sub-byte checkpoint packer (`pack_quantized_state_dict` / `unpack_quantized_state_dict`).
- [`model.py`](model.py) — Paired `BilingualNLITransformer` supporting FP32/progressive linear layers, attention/head bit protection, optional `RMSNorm`, `RotaryEmbedding` (`RoPE`), `QK-Norm`, `SwiGLU`, and bit ceilings (`backbone_max_bits` / `head_max_bits`).
- [`data_xnli.py`](data_xnli.py) — `facebook/xnli` (`ru` + `en`) loader and shared `10,000`-token ByteLevel BPE tokenizer builder.
- [`train_experiment.py`](train_experiment.py) — Reproducible `10M` training (`Classical`, `V1`–`V5`), atomic resume checkpoints, evaluation, JSON telemetry export, and Matplotlib figure generation.
- [`adaptive_v5_series.py`](adaptive_v5_series.py) — Controlled `V5.1`–`V5.8` (`10M`) benchmark harness implementing `log_tier_scales`, `consolidate_down`, cosine `bit_cost_curriculum`, 2-batch verification, Stage-2 `SAM`, and asymmetric embedding regularization.
- [`experiment_100m.py`](experiment_100m.py) — `100M` (`200k` multi-source NLI) scaling pipeline running `Classical FP32`, `Adaptive V5`, and `Adaptive V6 (99.17M)` with per-epoch resume and comparison plots.
- [`validate_fp32_vs_v5.py`](validate_fp32_vs_v5.py) — Independent disjoint-fold validation (`seed=123`, `20k` stress split, `10-bin ECE`, Brier score, Macro-F1, long-sequence robustness, and symmetric Best-Val checkpoint evaluation).
- [`export_models.py`](export_models.py) & [`inference.py`](inference.py) — Export of `10M` `.safetensors` checkpoints (`checkpoints/adaptive_v5_*.safetensors`, `checkpoints/classical_fp32_*.safetensors`) and **`31.30 MiB`** bit-packed `Adaptive V6 100M` checkpoints (`checkpoints/adaptive_v6_100m_*.safetensors`), plus unified RU/EN inference loader. *(Intermediate `V1–V4` and `V5.1–V5.8` ablations store complete telemetry, curves, and bit-width logs in [`results/experiment_results.json`](results/experiment_results.json) and [`results/adaptive_v5_series_results.json`](results/adaptive_v5_series_results.json) without duplicate weights in `checkpoints/`.)*
- [`test_progressive_quant.py`](test_progressive_quant.py) — Unit tests (`19/19` passing) covering quantization grids, `1.58b` ternary levels, `group_size`, `RoPE/SwiGLU`, `max_bits`, `SAM`, atomic resume parity, and sub-byte packed checkpoint round-trip.
- [`LICENSE`](LICENSE) — Apache License 2.0 (`Copyright 2026 Levon Ginosyan (Logn001)`).
- [`PAPER.md`](PAPER.md) — Complete scientific manuscript.
- [`RESEARCH_NOTES.md`](RESEARCH_NOTES.md) — Chronological research log.
---
## Quickstart & Reproduction
```bash
# 1. Run all unit tests (19/19)
python -m unittest -v test_progressive_quant.py
# 2. Download/cache XNLI (ru + en) and build shared BPE tokenizer
python data_xnli.py
# 3. Run full 10M iterative series (V5.1 .. V5.8 vs Classical FP32)
python adaptive_v5_series.py
# 4. Run 100M Adaptive V6 on 200k multi-source NLI (reusing saved FP32/V5 baselines)
python experiment_100m.py --mode v6 --dataset-mode expanded --epochs 4 --batch-size 64 --min-stage1-epochs 2 --max-stage1-epochs 2 --warmup-steps 250 --resume-dir .ckpt_100m_v6
# 5. Export compact 31.30 MiB bit-packed Adaptive V6 100M checkpoints and run inference
python export_models.py --v6-100m
python inference.py --checkpoint checkpoints/adaptive_v6_100m_best_val.safetensors --premise "Машина быстро едет по трассе." --hypothesis "Транспортное средство движется."
```
|