Views
No views yet
trust_remote_code=True and attn_implementation="flash_attention_2". This is especially important for this model, since BAAI/bge-reranker-v2-m3 supports long contexts of 8192 tokens. For such input length, the inference can be up to 400% faster with Flash Attention in comparison to the original model.1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3import numpy as np
4
5query = "Jak dożyć 100 lat?"
6answers = [
7 "Trzeba zdrowo się odżywiać i uprawiać sport.",
8 "Trzeba pić alkohol, imprezować i jeździć szybkimi autami.",
9 "Gdy trwała kampania politycy zapewniali, że rozprawią się z zakazem niedzielnego handlu."
10]
11
12model_name = "sdadas/polish-reranker-bge-v2"
13tokenizer = AutoTokenizer.from_pretrained(model_name)
14model = AutoModelForSequenceClassification.from_pretrained(
15 model_name,
16 trust_remote_code=True,
17 torch_dtype=torch.bfloat16,
18 attn_implementation="flash_attention_2",
19 device_map="cuda"
20)
21texts = [f"{query}</s></s>{answer}" for answer in answers]
22tokens = tokenizer(texts, padding="longest", max_length=8192, truncation=True, return_tensors="pt").to("cuda")
23output = model(**tokens)
24results = output.logits.detach().cpu().float().numpy()
25results = np.squeeze(results)
26print(results.tolist())1@article{dadas2024assessing,
2 title={Assessing generalization capability of text ranking models in Polish},
3 author={Sławomir Dadas and Małgorzata Grębowiec},
4 year={2024},
5 eprint={2402.14318},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}