smart-tree / README.md
PeetPedro's picture
Upload folder using huggingface_hub
355d424 verified
|
Raw History Blame Contribute Delete
6.3 kB
metadata
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

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

Or build from source:

git clone https://github.com/peterlodri-sec/smart-tree
cd smart-tree && cargo build --release

Usage

GGUF I2_S Validation

# 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

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

Inspect GGUF Files

# 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

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:

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

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

[dependencies]
smart-tree = { git = "https://github.com/peterlodri-sec/smart-tree" }
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:

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:

# 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

License

MIT OR Apache-2.0


Part of the eightb ecosystem.