michaelfeil commited on
Commit
c178c2b
1 Parent(s): 2d66e08

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +48 -0
README.md CHANGED
@@ -2866,6 +2866,54 @@ sentence_embeddings = torch.nn.functional.normalize(sentence_embeddings, p=2, di
2866
  print("Sentence embeddings:", sentence_embeddings)
2867
  ```
2868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2869
  ### Usage for Reranker
2870
 
2871
  Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding.
 
2866
  print("Sentence embeddings:", sentence_embeddings)
2867
  ```
2868
 
2869
+ #### Usage of the ONNX files
2870
+
2871
+ ```python
2872
+ from optimum.onnxruntime import ORTModelForFeatureExtraction # type: ignore
2873
+
2874
+ import torch
2875
+ from transformers import AutoModel, AutoTokenizer
2876
+
2877
+ tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-large-en-v1.5')
2878
+ model = AutoModel.from_pretrained('BAAI/bge-large-en-v1.5')
2879
+ model_ort = ORTModelForFeatureExtraction.from_pretrained('BAAI/bge-large-en-v1.5', file_name="onnx/model.onnx")
2880
+
2881
+ # Sentences we want sentence embeddings for
2882
+ sentences = ["样例数据-1", "样例数据-2"]
2883
+
2884
+ # Tokenize sentences
2885
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
2886
+ # for s2p(short query to long passage) retrieval task, add an instruction to query (not add instruction for passages)
2887
+ # encoded_input = tokenizer([instruction + q for q in queries], padding=True, truncation=True, return_tensors='pt')
2888
+
2889
+ model_output_ort = model_ort(**encoded_input)
2890
+ # Compute token embeddings
2891
+ with torch.no_grad():
2892
+ model_output = model(**encoded_input)
2893
+
2894
+ # model_output and model_output_ort are identical
2895
+
2896
+ ```
2897
+
2898
+ #### Usage via infinity
2899
+ Its also possible to deploy the onnx files with the [infinity_emb](https://github.com/michaelfeil/infinity) pip package.
2900
+ Recommended is `device="cuda", engine="torch"` with flash attention on gpu, and `device="cpu", engine="optimum"` for onnx inference.
2901
+
2902
+ ```python
2903
+ import asyncio
2904
+ from infinity_emb import AsyncEmbeddingEngine, EngineArgs
2905
+
2906
+ sentences = ["Embed this is sentence via Infinity.", "Paris is in France."]
2907
+ engine = AsyncEmbeddingEngine.from_args(
2908
+ EngineArgs(model_name_or_path = "BAAI/bge-large-en-v1.5", device="cpu", engine="optimum" # or engine="torch"
2909
+ ))
2910
+
2911
+ async def main():
2912
+ async with engine:
2913
+ embeddings, usage = await engine.embed(sentences=sentences)
2914
+ asyncio.run(main())
2915
+ ```
2916
+
2917
  ### Usage for Reranker
2918
 
2919
  Different from embedding model, reranker uses question and document as input and directly output similarity instead of embedding.