-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_sts_datasets.py
More file actions
438 lines (367 loc) · 17 KB
/
evaluate_sts_datasets.py
File metadata and controls
438 lines (367 loc) · 17 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import torch
import numpy as np
from scipy.stats import spearmanr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import argparse
from tqdm import tqdm
import os
import sys
import logging
import csv
from pathlib import Path
import requests
import zipfile
import tarfile
import io
import re
# Add the project root to the path so we can import the training module
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from training.byt5_sentence_encoder import ByT5SentenceEncoder
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def download_file(url, output_path):
"""
Download a file from a URL to the specified output path
Args:
url: URL to download from
output_path: Path to save the downloaded file
"""
logger.info(f"Downloading {url} to {output_path}")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(output_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return output_path
def extract_archive(archive_path, output_dir):
"""
Extract an archive file to the specified output directory
Args:
archive_path: Path to the archive file
output_dir: Directory to extract to
"""
logger.info(f"Extracting {archive_path} to {output_dir}")
if archive_path.endswith('.zip'):
with zipfile.ZipFile(archive_path, 'r') as zip_ref:
zip_ref.extractall(output_dir)
elif archive_path.endswith('.tar.gz') or archive_path.endswith('.tgz'):
with tarfile.open(archive_path, 'r:gz') as tar_ref:
tar_ref.extractall(output_dir)
else:
raise ValueError(f"Unsupported archive format: {archive_path}")
def download_and_prepare_datasets(data_dir="./sts_data"):
"""
Download and prepare STS datasets
Args:
data_dir: Directory to save datasets
Returns:
Dictionary mapping dataset names to their file paths
"""
os.makedirs(data_dir, exist_ok=True)
# STS datasets to download
sts_datasets = {
"STS12": "http://ixa2.si.ehu.es/stswiki/images/4/40/STS2012-en-test.zip",
"STS13": "http://ixa2.si.ehu.es/stswiki/images/2/2f/STS2013-en-test.zip",
"STS14": "http://ixa2.si.ehu.es/stswiki/images/8/8c/STS2014-en-test.zip",
"STS15": "http://ixa2.si.ehu.es/stswiki/images/d/da/STS2015-en-test.zip",
"STS16": "http://ixa2.si.ehu.es/stswiki/images/9/98/STS2016-en-test.zip",
"STSBenchmark": "http://ixa2.si.ehu.es/stswiki/images/4/48/Stsbenchmark.tar.gz"
}
dataset_paths = {}
# Add SICK-R as a special case using Hugging Face datasets
dataset_paths["SICK-R"] = "huggingface"
for dataset_name, url in sts_datasets.items():
dataset_dir = os.path.join(data_dir, dataset_name)
os.makedirs(dataset_dir, exist_ok=True)
# For STS datasets, download and extract archive
archive_name = url.split('/')[-1]
archive_path = os.path.join(dataset_dir, archive_name)
if not os.path.exists(archive_path):
download_file(url, archive_path)
# Extract if not already extracted
if not os.path.exists(os.path.join(dataset_dir, "extracted")):
extract_archive(archive_path, dataset_dir)
# Create a marker file to indicate extraction is complete
Path(os.path.join(dataset_dir, "extracted")).touch()
# For STSBenchmark, the path is straightforward
if dataset_name == "STSBenchmark":
dataset_paths[dataset_name] = os.path.join(dataset_dir, "stsbenchmark", "sts-test.csv")
else:
# For other datasets, we'll set the directory and find the actual files during evaluation
dataset_paths[dataset_name] = dataset_dir
return dataset_paths
def load_sts_data(dataset_name, file_path):
"""
Load STS dataset from file
Args:
dataset_name: Name of the dataset
file_path: Path to the dataset file or directory
Returns:
List of examples with sentence pairs and scores
"""
examples = []
if dataset_name == "SICK-R":
# SICK-R is loaded from Hugging Face datasets
try:
from datasets import load_dataset
logger.info("Loading SICK-R dataset from Hugging Face...")
dataset = load_dataset("mteb/sickr-sts", split="test")
for item in dataset:
sentence1 = item["sentence1"]
sentence2 = item["sentence2"]
# The score is already in the 0-5 range
score = item["score"]
examples.append({
"sentence1": sentence1,
"sentence2": sentence2,
"score": score
})
except Exception as e:
logger.error(f"Error loading SICK-R dataset from Hugging Face: {e}")
elif dataset_name == "STSBenchmark":
# STS Benchmark has a specific format
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
parts = line.strip().split('\t')
if len(parts) >= 7:
score = float(parts[4])
sentence1 = parts[5]
sentence2 = parts[6]
examples.append({
"sentence1": sentence1,
"sentence2": sentence2,
"score": score
})
elif dataset_name.startswith("STS"):
# STS12-16 have multiple files per year
if os.path.isdir(file_path):
# Find all .txt files in the directory
for root, _, files in os.walk(file_path):
for file in files:
if file.endswith('.txt') and not file.endswith('README.txt'):
file_examples = load_sts_file(os.path.join(root, file))
examples.extend(file_examples)
return examples
def load_sts_file(file_path):
"""
Load examples from a single STS file
Args:
file_path: Path to the STS file
Returns:
List of examples with sentence pairs and scores
"""
examples = []
with open(file_path, 'r', encoding='utf-8', errors='replace') as f:
for line in f:
parts = line.strip().split('\t')
if len(parts) >= 2:
# The first column is the similarity score
try:
score = float(parts[0])
# Handle cases where score is -1 (invalid)
if score == -1:
continue
# The rest depends on the file format
if len(parts) == 2:
# Some files have sentence pairs combined in the second column
text = parts[1]
sentences = text.split('\t')
if len(sentences) >= 2:
sentence1 = sentences[0]
sentence2 = sentences[1]
else:
# Try to split by a different delimiter
match = re.search(r'(.+?)(\s*\|\|\s*)(.+)', text)
if match:
sentence1 = match.group(1).strip()
sentence2 = match.group(3).strip()
else:
# Skip if we can't parse the sentences
continue
elif len(parts) >= 3:
# Most files have separate columns for each sentence
sentence1 = parts[1]
sentence2 = parts[2]
else:
# Skip if we don't have enough parts
continue
examples.append({
"sentence1": sentence1,
"sentence2": sentence2,
"score": score
})
except ValueError:
# Skip lines where the score is not a valid float
continue
return examples
def create_batcher(model, tokenizer, device, max_length, model_type):
"""
Create a batcher function for encoding sentences
Args:
model: The model to use for encoding
tokenizer: The tokenizer to use for tokenization
device: The device to run the model on
max_length: The maximum sequence length
model_type: The type of model ('custom' or 'huggingface')
Returns:
A function that encodes batches of sentences
"""
def encode_batch(sentences):
# Tokenize and encode
inputs = tokenizer(sentences,
return_tensors="pt",
padding=True,
truncation=True,
max_length=max_length)
# Move inputs to device
input_ids = inputs['input_ids'].to(device)
attention_mask = inputs['attention_mask'].to(device)
with torch.no_grad():
if model_type == "custom":
# The ByT5SentenceEncoder already handles pooling internally
sentence_embeddings = model(input_ids=input_ids, attention_mask=attention_mask)
else: # model_type == "huggingface"
# For the base Hugging Face model, we need to handle pooling ourselves
encoder_outputs = model(input_ids=input_ids, attention_mask=attention_mask)
token_embeddings = encoder_outputs.last_hidden_state # shape: (batch, seq_len, hidden)
mask = attention_mask.unsqueeze(-1).float()
token_embeddings = token_embeddings * mask
sentence_embeddings = token_embeddings.sum(dim=1) / mask.sum(dim=1)
return sentence_embeddings
return encode_batch
def evaluate_sts_dataset(model, encode_batch, examples, batch_size=32):
"""
Evaluate model on STS dataset
Args:
model: The model to evaluate
encode_batch: Function to encode batches of sentences
examples: List of examples with sentence pairs and scores
batch_size: Batch size for encoding
Returns:
Spearman correlation coefficient
"""
model.eval()
all_scores = []
all_similarities = []
# Process in batches
for i in tqdm(range(0, len(examples), batch_size), desc="Evaluating"):
batch = examples[i:i+batch_size]
# Extract sentences and scores
sentences1 = [example["sentence1"] for example in batch]
sentences2 = [example["sentence2"] for example in batch]
scores = [example["score"] for example in batch]
# Encode sentences
embeddings1 = encode_batch(sentences1)
embeddings2 = encode_batch(sentences2)
# Compute cosine similarity
for j in range(len(batch)):
emb1 = embeddings1[j].unsqueeze(0)
emb2 = embeddings2[j].unsqueeze(0)
similarity = torch.cosine_similarity(emb1, emb2).item()
all_scores.append(scores[j])
all_similarities.append(similarity)
# Compute Spearman correlation
correlation, p_value = spearmanr(all_scores, all_similarities)
return correlation * 100 # Convert to percentage scale
def main():
parser = argparse.ArgumentParser(description="Evaluate ByT5 sentence encoder on STS datasets.")
parser.add_argument("--model_name", type=str, default="google/byt5-small", help="Base model name for ByT5.")
parser.add_argument("--model_type", type=str, default="custom", choices=["custom", "huggingface"],
help="Type of model to use: 'custom' for ByT5SentenceEncoder or 'huggingface' for base HF model.")
parser.add_argument("--checkpoint_path", type=str, required=False,
help="Path to the pretrained model checkpoint (required for 'custom' model_type).")
parser.add_argument("--embedding_dim", type=int, default=768, help="Dimension of sentence embeddings.")
parser.add_argument("--pooling_strategy", type=str, default="mean", choices=["mean", "first"],
help="Pooling strategy for encoder outputs.")
parser.add_argument("--max_length", type=int, default=512, help="Max length for tokenization.")
parser.add_argument("--batch_size", type=int, default=32, help="Batch size for evaluation.")
parser.add_argument("--device", type=str, default="cuda", help="Device to use (cuda or cpu).")
parser.add_argument("--data_dir", type=str, default="./sts_data", help="Directory to store STS datasets.")
parser.add_argument("--datasets", type=str, nargs='+',
default=["STS12", "STS13", "STS14", "STS15", "STS16", "STSBenchmark", "SICK-R"],
help="STS datasets to evaluate on.")
args = parser.parse_args()
# Set device
device = torch.device(args.device if torch.cuda.is_available() and args.device == "cuda" else "cpu")
max_length = args.max_length
model_type = args.model_type
# Load tokenizer
print(f"Loading tokenizer for {args.model_name}...")
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
if model_type == "custom":
# Check if checkpoint_path is provided for custom model
if not args.checkpoint_path:
raise ValueError("checkpoint_path is required when model_type is 'custom'")
# Initialize the ByT5SentenceEncoder
print(f"Initializing ByT5SentenceEncoder with {args.model_name}...")
model = ByT5SentenceEncoder(
model_name=args.model_name,
embedding_dim=args.embedding_dim,
pooling_strategy=args.pooling_strategy
).to(device)
# Load the pretrained weights
print(f"Loading pretrained weights from {args.checkpoint_path}...")
checkpoint = torch.load(args.checkpoint_path, map_location=device)
# If the checkpoint contains a state_dict (from training), extract it
if 'model_state_dict' in checkpoint:
# This is a training checkpoint that contains the dual encoder
# We need to extract just the encoder part
state_dict = {}
for key, value in checkpoint['model_state_dict'].items():
# Remove the 'encoder.' prefix if it exists (from DualEncoder)
if key.startswith('encoder.'):
state_dict[key[8:]] = value
model.load_state_dict(state_dict)
else:
# This is a direct model state dict
model.load_state_dict(checkpoint)
else: # model_type == "huggingface"
# Load the base Hugging Face model
print(f"Loading base Hugging Face model {args.model_name}...")
model = AutoModelForSeq2SeqLM.from_pretrained(args.model_name).encoder.to(device)
model.eval()
print(f"Model loaded on {device}")
# Create the batcher function
encode_batch = create_batcher(model, tokenizer, device, max_length, model_type)
# Download and prepare datasets
dataset_paths = download_and_prepare_datasets(args.data_dir)
# Make sure the datasets package is installed for SICK-R
if "SICK-R" in args.datasets:
try:
import datasets
except ImportError:
logger.warning("The 'datasets' package is required for SICK-R evaluation.")
logger.warning("Installing datasets package...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "datasets"])
# Evaluate on each dataset
results = {}
for dataset_name in args.datasets:
if dataset_name in dataset_paths:
print(f"\nEvaluating on {dataset_name}...")
file_path = dataset_paths[dataset_name]
# Load dataset
examples = load_sts_data(dataset_name, file_path)
print(f"Loaded {len(examples)} examples from {dataset_name}")
if examples:
# Evaluate
correlation = evaluate_sts_dataset(model, encode_batch, examples, args.batch_size)
results[dataset_name] = correlation
print(f"{dataset_name} Spearman correlation: {correlation:.2f}")
else:
print(f"No examples found for {dataset_name}")
else:
print(f"Dataset {dataset_name} not found in downloaded datasets")
# Print summary of results
print("\nSummary of Results:")
print("-" * 50)
for dataset_name, correlation in results.items():
print(f"{dataset_name}: {correlation:.2f}")
# Calculate average
if results:
avg_correlation = sum(results.values()) / len(results)
print("-" * 50)
print(f"Average: {avg_correlation:.2f}")
if __name__ == "__main__":
main()