BAAI / bge-reranker-v2-minicpm-layerwise

huggingface.co
Total runs: 6.4K
24-hour runs: 0
7-day runs: -1.3K
30-day runs: -2.8K
Model's Last Updated: March 19 2024
text-classification

Introduction of bge-reranker-v2-minicpm-layerwise

Model Details of bge-reranker-v2-minicpm-layerwise

Reranker

More details please refer to our Github: FlagEmbedding .

Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding. You can get a relevance score by inputting query and passage to the reranker. And the score can be mapped to a float value in [0,1] by sigmoid function.

Model List
Model Base model Language layerwise 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.

You can select the model according your senario and resource.

Usage
Using FlagEmbedding
pip install -U FlagEmbedding
For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )

Get relevance scores (higher scores indicate more relevance):

from FlagEmbedding import FlagReranker
reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation

score = reranker.compute_score(['query', 'passage'])
print(score) # -5.65234375

# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
score = reranker.compute_score(['query', 'passage'], normalize=True)
print(score) # 0.003497010252573502

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.']])
print(scores) # [-8.1875, 5.26171875]

# You can map the scores into 0-1 by set "normalize=True", which will apply sigmoid function to the score
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)
print(scores) # [0.00027803096387751553, 0.9948403768236574]
For LLM-based reranker
from FlagEmbedding import FlagLLMReranker
reranker = FlagLLMReranker('BAAI/bge-reranker-v2-gemma', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
# 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

score = reranker.compute_score(['query', 'passage'])
print(score)

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.']])
print(scores)
For LLM-based layerwise reranker
from FlagEmbedding import LayerWiseFlagLLMReranker
reranker = LayerWiseFlagLLMReranker('BAAI/bge-reranker-v2-minicpm-layerwise', use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
# 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

score = reranker.compute_score(['query', 'passage'], cutoff_layers=[28]) # Adjusting 'cutoff_layers' to pick which layers are used for computing the score.
print(score)

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])
print(scores)
Using Huggingface transformers
For normal reranker (bge-reranker-base / bge-reranker-large / bge-reranker-v2-m3 )

Get relevance scores (higher scores indicate more relevance):

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-m3')
model = AutoModelForSequenceClassification.from_pretrained('BAAI/bge-reranker-v2-m3')
model.eval()

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.']]
with torch.no_grad():
    inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors='pt', max_length=512)
    scores = model(**inputs, return_dict=True).logits.view(-1, ).float()
    print(scores)
For LLM-based reranker
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
    if prompt is None:
        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'."
    sep = "\n"
    prompt_inputs = tokenizer(prompt,
                              return_tensors=None,
                              add_special_tokens=False)['input_ids']
    sep_inputs = tokenizer(sep,
                           return_tensors=None,
                           add_special_tokens=False)['input_ids']
    inputs = []
    for query, passage in pairs:
        query_inputs = tokenizer(f'A: {query}',
                                 return_tensors=None,
                                 add_special_tokens=False,
                                 max_length=max_length * 3 // 4,
                                 truncation=True)
        passage_inputs = tokenizer(f'B: {passage}',
                                   return_tensors=None,
                                   add_special_tokens=False,
                                   max_length=max_length,
                                   truncation=True)
        item = tokenizer.prepare_for_model(
            [tokenizer.bos_token_id] + query_inputs['input_ids'],
            sep_inputs + passage_inputs['input_ids'],
            truncation='only_second',
            max_length=max_length,
            padding=False,
            return_attention_mask=False,
            return_token_type_ids=False,
            add_special_tokens=False
        )
        item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
        item['attention_mask'] = [1] * len(item['input_ids'])
        inputs.append(item)
    return tokenizer.pad(
            inputs,
            padding=True,
            max_length=max_length + len(sep_inputs) + len(prompt_inputs),
            pad_to_multiple_of=8,
            return_tensors='pt',
    )

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-gemma')
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-gemma')
yes_loc = tokenizer('Yes', add_special_tokens=False)['input_ids'][0]
model.eval()

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.']]
with torch.no_grad():
    inputs = get_inputs(pairs, tokenizer)
    scores = model(**inputs, return_dict=True).logits[:, -1, yes_loc].view(-1, ).float()
    print(scores)
For LLM-based layerwise reranker
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

