ONNX Model Zoo

hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04

By hooman650 · ↓ 3 · ♥ 2 · Updated 2024-02-24

What's in this repo

This page lists the files in hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04, including model_optimized.onnx.

  • 1 ONNX models (.onnx files)
  • 9 files, including 5 .json, 1 .md, 1 .onnx, 1 .txt
  • Last updated 2024-02-24

ONNX files

File Size Download
model_optimized.onnx — Download

How to use

These ONNX files can be loaded with ONNX Runtime. To inspect a model graph, upload the file to the Netron viewer. Download links point to the original files on Hugging Face.

README

PubMedBERT Embeddings Matryoshka - ONNX - O4

O4 optimized weights of NeuML/pubmedbert-base-embeddings-matryoshka.

Usage

from optimum.onnxruntime import ORTModelForFeatureExtraction
from transformers import AutoTokenizer
import torch

# Mean Pooling - Take attention mask into account for correct averaging
def meanpooling(output, mask):
    embeddings = output[0] # First element of model_output contains all token embeddings
    mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
    return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)

# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']

model = ORTModelForFeatureExtraction.from_pretrained("hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04",provider="CUDAExecutionProvider")
tokenizer = AutoTokenizer.from_pretrained("hooman650/pubmedbert-base-embeddings-matryoshka-onnx-04")

# Tokenize sentences
inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt').to("cude") # if on GPU

# Compute token embeddings
with torch.no_grad():
    output = model(**inputs)

# Perform pooling. In this case, mean pooling.
embeddings = meanpooling(output, inputs['attention_mask'])

# Requested matryoshka dimensions
dimensions = 256

print("Sentence embeddings:")
print(embeddings[:, :dimensions])

Model info

feature-extraction transformers
transformers onnx bert feature-extraction en license:apache-2.0 text-embeddings-inference endpoints_compatible region:us
↓ 3 ♥ 2

Created: 2024-02-24

Updated: 2024-02-24

View on Hugging Face

Files (9)

.gitattributes
README.md
config.json
model_optimized.onnx ONNX
ort_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.txt

Related models