Update README.md
Browse files
README.md
CHANGED
@@ -16,14 +16,21 @@ pip install -U transformers==4.42.4 intel-extension-for-pytorch==2.3.100
|
|
16 |
Use the following example below to load the model with the transformers library, tokenize the text, run the model, and apply pooling to the output.
|
17 |
|
18 |
```
|
19 |
-
# example embedding code
|
20 |
import torch
|
21 |
from transformers import AutoTokenizer, AutoModel
|
22 |
import intel_extension_for_pytorch as ipex
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# load model
|
25 |
tokenizer = AutoTokenizer.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1')
|
26 |
-
model = AutoModel.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1',
|
|
|
27 |
model.eval()
|
28 |
|
29 |
# do IPEX optimization
|
@@ -48,14 +55,8 @@ with torch.no_grad(), torch.cpu.amp.autocast(cache_enabled=False,
|
|
48 |
# Call model
|
49 |
tokenized_text = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
|
50 |
model_output = model(**tokenized_text)
|
51 |
-
|
52 |
-
|
53 |
-
token_embeddings = model_output[0]
|
54 |
-
attention_mask = tokenized_text['attention_mask']
|
55 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
56 |
-
output_sum = torch.sum(token_embeddings * input_mask_expanded, 1)
|
57 |
-
embeddings = output_sum / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
58 |
-
embeddings = [embeddings[0].tolist()]
|
59 |
|
60 |
# Embeddings output
|
61 |
print(embeddings)
|
|
|
16 |
Use the following example below to load the model with the transformers library, tokenize the text, run the model, and apply pooling to the output.
|
17 |
|
18 |
```
|
|
|
19 |
import torch
|
20 |
from transformers import AutoTokenizer, AutoModel
|
21 |
import intel_extension_for_pytorch as ipex
|
22 |
|
23 |
+
def mean_pooling(model_output, attention_mask):
|
24 |
+
token_embeddings = model_output[0]
|
25 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
26 |
+
return torch.sum(token_embeddings * input_mask_expanded,
|
27 |
+
1) / torch.clamp(input_mask_expanded.sum(1),
|
28 |
+
min=1e-9)
|
29 |
+
|
30 |
# load model
|
31 |
tokenizer = AutoTokenizer.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1')
|
32 |
+
model = AutoModel.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1',
|
33 |
+
torchscript=True)
|
34 |
model.eval()
|
35 |
|
36 |
# do IPEX optimization
|
|
|
55 |
# Call model
|
56 |
tokenized_text = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
|
57 |
model_output = model(**tokenized_text)
|
58 |
+
sentence_embeddings = mean_pooling(model_output,tokenized_text['attention_mask'])
|
59 |
+
embeddings = sentence_embeddings[0].tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Embeddings output
|
62 |
print(embeddings)
|