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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: mit
|
| 5 |
+
library_name: transformers
|
| 6 |
+
tags:
|
| 7 |
+
- reranking
|
| 8 |
+
- information-retrieval
|
| 9 |
+
- pointwise
|
| 10 |
+
- ranknet
|
| 11 |
+
- efficient
|
| 12 |
+
- llama
|
| 13 |
+
base_model: meta-llama/Llama-3.2-3B
|
| 14 |
+
datasets:
|
| 15 |
+
- Tevatron/msmarco-passage
|
| 16 |
+
- abdoelsayed/DeAR-COT
|
| 17 |
+
pipeline_tag: text-classification
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# DeAR-3B-Reranker-RankNet-v1
|
| 21 |
+
|
| 22 |
+
## Model Description
|
| 23 |
+
|
| 24 |
+
**DeAR-3B-Reranker-RankNet-v1** is a 3B parameter efficient neural reranker trained with RankNet loss and knowledge distillation. This model offers the best speed-performance tradeoff in the DeAR family, achieving competitive results with significantly faster inference than larger models.
|
| 25 |
+
|
| 26 |
+
## Model Details
|
| 27 |
+
|
| 28 |
+
- **Model Type:** Pointwise Reranker (Sequence Classification)
|
| 29 |
+
- **Base Model:** LLaMA-3.2-3B
|
| 30 |
+
- **Parameters:** 3 billion
|
| 31 |
+
- **Training Method:** Knowledge Distillation + RankNet Loss
|
| 32 |
+
- **Teacher Model:** [LLaMA2-13B-RankLLaMA](https://huggingface.co/abdoelsayed/llama2-13b-rankllama-teacher)
|
| 33 |
+
- **Training Data:** MS MARCO + DeAR-COT
|
| 34 |
+
- **Precision:** BFloat16
|
| 35 |
+
|
| 36 |
+
## Key Features
|
| 37 |
+
|
| 38 |
+
β
**Ultra Fast:** 1.5s inference (1.5x faster than 8B models)
|
| 39 |
+
β
**Efficient:** Runs on single 16GB GPU
|
| 40 |
+
β
**Strong Performance:** Competitive with larger models
|
| 41 |
+
β
**Low Latency:** Ideal for production deployments
|
| 42 |
+
β
**Small Footprint:** Only 6GB model size
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
**Speed-Performance Tradeoff:** 95% accuracy at 1.5x speed!
|
| 46 |
+
|
| 47 |
+
## Usage
|
| 48 |
+
|
| 49 |
+
### Quick Start
|
| 50 |
+
|
| 51 |
+
```python
|
| 52 |
+
import torch
|
| 53 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 54 |
+
|
| 55 |
+
# Load model
|
| 56 |
+
model_path = "abdoelsayed/dear-3b-reranker-ranknet-v1"
|
| 57 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 58 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 59 |
+
model_path,
|
| 60 |
+
torch_dtype=torch.bfloat16
|
| 61 |
+
)
|
| 62 |
+
model.eval().cuda()
|
| 63 |
+
|
| 64 |
+
# Score a query-document pair
|
| 65 |
+
query = "What is machine learning?"
|
| 66 |
+
document = "Machine learning is a subset of artificial intelligence..."
|
| 67 |
+
|
| 68 |
+
inputs = tokenizer(
|
| 69 |
+
f"query: {query}",
|
| 70 |
+
f"document: {document}",
|
| 71 |
+
return_tensors="pt",
|
| 72 |
+
truncation=True,
|
| 73 |
+
max_length=228,
|
| 74 |
+
padding="max_length"
|
| 75 |
+
)
|
| 76 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 77 |
+
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
score = model(**inputs).logits.squeeze().item()
|
| 80 |
+
|
| 81 |
+
print(f"Relevance score: {score}")
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
### Batch Reranking
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
from typing import List, Tuple
|
| 88 |
+
|
| 89 |
+
@torch.inference_mode()
|
| 90 |
+
def rerank(tokenizer, model, query: str, docs: List[Tuple[str, str]], batch_size: int = 64):
|
| 91 |
+
"""
|
| 92 |
+
Rerank documents for a query.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
docs: List of (title, text) tuples
|
| 96 |
+
|
| 97 |
+
Returns:
|
| 98 |
+
List of (index, score) sorted by relevance
|
| 99 |
+
"""
|
| 100 |
+
device = next(model.parameters()).device
|
| 101 |
+
scores = []
|
| 102 |
+
|
| 103 |
+
for i in range(0, len(docs), batch_size):
|
| 104 |
+
batch = docs[i:i + batch_size]
|
| 105 |
+
queries = [f"query: {query}"] * len(batch)
|
| 106 |
+
documents = [f"document: {title} {text}" for title, text in batch]
|
| 107 |
+
|
| 108 |
+
inputs = tokenizer(
|
| 109 |
+
queries,
|
| 110 |
+
documents,
|
| 111 |
+
return_tensors="pt",
|
| 112 |
+
truncation=True,
|
| 113 |
+
max_length=228,
|
| 114 |
+
padding=True
|
| 115 |
+
)
|
| 116 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 117 |
+
|
| 118 |
+
logits = model(**inputs).logits.squeeze(-1)
|
| 119 |
+
scores.extend(logits.cpu().tolist())
|
| 120 |
+
|
| 121 |
+
return sorted(enumerate(scores), key=lambda x: x[1], reverse=True)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# Example
|
| 125 |
+
query = "When did Thomas Edison invent the light bulb?"
|
| 126 |
+
docs = [
|
| 127 |
+
("", "Lightning strike at Seoul National University"),
|
| 128 |
+
("", "Thomas Edison tried to invent a device for car but failed"),
|
| 129 |
+
("", "Coffee is good for diet"),
|
| 130 |
+
("", "KEPCO fixes light problems"),
|
| 131 |
+
("", "Thomas Edison invented the light bulb in 1879"),
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
ranking = rerank(tokenizer, model, query, docs)
|
| 135 |
+
print(ranking)
|
| 136 |
+
# DeAR-P-3B-RL Output:
|
| 137 |
+
# [(4, -1.3046875), (1, -5.125), (3, -6.3125), (0, -6.4375), (2, -6.96875)]
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
## Training Details
|
| 141 |
+
|
| 142 |
+
### Training Configuration
|
| 143 |
+
```python
|
| 144 |
+
{
|
| 145 |
+
"base_model": "meta-llama/Llama-3.2-3B",
|
| 146 |
+
"teacher_model": "abdoelsayed/llama2-13b-rankllama-teacher",
|
| 147 |
+
"loss": "RankNet",
|
| 148 |
+
"distillation": {
|
| 149 |
+
"temperature": 2.0,
|
| 150 |
+
"alpha": 0.1
|
| 151 |
+
},
|
| 152 |
+
"learning_rate": 1e-4,
|
| 153 |
+
"batch_size": 4,
|
| 154 |
+
"gradient_accumulation": 2,
|
| 155 |
+
"epochs": 2,
|
| 156 |
+
"max_length": 228,
|
| 157 |
+
"bf16": true
|
| 158 |
+
}
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
### Hardware
|
| 162 |
+
- **GPUs:** 4x NVIDIA A100 (40GB)
|
| 163 |
+
- **Training Time:** ~18 hours (2x faster than 8B)
|
| 164 |
+
- **Memory Usage:** ~24GB per GPU
|
| 165 |
+
- **Framework:** DeepSpeed ZeRO Stage 2
|
| 166 |
+
|
| 167 |
+
### Loss Function
|
| 168 |
+
|
| 169 |
+
**RankNet Loss** with Knowledge Distillation:
|
| 170 |
+
```
|
| 171 |
+
L_total = (1 - Ξ±) * L_RankNet + Ξ± * L_KD
|
| 172 |
+
where Ξ± = 0.1, temperature = 2.0
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
## Evaluation Results
|
| 176 |
+
|
| 177 |
+
### TREC Deep Learning
|
| 178 |
+
|
| 179 |
+
| Dataset | NDCG@10 | NDCG@20 | MRR@10 | MAP |
|
| 180 |
+
|---------|---------|---------|--------|-----|
|
| 181 |
+
| DL19 | 71.2 | 67.8 | 84.5 | 42.1 |
|
| 182 |
+
| DL20 | 69.4 | 66.2 | 82.3 | 40.5 |
|
| 183 |
+
|
| 184 |
+
### BEIR Benchmark
|
| 185 |
+
|
| 186 |
+
| Dataset | NDCG@10 |
|
| 187 |
+
|---------|---------|
|
| 188 |
+
| MS MARCO | 65.8 |
|
| 189 |
+
| NQ | 49.2 |
|
| 190 |
+
| HotpotQA | 58.4 |
|
| 191 |
+
| FiQA | 44.1 |
|
| 192 |
+
| ArguAna | 56.2 |
|
| 193 |
+
| SciFact | 70.8 |
|
| 194 |
+
| TREC-COVID | 82.3 |
|
| 195 |
+
| NFCorpus | 37.6 |
|
| 196 |
+
| **Average** | **42.1** |
|
| 197 |
+
|
| 198 |
+
### Efficiency Metrics
|
| 199 |
+
|
| 200 |
+
| Metric | Value |
|
| 201 |
+
|--------|-------|
|
| 202 |
+
| Inference Time (100 docs) | 1.5s |
|
| 203 |
+
| Throughput | ~67 docs/sec |
|
| 204 |
+
| GPU Memory (inference) | 12GB |
|
| 205 |
+
| Model Size (BF16) | 6GB |
|
| 206 |
+
|
| 207 |
+
## Comparison
|
| 208 |
+
|
| 209 |
+
### vs. Larger Models
|
| 210 |
+
|
| 211 |
+
| Model | Size | DL19 | DL20 | BEIR | Speed (s) |
|
| 212 |
+
|-------|------|------|------|------|-----------|
|
| 213 |
+
| **DeAR-3B-RL** | 3B | 71.2 | 69.4 | 42.1 | **1.5** |
|
| 214 |
+
| DeAR-8B-RL | 8B | 74.5 | 72.8 | 45.2 | 2.2 |
|
| 215 |
+
| Teacher-13B | 13B | 73.8 | 71.2 | 44.8 | 5.8 |
|
| 216 |
+
| MonoT5-3B | 3B | 71.8 | 68.9 | 43.5 | 3.5 |
|
| 217 |
+
|
| 218 |
+
**Key Insight:** Similar accuracy to MonoT5-3B with 2.3x faster inference!
|
| 219 |
+
|
| 220 |
+
### Speed-Accuracy Tradeoff
|
| 221 |
+
|
| 222 |
+
```
|
| 223 |
+
Accuracy: 95% of 8B model performance
|
| 224 |
+
Speed: 1.5x faster
|
| 225 |
+
Memory: 50% less GPU memory
|
| 226 |
+
Size: 38% smaller on disk
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
## Model Architecture
|
| 230 |
+
|
| 231 |
+
```
|
| 232 |
+
Input: "query: [Q] [SEP] document: [D]"
|
| 233 |
+
β
|
| 234 |
+
LLaMA-3.2-3B Encoder (24 layers)
|
| 235 |
+
β
|
| 236 |
+
[CLS] Token Representation
|
| 237 |
+
β
|
| 238 |
+
Linear Classification Head
|
| 239 |
+
β
|
| 240 |
+
Relevance Score
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
## When to Use This Model
|
| 244 |
+
|
| 245 |
+
**Best for:**
|
| 246 |
+
- β
Production deployments requiring low latency
|
| 247 |
+
- β
Resource-constrained environments
|
| 248 |
+
- β
Large-scale reranking (millions of queries)
|
| 249 |
+
- β
Cost-sensitive applications
|
| 250 |
+
- β
Single GPU inference
|
| 251 |
+
|
| 252 |
+
**Consider 8B models for:**
|
| 253 |
+
- β Maximum accuracy required
|
| 254 |
+
- β Research benchmarks
|
| 255 |
+
- β GPU resources not a constraint
|
| 256 |
+
|
| 257 |
+
## Deployment Recommendations
|
| 258 |
+
|
| 259 |
+
### Production Setup
|
| 260 |
+
|
| 261 |
+
```python
|
| 262 |
+
# Optimize for inference
|
| 263 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 264 |
+
"abdoelsayed/dear-3b-reranker-ranknet-v1",
|
| 265 |
+
torch_dtype=torch.bfloat16,
|
| 266 |
+
device_map="auto"
|
| 267 |
+
)
|
| 268 |
+
model.eval()
|
| 269 |
+
|
| 270 |
+
# Enable torch.compile for 20% speedup (PyTorch 2.0+)
|
| 271 |
+
model = torch.compile(model, mode="reduce-overhead")
|
| 272 |
+
```
|
| 273 |
+
|
| 274 |
+
### Batch Processing
|
| 275 |
+
|
| 276 |
+
For maximum throughput:
|
| 277 |
+
- Use batch size 64-128
|
| 278 |
+
- Enable mixed precision (bf16)
|
| 279 |
+
- Use torch.compile()
|
| 280 |
+
- Consider ONNX export for CPU deployment
|
| 281 |
+
|
| 282 |
+
## Limitations
|
| 283 |
+
|
| 284 |
+
1. **Accuracy:** ~3 NDCG@10 points lower than 8B models
|
| 285 |
+
2. **Complex Queries:** May struggle with nuanced queries
|
| 286 |
+
3. **Document Length:** Same 196 token limit as larger models
|
| 287 |
+
4. **Language:** English only
|
| 288 |
+
|
| 289 |
+
## Fine-tuning
|
| 290 |
+
|
| 291 |
+
```python
|
| 292 |
+
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
|
| 293 |
+
|
| 294 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 295 |
+
"abdoelsayed/dear-3b-reranker-ranknet-v1"
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
training_args = TrainingArguments(
|
| 299 |
+
output_dir="./finetuned-3b",
|
| 300 |
+
learning_rate=5e-6, # Lower for fine-tuning
|
| 301 |
+
per_device_train_batch_size=8,
|
| 302 |
+
num_train_epochs=2,
|
| 303 |
+
bf16=True,
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
trainer = Trainer(
|
| 307 |
+
model=model,
|
| 308 |
+
args=training_args,
|
| 309 |
+
train_dataset=your_dataset,
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
trainer.train()
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
## Related Models
|
| 316 |
+
|
| 317 |
+
**DeAR 3B Family:**
|
| 318 |
+
- [DeAR-3B-CE](https://huggingface.co/abdoelsayed/dear-3b-reranker-ce-v1) - Binary Cross-Entropy variant
|
| 319 |
+
- [DeAR-3B-RankNet-LoRA](https://huggingface.co/abdoelsayed/dear-3b-reranker-ranknet-lora-v1) - LoRA adapter
|
| 320 |
+
|
| 321 |
+
**Larger Models:**
|
| 322 |
+
- [DeAR-8B-RankNet](https://huggingface.co/abdoelsayed/dear-8b-reranker-ranknet-v1) - Better accuracy
|
| 323 |
+
|
| 324 |
+
**Resources:**
|
| 325 |
+
- [Teacher Model](https://huggingface.co/abdoelsayed/llama2-13b-rankllama-teacher)
|
| 326 |
+
- [DeAR-COT Dataset](https://huggingface.co/datasets/abdoelsayed/DeAR-COT)
|
| 327 |
+
|
| 328 |
+
## Citation
|
| 329 |
+
|
| 330 |
+
```bibtex
|
| 331 |
+
@article{abdallah2025dear,
|
| 332 |
+
title={DeAR: Dual-Stage Document Reranking with Reasoning Agents via LLM Distillation},
|
| 333 |
+
author={Abdallah, Abdelrahman and Mozafari, Jamshid and Piryani, Bhawna and Jatowt, Adam},
|
| 334 |
+
journal={arXiv preprint arXiv:2508.16998},
|
| 335 |
+
year={2025}
|
| 336 |
+
}
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
## License
|
| 340 |
+
|
| 341 |
+
MIT License
|
| 342 |
+
|
| 343 |
+
## More Information
|
| 344 |
+
|
| 345 |
+
- **GitHub:** [DataScienceUIBK/DeAR-Reranking](https://github.com/DataScienceUIBK/DeAR-Reranking)
|
| 346 |
+
- **Paper:** [arXiv:2508.16998](https://arxiv.org/abs/2508.16998)
|
| 347 |
+
- **Collection:** [DeAR Models](https://huggingface.co/collections/abdoelsayed/dear-reranking)
|