Yinlin Li commited on
Commit
5acb8bc
·
0 Parent(s):

feat: first commit

Browse files

Signed-off-by: Yinlin Li <yinlin@seal.io>

.gitattributes ADDED
@@ -0,0 +1 @@
 
 
1
+ *.gguf filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,308 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: sentence-similarity
3
+ tags:
4
+ - sentence-transformers
5
+ - feature-extraction
6
+ - sentence-similarity
7
+ license: mit
8
+ ---
9
+
10
+ # bge-m3-GGUF
11
+
12
+ **Model creator**: [BAAI](https://huggingface.co/BAAI)<br/>
13
+ **Original model**: [bge-m3](https://huggingface.co/BAAI/bge-m3)<br/>
14
+ **GGUF quantization**: based on llama.cpp release [61408e7f](https://github.com/ggerganov/llama.cpp/commit/61408e7fad082dc44a11c8a9f1398da4837aad44)
15
+
16
+ ---
17
+
18
+ For more details please refer to our github repo: https://github.com/FlagOpen/FlagEmbedding
19
+
20
+ # BGE-M3 ([paper](https://arxiv.org/pdf/2402.03216.pdf), [code](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/BGE_M3))
21
+
22
+ In this project, we introduce BGE-M3, which is distinguished for its versatility in Multi-Functionality, Multi-Linguality, and Multi-Granularity.
23
+ - Multi-Functionality: It can simultaneously perform the three common retrieval functionalities of embedding model: dense retrieval, multi-vector retrieval, and sparse retrieval.
24
+ - Multi-Linguality: It can support more than 100 working languages.
25
+ - Multi-Granularity: It is able to process inputs of different granularities, spanning from short sentences to long documents of up to 8192 tokens.
26
+
27
+
28
+
29
+ **Some suggestions for retrieval pipeline in RAG**
30
+
31
+ We recommend to use the following pipeline: hybrid retrieval + re-ranking.
32
+ - Hybrid retrieval leverages the strengths of various methods, offering higher accuracy and stronger generalization capabilities.
33
+ A classic example: using both embedding retrieval and the BM25 algorithm.
34
+ Now, you can try to use BGE-M3, which supports both embedding and sparse retrieval.
35
+ This allows you to obtain token weights (similar to the BM25) without any additional cost when generate dense embeddings.
36
+ To use hybrid retrieval, you can refer to [Vespa](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb
37
+ ) and [Milvus](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
38
+
39
+ - As cross-encoder models, re-ranker demonstrates higher accuracy than bi-encoder embedding model.
40
+ Utilizing the re-ranking model (e.g., [bge-reranker](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/reranker), [bge-reranker-v2](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/llm_reranker)) after retrieval can further filter the selected text.
41
+
42
+
43
+ ## News:
44
+ - 2024/7/1: **We update the MIRACL evaluation results of BGE-M3**. To reproduce the new results, you can refer to: [bge-m3_miracl_2cr](https://huggingface.co/datasets/hanhainebula/bge-m3_miracl_2cr). We have also updated our [paper](https://arxiv.org/pdf/2402.03216) on arXiv.
45
+ <details>
46
+ <summary> Details </summary>
47
+
48
+ The previous test results were lower because we mistakenly removed the passages that have the same id as the query from the search results. After correcting this mistake, the overall performance of BGE-M3 on MIRACL is higher than the previous results, but the experimental conclusion remains unchanged. The other results are not affected by this mistake. To reproduce the previous lower results, you need to add the `--remove-query` parameter when using `pyserini.search.faiss` or `pyserini.search.lucene` to search the passages.
49
+
50
+ </details>
51
+ - 2024/3/20: **Thanks Milvus team!** Now you can use hybrid retrieval of bge-m3 in Milvus: [pymilvus/examples
52
+ /hello_hybrid_sparse_dense.py](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
53
+ - 2024/3/8: **Thanks for the [experimental results](https://towardsdatascience.com/openai-vs-open-source-multilingual-embedding-models-e5ccb7c90f05) from @[Yannael](https://huggingface.co/Yannael). In this benchmark, BGE-M3 achieves top performance in both English and other languages, surpassing models such as OpenAI.**
54
+ - 2024/3/2: Release unified fine-tuning [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/unified_finetune) and [data](https://huggingface.co/datasets/Shitao/bge-m3-data)
55
+ - 2024/2/6: We release the [MLDR](https://huggingface.co/datasets/Shitao/MLDR) (a long document retrieval dataset covering 13 languages) and [evaluation pipeline](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR).
56
+ - 2024/2/1: **Thanks for the excellent tool from Vespa.** You can easily use multiple modes of BGE-M3 following this [notebook](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb)
57
+
58
+
59
+ ## Specs
60
+
61
+ - Model
62
+
63
+ | Model Name | Dimension | Sequence Length | Introduction |
64
+ |:----:|:---:|:---:|:---:|
65
+ | [BAAI/bge-m3](https://huggingface.co/BAAI/bge-m3) | 1024 | 8192 | multilingual; unified fine-tuning (dense, sparse, and colbert) from bge-m3-unsupervised|
66
+ | [BAAI/bge-m3-unsupervised](https://huggingface.co/BAAI/bge-m3-unsupervised) | 1024 | 8192 | multilingual; contrastive learning from bge-m3-retromae |
67
+ | [BAAI/bge-m3-retromae](https://huggingface.co/BAAI/bge-m3-retromae) | -- | 8192 | multilingual; extend the max_length of [xlm-roberta](https://huggingface.co/FacebookAI/xlm-roberta-large) to 8192 and further pretrained via [retromae](https://github.com/staoxiao/RetroMAE)|
68
+ | [BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5) | 1024 | 512 | English model |
69
+ | [BAAI/bge-base-en-v1.5](https://huggingface.co/BAAI/bge-base-en-v1.5) | 768 | 512 | English model |
70
+ | [BAAI/bge-small-en-v1.5](https://huggingface.co/BAAI/bge-small-en-v1.5) | 384 | 512 | English model |
71
+
72
+ - Data
73
+
74
+ | Dataset | Introduction |
75
+ |:----------------------------------------------------------:|:-------------------------------------------------:|
76
+ | [MLDR](https://huggingface.co/datasets/Shitao/MLDR) | Docuemtn Retrieval Dataset, covering 13 languages |
77
+ | [bge-m3-data](https://huggingface.co/datasets/Shitao/bge-m3-data) | Fine-tuning data used by bge-m3 |
78
+
79
+
80
+
81
+ ## FAQ
82
+
83
+ **1. Introduction for different retrieval methods**
84
+
85
+ - Dense retrieval: map the text into a single embedding, e.g., [DPR](https://arxiv.org/abs/2004.04906), [BGE-v1.5](https://github.com/FlagOpen/FlagEmbedding)
86
+ - Sparse retrieval (lexical matching): a vector of size equal to the vocabulary, with the majority of positions set to zero, calculating a weight only for tokens present in the text. e.g., BM25, [unicoil](https://arxiv.org/pdf/2106.14807.pdf), and [splade](https://arxiv.org/abs/2107.05720)
87
+ - Multi-vector retrieval: use multiple vectors to represent a text, e.g., [ColBERT](https://arxiv.org/abs/2004.12832).
88
+
89
+
90
+ **2. How to use BGE-M3 in other projects?**
91
+
92
+ For embedding retrieval, you can employ the BGE-M3 model using the same approach as BGE.
93
+ The only difference is that the BGE-M3 model no longer requires adding instructions to the queries.
94
+
95
+ For hybrid retrieval, you can use [Vespa](https://github.com/vespa-engine/pyvespa/blob/master/docs/sphinx/source/examples/mother-of-all-embedding-models-cloud.ipynb
96
+ ) and [Milvus](https://github.com/milvus-io/pymilvus/blob/master/examples/hello_hybrid_sparse_dense.py).
97
+
98
+
99
+ **3. How to fine-tune bge-M3 model?**
100
+
101
+ You can follow the common in this [example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/finetune)
102
+ to fine-tune the dense embedding.
103
+
104
+ If you want to fine-tune all embedding function of m3 (dense, sparse and colbert), you can refer to the [unified_fine-tuning example](https://github.com/FlagOpen/FlagEmbedding/tree/master/examples/unified_finetune)
105
+
106
+
107
+
108
+
109
+
110
+
111
+ ## Usage
112
+
113
+ Install:
114
+ ```
115
+ git clone https://github.com/FlagOpen/FlagEmbedding.git
116
+ cd FlagEmbedding
117
+ pip install -e .
118
+ ```
119
+ or:
120
+ ```
121
+ pip install -U FlagEmbedding
122
+ ```
123
+
124
+
125
+
126
+ ### Generate Embedding for text
127
+
128
+ - Dense Embedding
129
+ ```python
130
+ from FlagEmbedding import BGEM3FlagModel
131
+
132
+ model = BGEM3FlagModel('BAAI/bge-m3',
133
+ use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
134
+
135
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
136
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
137
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
138
+
139
+ embeddings_1 = model.encode(sentences_1,
140
+ batch_size=12,
141
+ max_length=8192, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
142
+ )['dense_vecs']
143
+ embeddings_2 = model.encode(sentences_2)['dense_vecs']
144
+ similarity = embeddings_1 @ embeddings_2.T
145
+ print(similarity)
146
+ # [[0.6265, 0.3477], [0.3499, 0.678 ]]
147
+ ```
148
+ You also can use sentence-transformers and huggingface transformers to generate dense embeddings.
149
+ Refer to [baai_general_embedding](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/baai_general_embedding#usage) for details.
150
+
151
+
152
+ - Sparse Embedding (Lexical Weight)
153
+ ```python
154
+ from FlagEmbedding import BGEM3FlagModel
155
+
156
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
157
+
158
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
159
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
160
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
161
+
162
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=False)
163
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=False)
164
+
165
+ # you can see the weight for each token:
166
+ print(model.convert_id_to_token(output_1['lexical_weights']))
167
+ # [{'What': 0.08356, 'is': 0.0814, 'B': 0.1296, 'GE': 0.252, 'M': 0.1702, '3': 0.2695, '?': 0.04092},
168
+ # {'De': 0.05005, 'fin': 0.1368, 'ation': 0.04498, 'of': 0.0633, 'BM': 0.2515, '25': 0.3335}]
169
+
170
+
171
+ # compute the scores via lexical mathcing
172
+ lexical_scores = model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_2['lexical_weights'][0])
173
+ print(lexical_scores)
174
+ # 0.19554901123046875
175
+
176
+ print(model.compute_lexical_matching_score(output_1['lexical_weights'][0], output_1['lexical_weights'][1]))
177
+ # 0.0
178
+ ```
179
+
180
+ - Multi-Vector (ColBERT)
181
+ ```python
182
+ from FlagEmbedding import BGEM3FlagModel
183
+
184
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
185
+
186
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
187
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
188
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
189
+
190
+ output_1 = model.encode(sentences_1, return_dense=True, return_sparse=True, return_colbert_vecs=True)
191
+ output_2 = model.encode(sentences_2, return_dense=True, return_sparse=True, return_colbert_vecs=True)
192
+
193
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][0]))
194
+ print(model.colbert_score(output_1['colbert_vecs'][0], output_2['colbert_vecs'][1]))
195
+ # 0.7797
196
+ # 0.4620
197
+ ```
198
+
199
+
200
+ ### Compute score for text pairs
201
+ Input a list of text pairs, you can get the scores computed by different methods.
202
+ ```python
203
+ from FlagEmbedding import BGEM3FlagModel
204
+
205
+ model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True)
206
+
207
+ sentences_1 = ["What is BGE M3?", "Defination of BM25"]
208
+ sentences_2 = ["BGE M3 is an embedding model supporting dense retrieval, lexical matching and multi-vector interaction.",
209
+ "BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document"]
210
+
211
+ sentence_pairs = [[i,j] for i in sentences_1 for j in sentences_2]
212
+
213
+ print(model.compute_score(sentence_pairs,
214
+ max_passage_length=128, # a smaller max length leads to a lower latency
215
+ weights_for_different_modes=[0.4, 0.2, 0.4])) # weights_for_different_modes(w) is used to do weighted sum: w[0]*dense_score + w[1]*sparse_score + w[2]*colbert_score
216
+
217
+ # {
218
+ # 'colbert': [0.7796499729156494, 0.4621465802192688, 0.4523794651031494, 0.7898575067520142],
219
+ # 'sparse': [0.195556640625, 0.00879669189453125, 0.0, 0.1802978515625],
220
+ # 'dense': [0.6259765625, 0.347412109375, 0.349853515625, 0.67822265625],
221
+ # 'sparse+dense': [0.482503205537796, 0.23454029858112335, 0.2332356721162796, 0.5122477412223816],
222
+ # 'colbert+sparse+dense': [0.6013619303703308, 0.3255828022956848, 0.32089319825172424, 0.6232916116714478]
223
+ # }
224
+ ```
225
+
226
+
227
+
228
+
229
+ ## Evaluation
230
+
231
+ We provide the evaluation script for [MKQA](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MKQA) and [MLDR](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR)
232
+
233
+ ### Benchmarks from the open-source community
234
+ ![avatar](./imgs/others.webp)
235
+ The BGE-M3 model emerged as the top performer on this benchmark (OAI is short for OpenAI).
236
+ For more details, please refer to the [article](https://towardsdatascience.com/openai-vs-open-source-multilingual-embedding-models-e5ccb7c90f05) and [Github Repo](https://github.com/Yannael/multilingual-embeddings)
237
+
238
+
239
+ ### Our results
240
+ - Multilingual (Miracl dataset)
241
+
242
+ ![avatar](./imgs/miracl.jpg)
243
+
244
+ - Cross-lingual (MKQA dataset)
245
+
246
+ ![avatar](./imgs/mkqa.jpg)
247
+
248
+ - Long Document Retrieval
249
+ - MLDR:
250
+ ![avatar](./imgs/long.jpg)
251
+ Please note that [MLDR](https://huggingface.co/datasets/Shitao/MLDR) is a document retrieval dataset we constructed via LLM,
252
+ covering 13 languages, including test set, validation set, and training set.
253
+ We utilized the training set from MLDR to enhance the model's long document retrieval capabilities.
254
+ Therefore, comparing baselines with `Dense w.o.long`(fine-tuning without long document dataset) is more equitable.
255
+ Additionally, this long document retrieval dataset will be open-sourced to address the current lack of open-source multilingual long text retrieval datasets.
256
+ We believe that this data will be helpful for the open-source community in training document retrieval models.
257
+
258
+ - NarritiveQA:
259
+ ![avatar](./imgs/nqa.jpg)
260
+
261
+ - Comparison with BM25
262
+
263
+ We utilized Pyserini to implement BM25, and the test results can be reproduced by this [script](https://github.com/FlagOpen/FlagEmbedding/tree/master/C_MTEB/MLDR#bm25-baseline).
264
+ We tested BM25 using two different tokenizers:
265
+ one using Lucene Analyzer and the other using the same tokenizer as M3 (i.e., the tokenizer of xlm-roberta).
266
+ The results indicate that BM25 remains a competitive baseline,
267
+ especially in long document retrieval.
268
+
269
+ ![avatar](./imgs/bm25.jpg)
270
+
271
+
272
+
273
+ ## Training
274
+ - Self-knowledge Distillation: combining multiple outputs from different
275
+ retrieval modes as reward signal to enhance the performance of single mode(especially for sparse retrieval and multi-vec(colbert) retrival)
276
+ - Efficient Batching: Improve the efficiency when fine-tuning on long text.
277
+ The small-batch strategy is simple but effective, which also can used to fine-tune large embedding model.
278
+ - MCLS: A simple method to improve the performance on long text without fine-tuning.
279
+ If you have no enough resource to fine-tuning model with long text, the method is useful.
280
+
281
+ Refer to our [report](https://arxiv.org/pdf/2402.03216.pdf) for more details.
282
+
283
+
284
+
285
+
286
+
287
+
288
+ ## Acknowledgement
289
+
290
+ Thanks to the authors of open-sourced datasets, including Miracl, MKQA, NarritiveQA, etc.
291
+ Thanks to the open-sourced libraries like [Tevatron](https://github.com/texttron/tevatron), [Pyserini](https://github.com/castorini/pyserini).
292
+
293
+
294
+
295
+ ## Citation
296
+
297
+ If you find this repository useful, please consider giving a star :star: and citation
298
+
299
+ ```
300
+ @misc{bge-m3,
301
+ title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
302
+ author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
303
+ year={2024},
304
+ eprint={2402.03216},
305
+ archivePrefix={arXiv},
306
+ primaryClass={cs.CL}
307
+ }
308
+ ```
bge-m3-FP16.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:daec91ffb5dd0c27411bd71f29932917c49cf529a641d0168496c3a501e3062c
3
+ size 1157671200
bge-m3-Q2_K.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8b0dacc25f20c7375700e7143e30ce48a8fbe44e66b035de243113b9cc5d44d8
3
+ size 366114880
bge-m3-Q3_K.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c4e95d4c9ad1680bdd44735e36e10b68f71a9470572fe3b1b5d9fc147299b3e1
3
+ size 402290752
bge-m3-Q4_0.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6eaafd7b20eecbcf9d239c670b8a06a0c23ce3ef5b3bbf37ec02db327203ac02
3
+ size 421558336
bge-m3-Q4_K_M.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d39681b26c61279ac1f82db35a04a05009e94c415b51c858ff571489a82fc06
3
+ size 437778496
bge-m3-Q5_0.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a2b6d898a86d9c702b97087806e6a3371b154d915bcbbb0d2ba47b00ca734fe2
3
+ size 459307072
bge-m3-Q5_K_M.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f93897db57c4385f1cde3f59234bececa234c0f9bafc646dfdaeebe7f65ea84d
3
+ size 467662912
bge-m3-Q6_K.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d7b2ddf8880de940dbdf484c2d1c0b2db5b68f0c342634819cdb3d32e5703c0a
3
+ size 499415104
bge-m3-Q8_0.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:950f4a8e5e19477a6d3c26d2f162233c20002c601f75e4b002e3239997821167
3
+ size 634553760
imgs/bm25.jpg ADDED
imgs/long.jpg ADDED
imgs/miracl.jpg ADDED
imgs/mkqa.jpg ADDED
imgs/nqa.jpg ADDED
imgs/others.webp ADDED