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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- prompt-routing
|
| 7 |
+
- difficulty-classifier
|
| 8 |
+
- deberta-v3
|
| 9 |
+
- llm-router
|
| 10 |
+
datasets:
|
| 11 |
+
- RowRed/prompts-24000-en
|
| 12 |
+
base_model:
|
| 13 |
+
- microsoft/deberta-v3-xsmall
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# DifficultyRouter: A Lightweight 3‑Tier Prompt Difficulty Router
|
| 17 |
+
|
| 18 |
+
DifficultyRouter is a successor to [ComplexityRouter](https://huggingface.co/RowRed/ComplexityRouter), specifically designed for **cost optimization** in LLM routing. It is finetuned from **microsoft/deberta-v3-xsmall** (≈22M params, ~8x smaller than the base model used previously) on a 20,000‑prompt dataset (2,000 L0 + 6,000 L1 → Tier 0; 6,000 L2 → Tier 1; 6,000 L3 → Tier 2).
|
| 19 |
+
|
| 20 |
+
It classifies prompts into **3 difficulty tiers** with a single classification head.
|
| 21 |
+
|
| 22 |
+
## Model Details
|
| 23 |
+
|
| 24 |
+
### Model Description
|
| 25 |
+
|
| 26 |
+
- **Model type:** Text Classification (3‑tier, single head)
|
| 27 |
+
- **Language:** English
|
| 28 |
+
- **Backbone:** microsoft/deberta-v3-xsmall
|
| 29 |
+
- **License:** Apache‑2.0
|
| 30 |
+
- **Finetuned from model:** microsoft/deberta-v3-xsmall
|
| 31 |
+
- **Training data:** `RowRed/prompts-24000-en` (L0 downsampled to 2,000, L1/L2/L3 kept fully)
|
| 32 |
+
|
| 33 |
+
### Model Sources
|
| 34 |
+
|
| 35 |
+
- **Dataset repository:** `https://huggingface.co/datasets/RowRed/prompts-24000-en`
|
| 36 |
+
- **Old model:** `RowRed/ComplexityRouter`
|
| 37 |
+
|
| 38 |
+
## Uses
|
| 39 |
+
|
| 40 |
+
### Direct Use
|
| 41 |
+
|
| 42 |
+
Route prompts to appropriate LLM tiers based on predicted difficulty:
|
| 43 |
+
|
| 44 |
+
| Tier | Meaning | Original Levels | Suggested LLM Tier |
|
| 45 |
+
|------|---------|-----------------|--------------------|
|
| 46 |
+
| 0 (Easy) | Simple lookups, basic Q&A, light reasoning | L0 + L1 | Fast/cheap model |
|
| 47 |
+
| 1 (Moderate) | Complex reasoning, deep domain knowledge | L2 | Standard model |
|
| 48 |
+
| 2 (Complex) | Very complex reasoning, niche expertise, edge cases | L3 | Frontier model |
|
| 49 |
+
|
| 50 |
+
### Out‑of‑Scope Use
|
| 51 |
+
|
| 52 |
+
- Multi‑turn conversation routing (single prompts only).
|
| 53 |
+
- Non‑English prompts (training data is English‑only).
|
| 54 |
+
- Prompts requiring image or multimodal understanding.
|
| 55 |
+
- 4‑level classification (use the old ComplexityRouter for 4 classes).
|
| 56 |
+
|
| 57 |
+
## Bias, Risks, and Limitations
|
| 58 |
+
|
| 59 |
+
- Training data includes synthetic augmentation; distribution may not match all real‑world prompt patterns.
|
| 60 |
+
- Tier 0 (merged L0+L1) has inherent ambiguity—some "trivial" and "simple" prompts are hard to distinguish from "moderate".
|
| 61 |
+
- DeBERTa‑v3‑xsmall has a smaller representation capacity than the base model, so it may miss very subtle difficulty cues in niche technical domains.
|
| 62 |
+
|
| 63 |
+
### ⚠️ Not Production‑Ready
|
| 64 |
+
This model is a research prototype.
|
| 65 |
+
|
| 66 |
+
- Accuracy is ~63.4%, meaning ~4 out of 10 prompts will be misrouted.
|
| 67 |
+
- Adjacent accuracy of 87.1% means 1 in 8 prompts will be sent to a tier that is still off by one level, leading to noticeable latency/cost misses.
|
| 68 |
+
- The model has not been stress‑tested on real‑world, messy, multi‑domain prompts. It was trained on synthetic augmentations.
|
| 69 |
+
|
| 70 |
+
## Training Details
|
| 71 |
+
|
| 72 |
+
### Training Data
|
| 73 |
+
|
| 74 |
+
- **Source:** `RowRed/prompts-24000-en` (approximately 24,000 raw prompts)
|
| 75 |
+
- **Used:** 20,000 prompts (L0 downsampled to 2,000; L1, L2, L3 kept at 6,000 each)
|
| 76 |
+
- **Preprocessing:** Original difficulty levels (0–3) are mapped to 3 tiers: `0+1 -> 0`, `2 -> 1`, `3 -> 2`.
|
| 77 |
+
- **L0 downsampling:** To combat overfitting, exactly `2,000` L0 samples were randomly sampled (config flag `l0_sample_size=2000`, locked).
|
| 78 |
+
- **Split:** 70% train / 18% validation / 12% held-out test (stratified).
|
| 79 |
+
|
| 80 |
+
### Training Procedure
|
| 81 |
+
|
| 82 |
+
- **Hardware:** NVIDIA T4 (16 GB VRAM, Google Colab)
|
| 83 |
+
- **Framework:** PyTorch + Hugging Face Transformers
|
| 84 |
+
- **Optimizer:** AdamW (lr=3e-5, weight_decay=0.1)
|
| 85 |
+
- **Scheduler:** Linear warmup (6% steps) → linear decay
|
| 86 |
+
- **Loss:** Weighted Cross‑Entropy with label smoothing=0.1
|
| 87 |
+
- **Batch size:** 16 (effective 32 with gradient accumulation)
|
| 88 |
+
- **Max sequence length:** 256 tokens
|
| 89 |
+
- **Epochs:** 12 max (Early stopping patience = 4 on F1-macro; stopped at epoch 7, best checkpoint at epoch 3)
|
| 90 |
+
- **Class balancing:** WeightedRandomSampler only; class weights in loss removed to avoid double-counting
|
| 91 |
+
- **Head:** 256-dim, dropout=0.1 (simpler head for less overfit)
|
| 92 |
+
- **Precision:** FP16 mixed precision
|
| 93 |
+
|
| 94 |
+
## Evaluation Results
|
| 95 |
+
|
| 96 |
+
Reported on the **held‑out test set** (~12% of 20k prompts) using the **best model checkpoint** (epoch 3, selected via validation F1-macro):
|
| 97 |
+
|
| 98 |
+
| Metric | Value |
|
| 99 |
+
|---------------------|--------|
|
| 100 |
+
| Exact Match Accuracy | 63.36% |
|
| 101 |
+
| Adjacent (±1) Accuracy | 87.14% |
|
| 102 |
+
| F1 Macro | 0.6375 |
|
| 103 |
+
| F1 Weighted | 0.6314 |
|
| 104 |
+
|
| 105 |
+
Training loss continued to decrease but validation metrics peaked at epoch 3; early stopping correctly caught the onset of overfitting.
|
| 106 |
+
|
| 107 |
+
## How to Get Started with the Model
|
| 108 |
+
|
| 109 |
+
The model uses a single-head architecture and saves via **safetensors** (load with `strict=True`). Use the same class as in training:
|
| 110 |
+
|
| 111 |
+
```python
|
| 112 |
+
from transformers import AutoTokenizer, AutoModel
|
| 113 |
+
import torch
|
| 114 |
+
import torch.nn as nn
|
| 115 |
+
|
| 116 |
+
class DifficultyRouter(nn.Module):
|
| 117 |
+
def __init__(self, model_name="microsoft/deberta-v3-xsmall", num_labels=3):
|
| 118 |
+
super().__init__()
|
| 119 |
+
self.backbone = AutoModel.from_pretrained(model_name)
|
| 120 |
+
hidden_size = self.backbone.config.hidden_size
|
| 121 |
+
self.classifier = nn.Sequential(
|
| 122 |
+
nn.Dropout(0.1), nn.Linear(hidden_size, 256), nn.GELU(),
|
| 123 |
+
nn.Dropout(0.1), nn.Linear(256, num_labels)
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
def forward(self, input_ids, attention_mask):
|
| 127 |
+
outputs = self.backbone(input_ids=input_ids, attention_mask=attention_mask)
|
| 128 |
+
cls_out = outputs.last_hidden_state[:, 0, :].to(torch.float32)
|
| 129 |
+
logits = self.classifier(cls_out)
|
| 130 |
+
return logits
|
| 131 |
+
|
| 132 |
+
# Load (safe, no pickle)
|
| 133 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 134 |
+
tokenizer = AutoTokenizer.from_pretrained("RowRed/DifficultyRouter")
|
| 135 |
+
model = DifficultyRouter()
|
| 136 |
+
model.load_state_dict(torch.load("model.safetensors", map_location=device), strict=True)
|
| 137 |
+
model.to(device).eval()
|
| 138 |
+
|
| 139 |
+
# Predict
|
| 140 |
+
prompts = ["What is 2+2?", "Explain quantum entanglement in detail."]
|
| 141 |
+
encoded = tokenizer(prompts, padding=True, truncation=True, max_length=256, return_tensors="pt").to(device)
|
| 142 |
+
with torch.no_grad():
|
| 143 |
+
logits = model(encoded["input_ids"], encoded["attention_mask"])
|
| 144 |
+
probs = torch.softmax(logits, dim=-1)
|
| 145 |
+
tiers = torch.argmax(probs, dim=-1)
|
| 146 |
+
|
| 147 |
+
for prompt, tier in zip(prompts, tiers):
|
| 148 |
+
print(f"Tier {tier.item()}: {prompt}")
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
## Citation
|
| 152 |
+
If you use this model, please cite:
|
| 153 |
+
|
| 154 |
+
```bibtex
|
| 155 |
+
@software{DifficultyRouter,
|
| 156 |
+
author = {RowRed},
|
| 157 |
+
title = {DifficultyRouter},
|
| 158 |
+
year = {2026},
|
| 159 |
+
url = {https://huggingface.co/RowRed/DifficultyRouter}
|
| 160 |
+
}
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
Additionally, acknowledge the base model:
|
| 164 |
+
|
| 165 |
+
```bibtex
|
| 166 |
+
@misc{he2021debertav3,
|
| 167 |
+
title={DeBERTaV3: Improving DeBERTa using ELECTRA-Style Pre-Training with Gradient-Disentangled Embedding Sharing},
|
| 168 |
+
author={Pengcheng He and Jianfeng Gao and Weizhu Chen},
|
| 169 |
+
year={2021},
|
| 170 |
+
eprint={2111.09543},
|
| 171 |
+
archivePrefix={arXiv},
|
| 172 |
+
primaryClass={cs.CL}
|
| 173 |
+
}
|
| 174 |
+
```
|
| 175 |
+
```bibtex
|
| 176 |
+
@inproceedings{
|
| 177 |
+
he2021deberta,
|
| 178 |
+
title={DEBERTA: DECODING-ENHANCED BERT WITH DISENTANGLED ATTENTION},
|
| 179 |
+
author={Pengcheng He and Xiaodong Liu and Jianfeng Gao and Weizhu Chen},
|
| 180 |
+
booktitle={International Conference on Learning Representations},
|
| 181 |
+
year={2021},
|
| 182 |
+
url={https://openreview.net/forum?id=XPZIaotutsD}
|
| 183 |
+
}
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
## License
|
| 187 |
+
This model is released under Apache‑2.0.
|
| 188 |
+
The backbone (microsoft/deberta-v3-xsmall) is MIT‑licensed.
|