forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_modernbert.py
More file actions
28 lines (24 loc) · 1.01 KB
/
reference_modernbert.py
File metadata and controls
28 lines (24 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from transformers import AutoModel, AutoTokenizer
import torch
import numpy as np
model_name = "ibm-granite/granite-embedding-english-r2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, attn_implementation="sdpa")
# Test inputs
# texts = ["Hello world", "Hello world"] # Same text twice
texts = ["The", "The"] # Same text twice
# Get embeddings
inputs = tokenizer(texts, return_tensors="pt", padding=True)
with torch.no_grad():
outputs = model(**inputs)
# CLS token pooling
embeddings = outputs.last_hidden_state[:, 0, :]
# Normalize if model expects it
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
# Check similarity
from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity(embeddings[0:1], embeddings[1:2])[0][0]
print(f"Reference similarity: {similarity}") # Should be ~1.0
print(f"First 10 elements: {embeddings[0][:10]}")
# Save for comparison
np.save("/tmp/reference_embedding.npy", embeddings[0].numpy())