def get_inputs(pairs, tokenizer, prompt=None, max_length=1024):
    if prompt is None:
        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'."
    sep = "\n"
    prompt_inputs = tokenizer(prompt,
                              return_tensors=None,
                              add_special_tokens=False)['input_ids']
    sep_inputs = tokenizer(sep,
                           return_tensors=None,
                           add_special_tokens=False)['input_ids']
    inputs = []
    for query, passage in pairs:
        query_inputs = tokenizer(f'A: {query}',
                                 return_tensors=None,
                                 add_special_tokens=False,
                                 max_length=max_length * 3 // 4,
                                 truncation=True)
        passage_inputs = tokenizer(f'B: {passage}',
                                   return_tensors=None,
                                   add_special_tokens=False,
                                   max_length=max_length,
                                   truncation=True)
        item = tokenizer.prepare_for_model(
            [tokenizer.bos_token_id] + query_inputs['input_ids'],
            sep_inputs + passage_inputs['input_ids'],
            truncation='only_second',
            max_length=max_length,
            padding=False,
            return_attention_mask=False,
            return_token_type_ids=False,
            add_special_tokens=False
        )
        item['input_ids'] = item['input_ids'] + sep_inputs + prompt_inputs
        item['attention_mask'] = [1] * len(item['input_ids'])
        inputs.append(item)
    return tokenizer.pad(
            inputs,
            padding=True,
            max_length=max_length + len(sep_inputs) + len(prompt_inputs),
            pad_to_multiple_of=8,
            return_tensors='pt',
    )

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('BAAI/bge-reranker-v2-minicpm-layerwise', trust_remote_code=True, torch_dtype=torch.bfloat16)
model = model.to('cuda')
model.eval()

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.']]
with torch.no_grad():
    inputs = get_inputs(pairs, tokenizer).to(model.device)
    all_scores = model(**inputs, return_dict=True, cutoff_layers=[28])
    all_scores = [scores[:, -1].view(-1, ).float() for scores in all_scores[0]]
    print(all_scores)
Fine-tune
Data Format

Train data should be a json file, where each line is a dict like this:

{"query": str, "pos": List[str], "neg":List[str], "prompt": str}

query is the query, and pos is a list of positive texts, neg is a list of negative texts, prompt indicates the relationship between query and texts. If you have no negative texts for a query, you can random sample some from the entire corpus as the negatives.

See toy_finetune_data.jsonl for a toy data file.

Train

You can fine-tune the reranker with the following code:

For llm-based reranker

torchrun --nproc_per_node {number of gpus} \
-m FlagEmbedding.llm_reranker.finetune_for_instruction.run \
--output_dir {path to save model} \
--model_name_or_path google/gemma-2b \
--train_data ./toy_finetune_data.jsonl \
--learning_rate 2e-4 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 16 \
--dataloader_drop_last True \
--query_max_len 512 \
--passage_max_len 512 \
--train_group_size 16 \
--logging_steps 1 \
--save_steps 2000 \
--save_total_limit 50 \
--ddp_find_unused_parameters False \
--gradient_checkpointing \
--deepspeed stage1.json \
--warmup_ratio 0.1 \
--bf16 \
--use_lora True \
--lora_rank 32 \
--lora_alpha 64 \
--use_flash_attn True \
--target_modules q_proj k_proj v_proj o_proj

For llm-based layerwise reranker

torchrun --nproc_per_node {number of gpus} \
-m FlagEmbedding.llm_reranker.finetune_for_layerwise.run \
--output_dir {path to save model} \
--model_name_or_path openbmb/MiniCPM-2B-dpo-bf16 \
--train_data ./toy_finetune_data.jsonl \
--learning_rate 2e-4 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 16 \
--dataloader_drop_last True \
--query_max_len 512 \
--passage_max_len 512 \
--train_group_size 16 \
--logging_steps 1 \
--save_steps 2000 \
--save_total_limit 50 \
--ddp_find_unused_parameters False \
--gradient_checkpointing \
--deepspeed stage1.json \
--warmup_ratio 0.1 \
--bf16 \
--use_lora True \
--lora_rank 32 \
--lora_alpha 64 \
--use_flash_attn True \
--target_modules q_proj k_proj v_proj o_proj \
--start_layer 8 \
--head_multi True \
--head_type simple \
--lora_extra_parameters linear_head

Our rerankers are initialized from google/gemma-2b (for llm-based reranker) and openbmb/MiniCPM-2B-dpo-bf16 (for llm-based layerwise reranker), and we train it on a mixture of multilingual datasets:

Evaluation
  • llama-index.

image-20240317193909373

  • BEIR.

rereank the top 100 results from bge-en-v1.5 large.

image-20240317174633333

rereank the top 100 results from e5 mistral 7b instruct.

image-20240317172949713

  • CMTEB-retrieval.
    It rereank the top 100 results from bge-zh-v1.5 large.

image-20240317173026235

  • miracl (multi-language).
    It rereank the top 100 results from bge-m3.

image-20240317173117639

Citation

If you find this repository useful, please consider giving a star and citation

