Jia Huei Tan
commited on
Commit
·
bdf3628
1
Parent(s):
3a2f403
Update README
Browse files
README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: mit
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
pipeline_tag: sentence-similarity
|
| 3 |
+
tags:
|
| 4 |
+
- sentence-similarity
|
| 5 |
+
language: en
|
| 6 |
license: mit
|
| 7 |
---
|
| 8 |
+
|
| 9 |
+
# ONNX Conversion of [BAAI/bge-reranker-base](https://huggingface.co/BAAI/bge-reranker-base)
|
| 10 |
+
|
| 11 |
+
- ONNX model for CPU with O3 optimisation
|
| 12 |
+
|
| 13 |
+
## Usage
|
| 14 |
+
|
| 15 |
+
```python
|
| 16 |
+
from itertools import product
|
| 17 |
+
|
| 18 |
+
import torch.nn.functional as F
|
| 19 |
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 20 |
+
from transformers import AutoTokenizer
|
| 21 |
+
|
| 22 |
+
sentences = [
|
| 23 |
+
"The llama (/ˈlɑːmə/) (Lama glama) is a domesticated South American camelid.",
|
| 24 |
+
"The alpaca (Lama pacos) is a species of South American camelid mammal.",
|
| 25 |
+
"The vicuña (Lama vicugna) (/vɪˈkuːnjə/) is one of the two wild South American camelids.",
|
| 26 |
+
]
|
| 27 |
+
queries = ["What is a llama?", "What is a harimau?", "How to fly a kite?"]
|
| 28 |
+
pairs = list(product(queries, sentences))
|
| 29 |
+
|
| 30 |
+
model_name = "EmbeddedLLM/bge-reranker-base-onnx-o3-cpu"
|
| 31 |
+
device = "cpu"
|
| 32 |
+
provider = "CPUExecutionProvider"
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 34 |
+
model = ORTModelForSequenceClassification.from_pretrained(
|
| 35 |
+
model_name, use_io_binding=True, provider=provider, device_map=device
|
| 36 |
+
)
|
| 37 |
+
inputs = tokenizer(
|
| 38 |
+
pairs,
|
| 39 |
+
padding=True,
|
| 40 |
+
truncation=True,
|
| 41 |
+
return_tensors="pt",
|
| 42 |
+
max_length=model.config.max_position_embeddings,
|
| 43 |
+
)
|
| 44 |
+
inputs = inputs.to(device)
|
| 45 |
+
scores = model(**inputs).logits.view(-1).cpu().numpy()
|
| 46 |
+
# Sort most similar to least
|
| 47 |
+
pairs = sorted(zip(pairs, scores), key=lambda x: x[1], reverse=True)
|
| 48 |
+
for ps in pairs:
|
| 49 |
+
print(ps)
|
| 50 |
+
```
|