File size: 6,299 Bytes
102aaba
 
 
 
 
 
 
 
 
8963552
102aaba
 
 
 
355d424
8963552
 
8215cbd
8963552
8215cbd
8963552
8215cbd
8963552
 
 
 
 
 
8215cbd
8963552
8215cbd
8963552
 
 
 
 
 
8215cbd
8963552
 
 
 
 
8215cbd
8963552
8215cbd
8963552
 
 
 
 
8215cbd
8963552
 
 
 
 
 
8215cbd
8963552
 
 
 
 
 
8215cbd
8963552
8215cbd
8963552
 
 
8215cbd
8963552
8215cbd
8963552
 
 
 
 
 
 
 
 
 
 
 
 
 
8215cbd
 
8963552
 
8215cbd
8963552
 
 
 
 
 
8215cbd
8963552
8215cbd
8963552
2f384ce
8963552
 
 
 
 
 
3d9a5b2
8963552
 
 
 
 
 
 
 
8215cbd
8963552
8215cbd
8963552
 
8215cbd
8963552
 
8215cbd
8963552
 
 
 
 
 
 
 
 
 
 
 
 
 
8215cbd
8963552
 
 
8215cbd
8963552
 
 
8215cbd
8963552
 
 
8215cbd
8963552
 
 
 
8215cbd
8963552
8215cbd
8963552
 
 
 
 
 
355d424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8963552
 
8215cbd
8963552
 
 
 
8215cbd
8963552
 
 
8215cbd
8963552
8215cbd
355d424
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
198
199
200
201
202
203
204
205
---
tags:
  - rust
  - quantization
  - ternary
  - bitnet
  - ggml
  - simd
  - edge-ai
  - binary-release
license: mit
library_name: smart-tree
---

# smart-tree v25.0.0 — Ternary Inference Engine
🚀 **Production-ready binary for BitNet 1.58-bit / GGUF I2_S (type 36) inference with SIMD acceleration**
## Overview

`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).

### Why Ternary?

| Aspect | Benefit |
|--------|---------|
| **Size** | 8× smaller than FP16 |
| **Speed** | Integer ADD replaces FMAC |
| **Energy** | ~12× less energy per MAC |
| **Quality** | Surprising resilience (straight-through estimator) |

## Installation

```bash
# Latest release
curl -LO https://huggingface.co/eightb/smart-tree/resolve/main/st
chmod +x st
./st --version
```

Or build from source:
```bash
git clone https://github.com/peterlodri-sec/smart-tree
cd smart-tree && cargo build --release
```

## Usage

### GGUF I2_S Validation
```bash
# Self-test roundtrip
./st gguf validate
# Output: Roundtrip: OK Bytes: 16

# Pack/unpack demonstration
./st gguf pack-test
# Input:  [-1, 0, 1, 0, -1, 1, -1, 1]
# Packed: [19, 22]
# Output: [-1, 0, 1, 0, -1, 1, -1, 1]
```

### BitNetWave64 ↔ I2_S Conversion
```bash
# Convert 64-trit wave to I2_S hex (16 bytes)
./st gguf convert --wave AAAAAAAAAAAAAAAA:5555555555555555
# Output: Hex: 22222222222222222222222222222222
```

### Inspect GGUF Files
```bash
# Scan for I2_S tensors in a GGUF file
./st gguf inspect --file falcon3-3b-i2s.gguf --verbose
```

## Technical Details

### I2_S Encoding (GGUF Type 36)
- **4 trits per byte**, MSB-first within byte
- Trit encoding: `00=-1`, `01=0`, `10=+1`, `11=pad→0`
- Byte layout: `[trit0:bits6-7][trit1:bits4-5][trit2:bits2-3][trit3:bits0-1]`
- Fully compatible with **Falcon3-3B-I2_S**, **BitNet.cpp**, **llama.cpp**

### BitNetWave64 Structure
```rust
pub struct BitNetWave64 {
    pub b_pos: u64,           // Positive rail (popcnt ≈ 21)
    pub b_neg: u64,           // Negative rail (popcnt ≈ 21)
    pub balance_permille: i16, // (pos-neg)*1000/(pos+neg)
    pub box_norm_q8: u8,       // sqrt(pos²+neg²)/sqrt(2)*256
}
```

### Scale Handling
Three strategies for dequantization:
```rust
pub enum I2SScaleSource {
    Sidecar,      // Embedded per-tensor scale
    Global(f32),  // Env PZ_I2S_SCALE or CLI override
    Strict,       // Error if no scale found
}
```

## NEW: SIMD-Accelerated Ternary GEMM (Experiment 3)

Integrated **FELIX SIMD hot-path engine** for blazing-fast ternary matrix multiplies:

### Backends
| Architecture | Instruction Set | Lanes | Throughput |
|--------------|-----------------|-------|------------|
| **AArch64** | NEON | 8 | ~4 cycles/64 trits |
| **x86_64** | SSE2/AVX2 | 8 | ~6 cycles/64 trits |
| **Any** | Portable SWAR | 4 | ~20 cycles/64 trits |

### API
```rust
use smart_tree::simd_ternary::{
    i2s_unpack_to_trits,
    simd_ternary_gemm_i2s,
    simd_ternary_row_i2s,
    simd_backend,
};

