SyedSyab commited on
Commit
4aaa9af
·
verified ·
1 Parent(s): 8a78060

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +256 -16
README.md CHANGED
@@ -1,5 +1,7 @@
1
  ---
2
  license: apache-2.0
 
 
3
  language:
4
  - en
5
  - ar
@@ -10,31 +12,269 @@ tags:
10
  - retrieval
11
  - contrastive-learning
12
  - multilingual
 
 
 
 
 
 
 
 
 
 
13
  ---
14
 
15
- # mentee-embed-v4 (41M) — trilingual embeddings trained from scratch
16
 
17
- A compact **41M-parameter, 384-dim** text embedding model for **Arabic, English and Urdu**, trained entirely from scratch (no pretrained base) in two stages on a single consumer GPU.
18
 
19
- ## Recipe
20
- 1. **Stage A — Masked language modeling** on ~31M tokens across the three languages
21
- 2. **Stage B — Contrastive fine-tuning** (symmetric InfoNCE, in-batch negatives + hard-negative column, temperature 0.05) on 2589852 triplets incl. **300K English↔Urdu parallel pairs (OPUS-100)**
22
 
23
- ## Results (Protocol B 15K-doc corpus-pool retrieval, higher is better)
24
- | 1 | paraphrase-multilingual-mpnet-base-v2 | MRR 0.829 · R@5 0.950 · R@100 0.997 | MRR 0.622 · R@5 0.757 · R@100 0.947 | MRR 0.534 · R@5 0.680 · R@100 0.903 | **0.662** |
25
- | 2 | paraphrase-multilingual-MiniLM-L12-v2 | MRR 0.817 · R@5 0.920 · R@100 0.993 | MRR 0.591 · R@5 0.710 · R@100 0.943 | MRR 0.469 · R@5 0.557 · R@100 0.847 | **0.626** |
26
- | 3 | mentee-embed-v4 (ours) ⭐ **ours** | MRR 0.695 · R@5 0.823 · R@100 0.970 | MRR 0.623 · R@5 0.727 · R@100 0.927 | MRR 0.336 · R@5 0.373 · R@100 0.643 | **0.551** |
27
- | 4 | all-MiniLM-L6-v2 | MRR 0.844 · R@5 0.967 · R@100 1.000 | MRR 0.100 · R@5 0.000 · R@100 0.010 | MRR 0.106 · R@5 0.007 · R@100 0.013 | **0.350** |
28
 
29
- Our model reaches **0.551** vs leader paraphrase-multilingual-mpnet-base-v2 at **0.662** on this suite.
30
- *Protocol details: queries ranked against the full pool; baselines run with their recommended usage (E5 query/passage prefixes applied).*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  ## Usage
 
33
  ```python
34
- from sentence_transformers import SentenceTransformer
35
- model = SentenceTransformer("<your-hf-user>/mentee-embed-v1")
36
- emb = model.encode(["مرحبا بالعالم", "hello world", "دنیا میں خوش آمدید"])
 
 
 
 
 
 
 
 
 
 
 
37
  ```
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  ## Limitations
40
- NLI+parallel derived training data; small-pool protocol is easier than full MIRACL; expect open-domain gaps versus web-scale models.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ pipeline_tag: sentence-similarity
4
+ library_name: transformers
5
  language:
6
  - en
7
  - ar
 
12
  - retrieval
13
  - contrastive-learning
14
  - multilingual
15
+ - from-scratch
16
+ datasets:
17
+ - sentence-transformers/all-NLI
18
+ - facebook/xnli
19
+ - Helsinki-NLP/opus-100
20
+ - sentence-transformers/msmarco-bm25
21
+ - nlpai-lab/miracl-multilingual-triplets
22
+ metrics:
23
+ - mrr@10
24
+ - spearman_correlation
25
  ---
26
 
27
+ # mentee-embed-v4
28
 
29
+ **A 41M-parameter trilingual text embedding model trained entirely from scratch no pretrained backbone, no BERT, no RoBERTa. Random initialization only.**
30
 
31
+ v4 adds mMARCO Arabic retrieval data (500K triplets) and a third distillation round with hard negatives, yielding significant improvements over v3 across all languages and benchmarks.
 
 
32
 
33
+ Developed by **Team MenteE AI** (menteeai.org) as part of an ongoing research effort to build competitive multilingual embeddings from the ground up for Arabic, English, and Urdu.
 
 
 
 
34
 
