Views
No views yet
| Model | Base model | Language | layerwise | compress ratio | compress layers | feature |
|---|---|---|---|---|---|---|
| BAAI/bge-reranker-base | xlm-roberta-base | Chinese and English | - | - | - | Lightweight reranker model, easy to deploy, with fast inference. |
| BAAI/bge-reranker-large | xlm-roberta-large | Chinese and English | - | - | - | Lightweight reranker model, easy to deploy, with fast inference. |
| BAAI/bge-reranker-v2-m3 | bge-m3 | Multilingual | - | - | - | Lightweight reranker model, possesses strong multilingual capabilities, easy to deploy, with fast inference. |
| BAAI/bge-reranker-v2-gemma | gemma-2b | Multilingual | - | - | - | Suitable for multilingual contexts, performs well in both English proficiency and multilingual capabilities. |
| BAAI/bge-reranker-v2-minicpm-layerwise | 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. |
| BAAI/bge-reranker-v2.5-gemma2-lightweight | 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. |
git clone https://github.com/FlagOpen/FlagEmbedding.git
cd FlagEmbedding
pip install -e .1from FlagEmbedding import LightWeightFlagLLMReranker
2reranker = LightWeightFlagLLMReranker('BAAI/bge-reranker-v2.5-gemma2-lightweight', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
3
4score = 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.
5print(score)
6
7scores = 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])
8print(scores)1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4def last_logit_pool(logits: torch.Tensor,
5 attention_mask: torch.Tensor) -> torch.Tensor:
6 left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
7 if left_padding:
8 return logits[:, -1]
9 else:
10 sequence_lengths = attention_mask.sum(dim=1) - 1
11 batch_size = logits.shape[0]
12 return torch.stack([logits[i, sequence_lengths[i]] for i in range(batch_size)], dim=0)
13
14def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
15 if prompt is None:
16 prompt = "Predict whether passage B contains an answer to query A."
17 sep = "\n"
18 prompt_inputs = tokenizer(prompt,
19 return_tensors=None,
20 add_special_tokens=False)['input_ids']
21 sep_inputs = tokenizer(sep,
22 return_tensors=None,
23 add_special_tokens=False)['input_ids']
24 inputs = []
25 query_lengths = []
26 prompt_lengths = []
27 for query, passage in pairs:
28 query_inputs = tokenizer(f'A: {query}',
29 return_tensors=None,
30 add_special_tokens=False,
31 max_length=max_length * 3 // 4,
32 truncation=True)
33 passage_inputs = tokenizer(f'B: {passage}',
34 return_tensors=None,
35 add_special_tokens=False,
36 max_length=max_length,
37 truncation=True)
38 item = tokenizer.prepare_for_model(
39 [tokenizer.bos_token_id] + query_inputs['input_ids'],
40 sep_inputs + passage_inputs['input_ids'],
41 truncation='only_second',
42 max_length=max_length,
43 padding=False,
44 return_attention_mask=False,
45 return_token_type_ids=False,
46 add_special_tokens=False
47 )
48 item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
49 item['attention_mask'] = [1] * len(item['input_ids'])
50 inputs.append(item)
51 query_lengths.append(len([tokenizer.bos_token_id] + query_inputs['input_ids'] + sep_inputs))
52 prompt_lengths.append(len(sep_inputs + prompt_inputs))
53
54 return tokenizer.pad(
55 inputs,
56 padding=True,
57 max_length=max_length + len(sep_inputs) + len(prompt_inputs),
58 pad_to_multiple_of=8,
59 return_tensors='pt',
60 ), query_lengths, prompt_lengths
61
62tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2.5-gemma2-lightweight', trust_remote_code=True)
63tokenizer.padding_side = 'right'
64model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2.5-gemma2-lightweight', trust_remote_code=True)
65model = model.to('cuda')
66model.eval()
67
68pairs = [['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.']]
69with torch.no_grad():
70 inputs, query_lengths, prompt_lengths = get_inputs(pairs, tokenizer)
71 inputs = inputs.to(model.device)
72 outputs = model(**inputs,
73 return_dict=True,
74 cutoff_layers=[28],
75 compress_ratio=2,
76 compress_layer=[24, 40],
77 query_lengths=query_lengths,
78 prompt_lengths=prompt_lengths)
79 scores = []
80 for i in range(len(outputs.logits)):
81 logits = last_logit_pool(outputs.logits[i], outputs.attention_masks[i])
82 scores.append(logits.cpu().float().tolist())
83 print(scores)gemma_config.py and gemma_model.py from BAAI/bge-reranker-v2.5-gemma2-lightweight in your local path."auto_map": {
"AutoConfig": "gemma_config.CostWiseGemmaConfig",
"AutoModel": "gemma_model.CostWiseGemmaModel",
"AutoModelForCausalLM": "gemma_model.CostWiseGemmaForCausalLM"
},compress_ratios=2, compress_layer=[8], cutoff_layers=[25].| BEIR | bge-large-en-v1.5 | Bge-rearanker v2 m3 | jina-reranker-v2-base-multilingual | bge-reranker-v2-gemma | bge-reranker-v2.5-gemma2-lightweight | bge-reranker-v2.5-gemma2-lightweight |
|---|---|---|---|---|---|---|
| Save Flops | - | - | - | - | 60% | 0 |
| ArguAna | 63.54 | 37.7 | 52.23 | 78.68 | 86.04 | 86.16 |
| ClimateFEVER | 36.49 | 37.99 | 34.65 | 39.07 | 48.41 | 48.48 |
| CQA | 42.23 | 38.24 | 40.21 | 45.85 | 49.18 | 48.9 |
| DBPedia | 44.16 | 48.15 | 49.31 | 49.92 | 51.98 | 52.11 |
| FEVER | 87.17 | 90.15 | 92.44 | 90.15 | 94.71 | 94.69 |
| FiQA2018 | 44.97 | 49.32 | 45.88 | 49.32 | 60.48 | 60.95 |
| HotpotQA | 74.11 | 84.51 | 81.81 | 86.15 | 87.84 | 87.89 |
| MSMARCO | 42.48 | 47.79 | 47.83 | 48.07 | 47.23 | 47.26 |
| NFCorpus | 38.12 | 34.85 | 37.73 | 39.73 | 41.4 | 41.64 |
| NQ | 55.04 | 69.37 | 67.35 | 72.6 | 75.37 | 75.58 |
| QuoraRetrieval | 89.06 | 89.13 | 87.81 | 90.37 | 91.25 | 91.18 |
| SCIDOCS | 22.62 | 18.25 | 20.21 | 21.65 | 23.71 | 23.87 |
| SciFact | 74.64 | 73.08 | 76.93 | 77.22 | 80.5 | 80.38 |
| Touche2020 | 25.08 | 35.68 | 32.45 | 35.68 | 30.64 | 31.09 |
| TRECCOVID | 74.89 | 83.39 | 80.89 | 85.51 | 84.26 | 84.85 |
| Mean | 54.31 | 55.36 | 56.52 | 60.71 | 63.1 | 63.67 |
| BEIR | e5-mistral-7b-instruct | bge-reranker-v2-gemma | bge-reranker-v2.5-gemma-lightweight | bge-reranker-v2.5-gemma-lightweight |
|---|---|---|---|---|
| Save Flops | - | - | 60% | 0 |
| ArguAna | 61.8 | 79.05 | 86.02 | 86.58 |
| ClimateFEVER | 38.37 | 37.66 | 47.27 | 47.13 |
| CQA | 42.97 | 46.16 | 49.06 | 49.53 |
| DBPedia | 48.84 | 50.77 | 52.45 | 52.87 |
| FEVER | 87.82 | 91.36 | 94.85 | 95.19 |
| FiQA2018 | 56.58 | 50.96 | 58.81 | 61.19 |
| HotpotQA | 75.72 | 86.99 | 88.49 | 88.82 |
| MSMARCO | 43.06 | 48.35 | 47.65 | 47.4 |
| NFCorpus | 38.58 | 39.25 | 42.28 | 42.17 |
| NQ | 63.56 | 73.44 | 75 | 76.28 |
| QuoraRetrieval | 89.59 | 90.44 | 91.09 | 91.18 |
| SCIDOCS | 16.3 | 20.77 | 22.2 | 22.69 |
| SciFact | 76.26 | 77.78 | 79.94 | 80.98 |
| Touche2020 | 26.24 | 35.79 | 28.69 | 31.17 |
| TRECCOVID | 87.07 | 88.13 | 86.61 | 87.36 |
| Mean | 56.85 | 61.13 | 63.36 | 64.04 |
| 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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
1@misc{li2023making,
2 title={Making Large Language Models A Better Foundation For Dense Retrieval},
3 author={Chaofan Li and Zheng Liu and Shitao Xiao and Yingxia Shao},
4 year={2023},
5 eprint={2312.15503},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}
9@misc{chen2024bge,
10 title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation},
11 author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
12 year={2024},
13 eprint={2402.03216},
14 archivePrefix={arXiv},
15 primaryClass={cs.CL}
16}