Views
No views yet
1<component id="me5_small_q" type="hugging-face-embedder">
2 <transformer-model path="me5/intfloat-multilingual-e5-small_quantized.onnx" />
3 <tokenizer-model path="me5/tokenizer.json" />
4 <normalize>true</normalize>
5 <pooling-strategy>mean</pooling-strategy>
6</component>
7
8<component id="me5_small" type="hugging-face-embedder">
9 <transformer-model path="me5/intfloat-multilingual-e5-small.onnx" />
10 <tokenizer-model path="me5/tokenizer.json" />
11 <normalize>true</normalize>
12 <pooling-strategy>mean</pooling-strategy>
13</component>
14
15url1 <component id="me5_small_fp16" type="hugging-face-embedder">
2 <transformer-model
3 url="https://huggingface.co/hotchpotch/vespa-onnx-intfloat-multilingual-e5-small/resolve/main/intfloat-multilingual-e5-small_fp16.onnx" />
4 <tokenizer-model
5 url="https://huggingface.co/hotchpotch/vespa-onnx-intfloat-multilingual-e5-small/resolve/main/tokenizer.json" />
6 <normalize>true</normalize>
7 <pooling-strategy>mean</pooling-strategy>
8 </component>
91from optimum.onnxruntime import ORTModelForSequenceClassification
2from transformers import AutoTokenizer
3from torch import Tensor
4import torch
5import torch.nn.functional as F
6
7model_name = "hotchpotch/vespa-onnx-intfloat-multilingual-e5-small"
8onnx_file_name = "intfloat-multilingual-e5-small.onnx"
9
10model = ORTModelForSequenceClassification.from_pretrained(
11 model_name, file_name=onnx_file_name
12)
13# override for last_hidden_states
14model.output_names["logits"] = 0
15tokenizer = AutoTokenizer.from_pretrained(model_name)
16
17
18def average_pool(last_hidden_state: Tensor, attention_mask: Tensor) -> Tensor:
19 last_hidden = last_hidden_state.masked_fill(~attention_mask[..., None].bool(), 0.0)
20 return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
21
22
23input_texts = [
24 "query: What is the capital of Japan?",
25 "query: 日本の首都は?", # "What is the capital of Japan?" in Japanese
26 "passage: ニューヨークは大きな都市です年エネ年エネ", # "New York is a big city" in Japanese
27 "passage: 東京は良い場所です", # "Tokyo is a good place" in Japanese, Tokyo is the capital of Japan.
28]
29
30batch_dict = tokenizer(
31 input_texts, max_length=512, padding=True, truncation=True, return_tensors="pt"
32)
33
34if "token_type_ids" not in batch_dict:
35 batch_dict["token_type_ids"] = torch.zeros_like(batch_dict["input_ids"])
36
37# logits is last_hidden_state
38last_hidden_states = model(**batch_dict).logits
39embeddings = average_pool(last_hidden_states, batch_dict["attention_mask"])
40
41# same vespa embeddings
42embeddings = F.normalize(embeddings, p=2, dim=1)
43
44# similarity score
45print(embeddings[:2] @ embeddings[2:].T)