@misc{li2023making,
      title={Making Large Language Models A Better Foundation For Dense Retrieval}, 
      author={Chaofan Li and Zheng Liu and Shitao Xiao and Yingxia Shao},
      year={2023},
      eprint={2312.15503},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
@misc{chen2024bge,
      title={BGE M3-Embedding: Multi-Lingual, Multi-Functionality, Multi-Granularity Text Embeddings Through Self-Knowledge Distillation}, 
      author={Jianlv Chen and Shitao Xiao and Peitian Zhang and Kun Luo and Defu Lian and Zheng Liu},
      year={2024},
      eprint={2402.03216},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

Runs of BAAI bge-reranker-v2-minicpm-layerwise on huggingface.co

6.4K
Total runs
0
24-hour runs
-345
3-day runs
-1.3K
7-day runs
-2.8K
30-day runs

More Information About bge-reranker-v2-minicpm-layerwise huggingface.co Model

More bge-reranker-v2-minicpm-layerwise license Visit here:

https://choosealicense.com/licenses/apache-2.0

bge-reranker-v2-minicpm-layerwise huggingface.co

bge-reranker-v2-minicpm-layerwise huggingface.co is an AI model on huggingface.co that provides bge-reranker-v2-minicpm-layerwise's model effect (), which can be used instantly with this BAAI bge-reranker-v2-minicpm-layerwise model. huggingface.co supports a free trial of the bge-reranker-v2-minicpm-layerwise model, and also provides paid use of the bge-reranker-v2-minicpm-layerwise. Support call bge-reranker-v2-minicpm-layerwise model through api, including Node.js, Python, http.

bge-reranker-v2-minicpm-layerwise huggingface.co Url

https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise

BAAI bge-reranker-v2-minicpm-layerwise online free

bge-reranker-v2-minicpm-layerwise huggingface.co is an online trial and call api platform, which integrates bge-reranker-v2-minicpm-layerwise's modeling effects, including api services, and provides a free online trial of bge-reranker-v2-minicpm-layerwise, you can try bge-reranker-v2-minicpm-layerwise online for free by clicking the link below.

BAAI bge-reranker-v2-minicpm-layerwise online free url in huggingface.co:

https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise

bge-reranker-v2-minicpm-layerwise install

bge-reranker-v2-minicpm-layerwise is an open source model from GitHub that offers a free installation service, and any user can find bge-reranker-v2-minicpm-layerwise on GitHub to install. At the same time, huggingface.co provides the effect of bge-reranker-v2-minicpm-layerwise install, users can directly use bge-reranker-v2-minicpm-layerwise installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

bge-reranker-v2-minicpm-layerwise install url in huggingface.co:

https://huggingface.co/BAAI/bge-reranker-v2-minicpm-layerwise

Url of bge-reranker-v2-minicpm-layerwise

bge-reranker-v2-minicpm-layerwise huggingface.co Url

Provider of bge-reranker-v2-minicpm-layerwise huggingface.co

BAAI
ORGANIZATIONS

Other API from BAAI

huggingface.co

Total runs: 2.1M
Run Growth: -106.0K
Growth Rate: -5.08%
Updated: July 03 2024
huggingface.co

Total runs: 2.1M
Run Growth: -2.3M
Growth Rate: -109.47%
Updated: February 21 2024
huggingface.co

Total runs: 1.6M
Run Growth: -299.2K
Growth Rate: -18.60%
Updated: February 21 2024
huggingface.co

Total runs: 788.4K
Run Growth: 124.6K
Growth Rate: 15.80%
Updated: December 13 2023
huggingface.co

Total runs: 457.4K
Run Growth: 35.4K
Growth Rate: 7.75%
Updated: October 12 2023
huggingface.co

Total runs: 170.4K
Run Growth: -18.3K
Growth Rate: -10.74%
Updated: November 14 2023
huggingface.co

Total runs: 139.6K
Run Growth: 50.3K
Growth Rate: 35.99%
Updated: October 12 2023
huggingface.co

Total runs: 69.8K
Run Growth: 1.3K
Growth Rate: 1.86%
Updated: April 17 2024
huggingface.co

Total runs: 33.2K
Run Growth: 31.5K
Growth Rate: 94.85%
Updated: October 12 2023
huggingface.co

Total runs: 28.0K
Run Growth: 11.7K
Growth Rate: 41.71%
Updated: October 12 2023
huggingface.co

Total runs: 22.4K
Run Growth: -2.2K
Growth Rate: -9.73%
Updated: October 12 2023
huggingface.co

Total runs: 21.1K
Run Growth: -5.5K
Growth Rate: -26.04%
Updated: January 15 2025
huggingface.co

Total runs: 5.2K
Run Growth: -4.3K
Growth Rate: -88.74%
Updated: February 22 2024
huggingface.co

Total runs: 5.1K
Run Growth: 1.1K
Growth Rate: 21.98%
Updated: September 21 2023
huggingface.co

Total runs: 5.0K
Run Growth: 736
Growth Rate: 14.63%
Updated: August 15 2024
huggingface.co

Total runs: 4.2K
Run Growth: -2.5K
Growth Rate: -59.84%
Updated: December 26 2022
huggingface.co

Total runs: 2.8K
Run Growth: 7
Growth Rate: 0.25%
Updated: October 12 2023
huggingface.co

Total runs: 2.7K
Run Growth: 1.0K
Growth Rate: 37.57%
Updated: August 15 2024
huggingface.co

Total runs: 2.3K
Run Growth: 675
Growth Rate: 28.95%
Updated: February 07 2024
huggingface.co

Total runs: 2.3K
Run Growth: -11.8K
Growth Rate: -272.27%
Updated: October 23 2024
huggingface.co

Total runs: 1.7K
Run Growth: 320
Growth Rate: 18.32%
Updated: September 18 2023
huggingface.co

Total runs: 1.6K
Run Growth: -398
Growth Rate: -24.48%
Updated: November 28 2024
huggingface.co

Total runs: 1.4K
Run Growth: -3.5K
Growth Rate: -136.57%
Updated: October 23 2024
huggingface.co

Total runs: 1.3K
Run Growth: -3.0K
Growth Rate: -174.67%
Updated: October 24 2024
huggingface.co

Total runs: 1.0K
Run Growth: 725
Growth Rate: 74.51%
Updated: April 02 2024
huggingface.co

Total runs: 717
Run Growth: -9.0K
Growth Rate: -1259.83%
Updated: March 07 2024
huggingface.co

Total runs: 692
Run Growth: 230
Growth Rate: 33.24%
Updated: October 27 2023
huggingface.co

Total runs: 664
Run Growth: -60
Growth Rate: -9.04%
Updated: June 07 2024
huggingface.co

Total runs: 654
Run Growth: 0
Growth Rate: 0.00%
Updated: January 15 2025
huggingface.co

Total runs: 443
Run Growth: 0
Growth Rate: 0.00%
Updated: January 14 2025
huggingface.co

Total runs: 301
Run Growth: 296
Growth Rate: 98.34%
Updated: April 18 2023
huggingface.co

Total runs: 149
Run Growth: -187
Growth Rate: -125.50%
Updated: August 15 2024
huggingface.co

Total runs: 115
Run Growth: 58
Growth Rate: 50.43%
Updated: August 23 2023
huggingface.co

Total runs: 114
Run Growth: 0
Growth Rate: 0.00%
Updated: January 20 2025
huggingface.co

Total runs: 109
Run Growth: -111
Growth Rate: -176.19%
Updated: April 19 2024
huggingface.co

Total runs: 104
Run Growth: -41
Growth Rate: -39.42%
Updated: June 24 2024
huggingface.co

Total runs: 96
Run Growth: 7
Growth Rate: 7.29%
Updated: December 21 2023
huggingface.co

Total runs: 73
Run Growth: 33
Growth Rate: 45.21%
Updated: August 23 2023
huggingface.co

Total runs: 52
Run Growth: -207
Growth Rate: -398.08%
Updated: June 21 2024
huggingface.co

Total runs: 44
Run Growth: -57
Growth Rate: -129.55%
Updated: December 21 2023
huggingface.co

Total runs: 35
Run Growth: -242
Growth Rate: -691.43%
Updated: June 24 2024
huggingface.co

Total runs: 35
Run Growth: -59
Growth Rate: -168.57%
Updated: August 15 2024
huggingface.co

Total runs: 32
Run Growth: -10.2K
Growth Rate: -31881.25%
Updated: February 07 2024
huggingface.co

Total runs: 28
Run Growth: -36
Growth Rate: -128.57%
Updated: August 15 2024
huggingface.co

Total runs: 27
Run Growth: 0
Growth Rate: 0.00%
Updated: January 01 2025
huggingface.co

Total runs: 26
Run Growth: 14
Growth Rate: 53.85%
Updated: July 24 2023
huggingface.co

Total runs: 24
Run Growth: -23
Growth Rate: -95.83%
Updated: August 28 2024
huggingface.co

Total runs: 21
Run Growth: -52
Growth Rate: -247.62%
Updated: December 31 2022
huggingface.co

Total runs: 17
Run Growth: -109
Growth Rate: -641.18%
Updated: May 13 2024
huggingface.co

Total runs: 16
Run Growth: -22
Growth Rate: -137.50%
Updated: July 02 2024