35
+ ---
36
+
37
+ ## Key Facts
38
+
39
+ | Property | Value |
40
+ |----------|-------|
41
+ | Parameters | **41M** |
42
+ | Embedding dimension | 384 |
43
+ | Max sequence length | 128 tokens |
44
+ | Languages | Arabic · English · Urdu |
45
+ | Architecture | 12-layer Transformer, custom BPE tokenizer (50K vocab) |
46
+ | Initialization | **Random** (trained from scratch) |
47
+ | Training data | ~2.6M triplets (NLI + MS-MARCO + OPUS + MIRACL + mMARCO Arabic) |
48
+ | Pooling | Mean pooling |
49
+ | Training objective | Relational distillation + InfoNCE contrastive (teacher: `multilingual-e5-base`) |
50
+
51
+ ---
52
+
53
+ ## What "From Scratch" Means
54
+
55
+ Most embedding models fine-tune an existing pretrained encoder (BERT, RoBERTa, MPNet). **mentee-embed-v4 does not.** We:
56
+
57
+ 1. Trained a **custom BPE tokenizer** on Arabic, English, and Urdu text
58
+ 2. Initialized a **12-layer Transformer with random weights**
59
+ 3. Ran **masked language modeling** pretraining (50K steps on trilingual corpus)
60
+ 4. Applied **three-round contrastive distillation** with hard negative mining
61
+
62
+ No pretrained checkpoint was used at any stage.
63
+
64
+ ---
65
+
66
+ ## v4 vs v3 Improvements
67
+
68
+ | Benchmark | v3 | v4 | Change |
69
+ |-----------|----|----|--------|
70
+ | Bench MRR@10 (all) | 0.103 | **0.252** | +146% |
71
+ | MIRACL EN MRR@10 | 0.768 | **0.916** | +19% |
72
+ | MIRACL AR MRR@10 | 0.447 | **0.874** | +96% |
73
+ | MIRACL UR MRR@10 | 0.354 | **0.572** | +62% |
74
+ | MS-MARCO MRR@10 | 0.592 | **0.706** | +19% |
75
+ | STS-B Spearman | 0.683 | **0.725** | +6% |
76
+
77
+ Key improvements come from:
78
+ - **mMARCO Arabic** — 500K Arabic retrieval triplets added to training
79
+ - **50K MLM steps** — 6x more pretraining than v3 (8K steps)
80
+ - **3 distillation rounds** — extra hard-negative round for better discrimination
81
+
82
+ ---
83
+
84
+ ## Benchmark Results
85
+
86
+ All baselines evaluated under identical conditions on the same hardware (RTX 5090).
87
+
88
+ ### MenteE Embed Bench (Custom EN/AR/UR Pilot)
89
+
90
+ *121 queries · 18 domains · dialect Arabic · Roman Urdu · hard negatives*
91
+
92
+ | Model | Params | EN MRR@10 | AR MRR@10 | UR MRR@10 | Dialect AR | Roman UR | All MRR@10 |
93
+ |---|---|---|---|---|---|---|---|
94
+ | **mentee-embed-v4** | **41M** | 0.369 | 0.120 | 0.261 | 0.095 | 0.399 | **0.252** |
95
+ | paraphrase-mpnet-base-v2 | 278M | 0.764 | 0.602 | 0.611 | 0.445 | 0.661 | 0.661 |
96
+ | paraphrase-MiniLM-L12-v2 | 118M | 0.682 | 0.568 | 0.519 | 0.385 | 0.480 | 0.592 |
97
+ | multilingual-e5-base | 278M | 0.667 | 0.352 | 0.413 | 0.310 | 0.430 | 0.481 |
98
+ | all-MiniLM-L6-v2 | 23M | 0.873 | 0.087 | 0.289 | 0.084 | 0.486 | 0.425 |
99
+
100
+ ### Protocol A — In-batch Retrieval (MIRACL)
101
+
102
+ *Format: acc@1 / MRR@10*
103
+
104
+ | Model | MIRACL-EN | MIRACL-AR | MIRACL-UR |
105
+ |-------|-----------|-----------|-----------|
106
+ | **mentee-embed-v4** | 0.870 / **0.916** | 0.825 / **0.874** | 0.475 / **0.572** |
107
+ | paraphrase-mpnet-base-v2 | 0.965 / 0.982 | 0.860 / 0.898 | 0.755 / 0.824 |
108
+ | paraphrase-MiniLM-L12-v2 | 0.965 / 0.979 | 0.815 / 0.868 | 0.720 / 0.785 |
109
+ | multilingual-e5-base | 0.980 / 0.990 | 0.940 / 0.958 | 0.960 / 0.970 |
110
+ | all-MiniLM-L6-v2 | 0.980 / 0.990 | 0.005 / 0.027 | 0.015 / 0.031 |
111
+
112
+ > v4 achieves strong MIRACL scores despite being 6.8x smaller than mpnet-base.
113
+
114
+ ### Protocol C — MS-MARCO Corpus Retrieval (10K passages)
115
+
116
+ | Model | MRR@10 | R@5 | R@100 |
117
+ |-------|--------|-----|-------|
118
+ | all-MiniLM-L6-v2 | 0.877 | 0.992 | 0.992 |
119
+ | multilingual-e5-base | 0.856 | 0.992 | 0.996 |
120
+ | paraphrase-mpnet-base-v2 | 0.803 | 0.964 | 0.988 |
121
+ | **mentee-embed-v4** | **0.706** | **0.913** | **0.980** |
122
+
123
+ ### STS-B (Spearman Correlation)
124
+
125
+ | Model | Spearman |
126
+ |-------|----------|
127
+ | paraphrase-mpnet-base-v2 | 0.868 |
128
+ | bge-small-en-v1.5 | 0.859 |
129
+ | paraphrase-MiniLM-L12-v2 | 0.844 |
130
+ | multilingual-e5-base | 0.842 |
131
+ | **mentee-embed-v4** | **0.725** |
132
+
133
+ ---
134
+
135
+ ## Speed & Efficiency
136
+
137
+ Measured on RTX 5090.
138
+
139
+ | Model | Params | Sents/sec (bs=128) | Latency@1 (ms) | VRAM (MB) | Cost/1B sents |
140
+ |---|---|---|---|---|---|
141
+ | **mentee-embed-v4** | **41M** | **18,115** | **5.15** | **175** | **$7.15** |
142
+ | all-MiniLM-L6-v2 | 23M | 12,445 | 4.71 | 103 | $10.40 |
143
+ | multilingual-e5-small | 118M | 9,749 | 7.88 | 482 | $13.28 |
144
+ | paraphrase-MiniLM-L12-v2 | 118M | 9,456 | 8.17 | 573 | $13.69 |
145
+ | paraphrase-mpnet-base-v2 | 278M | 5,158 | 8.04 | 1126 | $25.10 |
146
+ | multilingual-e5-base | 278M | 5,379 | 8.07 | 1126 | $24.07 |
147
+
148
+ > v4 is the **fastest model tested** — 3.5x faster than mpnet-base, 45% faster than MiniLM-L12.
149
+
150
+ ### Efficiency Metrics
151
+
152
+ | Model | Bench MRR/1M params | Sents/sec per 1M params |
153
+ |---|---|---|
154
+ | **mentee-embed-v4** | **0.525** | **441.82** |
155
+ | paraphrase-MiniLM-L12-v2 | 0.452 | 80.34 |
156
+ | paraphrase-mpnet-base-v2 | 0.224 | 18.55 |
157
+ | multilingual-e5-base | 0.166 | 19.35 |
158
+
159
+ ---
160
+
161
+ ## Training Data
162
+
163
+ | Source | Language | Triplets | Type |
164
+ |--------|----------|----------|------|
165
+ | all-NLI (sentence-transformers) | EN | 558K | NLI triplets |
166
+ | XNLI | AR | 128K | NLI triplets |
167
+ | XNLI | UR | 125K | NLI triplets |
168
+ | OPUS-100 EN-UR | EN/UR | 300K | Parallel translation |
169
+ | OPUS-100 AR-EN | AR/EN | 300K | Parallel translation |
170
+ | MS-MARCO BM25 triplets | EN | 500K | Passage retrieval |
171
+ | MS-MARCO hard negatives | EN | 200K | Hard retrieval |
172
+ | mMARCO Arabic | AR | 500K | Passage retrieval |
173
+ | MIRACL | EN/AR/UR | ~9K | Wikipedia retrieval |
174
+ | **Total** | | **~2.6M** | |
175
+
176
+ ---
177
+
178
+ ## Training Pipeline
179
+
180
+ ```
181
+ Stage 1 — MLM Pretraining (50K steps)
182
+ Random init → masked language modeling on trilingual corpus
183
+ 50,000 steps · batch=64 · vocab=50K BPE
184
+
185
+ Stage 2 — Teacher Encoding
186
+ Teacher: intfloat/multilingual-e5-base (768-dim)
187
+ Encode all 2.6M triplets to soft labels
188
+
189
+ Stage 3 — Distillation Round 1 (no hard negatives)
190
+ InfoNCE contrastive + relational distillation
191
+ 4,000 steps · batch=512 · temp=0.05
192
+
193
+ Stage 4 — Hard Negative Mining Round 1
194
+ GPU-accelerated top-5 mining across full 2.6M corpus
195
+
196
+ Stage 5 — Distillation Round 2 (with hard negatives)
197
+ Same objective + mined hard negatives per anchor
198
+ 10,000 steps · batch=512 · temp=0.05
199
+
200
+ Stage 6 — Hard Negative Mining Round 2 (on improved model)
201
+ Re-mine with the better round-2 model
202
+
203
+ Stage 7 — Distillation Round 3 (final polish)
204
+ Train on round-2 mined negatives
205
+ 15,000 cumulative steps · batch=512 · temp=0.05
206
+ ```
207
+
208
+ ---
209
 
