Text Ranking
Transformers
Safetensors
Greek
English
llama_bidirec
text-classification
greek
english
reranker
cross-encoder
rag
nemotron
custom_code
Eval Results (legacy)
text-embeddings-inference
Instructions to use KIEFERSA/Sophea-Nemo-Reranker with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use KIEFERSA/Sophea-Nemo-Reranker with Transformers:
# Load model directly from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained("KIEFERSA/Sophea-Nemo-Reranker", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
c58d757
0
Parent(s):
Initial release
Browse files- .gitattributes +36 -0
- README.md +187 -0
- base_vs_ft.png +0 -0
- config.json +48 -0
- llama_bidirectional_model.py +362 -0
- model.safetensors +3 -0
- stack.png +0 -0
- tokenizer.json +3 -0
- tokenizer_config.json +15 -0
.gitattributes
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- el
|
| 4 |
+
- en
|
| 5 |
+
license: other
|
| 6 |
+
license_name: nvidia-open-model-license
|
| 7 |
+
base_model: nvidia/llama-nemotron-rerank-1b-v2
|
| 8 |
+
pipeline_tag: text-ranking
|
| 9 |
+
library_name: transformers
|
| 10 |
+
tags:
|
| 11 |
+
- greek
|
| 12 |
+
- english
|
| 13 |
+
- reranker
|
| 14 |
+
- cross-encoder
|
| 15 |
+
- rag
|
| 16 |
+
- nemotron
|
| 17 |
+
model-index:
|
| 18 |
+
- name: Sophea-Nemo-Reranker
|
| 19 |
+
results:
|
| 20 |
+
- task:
|
| 21 |
+
type: text-ranking
|
| 22 |
+
name: Greek+English reranking (5 domains, rerank top-50)
|
| 23 |
+
dataset:
|
| 24 |
+
type: retrieval
|
| 25 |
+
name: Internal Greek+English retrieval benchmark
|
| 26 |
+
metrics:
|
| 27 |
+
- type: nDCG@10
|
| 28 |
+
name: nDCG@10 (mean of 5 domains)
|
| 29 |
+
value: 0.8172
|
| 30 |
+
- type: recall@10
|
| 31 |
+
name: Recall@10 (mean of 5 domains)
|
| 32 |
+
value: 0.9267
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
<div style="display:flex;align-items:center;gap:18px;padding:16px 20px;margin-bottom:16px;border-radius:14px;background:linear-gradient(100deg,#e8f5ec 0%,#f7fbf8 62%);border:1px solid #c6e7d1"><img alt="KIEFERSA" style="flex:0 0 auto;height:58px;width:58px;border-radius:12px;object-fit:contain" src="data:image/webp;base64,UklGRgQNAABXRUJQVlA4WAoAAAAQAAAAxwAAxwAAQUxQSIsKAAABsMf/nyE52q+qZjd2nluEh00OsY2zbdsOH51t27Zt2744F1vrrqrf69X1m+mumq4K/ooISG4bSZKUmIIOeag1w1Ud8wFwPDgH2Om9zYFD2AcX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height:1.2">Sophea-Nemo-Reranker</div><div style="font-size:.86rem;color:#3a5a47;margin-top:2px">Greek + English cross-encoder reranker — fine-tuned NVIDIA Llama-Nemotron-Rerank-1B-v2</div></div></div>
|
| 36 |
+
|
| 37 |
+
**Sophea-Nemo-Reranker** is a Greek + English **cross-encoder reranker**, fine-tuned from
|
| 38 |
+
**[NVIDIA llama-nemotron-rerank-1b-v2](https://huggingface.co/nvidia/llama-nemotron-rerank-1b-v2)** for
|
| 39 |
+
Greek and English reranking. It is the second (precision) stage of a
|
| 40 |
+
two-stage Greek RAG stack; the matching first stage is
|
| 41 |
+
**[Sophea-Nemo-Embedding](https://huggingface.co/KIEFERSA/Sophea-Nemo-Embedding)**.
|
| 42 |
+
|
| 43 |
+
- **Creator:** Kiefer SA
|
| 44 |
+
- **Base model:** `nvidia/llama-nemotron-rerank-1b-v2` (`LlamaBidirectionalForSequenceClassification`,
|
| 45 |
+
**bidirectional**, single relevance logit; `trust_remote_code`)
|
| 46 |
+
- **Languages:** Greek + English
|
| 47 |
+
- **Input template:** a single sequence — `question:{q} \n \n passage:{p}`
|
| 48 |
+
- **Training:** BCE on labeled (query, doc) pairs, 1 epoch, max-len 4096, bf16
|
| 49 |
+
|
| 50 |
+
> **Greek is out-of-distribution for the base.** The base reranker lists 26 languages and **Greek is
|
| 51 |
+
> not one of them** — this fine-tune adapts its trained relevance head to Greek.
|
| 52 |
+
|
| 53 |
+
## Why it matters — base vs fine-tuned
|
| 54 |
+
|
| 55 |
+
Reranking the fixed top-50 candidates over 5 domains (nDCG@10), the fine-tune improves on the base
|
| 56 |
+
Nemotron reranker **on every domain**:
|
| 57 |
+
|
| 58 |
+

|
| 59 |
+
|
| 60 |
+
<div style="overflow-x:auto">
|
| 61 |
+
<table style="width:100%;border-collapse:collapse;font-size:13px;font-variant-numeric:tabular-nums;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif">
|
| 62 |
+
<thead><tr>
|
| 63 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:left;">metric</th>
|
| 64 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Val</th>
|
| 65 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Energy</th>
|
| 66 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Legal</th>
|
| 67 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Finance</th>
|
| 68 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Medical</th>
|
| 69 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Mean</th>
|
| 70 |
+
</tr></thead><tbody>
|
| 71 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">nDCG@10 — base</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.793</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.741</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.966</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.702</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.651</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.770</td></tr>
|
| 72 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">nDCG@10 — fine-tuned</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.874</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.780</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.969</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.754</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.709</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.817</td></tr>
|
| 73 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Δ nDCG@10</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.081</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.039</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.004</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.052</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.058</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.047</td></tr>
|
| 74 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Recall@10 — base</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.953</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.847</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.993</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.853</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.773</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.884</td></tr>
|
| 75 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">Recall@10 — fine-tuned</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.993</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.873</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.987</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.907</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.873</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.927</td></tr>
|
| 76 |
+
</tbody></table></div>
|
| 77 |
+
|
| 78 |
+
**Read.** Mean **nDCG@10 0.770 → 0.817
|
| 79 |
+
(+0.047)**, better on all five domains, with the
|
| 80 |
+
largest gains on the hardest ones (medical +0.06, finance +0.05, val +0.08). In the two-stage stack it
|
| 81 |
+
is also the strongest reranker we measured — edging a fine-tuned Qwen3-0.6B cross-encoder and beating
|
| 82 |
+
the base by a clear margin.
|
| 83 |
+
|
| 84 |
+
## Size comparison — vs base Qwen3-Reranker (off-the-shelf)
|
| 85 |
+
|
| 86 |
+
Reranking the same fixed top-50 candidates, same protocol (150 queries/domain), nDCG@10 — your
|
| 87 |
+
**fine-tuned 1B** reranker **edges the base Qwen3-Reranker-8B** and clearly beats 4B / 0.6B, at a
|
| 88 |
+
fraction of the size:
|
| 89 |
+
|
| 90 |
+
<div style="overflow-x:auto">
|
| 91 |
+
<table style="width:100%;border-collapse:collapse;font-size:13px;font-variant-numeric:tabular-nums;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif">
|
| 92 |
+
<thead><tr>
|
| 93 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:left;">model</th>
|
| 94 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Val</th>
|
| 95 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Energy</th>
|
| 96 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Legal</th>
|
| 97 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Finance</th>
|
| 98 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Medical</th>
|
| 99 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Mean</th>
|
| 100 |
+
</tr></thead><tbody>
|
| 101 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">Sophea-Nemo-Reranker (ours · 1B · fine-tuned)</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.874</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.780</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.969</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.754</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.709</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.817</td></tr>
|
| 102 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Qwen3-Reranker-8B (base, off-the-shelf)</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.858</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.796</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.975</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.763</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.672</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.813</td></tr>
|
| 103 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Qwen3-Reranker-4B (base, off-the-shelf)</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.865</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.782</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.966</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.735</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.647</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.799</td></tr>
|
| 104 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Qwen3-Reranker-0.6B (base, off-the-shelf)</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.812</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.746</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.960</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.692</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.634</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.769</td></tr>
|
| 105 |
+
</tbody></table></div>
|
| 106 |
+
|
| 107 |
+
**Read.** Mean nDCG@10 **0.817** (ours, 1B) vs **0.813**
|
| 108 |
+
(Qwen3-Reranker-8B), **0.799** (4B), **0.769** (0.6B) — and
|
| 109 |
+
ours is strongest on the hardest domain (medical 0.709 vs 8B's 0.672).
|
| 110 |
+
A small, Greek-tuned model beats a general reranker 8× its size.
|
| 111 |
+
|
| 112 |
+
## End-to-end two-stage pipeline
|
| 113 |
+
|
| 114 |
+
The two models are designed to run **together**: the embedder retrieves the top-50 candidates, the
|
| 115 |
+
reranker re-orders them. Measured end-to-end on the same 5-domain Greek+English eval (150 queries/set,
|
| 116 |
+
correct `query:`/`passage:` prompts):
|
| 117 |
+
|
| 118 |
+

|
| 119 |
+
|
| 120 |
+
<div style="overflow-x:auto">
|
| 121 |
+
<table style="width:100%;border-collapse:collapse;font-size:13px;font-variant-numeric:tabular-nums;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif">
|
| 122 |
+
<thead><tr>
|
| 123 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:left;">metric</th>
|
| 124 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Val</th>
|
| 125 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Energy</th>
|
| 126 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Legal</th>
|
| 127 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Finance</th>
|
| 128 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Medical</th>
|
| 129 |
+
<th style="padding:10px 8px;font-weight:500;border-bottom:2px solid #0a8043;color:#0a8043;font-size:14px;text-align:center;">Mean</th>
|
| 130 |
+
</tr></thead><tbody>
|
| 131 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">nDCG@10 — embedder alone</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.863</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.802</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.939</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.702</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.689</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">0.799</td></tr>
|
| 132 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">nDCG@10 — + reranker (stack)</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.867</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.780</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.976</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.763</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.703</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.818</td></tr>
|
| 133 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);">Δ from reranking</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.004</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">-0.022</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.037</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.061</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.013</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);">+0.019</td></tr>
|
| 134 |
+
<tr><td style="padding:7px 8px;padding-left:20px;text-align:left;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">Recall@10 — full stack</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.987</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.873</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.993</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.920</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.860</td><td style="padding:7px 8px;text-align:center;border-bottom:1px solid rgba(128,128,128,.15);font-weight:700;color:#0a8043;">0.927</td></tr>
|
| 135 |
+
</tbody></table></div>
|
| 136 |
+
|
| 137 |
+
**Read.** The full stack reaches **mean nDCG@10 0.818** (Recall@10 0.927) — the
|
| 138 |
+
strongest configuration measured. The reranker adds the most where the embedder is weakest
|
| 139 |
+
(**finance +0.061, legal +0.037**), i.e. it sharpens precision on the hard
|
| 140 |
+
cases. On **energy** it slightly regresses (-0.022) and on `val` the gain is marginal,
|
| 141 |
+
because the embedder alone is already very strong there and leaves little headroom — for the easiest,
|
| 142 |
+
already-high-recall domains you can skip the rerank stage if latency matters.
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
## Usage (transformers)
|
| 146 |
+
|
| 147 |
+
```python
|
| 148 |
+
import torch
|
| 149 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 150 |
+
|
| 151 |
+
name = "KIEFERSA/Sophea-Nemo-Reranker"
|
| 152 |
+
tok = AutoTokenizer.from_pretrained(name, trust_remote_code=True)
|
| 153 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 154 |
+
name, num_labels=1, dtype=torch.bfloat16, trust_remote_code=True).eval()
|
| 155 |
+
|
| 156 |
+
def score(query, passage):
|
| 157 |
+
text = f"question:{query} \n \n passage:{passage}"
|
| 158 |
+
enc = tok(text, return_tensors="pt", truncation=True, max_length=4096)
|
| 159 |
+
with torch.no_grad():
|
| 160 |
+
return model(**enc).logits.squeeze(-1).item() # higher = more relevant
|
| 161 |
+
|
| 162 |
+
# rank a candidate list by descending score
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
Feed the base's own `question:/passage:` template — it has a **trained** relevance head, so matching
|
| 166 |
+
its template adapts that head rather than fighting it. Needs `trust_remote_code=True`.
|
| 167 |
+
|
| 168 |
+
## Serving with vLLM
|
| 169 |
+
|
| 170 |
+
```bash
|
| 171 |
+
vllm serve KIEFERSA/Sophea-Nemo-Reranker --runner pooling --trust-remote-code
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
Exposes `/rerank`, `/v1/rerank` (Jina), `/v2/rerank` (Cohere) and `/score`:
|
| 175 |
+
|
| 176 |
+
```bash
|
| 177 |
+
curl http://localhost:8000/rerank -H 'Content-Type: application/json' -d '{
|
| 178 |
+
"model": "KIEFERSA/Sophea-Nemo-Reranker",
|
| 179 |
+
"query": "Ποια είναι η πρωτεύουσα της Ελλάδας;",
|
| 180 |
+
"documents": ["Η Αθήνα είναι η πρωτεύουσα της Ελλάδας.", "Το Παρίσι είναι στη Γαλλία."]
|
| 181 |
+
}'
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
## License
|
| 185 |
+
Fine-tune of `nvidia/llama-nemotron-rerank-1b-v2` — governed by the **NVIDIA Open Model License** and
|
| 186 |
+
the **Llama 3.2 Community License**; commercial use permitted under those terms (note: **not**
|
| 187 |
+
Apache-2.0). Review the base model's license before use.
|
base_vs_ft.png
ADDED
|
config.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaBidirectionalForSequenceClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"auto_map": {
|
| 8 |
+
"AutoConfig": "llama_bidirectional_model.LlamaBidirectionalConfig",
|
| 9 |
+
"AutoModelForSequenceClassification": "llama_bidirectional_model.LlamaBidirectionalForSequenceClassification"
|
| 10 |
+
},
|
| 11 |
+
"bos_token_id": 128000,
|
| 12 |
+
"dtype": "bfloat16",
|
| 13 |
+
"eos_token_id": 128001,
|
| 14 |
+
"head_dim": 64,
|
| 15 |
+
"hidden_act": "silu",
|
| 16 |
+
"hidden_size": 2048,
|
| 17 |
+
"id2label": {
|
| 18 |
+
"0": "LABEL_0"
|
| 19 |
+
},
|
| 20 |
+
"initializer_range": 0.02,
|
| 21 |
+
"intermediate_size": 8192,
|
| 22 |
+
"label2id": {
|
| 23 |
+
"LABEL_0": 0
|
| 24 |
+
},
|
| 25 |
+
"max_position_embeddings": 131072,
|
| 26 |
+
"mlp_bias": false,
|
| 27 |
+
"model_type": "llama_bidirec",
|
| 28 |
+
"num_attention_heads": 32,
|
| 29 |
+
"num_hidden_layers": 16,
|
| 30 |
+
"num_key_value_heads": 8,
|
| 31 |
+
"pad_token_id": 128001,
|
| 32 |
+
"pooling": "avg",
|
| 33 |
+
"pretraining_tp": 1,
|
| 34 |
+
"rms_norm_eps": 1e-05,
|
| 35 |
+
"rope_parameters": {
|
| 36 |
+
"factor": 32.0,
|
| 37 |
+
"high_freq_factor": 4.0,
|
| 38 |
+
"low_freq_factor": 1.0,
|
| 39 |
+
"original_max_position_embeddings": 8192,
|
| 40 |
+
"rope_theta": 500000.0,
|
| 41 |
+
"rope_type": "llama3"
|
| 42 |
+
},
|
| 43 |
+
"temperature": 1.0,
|
| 44 |
+
"tie_word_embeddings": true,
|
| 45 |
+
"transformers_version": "5.13.0",
|
| 46 |
+
"use_cache": false,
|
| 47 |
+
"vocab_size": 128256
|
| 48 |
+
}
|
llama_bidirectional_model.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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5 |
+
|
| 6 |
+
Modifies LlamaModel to use bidirectional (non-causal) attention so each token
|
| 7 |
+
attends to all others — required for cross-encoder scoring of query-document pairs.
|
| 8 |
+
|
| 9 |
+
Provides three classes:
|
| 10 |
+
- LlamaBidirectionalConfig: Adds pooling and temperature to LlamaConfig.
|
| 11 |
+
- LlamaBidirectionalModel: LlamaModel with causal masking replaced by
|
| 12 |
+
bidirectional masking. Overrides forward() to support transformers >=4.44.
|
| 13 |
+
- LlamaBidirectionalForSequenceClassification: Pools hidden states and
|
| 14 |
+
projects to a relevance score via a linear head.
|
| 15 |
+
|
| 16 |
+
Transformers version compatibility (>=4.44 including 5.0+):
|
| 17 |
+
The forward() implementation handles these API changes at import time via
|
| 18 |
+
inspect.signature() on LlamaDecoderLayer and DynamicCache:
|
| 19 |
+
|
| 20 |
+
< 4.53: _update_causal_mask exists on LlamaModel (not used here).
|
| 21 |
+
4.53+: Masking moved to masking_utils; requires full forward() override.
|
| 22 |
+
< 4.54: Decoder layer returns a tuple.
|
| 23 |
+
4.54+: Decoder layer returns a tensor.
|
| 24 |
+
< 4.56: Cache kwarg is ``past_key_value`` (singular).
|
| 25 |
+
4.56+: Cache kwarg is ``past_key_values`` (plural); DynamicCache accepts config.
|
| 26 |
+
5.0+: Native ``create_bidirectional_mask`` in masking_utils.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
import inspect
|
| 30 |
+
from typing import Optional, Union, Tuple, List
|
| 31 |
+
|
| 32 |
+
import torch
|
| 33 |
+
import torch.nn as nn
|
| 34 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
| 35 |
+
from transformers.modeling_outputs import SequenceClassifierOutputWithPast
|
| 36 |
+
from transformers.cache_utils import Cache, DynamicCache
|
| 37 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast
|
| 38 |
+
from transformers.models.llama.configuration_llama import LlamaConfig
|
| 39 |
+
from transformers.models.llama.modeling_llama import (
|
| 40 |
+
LlamaDecoderLayer,
|
| 41 |
+
LlamaModel,
|
| 42 |
+
LlamaPreTrainedModel,
|
| 43 |
+
)
|
| 44 |
+
from transformers.utils import logging
|
| 45 |
+
|
| 46 |
+
logger = logging.get_logger(__name__)
|
| 47 |
+
|
| 48 |
+
# Check if native create_bidirectional_mask exists (transformers >= 5.0)
|
| 49 |
+
try:
|
| 50 |
+
from transformers.masking_utils import create_bidirectional_mask
|
| 51 |
+
|
| 52 |
+
_HAS_NATIVE_BIDIRECTIONAL_MASK = True
|
| 53 |
+
except ImportError:
|
| 54 |
+
from transformers.modeling_attn_mask_utils import _prepare_4d_attention_mask
|
| 55 |
+
|
| 56 |
+
_HAS_NATIVE_BIDIRECTIONAL_MASK = False
|
| 57 |
+
|
| 58 |
+
# Detect API differences via introspection
|
| 59 |
+
_decoder_forward_params = inspect.signature(LlamaDecoderLayer.forward).parameters
|
| 60 |
+
_dynamic_cache_init_params = inspect.signature(DynamicCache.__init__).parameters
|
| 61 |
+
|
| 62 |
+
# past_key_value (singular) in < 4.56, past_key_values (plural) in >= 4.56
|
| 63 |
+
_USE_PLURAL_CACHE_PARAM = "past_key_values" in _decoder_forward_params
|
| 64 |
+
# DynamicCache accepts config parameter in >= 4.56
|
| 65 |
+
_DYNAMIC_CACHE_ACCEPTS_CONFIG = "config" in _dynamic_cache_init_params
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class LlamaBidirectionalConfig(LlamaConfig):
|
| 69 |
+
"""Configuration for LlamaBidirectionalModel with pooling and temperature settings."""
|
| 70 |
+
|
| 71 |
+
model_type = "llama_bidirec"
|
| 72 |
+
|
| 73 |
+
def __init__(
|
| 74 |
+
self, pooling: str = "avg", temperature: float = 1.0, **kwargs
|
| 75 |
+
) -> None:
|
| 76 |
+
"""
|
| 77 |
+
Initialize bidirectional Llama configuration.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
pooling: Pooling strategy for embeddings ("avg", "cls", "last", etc.)
|
| 81 |
+
temperature: Temperature scaling for embeddings
|
| 82 |
+
**kwargs: Additional arguments passed to LlamaConfig
|
| 83 |
+
"""
|
| 84 |
+
self.pooling = pooling
|
| 85 |
+
self.temperature = temperature
|
| 86 |
+
super().__init__(**kwargs)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class LlamaBidirectionalModel(LlamaModel):
|
| 90 |
+
"""
|
| 91 |
+
LlamaModel modified to use bidirectional (non-causal) attention.
|
| 92 |
+
|
| 93 |
+
In standard Llama, each token can only attend to previous tokens (causal attention).
|
| 94 |
+
This model removes that restriction, allowing each token to attend to all tokens
|
| 95 |
+
in the sequence, which is useful for embedding tasks.
|
| 96 |
+
|
| 97 |
+
The key modifications are:
|
| 98 |
+
1. Setting is_causal=False on all attention layers
|
| 99 |
+
2. Using a bidirectional attention mask instead of causal mask
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
config_class = LlamaBidirectionalConfig
|
| 103 |
+
|
| 104 |
+
def __init__(self, config: LlamaConfig) -> None:
|
| 105 |
+
super().__init__(config)
|
| 106 |
+
for layer in self.layers:
|
| 107 |
+
layer.self_attn.is_causal = False
|
| 108 |
+
|
| 109 |
+
def _create_bidirectional_mask(
|
| 110 |
+
self,
|
| 111 |
+
input_embeds: torch.Tensor,
|
| 112 |
+
attention_mask: torch.Tensor | None,
|
| 113 |
+
) -> torch.Tensor | None:
|
| 114 |
+
"""
|
| 115 |
+
Create bidirectional attention mask.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
input_embeds: Input embeddings tensor of shape (batch_size, seq_len, hidden_size)
|
| 119 |
+
attention_mask: Optional 2D attention mask of shape (batch_size, seq_len)
|
| 120 |
+
where 1 indicates tokens to attend to and 0 indicates masked tokens
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
4D attention mask suitable for the attention implementation, or None
|
| 124 |
+
if no masking is needed
|
| 125 |
+
"""
|
| 126 |
+
if attention_mask is None:
|
| 127 |
+
return None
|
| 128 |
+
|
| 129 |
+
if _HAS_NATIVE_BIDIRECTIONAL_MASK:
|
| 130 |
+
return create_bidirectional_mask(
|
| 131 |
+
self.config,
|
| 132 |
+
input_embeds,
|
| 133 |
+
attention_mask=attention_mask,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Fallback for transformers < 5.0 without create_bidirectional_mask
|
| 137 |
+
|
| 138 |
+
# Flash attention handles 2D masks internally; only pass mask if there
|
| 139 |
+
# are actually masked tokens (zeros), otherwise return None for efficiency
|
| 140 |
+
if getattr(self.config, "_attn_implementation", None) == "flash_attention_2":
|
| 141 |
+
has_masked_tokens = (attention_mask == 0).any()
|
| 142 |
+
return attention_mask if has_masked_tokens else None
|
| 143 |
+
|
| 144 |
+
return _prepare_4d_attention_mask(attention_mask, input_embeds.dtype)
|
| 145 |
+
|
| 146 |
+
def forward(
|
| 147 |
+
self,
|
| 148 |
+
input_ids: torch.LongTensor | None = None,
|
| 149 |
+
attention_mask: torch.Tensor | None = None,
|
| 150 |
+
position_ids: torch.LongTensor | None = None,
|
| 151 |
+
past_key_values: Cache | None = None,
|
| 152 |
+
inputs_embeds: torch.FloatTensor | None = None,
|
| 153 |
+
cache_position: torch.LongTensor | None = None,
|
| 154 |
+
use_cache: bool | None = None,
|
| 155 |
+
**kwargs,
|
| 156 |
+
) -> BaseModelOutputWithPast:
|
| 157 |
+
"""
|
| 158 |
+
Forward pass with bidirectional attention.
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
input_ids: Input token IDs of shape (batch_size, seq_len)
|
| 162 |
+
attention_mask: Attention mask of shape (batch_size, seq_len)
|
| 163 |
+
position_ids: Position IDs for rotary embeddings
|
| 164 |
+
past_key_values: Cached key/value states for incremental decoding
|
| 165 |
+
inputs_embeds: Pre-computed input embeddings (alternative to input_ids)
|
| 166 |
+
cache_position: Position indices for cache updates
|
| 167 |
+
use_cache: Whether to return cached key/value states
|
| 168 |
+
**kwargs: Additional arguments passed to decoder layers
|
| 169 |
+
|
| 170 |
+
Returns:
|
| 171 |
+
BaseModelOutputWithPast containing last_hidden_state and past_key_values
|
| 172 |
+
"""
|
| 173 |
+
if (input_ids is None) ^ (inputs_embeds is not None):
|
| 174 |
+
raise ValueError(
|
| 175 |
+
"You must specify exactly one of input_ids or inputs_embeds"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if inputs_embeds is None:
|
| 179 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
| 180 |
+
|
| 181 |
+
# Initialize cache if needed
|
| 182 |
+
if use_cache and past_key_values is None:
|
| 183 |
+
if _DYNAMIC_CACHE_ACCEPTS_CONFIG:
|
| 184 |
+
past_key_values = DynamicCache(config=self.config)
|
| 185 |
+
else:
|
| 186 |
+
past_key_values = DynamicCache()
|
| 187 |
+
|
| 188 |
+
if cache_position is None:
|
| 189 |
+
past_seen_tokens = (
|
| 190 |
+
past_key_values.get_seq_length() if past_key_values is not None else 0
|
| 191 |
+
)
|
| 192 |
+
cache_position = torch.arange(
|
| 193 |
+
past_seen_tokens,
|
| 194 |
+
past_seen_tokens + inputs_embeds.shape[1],
|
| 195 |
+
device=inputs_embeds.device,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
if position_ids is None:
|
| 199 |
+
position_ids = cache_position.unsqueeze(0)
|
| 200 |
+
|
| 201 |
+
bidirectional_mask = self._create_bidirectional_mask(
|
| 202 |
+
inputs_embeds, attention_mask
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
hidden_states = inputs_embeds
|
| 206 |
+
position_embeddings = self.rotary_emb(hidden_states, position_ids)
|
| 207 |
+
|
| 208 |
+
# Build decoder layer kwargs with correct cache parameter name
|
| 209 |
+
# (past_key_value in < 4.56, past_key_values in >= 4.56)
|
| 210 |
+
layer_kwargs = {
|
| 211 |
+
"attention_mask": bidirectional_mask,
|
| 212 |
+
"position_ids": position_ids,
|
| 213 |
+
"use_cache": use_cache,
|
| 214 |
+
"cache_position": cache_position,
|
| 215 |
+
"position_embeddings": position_embeddings,
|
| 216 |
+
}
|
| 217 |
+
if _USE_PLURAL_CACHE_PARAM:
|
| 218 |
+
layer_kwargs["past_key_values"] = past_key_values
|
| 219 |
+
else:
|
| 220 |
+
layer_kwargs["past_key_value"] = past_key_values
|
| 221 |
+
|
| 222 |
+
for decoder_layer in self.layers[: self.config.num_hidden_layers]:
|
| 223 |
+
layer_outputs = decoder_layer(hidden_states, **layer_kwargs)
|
| 224 |
+
|
| 225 |
+
# Decoder returns tuple in < 4.54, tensor in >= 4.54
|
| 226 |
+
if isinstance(layer_outputs, tuple):
|
| 227 |
+
hidden_states = layer_outputs[0]
|
| 228 |
+
else:
|
| 229 |
+
hidden_states = layer_outputs
|
| 230 |
+
|
| 231 |
+
hidden_states = self.norm(hidden_states)
|
| 232 |
+
|
| 233 |
+
return BaseModelOutputWithPast(
|
| 234 |
+
last_hidden_state=hidden_states,
|
| 235 |
+
past_key_values=past_key_values,
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def pool(
|
| 240 |
+
last_hidden_states: torch.Tensor, attention_mask: torch.Tensor, pool_type: str
|
| 241 |
+
) -> torch.Tensor:
|
| 242 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
| 243 |
+
|
| 244 |
+
if pool_type == "avg":
|
| 245 |
+
emb = last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
| 246 |
+
elif pool_type == "weighted_avg":
|
| 247 |
+
emb = last_hidden.sum(dim=1)
|
| 248 |
+
elif pool_type == "cls":
|
| 249 |
+
emb = last_hidden[:, 0]
|
| 250 |
+
elif pool_type == "last":
|
| 251 |
+
left_padding = attention_mask[:, -1].sum() == attention_mask.shape[0]
|
| 252 |
+
if left_padding:
|
| 253 |
+
emb = last_hidden[:, -1]
|
| 254 |
+
else:
|
| 255 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
| 256 |
+
batch_size = last_hidden.shape[0]
|
| 257 |
+
emb = last_hidden[
|
| 258 |
+
torch.arange(batch_size, device=last_hidden.device), sequence_lengths
|
| 259 |
+
]
|
| 260 |
+
else:
|
| 261 |
+
raise ValueError(f"pool_type {pool_type} not supported")
|
| 262 |
+
|
| 263 |
+
return emb
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
class LlamaBidirectionalForSequenceClassification(LlamaPreTrainedModel):
|
| 267 |
+
config_class = LlamaBidirectionalConfig
|
| 268 |
+
|
| 269 |
+
def __init__(self, config):
|
| 270 |
+
super().__init__(config)
|
| 271 |
+
self.num_labels = config.num_labels
|
| 272 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
| 273 |
+
self.model = LlamaBidirectionalModel(config)
|
| 274 |
+
|
| 275 |
+
# Initialize weights and apply final processing
|
| 276 |
+
self.post_init()
|
| 277 |
+
|
| 278 |
+
def forward(
|
| 279 |
+
self,
|
| 280 |
+
input_ids: Optional[torch.LongTensor] = None,
|
| 281 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 282 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 283 |
+
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
|
| 284 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
| 285 |
+
labels: Optional[torch.LongTensor] = None,
|
| 286 |
+
use_cache: Optional[bool] = None,
|
| 287 |
+
output_attentions: Optional[bool] = None,
|
| 288 |
+
output_hidden_states: Optional[bool] = None,
|
| 289 |
+
return_dict: Optional[bool] = None,
|
| 290 |
+
**kwargs,
|
| 291 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
| 292 |
+
r"""
|
| 293 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
| 294 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
| 295 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
| 296 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
| 297 |
+
"""
|
| 298 |
+
return_dict = (
|
| 299 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
transformer_outputs = self.model(
|
| 303 |
+
input_ids,
|
| 304 |
+
attention_mask=attention_mask,
|
| 305 |
+
position_ids=position_ids,
|
| 306 |
+
past_key_values=past_key_values,
|
| 307 |
+
inputs_embeds=inputs_embeds,
|
| 308 |
+
use_cache=use_cache,
|
| 309 |
+
output_attentions=output_attentions,
|
| 310 |
+
output_hidden_states=output_hidden_states,
|
| 311 |
+
return_dict=return_dict,
|
| 312 |
+
**kwargs,
|
| 313 |
+
)
|
| 314 |
+
hidden_states = transformer_outputs[0]
|
| 315 |
+
|
| 316 |
+
pooled_hidden_states = pool(
|
| 317 |
+
last_hidden_states=hidden_states,
|
| 318 |
+
attention_mask=attention_mask,
|
| 319 |
+
pool_type=self.config.pooling,
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
pooled_logits = self.score(pooled_hidden_states)
|
| 323 |
+
pooled_logits = pooled_logits / self.config.temperature
|
| 324 |
+
|
| 325 |
+
loss = None
|
| 326 |
+
if labels is not None:
|
| 327 |
+
labels = labels.to(pooled_logits.device)
|
| 328 |
+
if self.config.problem_type is None:
|
| 329 |
+
if self.num_labels == 1:
|
| 330 |
+
self.config.problem_type = "regression"
|
| 331 |
+
elif self.num_labels > 1 and (
|
| 332 |
+
labels.dtype == torch.long or labels.dtype == torch.int
|
| 333 |
+
):
|
| 334 |
+
self.config.problem_type = "single_label_classification"
|
| 335 |
+
else:
|
| 336 |
+
self.config.problem_type = "multi_label_classification"
|
| 337 |
+
|
| 338 |
+
if self.config.problem_type == "regression":
|
| 339 |
+
loss_fct = MSELoss()
|
| 340 |
+
if self.num_labels == 1:
|
| 341 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
| 342 |
+
else:
|
| 343 |
+
loss = loss_fct(pooled_logits, labels)
|
| 344 |
+
elif self.config.problem_type == "single_label_classification":
|
| 345 |
+
loss_fct = CrossEntropyLoss()
|
| 346 |
+
loss = loss_fct(
|
| 347 |
+
pooled_logits.view(-1, self.num_labels), labels.view(-1)
|
| 348 |
+
)
|
| 349 |
+
elif self.config.problem_type == "multi_label_classification":
|
| 350 |
+
loss_fct = BCEWithLogitsLoss()
|
| 351 |
+
loss = loss_fct(pooled_logits, labels)
|
| 352 |
+
if not return_dict:
|
| 353 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
| 354 |
+
return ((loss,) + output) if loss is not None else output
|
| 355 |
+
|
| 356 |
+
return SequenceClassifierOutputWithPast(
|
| 357 |
+
loss=loss,
|
| 358 |
+
logits=pooled_logits,
|
| 359 |
+
past_key_values=transformer_outputs.past_key_values,
|
| 360 |
+
hidden_states=transformer_outputs.hidden_states,
|
| 361 |
+
attentions=transformer_outputs.attentions,
|
| 362 |
+
)
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f05ed7b48677a1c574b2c33c732867414eb70fdb7df1039a9962824ce658b655
|
| 3 |
+
size 2471649792
|
stack.png
ADDED
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8df7c1e572298d8e41141627b38c75e5103e131d16e1c334f365bb0a930b0260
|
| 3 |
+
size 17210189
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "<|begin_of_text|>",
|
| 4 |
+
"clean_up_tokenization_spaces": true,
|
| 5 |
+
"eos_token": "<|end_of_text|>",
|
| 6 |
+
"is_local": true,
|
| 7 |
+
"local_files_only": false,
|
| 8 |
+
"model_input_names": [
|
| 9 |
+
"input_ids",
|
| 10 |
+
"attention_mask"
|
| 11 |
+
],
|
| 12 |
+
"model_max_length": 4096,
|
| 13 |
+
"pad_token": "<|end_of_text|>",
|
| 14 |
+
"tokenizer_class": "TokenizersBackend"
|
| 15 |
+
}
|