println!("Backend: {}", simd_backend()); // "NEON (AArch64)" on Mac/Apple Silicon

// Full matrix GEMM
simd_ternary_gemm_i2s(&weights_i2s, rows, cols, &activations, &mut output, scales);

// Autoregressive single-row (lowest latency)
let out_val = simd_ternary_row_i2s(&row_weights_i2s, &activations, scale);
```

### Performance Characteristics
- **Zero-allocation** hot path (stack buffers for ≤256 trits)
- **Cache-line aware** chunking (64 trits = 16 bytes = 1 L1 line)
- **Prefetch hints** for sequential layer traversal
- **Exact integer arithmetic** throughout (no FP in hot path)

## Integration

### As Rust Dependency
```toml
[dependencies]
smart-tree = { git = "https://github.com/peterlodri-sec/smart-tree" }
```

```rust
use smart_tree::gguf_interop::{I2SPacker, GgufI2SBridge};
use smart_tree::simd_ternary::{simd_ternary_gemm_i2s, simd_backend};

// Low-level pack
let bytes = I2SPacker::pack_trits(&[-1, 0, 1, 0]);
let trits = I2SPacker::unpack_trits(&bytes);

// High-level bridge
let i2s = GgufI2SBridge::wave64_to_i2s(&wave)?;
let wave = GgufI2SBridge::i2s_to_wave64(&i2s)?;

// SIMD GEMM
println!("Running on: {}", simd_backend());
simd_ternary_gemm_i2s(&weights, rows, cols, &acts, &mut out, scales);
```

## Testing

All cores validated:
```bash
cargo test --lib ternary_bridge    # 7 tests - lossless bridges
cargo test --lib gguf_interop      # 10 tests - GGUF I2_S roundtrips
cargo test --lib simd_ternary      # 5 tests - SIMD GEMM correctness
```
## NEW: Mixed-Precision Profiler (Experiment 5)
Layer-wise sensitivity analysis for optimal ternary quantization decisions:
```bash
# Profile a GGUF model
./st profile --model model.gguf --samples 128 --max-seq-len 512
# Export Pareto frontier as JSON
./st profile --model model.gguf --output json --out pareto.json
# CSV for spreadsheet analysis
./st profile --model model.gguf --output csv --out layers.csv
```
Algorithm:
1. **Calibration**: Run representative inputs through the model
2. **Activation Collection**: Capture post-residual activations per layer
3. **Perturbation Analysis**: Simulate quantization at multiple bit-widths (2, 3, 4, 8...)
4. **Sensitivity Metrics**: Cosine similarity, KL-divergence, Signal-to-Noise Ratio
5. **Composite Ranking**: Aggregate metrics into per-layer sensitivity score
6. **Pareto Frontier**: Greedy assignment finds optimal accuracy-vs-bits tradeoffs
Output:
* **Human-readable table**: Top-K most sensitive layers with slopes
* **JSON**: Full per-layer, per-bit-width metrics for downstream tools
* **CSV**: Spreadsheet-ready layer rankings with all metrics
Enables mixed-precision quantization: keep sensitive layers at higher precision, aggressively compress robust layers.

## Related Work

- **[BitNet 1.58](https://arxiv.org/abs/2402.17764)**: Original ternary transformer
- **[Falcon3-I2_S](https://huggingface.co/tiiuae/Falcon3-3B-Instruct-GGUF)**: Reference GGUF I2_S model
- **[enthea](https://github.com/peterlodri-sec/enthea)**: Sister crate for quantized GEMM/attention
- **[FELIX Assembly](https://github.com/peterlodri-sec/felix-assembly)**: CPU-agnostic SIMD ISA

## License

MIT OR Apache-2.0

---

Part of the [**eightb**](https://huggingface.co/eightb) ecosystem.