210
  ## Usage
211
+
212
  ```python
213
+ # pip install torch transformers tokenizers huggingface_hub
214
+ from transformers import AutoModel, AutoTokenizer
215
+
216
+ tok = AutoTokenizer.from_pretrained("MenteEAI/mentee-embed-v4", trust_remote_code=True)
217
+ model = AutoModel.from_pretrained("MenteEAI/mentee-embed-v4", trust_remote_code=True)
218
+
219
+ sentences = [
220
+ "Hello, how are you?",
221
+ "مرحبا، كيف حالك؟",
222
+ "ہیلو، آپ کیسے ہیں؟"
223
+ ]
224
+
225
+ embeddings = model.encode(sentences, tokenizer=tok)
226
+ print(embeddings.shape) # torch.Size([3, 384])
227
  ```
228
 
229
+ > `trust_remote_code=True` is required — standard for custom-architecture models on HuggingFace. The code runs entirely on your machine.
230
+
231
+ ### Similarity search
232
+ ```python
233
+ from transformers import AutoModel, AutoTokenizer
234
+
235
+ tok = AutoTokenizer.from_pretrained("MenteEAI/mentee-embed-v4", trust_remote_code=True)
236
+ model = AutoModel.from_pretrained("MenteEAI/mentee-embed-v4", trust_remote_code=True)
237
+
238
+ query = model.encode(["What is machine learning?"], tokenizer=tok)
239
+ passages = model.encode([
240
+ "Machine learning is a subset of artificial intelligence.",
241
+ "The weather today is sunny.",
242
+ "تعلم الآلة هو فرع من فروع الذكاء الاصطناعي.",
243
+ ], tokenizer=tok)
244
+
245
+ scores = query @ passages.T
246
+ print(scores) # tensor([[0.81, 0.60, 0.79]])
247
+ ```
248
+
249
+ ---
250
+
251
  ## Limitations
