-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mteb_wrapper.py
More file actions
219 lines (165 loc) · 7.38 KB
/
test_mteb_wrapper.py
File metadata and controls
219 lines (165 loc) · 7.38 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Simple test script to verify the MTEB wrapper works correctly.
"""
import torch
from mteb_wrapper import Qwen3VLEmbeddingWrapper
from PIL import Image
import requests
from io import BytesIO
def test_wrapper_initialization():
"""Test that the wrapper initializes correctly."""
print("Testing wrapper initialization...")
try:
wrapper = Qwen3VLEmbeddingWrapper(
model_name='retriever-qwen3vl-colpali',
device='cuda:0' if torch.cuda.is_available() else 'cpu',
dtype=torch.float16,
image_size=784,
use_peft=True,
)
print("✓ Wrapper initialized successfully")
return wrapper
except Exception as e:
print(f"✗ Failed to initialize wrapper: {e}")
raise
def test_text_embeddings(wrapper):
"""Test text (query) embedding generation."""
print("\nTesting text embeddings...")
try:
from datasets import Dataset
from torch.utils.data import DataLoader
queries = [
"Where can we find the animal llama?",
"What is the LLaMA AI model?",
"Photo of a dog"
]
query_dataset = Dataset.from_dict({"text": queries})
query_loader = DataLoader(query_dataset, batch_size=2)
embeddings = wrapper.get_text_embeddings(query_loader)
assert embeddings.shape[0] == len(queries), "Number of embeddings doesn't match number of queries"
assert len(embeddings.shape) == 2, "Embeddings should be 2D (batch, dim)"
# Check normalization (allow for float16->float32 conversion tolerance)
norms = torch.norm(embeddings, dim=1)
assert torch.allclose(
norms,
torch.ones(len(queries)),
atol=1e-3
), f"Embeddings should be normalized, got norms: {norms}"
print(f"✓ Text embeddings: shape={embeddings.shape}, dtype={embeddings.dtype}")
print(f" Sample embedding norm: {torch.norm(embeddings[0]).item():.6f}")
return embeddings
except Exception as e:
print(f"✗ Failed to generate text embeddings: {e}")
raise
def test_image_embeddings(wrapper):
"""Test image (document) embedding generation."""
print("\nTesting image embeddings...")
try:
from datasets import Dataset
from torch.utils.data import DataLoader
# Load test images
urls = [
"https://huggingface.co/Tevatron/dse-phi3-docmatix-v2/resolve/main/animal-llama.png",
"https://huggingface.co/Tevatron/dse-phi3-docmatix-v2/resolve/main/meta-llama.png",
]
headers = {'User-Agent': 'MTEB Test 1.0'}
images = []
for url in urls:
try:
response = requests.get(url, headers=headers, timeout=10)
images.append(Image.open(BytesIO(response.content)))
except Exception as e:
print(f"Warning: Failed to load image from {url}: {e}")
print("Using a dummy image instead...")
images.append(Image.new('RGB', (224, 224), color='red'))
image_dataset = Dataset.from_dict({"image": images})
# Custom collate function for PIL images
def collate_fn(batch):
return {"image": [item["image"] for item in batch]}
image_loader = DataLoader(image_dataset, batch_size=2, collate_fn=collate_fn)
embeddings = wrapper.get_image_embeddings(image_loader)
assert embeddings.shape[0] == len(images), "Number of embeddings doesn't match number of images"
assert len(embeddings.shape) == 2, "Embeddings should be 2D (batch, dim)"
# Check normalization (allow for float16->float32 conversion tolerance)
norms = torch.norm(embeddings, dim=1)
assert torch.allclose(
norms,
torch.ones(len(images)),
atol=1e-3
), f"Embeddings should be normalized, got norms: {norms}"
print(f"✓ Image embeddings: shape={embeddings.shape}, dtype={embeddings.dtype}")
print(f" Sample embedding norm: {torch.norm(embeddings[0]).item():.6f}")
return embeddings
except Exception as e:
print(f"✗ Failed to generate image embeddings: {e}")
raise
def test_similarity_computation(query_embeddings, image_embeddings):
"""Test similarity computation between queries and images."""
print("\nTesting similarity computation...")
try:
similarities = torch.nn.functional.cosine_similarity(
query_embeddings.unsqueeze(1),
image_embeddings.unsqueeze(0),
dim=-1
)
assert similarities.shape == (query_embeddings.shape[0], image_embeddings.shape[0]), \
"Similarity matrix has wrong shape"
assert torch.all(similarities >= -1.0) and torch.all(similarities <= 1.0), \
"Cosine similarities should be in [-1, 1]"
print(f"✓ Similarity computation: shape={similarities.shape}")
print(f" Similarity matrix:")
print(similarities.numpy())
# Print top match for each query
for i in range(similarities.shape[0]):
top_idx = similarities[i].argmax().item()
top_score = similarities[i, top_idx].item()
print(f" Query {i} -> Image {top_idx} (score: {top_score:.4f})")
return similarities
except Exception as e:
print(f"✗ Failed to compute similarities: {e}")
raise
def test_embedding_dimensions(wrapper, query_embeddings, image_embeddings):
"""Test that embedding dimensions match expected values."""
print("\nTesting embedding dimensions...")
try:
assert query_embeddings.shape[1] == image_embeddings.shape[1], \
"Query and image embeddings should have the same dimension"
embed_dim = query_embeddings.shape[1]
print(f"✓ Embedding dimension: {embed_dim}")
return embed_dim
except Exception as e:
print(f"✗ Failed dimension test: {e}")
raise
def main():
"""Run all tests."""
print("=" * 80)
print("MTEB Wrapper Test Suite")
print("=" * 80)
try:
# Test 1: Initialization
wrapper = test_wrapper_initialization()
# Test 2: Text embeddings
query_embeddings = test_text_embeddings(wrapper)
# Test 3: Image embeddings
image_embeddings = test_image_embeddings(wrapper)
# Test 4: Embedding dimensions
embed_dim = test_embedding_dimensions(wrapper, query_embeddings, image_embeddings)
# Test 5: Similarity computation
similarities = test_similarity_computation(query_embeddings, image_embeddings)
print("\n" + "=" * 80)
print("All tests passed! ✓")
print("=" * 80)
print(f"\nModel Summary:")
print(f" - Device: {wrapper.device}")
print(f" - Embedding dimension: {embed_dim}")
print(f" - Image size: {wrapper.image_size}")
print(f" - Using PEFT: {wrapper.use_peft}")
return True
except Exception as e:
print("\n" + "=" * 80)
print(f"Tests failed: {e}")
print("=" * 80)
return False
if __name__ == "__main__":
success = main()
exit(0 if success else 1)