Upload folder using huggingface_hub
Browse files
README.md
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
pipeline_tag: text-classification
|
4 |
+
tags:
|
5 |
+
- transformers
|
6 |
+
- sentence-transformers
|
7 |
+
language:
|
8 |
+
- multilingual
|
9 |
+
---
|
10 |
+
|
11 |
+
# Reranker
|
12 |
+
|
13 |
+
**More details please refer to our Github: [FlagEmbedding](https://github.com/FlagOpen/FlagEmbedding/tree/master).**
|
14 |
+
|
15 |
+
- [Model List](#model-list)
|
16 |
+
- [Usage](#usage)
|
17 |
+
- [Fine-tuning](#fine-tune)
|
18 |
+
- [Evaluation](#evaluation)
|
19 |
+
- [Citation](#citation)
|
20 |
+
|
21 |
+
Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding.
|
22 |
+
You can get a relevance score by inputting query and passage to the reranker.
|
23 |
+
And the score can be mapped to a float value in [0,1] by sigmoid function.
|
24 |
+
|
25 |
+
Here, we introduce a lightweight reranker **bge-reranker-v2.5-gemma2-lightweight**, which is a multilingual model trained based on gemma2-9b. By integrating token compression capabilities and layerwise reduction, the model can maintain outstanding performance while saving significant resources.
|
26 |
+
|
27 |
+
Our model primarily demonstrates the following capabilities:
|
28 |
+
|
29 |
+
- Lightweight: The model can be made lightweight through token compression, layerwise reduction, or a combination of both.
|
30 |
+
- Outstanding performance: The model has achieved new state-of-the-art (SOTA) performance on both BEIR and MIRACL.
|
31 |
+
|
32 |
+
We will release a technical report about lightweight reranker soon with more details.
|
33 |
+
|
34 |
+
|
35 |
+
## Model List
|
36 |
+
|
37 |
+
| Model | Base model | Language | layerwise | compress ratio | compress layers | feature |
|
38 |
+
|:--------------------------------------------------------------------------|:--------:|:-----------------------------------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------:|------------------------------------------------------------------------------------------------|
|
39 |
+
| [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base) | [xlm-roberta-base](https://huggingface.co/xlm-roberta-base) | Chinese and English | - | - | - | Lightweight reranker model, easy to deploy, with fast inference. |
|
40 |
+
| [BAAI/bge-reranker-large](https://huggingface.co/BAAI/bge-reranker-large) | [xlm-roberta-large](https://huggingface.co/FacebookAI/xlm-roberta-large) | Chinese and English | - | - | - | Lightweight reranker model, easy to deploy, with fast inference. |
|
41 |
+
| [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3) | [bge-m3](https://huggingface.co/BAAI/bge-m3) | Multilingual | - | - | - | Lightweight reranker model, possesses strong multilingual capabilities, easy to deploy, with fast inference. |
|
42 |
+
| [BAAI/bge-reranker-v2-gemma](https://huggingface.co/BAAI/bge-reranker-v2-gemma) | [gemma-2b](https://huggingface.co/google/gemma-2b) | Multilingual | - | - | - | Suitable for multilingual contexts, performs well in both English proficiency and multilingual capabilities. |
|
43 |
+
| [BAAI/bge-reranker-v2-minicpm-layerwise](https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise) | [MiniCPM-2B-dpo-bf16](https://huggingface.co/openbmb/MiniCPM-2B-dpo-bf16) | Multilingual | 8-40 | - | - | Suitable for multilingual contexts, performs well in both English and Chinese proficiency, allows freedom to select layers for output, facilitating accelerated inference. |
|
44 |
+
| [BAAI/bge-reranker-v2.5-gemma2-lightweight](https://huggingface.co/BAAI/bge-reranker-v2.5-gemma2-lightweight) | [google/gemma-2-9b](https://huggingface.co/google/gemma-2-9b) | Multilingual | 8-42 | 1, 2, 4, 8 | [8, 16, 24, 32, 40] | Suitable for multilingual contexts, performs well in both English and Chinese proficiency, allows freedom to select layers, compress ratio and compress layers for output, facilitating accelerated inference. |
|
45 |
+
|
46 |
+
|
47 |
+
You can select the model according your senario and resource.
|
48 |
+
- For **multilingual**, utilize [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3), [BAAI/bge-reranker-v2-gemma](https://huggingface.co/BAAI/bge-reranker-v2-gemma) and [BAAI/bge-reranker-v2.5-gemma2-lightweight](https://huggingface.co/BAAI/bge-reranker-v2.5-gemma2-lightweight)
|
49 |
+
|
50 |
+
- For **Chinese or English**, utilize [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3) and [BAAI/bge-reranker-v2-minicpm-layerwise](https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise).
|
51 |
+
|
52 |
+
- For **efficiency**, utilize [BAAI/bge-reranker-v2-m3](https://huggingface.co/BAAI/bge-reranker-v2-m3) and the low layer of [BAAI/bge-reranker-v2-minicpm-layerwise](https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise).
|
53 |
+
|
54 |
+
- For better performance, recommand [BAAI/bge-reranker-v2-minicpm-layerwise](https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise) and [BAAI/bge-reranker-v2-gemma](https://huggingface.co/BAAI/bge-reranker-v2-gemma)
|
55 |
+
|
56 |
+
## Usage
|
57 |
+
### Using FlagEmbedding
|
58 |
+
|
59 |
+
```
|
60 |
+
pip install -U FlagEmbedding
|
61 |
+
```
|
62 |
+
|
63 |
+
#### For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )
|
64 |
+
|
65 |
+
Get relevance scores (higher scores indicate more relevance):
|
66 |
+
|
67 |
+
```python
|
68 |
+
from FlagEmbedding import FlagReranker
|
69 |
+
reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
70 |
+
|
71 |
+
score = reranker.compute_score(['query', 'passage'])
|
72 |
+
print(score) # -5.65234375
|
73 |
+
|
74 |
+
# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
|
75 |
+
score = reranker.compute_score(['query', 'passage'], normalize=True)
|
76 |
+
print(score) # 0.003497010252573502
|
77 |
+
|
78 |
+
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']])
|
79 |
+
print(scores) # [-8.1875, 5.26171875]
|
80 |
+
|
81 |
+
# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
|
82 |
+
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']], normalize=True)
|
83 |
+
print(scores) # [0.00027803096387751553, 0.9948403768236574]
|
84 |
+
```
|
85 |
+
|
86 |
+
#### For LLM-based reranker
|
87 |
+
|
88 |
+
```python
|
89 |
+
from FlagEmbedding import FlagLLMReranker
|
90 |
+
reranker = FlagLLMReranker('BAAI/bge-reranker-v2-gemma', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
91 |
+
# reranker = FlagLLMReranker('BAAI/bge-reranker-v2-gemma', use_bf16=True) # You can also set use_bf16=True to speed up computation with a slight performance degradation
|
92 |
+
|
93 |
+
score = reranker.compute_score(['query', 'passage'])
|
94 |
+
print(score)
|
95 |
+
|
96 |
+
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']])
|
97 |
+
print(scores)
|
98 |
+
```
|
99 |
+
|
100 |
+
#### For LLM-based layerwise reranker
|
101 |
+
|
102 |
+
```python
|
103 |
+
from FlagEmbedding import LayerWiseFlagLLMReranker
|
104 |
+
reranker = LayerWiseFlagLLMReranker('BAAI/bge-reranker-v2-minicpm-layerwise', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
105 |
+
# reranker = LayerWiseFlagLLMReranker('BAAI/bge-reranker-v2-minicpm-layerwise', use_bf16=True) # You can also set use_bf16=True to speed up computation with a slight performance degradation
|
106 |
+
|
107 |
+
score = reranker.compute_score(['query', 'passage'], cutoff_layers=[28]) # Adjusting 'cutoff_layers' to pick which layers are used for computing the score.
|
108 |
+
print(score)
|
109 |
+
|
110 |
+
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']], cutoff_layers=[28])
|
111 |
+
print(scores)
|
112 |
+
```
|
113 |
+
|
114 |
+
#### For LLM-based lightweight reranker
|
115 |
+
|
116 |
+
```python
|
117 |
+
from FlagEmbedding import LightWeightFlagLLMReranker
|
118 |
+
reranker = LightWeightFlagLLMReranker('BAAI/bge-reranker-v2.5-gemma2-lightweight', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
|
119 |
+
|
120 |
+
score = reranker.compute_score(['query', 'passage'], cutoff_layers=[28], compress_ratio=2, compress_layer=[24, 40]) # Adjusting 'cutoff_layers' to pick which layers are used for computing the score.
|
121 |
+
print(score)
|
122 |
+
|
123 |
+
scores = reranker.compute_score([['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']], cutoff_layers=[28], compress_ratio=2, compress_layer=[24, 40])
|
124 |
+
print(scores)
|
125 |
+
```
|
126 |
+
|
127 |
+
### Using Huggingface transformers
|
128 |
+
|
129 |
+
#### For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )
|
130 |
+
|
131 |
+
Get relevance scores (higher scores indicate more relevance):
|
132 |
+
|
133 |
+
```python
|
134 |
+
import torch
|
135 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
136 |
+
|
137 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-m3')
|
138 |
+
model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-v2-m3')
|
139 |
+
model.eval()
|
140 |
+
|
141 |
+
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
|
142 |
+
with torch.no_grad():
|
143 |
+
inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512)
|
144 |
+
scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
|
145 |
+
print(scores)
|
146 |
+
```
|
147 |
+
|
148 |
+
#### For LLM-based reranker
|
149 |
+
|
150 |
+
```python
|
151 |
+
import torch
|
152 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
153 |
+
|
154 |
+
def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
|
155 |
+
if prompt is None:
|
156 |
+
prompt = "Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'."
|
157 |
+
sep = "\n"
|
158 |
+
prompt_inputs = tokenizer(prompt,
|
159 |
+
return_tensors=None,
|
160 |
+
add_special_tokens=False)['input_ids']
|
161 |
+
sep_inputs = tokenizer(sep,
|
162 |
+
return_tensors=None,
|
163 |
+
add_special_tokens=False)['input_ids']
|
164 |
+
inputs = []
|
165 |
+
for query, passage in pairs:
|
166 |
+
query_inputs = tokenizer(f'A: {query}',
|
167 |
+
return_tensors=None,
|
168 |
+
add_special_tokens=False,
|
169 |
+
max_length=max_length * 3 // 4,
|
170 |
+
truncation=True)
|
171 |
+
passage_inputs = tokenizer(f'B: {passage}',
|
172 |
+
return_tensors=None,
|
173 |
+
add_special_tokens=False,
|
174 |
+
max_length=max_length,
|
175 |
+
truncation=True)
|
176 |
+
item = tokenizer.prepare_for_model(
|
177 |
+
[tokenizer.bos_token_id] + query_inputs['input_ids'],
|
178 |
+
sep_inputs + passage_inputs['input_ids'],
|
179 |
+
truncation='only_second',
|
180 |
+
max_length=max_length,
|
181 |
+
padding=False,
|
182 |
+
return_attention_mask=False,
|
183 |
+
return_token_type_ids=False,
|
184 |
+
add_special_tokens=False
|
185 |
+
)
|
186 |
+
item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
|
187 |
+
item['attention_mask'] = [1] * len(item['input_ids'])
|
188 |
+
inputs.append(item)
|
189 |
+
return tokenizer.pad(
|
190 |
+
inputs,
|
191 |
+
padding=True,
|
192 |
+
max_length=max_length + len(sep_inputs) + len(prompt_inputs),
|
193 |
+
pad_to_multiple_of=8,
|
194 |
+
return_tensors='pt',
|
195 |
+
)
|
196 |
+
|
197 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-gemma')
|
198 |
+
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-gemma')
|
199 |
+
yes_loc = tokenizer('Yes', add_special_tokens=False)['input_ids'][0]
|
200 |
+
model.eval()
|
201 |
+
|
202 |
+
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
|
203 |
+
with torch.no_grad():
|
204 |
+
inputs = get_inputs(pairs, tokenizer)
|
205 |
+
scores = model(**inputs, return_dict=True).logits[:, -1, yes_loc].view(-1, ).float()
|
206 |
+
print(scores)
|
207 |
+
```
|
208 |
+
|
209 |
+
#### For LLM-based layerwise reranker
|
210 |
+
|
211 |
+
```python
|
212 |
+
import torch
|
213 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
214 |
+
|
215 |
+
def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
|
216 |
+
if prompt is None:
|
217 |
+
prompt = "Given a query A and a passage B, determine whether the passage contains an answer to the query by providing a prediction of either 'Yes' or 'No'."
|
218 |
+
sep = "\n"
|
219 |
+
prompt_inputs = tokenizer(prompt,
|
220 |
+
return_tensors=None,
|
221 |
+
add_special_tokens=False)['input_ids']
|
222 |
+
sep_inputs = tokenizer(sep,
|
223 |
+
return_tensors=None,
|
224 |
+
add_special_tokens=False)['input_ids']
|
225 |
+
inputs = []
|
226 |
+
for query, passage in pairs:
|
227 |
+
query_inputs = tokenizer(f'A: {query}',
|
228 |
+
return_tensors=None,
|
229 |
+
add_special_tokens=False,
|
230 |
+
max_length=max_length * 3 // 4,
|
231 |
+
truncation=True)
|
232 |
+
passage_inputs = tokenizer(f'B: {passage}',
|
233 |
+
return_tensors=None,
|
234 |
+
add_special_tokens=False,
|
235 |
+
max_length=max_length,
|
236 |
+
truncation=True)
|
237 |
+
item = tokenizer.prepare_for_model(
|
238 |
+
[tokenizer.bos_token_id] + query_inputs['input_ids'],
|
239 |
+
sep_inputs + passage_inputs['input_ids'],
|
240 |
+
truncation='only_second',
|
241 |
+
max_length=max_length,
|
242 |
+
padding=False,
|
243 |
+
return_attention_mask=False,
|
244 |
+
return_token_type_ids=False,
|
245 |
+
add_special_tokens=False
|
246 |
+
)
|
247 |
+
item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
|
248 |
+
item['attention_mask'] = [1] * len(item['input_ids'])
|
249 |
+
inputs.append(item)
|
250 |
+
return tokenizer.pad(
|
251 |
+
inputs,
|
252 |
+
padding=True,
|
253 |
+
max_length=max_length + len(sep_inputs) + len(prompt_inputs),
|
254 |
+
pad_to_multiple_of=8,
|
255 |
+
return_tensors='pt',
|
256 |
+
)
|
257 |
+
|
258 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True)
|
259 |
+
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True, torch_dtype=torch.bfloat16)
|
260 |
+
model = model.to('cuda')
|
261 |
+
model.eval()
|
262 |
+
|
263 |
+
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
|
264 |
+
with torch.no_grad():
|
265 |
+
inputs = get_inputs(pairs, tokenizer).to(model.device)
|
266 |
+
all_scores = model(**inputs, return_dict=True, cutoff_layers=[28])
|
267 |
+
all_scores = [scores[:, -1].view(-1, ).float() for scores in all_scores[0]]
|
268 |
+
print(all_scores)
|
269 |
+
```
|
270 |
+
|
271 |
+
#### For LLM-based lightweight reranker
|
272 |
+
|
273 |
+
```python
|
274 |
+
import torch
|
275 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
276 |
+
|
277 |
+
def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
|
278 |
+
if prompt is None:
|
279 |
+
prompt = "Predict whether passage B contains an answer to query A."
|
280 |
+
sep = "\n"
|
281 |
+
prompt_inputs = tokenizer(prompt,
|
282 |
+
return_tensors=None,
|
283 |
+
add_special_tokens=False)['input_ids']
|
284 |
+
sep_inputs = tokenizer(sep,
|
285 |
+
return_tensors=None,
|
286 |
+
add_special_tokens=False)['input_ids']
|
287 |
+
inputs = []
|
288 |
+
for query, passage in pairs:
|
289 |
+
query_inputs = tokenizer(f'A: {query}',
|
290 |
+
return_tensors=None,
|
291 |
+
add_special_tokens=False,
|
292 |
+
max_length=max_length * 3 // 4,
|
293 |
+
truncation=True)
|
294 |
+
passage_inputs = tokenizer(f'B: {passage}',
|
295 |
+
return_tensors=None,
|
296 |
+
add_special_tokens=False,
|
297 |
+
max_length=max_length,
|
298 |
+
truncation=True)
|
299 |
+
item = tokenizer.prepare_for_model(
|
300 |
+
[tokenizer.bos_token_id] + query_inputs['input_ids'],
|
301 |
+
sep_inputs + passage_inputs['input_ids'],
|
302 |
+
truncation='only_second',
|
303 |
+
max_length=max_length,
|
304 |
+
padding=False,
|
305 |
+
return_attention_mask=False,
|
306 |
+
return_token_type_ids=False,
|
307 |
+
add_special_tokens=False
|
308 |
+
)
|
309 |
+
item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
|
310 |
+
item['attention_mask'] = [1] * len(item['input_ids'])
|
311 |
+
inputs.append(item)
|
312 |
+
return tokenizer.pad(
|
313 |
+
inputs,
|
314 |
+
padding=True,
|
315 |
+
max_length=max_length + len(sep_inputs) + len(prompt_inputs),
|
316 |
+
pad_to_multiple_of=8,
|
317 |
+
return_tensors='pt',
|
318 |
+
)
|
319 |
+
|
320 |
+
tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2.5-gemma2-lightweight', trust_remote_code=True)
|
321 |
+
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2.5-gemma2-lightweight', trust_remote_code=True)
|
322 |
+
model = model.to('cuda')
|
323 |
+
model.eval()
|
324 |
+
|
325 |
+
pairs = [['what is panda?', 'hi'], ['what is panda?', 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.']]
|
326 |
+
with torch.no_grad():
|
327 |
+
inputs = get_inputs(pairs, tokenizer).to(model.device)
|
328 |
+
all_scores = model(**inputs, return_dict=True, cutoff_layers=[28], compress_ratio=2, compress_layer=[24, 40])
|
329 |
+
all_scores = [scores[:, -1].view(-1, ).float() for scores in all_scores[0]]
|
330 |
+
print(all_scores)
|
331 |
+
```
|
332 |
+
|
333 |
+
## Evaluation
|
334 |
+
|
335 |
+
- **BEIR:**
|
336 |
+
|
337 |
+
| BEIR | bge-large-en-v1.5 | Bge-rearanker v2 m3 | jina-reranker-v2-base-multilingual | bge-reranker-v2.5-gemma2-lightweight | bge-reranker-v2.5-gemma2-lightweight |
|
338 |
+
| :----------------: | :---------------: | :-----------------: | :--------------------------------: | :----------------------------------: | :----------------------------------: |
|
339 |
+
| **Save** **Flops** | - | - | - | 60% | 0 |
|
340 |
+
| **ArguAna** | 63.54 | 37.7 | 52.23 | 86.04 | 86.16 |
|
341 |
+
| **ClimateFEVER** | 36.49 | 37.99 | 34.65 | 48.41 | 48.48 |
|
342 |
+
| **CQA** | 42.23 | 38.24 | 40.21 | 49.18 | 48.9 |
|
343 |
+
| **DBPedia** | 44.16 | 48.15 | 49.31 | 51.98 | 52.11 |
|
344 |
+
| **FEVER** | 87.17 | 90.15 | 92.44 | 94.71 | 94.69 |
|
345 |
+
| **FiQA2018** | 44.97 | 49.32 | 45.88 | 60.48 | 60.95 |
|
346 |
+
| **HotpotQA** | 74.11 | 84.51 | 81.81 | 87.84 | 87.89 |
|
347 |
+
| **MSMARCO** | 42.48 | 47.79 | 47.83 | 47.23 | 47.26 |
|
348 |
+
| **NFCorpus** | 38.12 | 34.85 | 37.73 | 41.4 | 41.64 |
|
349 |
+
| **NQ** | 55.04 | 69.37 | 67.35 | 75.37 | 75.58 |
|
350 |
+
| **QuoraRetrieval** | 89.06 | 89.13 | 87.81 | 91.25 | 91.18 |
|
351 |
+
| **SCIDOCS** | 22.62 | 18.25 | 20.21 | 23.71 | 23.87 |
|
352 |
+
| **SciFact** | 74.64 | 73.08 | 76.93 | 80.5 | 80.38 |
|
353 |
+
| **Touche2020** | 25.08 | 35.68 | 32.45 | 30.64 | 31.09 |
|
354 |
+
| **TRECCOVID** | 74.89 | 83.39 | 80.89 | 84.26 | 84.85 |
|
355 |
+
| **Mean** | 54.31 | 55.36 | 56.52 | 63.1 | **63.67** |
|
356 |
+
|
357 |
+
| BEIR | e5-mistral-7b-instruct | Bge-rearanker v2 m3 | bge-reranker-v2.5-gemma-lightweight | bge-reranker-v2.5-gemma-lightweight |
|
358 |
+
| :----------------: | :--------------------: | :-----------------: | :---------------------------------: | :---------------------------------: |
|
359 |
+
| **Save Flops** | - | - | 60% | 0 |
|
360 |
+
| **ArguAna** | 61.8 | 79.05 | 86.02 | 86.58 |
|
361 |
+
| **ClimateFEVER** | 38.37 | 37.66 | 47.27 | 47.13 |
|
362 |
+
| **CQA** | 42.97 | 46.16 | 49.06 | 49.53 |
|
363 |
+
| **DBPedia** | 48.84 | 50.77 | 52.45 | 52.87 |
|
364 |
+
| **FEVER** | 87.82 | 91.36 | 94.85 | 95.19 |
|
365 |
+
| **FiQA2018** | 56.58 | 50.96 | 58.81 | 61.19 |
|
366 |
+
| **HotpotQA** | 75.72 | 86.99 | 88.49 | 88.82 |
|
367 |
+
| **MSMARCO** | 43.06 | 48.35 | 47.65 | 47.4 |
|
368 |
+
| **NFCorpus** | 38.58 | 39.25 | 42.28 | 42.17 |
|
369 |
+
| **NQ** | 63.56 | 73.44 | 75 | 76.28 |
|
370 |
+
| **QuoraRetrieval** | 89.59 | 90.44 | 91.09 | 91.18 |
|
371 |
+
| **SCIDOCS** | 16.3 | 20.77 | 22.2 | 22.69 |
|
372 |
+
| **SciFact** | 76.26 | 77.78 | 79.94 | 80.98 |
|
373 |
+
| **Touche2020** | 26.24 | 35.79 | 28.69 | 31.17 |
|
374 |
+
| **TRECCOVID** | 87.07 | 88.13 | 86.61 | 87.36 |
|
375 |
+
| **Mean** | 56.85 | 61.13 | 63.36 | **64.04** |
|
376 |
+
|
377 |
+
- **MIRACL**:
|
378 |
+
|
379 |
+
| MIRACL (dev, nDCG@10) | Average (18) | save flops | ar | bn | en | es | fa | fi | fr | hi | id | ja | ko | ru | sw | te | th | zh | de | yo |
|
380 |
+
| :--------------------------------------: | :----------: | :--------: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: |
|
381 |
+
| **bge-m3 (Dense)** | 69.2 | - | 78.4 | 80.0 | 56.9 | 56.1 | 60.9 | 78.6 | 58.3 | 59.5 | 56.1 | 72.8 | 69.9 | 70.1 | 78.7 | 86.2 | 82.6 | 62.7 | 56.7 | 81.8 |
|
382 |
+
| **jina-reranker-v2-base-multilingual** | 69.6 | - | 73.4 | 81.9 | 58.9 | 58.6 | 60.5 | 77.2 | 56.1 | 62.7 | 59.6 | 72.7 | 74.0 | 67.1 | 78.1 | 85.8 | 81.2 | 63.0 | 58.2 | 84.2 |
|
383 |
+
| **bge-reranker-v2-m3** | 74.4 | - | 81.7 | 84.6 | 63.5 | 64.4 | 65.7 | 82.4 | 63.7 | 68.5 | 62.7 | 80.0 | 73.8 | 76.9 | 82.3 | 89.4 | 85.3 | 65.2 | 62.7 | 87.4 |
|
384 |
+
| **bge-reranker-v2-gemma** | 75.0 | - | 82.3 | 85.0 | 66.6 | 65.3 | 65.5 | 82.6 | 65.4 | 69.4 | 61.2 | 79.7 | 75.1 | 78.3 | 81.8 | 89.6 | 86.1 | 66.8 | 64.0 | 85.9 |
|
385 |
+
| **bge-reranker-v2.5-gemma2-lightweight** | 77.1 | 60% | 82.5 | 87.8 | 68.6 | 67.6 | 67.5 | 82.8 | 68.5 | 71.4 | 63.8 | 82.8 | 75.9 | 79.8 | 84.8 | 90.8 | 88.1 | 69.9 | 65.8 | 89.6 |
|
386 |
+
| **bge-reranker-v2.5-gemma-lightweight** | **77.3** | 0 | 82.8 | 87.6 | 69.3 | 67.8 | 67.4 | 83.3 | 68.5 | 71.3 | 63.8 | 83.6 | 75.7 | 80.1 | 85.1 | 90.8 | 88.7 | 69.9 | 65.6 | 89.8 |
|
387 |
+
|
388 |
+
|
389 |
+
|
390 |
+
## Citation
|
391 |
+
|
392 |
+
If you find this repository useful, please consider giving a star and citation
|
393 |
+
|
394 |
+
```bibtex
|
395 |
+
@misc{li2023making,
|
396 |
+
title={Making Large Language Models A Better Foundation For Dense Retrieval},
|
397 |
+
author={Chaofan Li and Zheng Liu and Shitao Xiao and Yingxia Shao},
|
398 |
+
year={2023},
|
399 |
+
eprint={2312.15503},
|
400 |
+
archivePrefix={arXiv},
|
401 |
+
primaryClass={cs.CL}
|
402 |
+
}
|
403 |
+
@misc{chen2024bge,
|
404 |
+
title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
|
405 |
+
author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
|
406 |
+
year={2024},
|
407 |
+
eprint={2402.03216},
|
408 |
+
archivePrefix={arXiv},
|
409 |
+
primaryClass={cs.CL}
|
410 |
+
}
|
411 |
+
```
|