vamsibanda
commited on
Commit
•
f788f36
1
Parent(s):
2e34e25
Update README.md
Browse files
README.md
CHANGED
@@ -10,66 +10,21 @@ tags:
|
|
10 |
- onnx
|
11 |
---
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
This is the ONNX model of sentence-transformers/all-roberta-large-v1 [https://seb.sbert.net]. Currently, Hugging Face does not support downloading ONNX model and generate embeddings. I have created a workaround using sbert and optimum together to generate embeddings.
|
16 |
|
|
|
|
|
17 |
```
|
18 |
-
pip install
|
19 |
-
pip install onnxruntime==1.10.0
|
20 |
-
pip install transformers>4.6.1
|
21 |
-
pip install sentencepiece
|
22 |
-
pip install sentence-transformers
|
23 |
-
pip install optimum
|
24 |
-
pip install torch==1.9.0
|
25 |
```
|
26 |
-
|
27 |
Then you can use the model like this:
|
28 |
-
|
29 |
```python
|
30 |
-
import
|
31 |
-
from sentence_transformers.util import snapshot_download
|
32 |
-
from transformers import AutoTokenizer
|
33 |
-
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
34 |
-
from sentence_transformers.models import Transformer, Pooling, Dense
|
35 |
-
import torch
|
36 |
-
from transformers.modeling_outputs import BaseModelOutput
|
37 |
-
import torch.nn.functional as F
|
38 |
-
import shutil
|
39 |
-
|
40 |
-
model_name = 'vamsibanda/sbert-onnx-all-roberta-large-v1'
|
41 |
-
cache_folder = './'
|
42 |
-
model_path = os.path.join(cache_folder, model_name.replace("/", "_"))
|
43 |
-
|
44 |
-
def download_onnx_model(model_name, cache_folder, model_path, force_download = False):
|
45 |
-
if force_download and os.path.exists(model_path):
|
46 |
-
shutil.rmtree(model_path)
|
47 |
-
elif os.path.exists(model_path):
|
48 |
-
return
|
49 |
-
snapshot_download(model_name,
|
50 |
-
cache_dir=cache_folder,
|
51 |
-
library_name='sentence-transformers'
|
52 |
-
)
|
53 |
-
return
|
54 |
-
|
55 |
-
def mean_pooling(model_output, attention_mask):
|
56 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
57 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
58 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
59 |
-
|
60 |
-
def generate_embedding(text):
|
61 |
-
token = tokenizer(text, return_tensors='pt')
|
62 |
-
embedding = model(input_ids=token['input_ids'], attention_mask=token['attention_mask'])
|
63 |
-
embedding = mean_pooling(embedding, token['attention_mask'])
|
64 |
-
embedding = F.normalize(embedding, p=2, dim=1)
|
65 |
-
return embedding.tolist()[0]
|
66 |
-
|
67 |
-
|
68 |
-
_ = download_onnx_model(model_name, cache_folder, model_path)
|
69 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
70 |
-
model = ORTModelForFeatureExtraction.from_pretrained(model_path, force_download=False)
|
71 |
-
pooling_layer = Pooling.load(f"{model_path}/1_Pooling")
|
72 |
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
```
|
|
|
10 |
- onnx
|
11 |
---
|
12 |
|
13 |
+
# ONNX convert all-roberta-large-v1
|
14 |
+
## Conversion of [sentence-transformers/all-roberta-large-v1](https://huggingface.co/sentence-transformers/all-roberta-large-v1)
|
|
|
15 |
|
16 |
+
## Usage (HuggingFace Optimum)
|
17 |
+
Using this model becomes easy when you have [optimum](https://github.com/huggingface/optimum) installed:
|
18 |
```
|
19 |
+
python -m pip install optimum
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
```
|
|
|
21 |
Then you can use the model like this:
|
|
|
22 |
```python
|
23 |
+
from optimum.onnxruntime.modeling_ort import ORTModelForCustomTasks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
model = ORTModelForCustomTasks.from_pretrained("vamsibanda/sbert-all-roberta-large-v1-with-pooler")
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained("vamsibanda/sbert-all-roberta-large-v1-with-pooler")
|
27 |
+
inputs = tokenizer("I love burritos!", return_tensors="pt")
|
28 |
+
pred = model(**inputs)
|
29 |
+
embedding = pred['pooler_output']
|
30 |
```
|