Views
No views yet
use_raw_attention_mask=True due to this issue1import torch.nn.functional as F
2from optimum.onnxruntime import ORTModelForFeatureExtraction
3from transformers import AutoTokenizer
4
5sentences = [
6 "The llama (/ˈlɑːmə/) (Lama glama) is a domesticated South American camelid.",
7 "The alpaca (Lama pacos) is a species of South American camelid mammal.",
8 "The vicuña (Lama vicugna) (/vɪˈkuːnjə/) is one of the two wild South American camelids.",
9]
10
11model_name = "EmbeddedLLM/bge-base-en-v1.5-onnx-o3-cpu"
12device = "cpu"
13provider = "CPUExecutionProvider"
14tokenizer = AutoTokenizer.from_pretrained(model_name)
15model = ORTModelForFeatureExtraction.from_pretrained(
16 model_name, use_io_binding=True, provider=provider, device_map=device
17)
18inputs = tokenizer(
19 sentences,
20 padding=True,
21 truncation=True,
22 return_tensors="pt",
23 max_length=model.config.max_position_embeddings,
24)
25inputs = inputs.to(device)
26embeddings = model(**inputs).last_hidden_state[:, 0]
27embeddings = F.normalize(embeddings, p=2, dim=1)
28print(embeddings.cpu().numpy().shape)