Sentence Similarity
sentence-transformers
Safetensors
Hebrew
hebrew
semantic-retrieval
information-retrieval
dense-retrieval
reranking
bge-m3
competition
Instructions to use HebArabNlpProject/Semantic-Retrieval-3rd-place with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use HebArabNlpProject/Semantic-Retrieval-3rd-place with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("HebArabNlpProject/Semantic-Retrieval-3rd-place") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- he
|
| 4 |
+
tags:
|
| 5 |
+
- hebrew
|
| 6 |
+
- semantic-retrieval
|
| 7 |
+
- information-retrieval
|
| 8 |
+
- dense-retrieval
|
| 9 |
+
- reranking
|
| 10 |
+
- bge-m3
|
| 11 |
+
- sentence-transformers
|
| 12 |
+
- competition
|
| 13 |
+
pipeline_tag: sentence-similarity
|
| 14 |
+
license: other
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Hebrew Semantic Retrieval — 3rd Place Solution
|
| 18 |
+
|
| 19 |
+
**Competition:** Hebrew Semantic Retrieval Challenge by MAFAT DDR&D (Directorate of Defense Research & Development) in partnership with the **Israel National NLP Program**
|
| 20 |
+
|
| 21 |
+
**Result:** 🥉 **3rd place** — nDCG@20 = **0.652538** (private test set) · **0.432286** (public test set)
|
| 22 |
+
|
| 23 |
+
**Author:** kdbrodt
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
## Overview
|
| 28 |
+
|
| 29 |
+
This repository contains the complete inference code and fine-tuned models for the 3rd-place solution to the **Hebrew Semantic Retrieval Challenge**. The challenge tasked participants with ranking Hebrew paragraphs from a 127,731-passage corpus in response to natural-language Hebrew queries, evaluated by **NDCG@20**.
|
| 30 |
+
|
| 31 |
+
The solution is a clean, end-to-end two-stage retrieve-then-rerank pipeline built entirely on the [FlagEmbedding](https://github.com/FlagOpen/FlagEmbedding) (`BAAI/bge-m3`) family. Both the dense embedder and the cross-encoder reranker were fine-tuned directly on the competition's annotated Hebrew data.
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## The Challenge
|
| 36 |
+
|
| 37 |
+
| Property | Detail |
|
| 38 |
+
|---|---|
|
| 39 |
+
| Organizer | MAFAT DDR&D + Israel National NLP Program |
|
| 40 |
+
| Corpus size | 127,731 Hebrew paragraphs |
|
| 41 |
+
| Data sources | Hebrew Wikipedia, Kol-Zchut (legal/civil-rights), Knesset committee protocols |
|
| 42 |
+
| Evaluation metric | NDCG@20 |
|
| 43 |
+
| Phase I | Public leaderboard (Codabench) |
|
| 44 |
+
| Phase II | Private test set with additional human annotation of previously unseen retrievals |
|
| 45 |
+
| Relevance scale | 0–4 (human annotated) |
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## Solution Architecture
|
| 50 |
+
|
| 51 |
+
A straightforward two-stage pipeline: dense retrieval followed by cross-encoder reranking.
|
| 52 |
+
|
| 53 |
+
```
|
| 54 |
+
Query
|
| 55 |
+
│
|
| 56 |
+
▼
|
| 57 |
+
[BGE-M3 Dense Retriever] (fine-tuned, CLS pooling, FP16)
|
| 58 |
+
│ cosine similarity over 127k passages
|
| 59 |
+
▼
|
| 60 |
+
Top-100 Candidates
|
| 61 |
+
│
|
| 62 |
+
▼
|
| 63 |
+
[BGE-Reranker-v2-M3] (fine-tuned binary classifier, FP16)
|
| 64 |
+
│ query-passage pairs scored, max_length=512
|
| 65 |
+
▼
|
| 66 |
+
Final Top-20 Results
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
### Stage 1 — Dense Retrieval
|
| 70 |
+
|
| 71 |
+
The fine-tuned `bge-m3` encoder produces **CLS-token embeddings** (L2-normalized, FP16) for all corpus passages at preprocessing time. At query time, a single query embedding is computed and scored against all corpus embeddings via **dot-product similarity** (equivalent to cosine similarity on normalized vectors). The top-100 passages are selected for reranking.
|
| 72 |
+
|
| 73 |
+
| Property | Value |
|
| 74 |
+
|---|---|
|
| 75 |
+
| Model | `test_encoder_only_base_bge_m3_new1` (fine-tuned `BAAI/bge-m3`) |
|
| 76 |
+
| Pooling | CLS token |
|
| 77 |
+
| Normalization | L2 |
|
| 78 |
+
| Precision | FP16 |
|
| 79 |
+
| Max length | 512 tokens |
|
| 80 |
+
| Batch size (corpus) | 64 |
|
| 81 |
+
| Retrieval pool | Top-100 candidates |
|
| 82 |
+
|
| 83 |
+
### Stage 2 — Cross-Encoder Reranking
|
| 84 |
+
|
| 85 |
+
The top-100 candidates are re-scored by the fine-tuned `bge-reranker-v2-m3`, a sequence classification model that takes concatenated `[query, passage]` pairs as input and outputs a relevance logit. Passages are sorted by length before scoring to minimize padding overhead. The top-20 by reranker score are returned.
|
| 86 |
+
|
| 87 |
+
| Property | Value |
|
| 88 |
+
|---|---|
|
| 89 |
+
| Model | `test_encoder_only_base_bge_reranker_v2_m3_new1` (fine-tuned `BAAI/bge-reranker-v2-m3`) |
|
| 90 |
+
| Max length | 512 tokens |
|
| 91 |
+
| Batch size | 16 |
|
| 92 |
+
| Output | Top-20 by reranker logit |
|
| 93 |
+
|
| 94 |
+
---
|
| 95 |
+
|
| 96 |
+
## Fine-Tuning
|
| 97 |
+
|
| 98 |
+
Both models were fine-tuned on the competition's annotated Hebrew training set using the [FlagEmbedding](https://github.com/FlagOpen/FlagEmbedding) framework.
|
| 99 |
+
|
| 100 |
+
**Training data construction:**
|
| 101 |
+
- Every query–document pair with a **positive relevance score (> 0)** was treated as a positive example.
|
| 102 |
+
- Every pair with a **score of 0** was treated as a negative example.
|
| 103 |
+
|
| 104 |
+
**Embedder (`bge-m3`):** Trained with **KL-divergence loss** to produce embeddings that better separate relevant from irrelevant documents.
|
| 105 |
+
|
| 106 |
+
**Reranker (`bge-reranker-v2-m3`):** Trained as a **binary classifier** on the same positive/negative pairs, learning to predict relevance probability directly.
|
| 107 |
+
|
| 108 |
+
| Hyperparameter | Value |
|
| 109 |
+
|---|---|
|
| 110 |
+
| Epochs | 2 |
|
| 111 |
+
| Batch size per device | 2 |
|
| 112 |
+
| Learning rate | 5e-6 |
|
| 113 |
+
| Hardware | 2 × Nvidia Tesla V100-SXM2-32GB |
|
| 114 |
+
| Training time | ~1 hour |
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## Included Models (fine-tuned)
|
| 119 |
+
|
| 120 |
+
| Path in repo | Base model | Fine-tuning |
|
| 121 |
+
|---|---|---|
|
| 122 |
+
| `models/test_encoder_only_base_bge_m3_new1/` | `BAAI/bge-m3` | KL-divergence loss on competition data ✨ |
|
| 123 |
+
| `models/test_encoder_only_base_bge_reranker_v2_m3_new1/` | `BAAI/bge-reranker-v2-m3` | Binary classification on competition data ✨ |
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
## Repository Structure
|
| 128 |
+
|
| 129 |
+
```
|
| 130 |
+
model.py ← Full inference pipeline (preprocess + predict)
|
| 131 |
+
prepare.py ← Data preparation script
|
| 132 |
+
train.sh ← Training script
|
| 133 |
+
models/
|
| 134 |
+
test_encoder_only_base_bge_m3_new1/ ← Fine-tuned BGE-M3 embedder ✨
|
| 135 |
+
test_encoder_only_base_bge_reranker_v2_m3_new1/ ← Fine-tuned BGE reranker ✨
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
|
| 140 |
+
## Usage
|
| 141 |
+
|
| 142 |
+
The pipeline exposes two functions matching the competition API:
|
| 143 |
+
|
| 144 |
+
```python
|
| 145 |
+
from model import preprocess, predict
|
| 146 |
+
|
| 147 |
+
# Build corpus index (run once)
|
| 148 |
+
# corpus_dict: {doc_id: {"passage": "..."}, ...}
|
| 149 |
+
preprocessed = preprocess(corpus_dict)
|
| 150 |
+
|
| 151 |
+
# Query at inference time
|
| 152 |
+
results = predict({"query": "מה הזכויות של שוכרי דירה?"}, preprocessed)
|
| 153 |
+
# Returns: [{"paragraph_uuid": "...", "score": 1.23}, ...] (top-20)
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
**Requirements:**
|
| 157 |
+
```
|
| 158 |
+
torch
|
| 159 |
+
transformers
|
| 160 |
+
numpy
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
**Hardware:** A CUDA-capable GPU is required. Inference takes less than 1.5 hours on an `g5.xlarge` instance.
|
| 164 |
+
|
| 165 |
+
---
|
| 166 |
+
|
| 167 |
+
## Reproducing the Models
|
| 168 |
+
|
| 169 |
+
**1. Prepare data:**
|
| 170 |
+
```bash
|
| 171 |
+
# Download competition data and unzip into `hsrc/` folder
|
| 172 |
+
python prepare.py
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
**2. Train:**
|
| 176 |
+
```bash
|
| 177 |
+
sh ./train.sh
|
| 178 |
+
```
|
| 179 |
+
Training takes ~1 hour on 2 × V100-SXM2-32GB GPUs.
|
| 180 |
+
|
| 181 |
+
---
|
| 182 |
+
|
| 183 |
+
## Technical Notes
|
| 184 |
+
|
| 185 |
+
- Both models are loaded in **FP16** via `torch_dtype=torch.float16` and `device_map` for automatic GPU placement.
|
| 186 |
+
- Corpus passages are **sorted by length** before embedding to reduce padding overhead during batch encoding.
|
| 187 |
+
- The reranker also sorts candidates by passage length before scoring batches.
|
| 188 |
+
- Fallback: if reranking fails, the pipeline falls back to returning the top-20 by dense retrieval score.
|
| 189 |
+
|
| 190 |
+
---
|
| 191 |
+
|
| 192 |
+
## Results
|
| 193 |
+
|
| 194 |
+
| Phase | NDCG@20 | Rank |
|
| 195 |
+
|---|---|---|
|
| 196 |
+
| Public (Phase I) | **0.432286** | 🥉 3rd |
|
| 197 |
+
| Private (Phase II) | **0.652538** | 🥉 3rd |
|
| 198 |
+
|
| 199 |
+
> The large gap between public and private scores reflects the private phase's additional human annotation of previously un-annotated retrieved documents, significantly boosting NDCG for systems that retrieved relevant but unannotated paragraphs.
|
| 200 |
+
|
| 201 |
+
---
|
| 202 |
+
|
| 203 |
+
## Citation
|
| 204 |
+
|
| 205 |
+
If you use this solution or the models in this repository, please acknowledge the **Hebrew Semantic Retrieval Challenge** by MAFAT DDR&D and the Israel National NLP Program, and credit **kdbrodt** as the solution author.
|
| 206 |
+
|
| 207 |
+
---
|
| 208 |
+
|
| 209 |
+
## Acknowledgements
|
| 210 |
+
|
| 211 |
+
- MAFAT DDR&D and the **Israel National NLP Program** for organizing the challenge and providing the annotated Hebrew corpus.
|
| 212 |
+
- The authors of `BAAI/bge-m3` and `BAAI/bge-reranker-v2-m3`.
|
| 213 |
+
- The [FlagEmbedding](https://github.com/FlagOpen/FlagEmbedding) team for the training framework.
|