File size: 3,975 Bytes
d6c063b
 
6c50b77
 
d6c063b
 
 
 
 
 
 
 
 
6c50b77
d6c063b
 
6c50b77
 
 
d6c063b
6c50b77
d6c063b
6c50b77
d6c063b
6c50b77
 
 
 
d6c063b
6c50b77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c063b
6c50b77
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
---
license: apache-2.0
language:
- en
pipeline_tag: text-classification
tags:
- llm-routing
- router
- from-scratch
- small-language-model
- on-device
- privoid
- gguf
- safetensors
---

<p align="center">
  <img src="routing_curve_test.png" width="80%" alt="Privoid Router Evaluation">
</p>

# 🧭 Privoid Router (Privoid-Router-34M)

Privoid-Router-34M is an ultra-lightweight, 34-million parameter routing model **engineered entirely from scratch**. It acts as an intelligent local gatekeeper to orchestrate low-cost local routing workflows. The model dynamically predicts whether an incoming prompt requires strong reasoning (routing to a Frontier Model API like GPT-4) or can be safely and accurately executed locally using a resource-efficient Small Language Model (SLM).

## πŸ“Š Key Highlights & Results
- **Zero-leakage Custom Pipeline**: Developed with a custom BPE Tokenizer (16k vocab) and custom Transformer architecture (RoPE, RMSNorm, SwiGLU) trained from scratch.
- **Unmatched Resource Efficiency**: At just **34M parameters**, it runs comfortably on edge devices and consumer CPUs, requiring 3x fewer parameters than standard BERT-based alternatives.
- **Substantial Cost Savings**: Cuts modeled API operational costs by **49.6%** while recovering **80.7%** of the optimal quality gap.

---

## πŸ“ˆ Evaluation Benchmarks

### 1. Baselines vs. Privoid Router (Held-Out Test Set, n=5000)

| Routing Strategy | Area Under ROC (AUC) | Agreement with Oracle (APGR) | Call-to-Frontier Share (CPT@80%) |
| :--- | :---: | :---: | :---: |
| **Random Routing** | 0.500 | 0.500 | 80.0% |
| πŸ† **Privoid-Router-34M (Ours)** | **0.754** | **0.720** | **47.3%** |
| **Oracle (Perfect Routing)** | 1.000 | 1.000 | ~20.0% |

### 2. Literature Comparison against Established Routers

| Model / Method | Parameters | Area Under ROC (AUC) | APGR Score | Frontier Traffic (CPT@80%) |
| :--- | :---: | :---: | :---: | :---: |
| **TF-IDF + Logistic Regression** | <1M | 0.670 | 0.620 | 61.0% |
| **RouteLLM (Matrix Factorization)** | ~50M | 0.740 | 0.700 | 49.0% |
| 🌟 **Privoid-Router-34M (Ours)** | **34M** | **0.754** | **0.720** | **47.3%** |
| **RouteLLM (BERT-based)** | ~110M | 0.780 | 0.760 | 43.0% |

<p align="center">
  <img src="external_model_comparison.png" width="90%" alt="External Benchmarks">
</p>

---

## πŸ› οΈ Multi-Format Integration & Usage

### 1. Python (PyTorch / Safetensors)
Ensure you have `safetensors`, `tokenizers`, and `torch` installed. Put `privoid_model.py` in your working path:

```python
import json, torch
from safetensors.torch import load_file
from tokenizers import Tokenizer
from privoid_model import PrivoidRouter

cfg = json.load(open('config.json'))
tok = Tokenizer.from_file('tokenizer.json')
model = PrivoidRouter(cfg, dropout=0.0)
model.load_state_dict(load_file('model.safetensors'))
model.eval()

def route(prompt):
    ids = torch.tensor([[2] + tok.encode(prompt, add_special_tokens=False).ids[:cfg['max_len'] - 1]])
    p = torch.sigmoid(model(ids, ids != 0)).item()
    decision = 'frontier' if p >= cfg['threshold'] else 'local'
    return decision, p

decision, score = route("Design a distributed rate limiter for 1M requests/sec.")
print(f"Route to: {decision.upper()} (score: {score:.3f})")
```

### 2. Local Inference via GGUF
Deploy locally with `llama.cpp` using the lightweight `model.gguf` asset:

```bash
./llama-cli -m model.gguf -p "[CLS] Is the earth round?" --temp 0.0
```

---

### 🧬 Training & Architecture
- **Stage 1 (MLM Pre-training)**: Pre-trained with Masked Language Modeling on 300 million tokens of FineWeb-Edu + routing user prompts.
- **Stage 2 (Router Fine-tuning)**: Fully supervised classifier head fine-tuning using quality scores derived from `routellm/gpt4_dataset` (score threshold < 4 indicates frontier delegation required).
- **Optimizer**: AdamW with cosine learning rate decay and early stopping validation guarding.

Built with πŸ’™ by [privoid.ai](https://privoid.ai).