252
+
253
+ - **Custom benchmark gap** — v4 scores 0.252 vs 0.661 for mpnet-base on the custom bench; the gap reflects the difference between 2.6M training pairs and web-scale data
254
+ - **Arabic and Urdu** lag behind English on the custom bench, though MIRACL scores are strong (AR 0.874, UR 0.572)
255
+ - Vocabulary limited to 50K tokens trained on ~2.6M sentences — rare scripts and dialects may tokenize poorly
256
+ - Not evaluated on MTEB full suite yet
257
+
258
+ ---
259
+
260
+ ## Citation
261
+
262
+ ```bibtex
263
+ @misc{mentee-embed-v4-2026,
264
+ title = {How Far Can Multilingual Text Embeddings Be Trained From Scratch?
265
+ A Compute-Efficient Study of Arabic, English, and Urdu},
266
+ author = {Shah, Syed Syab Ahmad and Sania, Shakeel and Hamza, Rustam and Mahboob, Iqbal},
267
+ year = {2026},
268
+ doi = {10.5281/zenodo.22117673},
269
+ url = {https://doi.org/10.5281/zenodo.22117673},
270
+ note = {MenteE AI. Apache-2.0 License}
271
+ }
272
+ ```
273
+
274
+ ---
275
+
276
+ ## About MenteE AI
277
+
278
+ Built by **Syed Syab Ahmad Shah** and Team MenteE AI.
279
+ [menteeai.org](https://menteeai.org) · syab@menteeai.org
280
+ Research paper: [10.5281/zenodo.22117673](https://doi.org/10.5281/zenodo.22117673)