Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -6,163 +6,223 @@ tags:
|
|
| 6 |
- bitnet
|
| 7 |
- ggml
|
| 8 |
- simd
|
| 9 |
-
- lora
|
| 10 |
-
- finetuning
|
| 11 |
- edge-ai
|
|
|
|
| 12 |
license: mit
|
| 13 |
library_name: smart-tree
|
| 14 |
---
|
| 15 |
|
| 16 |
-
#
|
| 17 |
|
| 18 |
-
|
| 19 |
-
<img src="https://img.shields.io/badge/rust-%23dea584.svg?logo=rust&logoColor=white"/>
|
| 20 |
-
<img src="https://img.shields.io/badge/cloudflare-F38020?logo=cloudflare&logoColor=white"/>
|
| 21 |
-
<img src="https://img.shields.io/badge/webassembly-654FF0?logo=webassembly&logoColor=white"/>
|
| 22 |
-
<img src="https://img.shields.io/badge/mlx-ECEEF1?logo=apple&logoColor=black"/>
|
| 23 |
-
</p>
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
---
|
| 28 |
|
| 29 |
-
##
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|-----------|---------|------------|-----------------|
|
| 35 |
-
| BF16 | 2× | 1× | Baseline |
|
| 36 |
-
| INT8 | ¼× | ~⅓× | Near-baseline |
|
| 37 |
-
| **INT2/I2_S** | **⅛×** | **~¹⁄₁₂×** | **Surprisingly good** |
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
##
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
| [`quantlab`](https://github.com/peterlodri-sec/quantlab) | Automated quantization search: Pareto-Q, KD pipelines, RL tuning | 🚧 Building |
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
```bash
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
./st gguf
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
./st
|
| 69 |
```
|
| 70 |
|
| 71 |
-
##
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
```
|
| 77 |
|
|
|
|
|
|
|
| 78 |
```rust
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
let packed = I2SPacker::pack_trits(&[-1, 0, 1, 0, -1, 1]);
|
| 85 |
|
| 86 |
-
|
| 87 |
-
let i2s_bytes = GgufI2SBridge::wave64_to_i2s(&wave)?;
|
| 88 |
-
let recovered = GgufI2SBridge::i2s_to_wave64(&i2s_bytes)?;
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
```
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
```
|
| 110 |
-
Zero-copy conversions with exact arithmetic guarantees.
|
| 111 |
-
|
| 112 |
-
### GGUF Native I2_S Support — Experiment 2 ✅
|
| 113 |
-
First-class support for `quantization_type = 36 (I2_S)`:
|
| 114 |
-
- MSB-first 4-trits/byte (Bits 6-7, 4-5, 2-3, 0-1)
|
| 115 |
-
- Embedded/scales/env-configurable dequantization
|
| 116 |
-
- Falcon3, BitNet.cpp, llama.cpp compatible
|
| 117 |
-
|
| 118 |
-
### SIMD Kernels — Experiment 3 ✅
|
| 119 |
-
- **ARM64 NEON**: 8-lane vectorized, `vld1q_s16` + `vmull` + `vpadal`
|
| 120 |
-
- **AVX2**: 8-lane `VPBLENDVB` mask blend, `VPMADDWD` horizontal sum
|
| 121 |
-
- **Scalar fallback**: Branch-free, predictable latency
|
| 122 |
-
- Integrated FELIX sovereign assembly engine
|
| 123 |
-
|
| 124 |
-
### LoRA-T Fine-Tuning — Experiment 4 ✅
|
| 125 |
-
- **Freeze** pretrained ternary weights (I2_S / BitNetWave64)
|
| 126 |
-
- **Inject** trainable low-rank adapters (A @ B) in FP32
|
| 127 |
-
- **Forward**: `y = W_ternary @ x + α * (A @ B) @ x`
|
| 128 |
-
- **Gradient** flows only through A, B (ranks 8-64)
|
| 129 |
-
- **Fusion** for zero-overhead inference deployment
|
| 130 |
-
- Factories for Qwen2.5-0.5B, custom transformers
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
##
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
-
|
| 137 |
-
- [x] **Experiment 2**: GGUF I2_S (type 36) Interop
|
| 138 |
-
- [x] **Experiment 3**: ARM64 NEON / AVX2 SIMD kernels for ternary GEMM
|
| 139 |
-
- [x] **Experiment 4**: QLoRA-style ternary fine-tuning (LoRA-T)
|
| 140 |
-
- [ ] **Experiment 5**: Mixed-precision profiler (layer-wise sensitivity)
|
| 141 |
-
- [ ] **Experiment 6**: Residual injection scheduler (dynamic precision)
|
| 142 |
-
- [ ] **Experiment 7**: Pareto-Q sequential allocator
|
| 143 |
-
- [ ] **Experiment 8**: KD pipeline teacher→student (ternary)
|
| 144 |
-
- [ ] **Experiment 9**: BITCOS cosine similarity retrieval
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
-
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
|
| 168 |
-
|
|
|
|
| 6 |
- bitnet
|
| 7 |
- ggml
|
| 8 |
- simd
|
|
|
|
|
|
|
| 9 |
- edge-ai
|
| 10 |
+
- binary-release
|
| 11 |
license: mit
|
| 12 |
library_name: smart-tree
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# smart-tree — Ternary Inference Engine
|
| 16 |
|
| 17 |
+
🚀 **Production-ready binary for BitNet 1.58-bit / GGUF I2_S (type 36) inference with SIMD acceleration**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
## Overview
|
| 20 |
|
| 21 |
+
`smart-tree` is a standalone CLI and library for ultra-low-precision neural network inference. Designed specifically for **ternary weights ({-1, 0, +1})** encoded in GGUF's `I2_S` format (quantization type 36).
|
| 22 |
|
| 23 |
+
### Why Ternary?
|
| 24 |
|
| 25 |
+
| Aspect | Benefit |
|
| 26 |
+
|--------|---------|
|
| 27 |
+
| **Size** | 8× smaller than FP16 |
|
| 28 |
+
| **Speed** | Integer ADD replaces FMAC |
|
| 29 |
+
| **Energy** | ~12× less energy per MAC |
|
| 30 |
+
| **Quality** | Surprising resilience (straight-through estimator) |
|
| 31 |
|
| 32 |
+
## Installation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
```bash
|
| 35 |
+
# Latest release
|
| 36 |
+
curl -LO https://huggingface.co/eightb/smart-tree/resolve/main/st
|
| 37 |
+
chmod +x st
|
| 38 |
+
./st --version
|
| 39 |
+
```
|
| 40 |
|
| 41 |
+
Or build from source:
|
| 42 |
+
```bash
|
| 43 |
+
git clone https://github.com/peterlodri-sec/smart-tree
|
| 44 |
+
cd smart-tree && cargo build --release
|
| 45 |
+
```
|
| 46 |
|
| 47 |
+
## Usage
|
| 48 |
|
| 49 |
+
### GGUF I2_S Validation
|
| 50 |
+
```bash
|
| 51 |
+
# Self-test roundtrip
|
| 52 |
+
./st gguf validate
|
| 53 |
+
# Output: Roundtrip: OK Bytes: 16
|
|
|
|
| 54 |
|
| 55 |
+
# Pack/unpack demonstration
|
| 56 |
+
./st gguf pack-test
|
| 57 |
+
# Input: [-1, 0, 1, 0, -1, 1, -1, 1]
|
| 58 |
+
# Packed: [19, 22]
|
| 59 |
+
# Output: [-1, 0, 1, 0, -1, 1, -1, 1]
|
| 60 |
+
```
|
| 61 |
|
| 62 |
+
### BitNetWave64 ↔ I2_S Conversion
|
| 63 |
+
```bash
|
| 64 |
+
# Convert 64-trit wave to I2_S hex (16 bytes)
|
| 65 |
+
./st gguf convert --wave AAAAAAAAAAAAAAAA:5555555555555555
|
| 66 |
+
# Output: Hex: 22222222222222222222222222222222
|
| 67 |
+
```
|
| 68 |
|
| 69 |
+
### Inspect GGUF Files
|
| 70 |
```bash
|
| 71 |
+
# Scan for I2_S tensors in a GGUF file
|
| 72 |
+
./st gguf inspect --file falcon3-3b-i2s.gguf --verbose
|
| 73 |
+
```
|
| 74 |
|
| 75 |
+
### Mixed-Precision Profiling (Experiment 5)
|
| 76 |
+
```bash
|
| 77 |
+
# Profile a GGUF model
|
| 78 |
+
./st profile --model model.gguf --samples 128 --max-seq-len 512
|
| 79 |
|
| 80 |
+
# Export Pareto frontier as JSON
|
| 81 |
+
./st profile --model model.gguf --output json --out pareto.json
|
| 82 |
|
| 83 |
+
# CSV for spreadsheet analysis
|
| 84 |
+
./st profile --model model.gguf --output csv --out layers.csv
|
| 85 |
```
|
| 86 |
|
| 87 |
+
## Technical Details
|
| 88 |
|
| 89 |
+
### I2_S Encoding (GGUF Type 36)
|
| 90 |
+
- **4 trits per byte**, MSB-first within byte
|
| 91 |
+
- Trit encoding: `00=-1`, `01=0`, `10=+1`, `11=pad→0`
|
| 92 |
+
- Byte layout: `[trit0:bits6-7][trit1:bits4-5][trit2:bits2-3][trit3:bits0-1]`
|
| 93 |
+
- Fully compatible with **Falcon3-3B-I2_S**, **BitNet.cpp**, **llama.cpp**
|
| 94 |
+
|
| 95 |
+
### BitNetWave64 Structure
|
| 96 |
+
```rust
|
| 97 |
+
pub struct BitNetWave64 {
|
| 98 |
+
pub b_pos: u64, // Positive rail (popcnt ≈ 21)
|
| 99 |
+
pub b_neg: u64, // Negative rail (popcnt ≈ 21)
|
| 100 |
+
pub balance_permille: i16, // (pos-neg)*1000/(pos+neg)
|
| 101 |
+
pub box_norm_q8: u8, // sqrt(pos²+neg²)/sqrt(2)*256
|
| 102 |
+
}
|
| 103 |
```
|
| 104 |
|
| 105 |
+
### Scale Handling
|
| 106 |
+
Three strategies for dequantization:
|
| 107 |
```rust
|
| 108 |
+
pub enum I2SScaleSource {
|
| 109 |
+
Sidecar, // Embedded per-tensor scale
|
| 110 |
+
Global(f32), // Env PZ_I2S_SCALE or CLI override
|
| 111 |
+
Strict, // Error if no scale found
|
| 112 |
+
}
|
| 113 |
+
```
|
| 114 |
|
| 115 |
+
## NEW: SIMD-Accelerated Ternary GEMM (Experiment 3)
|
|
|
|
| 116 |
|
| 117 |
+
Integrated **FELIX SIMD hot-path engine** for blazing-fast ternary matrix multiplies:
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
### Backends
|
| 120 |
+
| Architecture | Instruction Set | Lanes | Throughput |
|
| 121 |
+
|--------------|-----------------|-------|------------|
|
| 122 |
+
| **AArch64** | NEON | 8 | ~4 cycles/64 trits |
|
| 123 |
+
| **x86_64** | SSE2/AVX2 | 8 | ~6 cycles/64 trits |
|
| 124 |
+
| **Any** | Portable SWAR | 4 | ~20 cycles/64 trits |
|
| 125 |
|
| 126 |
+
### API
|
| 127 |
+
```rust
|
| 128 |
+
use smart_tree::simd_ternary::{
|
| 129 |
+
i2s_unpack_to_trits,
|
| 130 |
+
simd_ternary_gemm_i2s,
|
| 131 |
+
simd_ternary_row_i2s,
|
| 132 |
+
simd_backend,
|
| 133 |
+
};
|
| 134 |
|
| 135 |
+
println!("Backend: {}", simd_backend()); // "NEON (AArch64)" on Mac/Apple Silicon
|
| 136 |
|
| 137 |
+
// Full matrix GEMM
|
| 138 |
+
simd_ternary_gemm_i2s(&weights_i2s, rows, cols, &activations, &mut output, scales);
|
| 139 |
|
| 140 |
+
// Autoregressive single-row (lowest latency)
|
| 141 |
+
let out_val = simd_ternary_row_i2s(&row_weights_i2s, &activations, scale);
|
| 142 |
```
|
| 143 |
+
|
| 144 |
+
### Performance Characteristics
|
| 145 |
+
- **Zero-allocation** hot path (stack buffers for ≤256 trits)
|
| 146 |
+
- **Cache-line aware** chunking (64 trits = 16 bytes = 1 L1 line)
|
| 147 |
+
- **Prefetch hints** for sequential layer traversal
|
| 148 |
+
- **Exact integer arithmetic** throughout (no FP in hot path)
|
| 149 |
+
|
| 150 |
+
## NEW: Mixed-Precision Profiler (Experiment 5)
|
| 151 |
+
|
| 152 |
+
Layer-wise sensitivity analysis for optimal ternary quantization decisions:
|
| 153 |
+
|
| 154 |
+
```bash
|
| 155 |
+
# Profile a GGUF model
|
| 156 |
+
./st profile --model model.gguf --samples 128 --max-seq-len 512
|
| 157 |
+
|
| 158 |
+
# Export Pareto frontier as JSON
|
| 159 |
+
./st profile --model model.gguf --output json --out pareto.json
|
| 160 |
+
|
| 161 |
+
# CSV for spreadsheet analysis
|
| 162 |
+
./st profile --model model.gguf --output csv --out layers.csv
|
| 163 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
### Algorithm
|
| 166 |
+
1. **Calibration**: Run representative inputs through the model
|
| 167 |
+
2. **Activation Collection**: Capture post-residual activations per layer
|
| 168 |
+
3. **Perturbation Analysis**: Simulate quantization at multiple bit-widths (2, 3, 4, 8...)
|
| 169 |
+
4. **Sensitivity Metrics**: Cosine similarity, KL-divergence, Signal-to-Noise Ratio
|
| 170 |
+
5. **Composite Ranking**: Aggregate metrics into per-layer sensitivity score
|
| 171 |
+
6. **Pareto Frontier**: Greedy assignment finds optimal accuracy-vs-bits tradeoffs
|
| 172 |
|
| 173 |
+
### Output
|
| 174 |
+
- **Human-readable table**: Top-K most sensitive layers with slopes
|
| 175 |
+
- **JSON**: Full per-layer, per-bit-width metrics for downstream tools
|
| 176 |
+
- **CSV**: Spreadsheet-ready layer rankings with all metrics
|
| 177 |
|
| 178 |
+
Enables mixed-precision quantization: keep sensitive layers at higher precision, aggressively compress robust layers.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
## Integration
|
| 181 |
+
|
| 182 |
+
### As Rust Dependency
|
| 183 |
+
```toml
|
| 184 |
+
[dependencies]
|
| 185 |
+
smart-tree = { git = "https://github.com/peterlodri-sec/smart-tree" }
|
| 186 |
+
```
|
| 187 |
|
| 188 |
+
```rust
|
| 189 |
+
use smart_tree::gguf_interop::{I2SPacker, GgufI2SBridge};
|
| 190 |
+
use smart_tree::simd_ternary::{simd_ternary_gemm_i2s, simd_backend};
|
| 191 |
|
| 192 |
+
// Low-level pack
|
| 193 |
+
let bytes = I2SPacker::pack_trits(&[-1, 0, 1, 0]);
|
| 194 |
+
let trits = I2SPacker::unpack_trits(&bytes);
|
| 195 |
|
| 196 |
+
// High-level bridge
|
| 197 |
+
let i2s = GgufI2SBridge::wave64_to_i2s(&wave)?;
|
| 198 |
+
let wave = GgufI2SBridge::i2s_to_wave64(&i2s)?;
|
| 199 |
|
| 200 |
+
// SIMD GEMM
|
| 201 |
+
println!("Running on: {}", simd_backend());
|
| 202 |
+
simd_ternary_gemm_i2s(&weights, rows, cols, &acts, &mut out, scales);
|
| 203 |
+
```
|
| 204 |
|
| 205 |
+
## Testing
|
| 206 |
|
| 207 |
+
All cores validated:
|
| 208 |
+
```bash
|
| 209 |
+
cargo test --lib ternary_bridge # 7 tests - lossless bridges
|
| 210 |
+
cargo test --lib gguf_interop # 10 tests - GGUF I2_S roundtrips
|
| 211 |
+
cargo test --lib simd_ternary # 5 tests - SIMD GEMM correctness
|
| 212 |
+
cargo test --lib profiler # 16 tests - sensitivity metrics & pipeline
|
| 213 |
+
```
|
| 214 |
+
|
| 215 |
+
## Related Work
|
| 216 |
|
| 217 |
+
- **[BitNet 1.58](https://arxiv.org/abs/2402.17764)**: Original ternary transformer
|
| 218 |
+
- **[Falcon3-I2_S](https://huggingface.co/tiiuae/Falcon3-3B-Instruct-GGUF)**: Reference GGUF I2_S model
|
| 219 |
+
- **[enthea](https://github.com/peterlodri-sec/enthea)**: Sister crate for quantized GEMM/attention
|
| 220 |
+
- **[FELIX Assembly](https://github.com/peterlodri-sec/felix-assembly)**: CPU-agnostic SIMD ISA
|
| 221 |
|
| 222 |
+
## License
|
| 223 |
+
|
| 224 |
+
MIT OR Apache-2.0
|
| 225 |
|
| 226 |
+
---
|
| 227 |
|
| 228 |
+
Part of the [**eightb**](https://huggingface.co/eightb) ecosystem.
|
st
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:15017ef9d90d698c434e3acc2b4ba273211cbd72c7e80d3bf8fccef31087461c
|
| 3 |
+
size 33892704
|