-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rouge_torch.py
More file actions
556 lines (439 loc) · 22.5 KB
/
test_rouge_torch.py
File metadata and controls
556 lines (439 loc) · 22.5 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import unittest
import torch
from rouge_torch import ROUGEScoreTorch, create_vocab_and_tokenizer, text_to_logits
class TestROUGEScoreTorch(unittest.TestCase):
"""Unit tests for ROUGEScoreTorch class."""
@classmethod
def setUpClass(cls):
"""Set up test fixtures for the entire test class."""
cls.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
(
cls.word_to_id,
cls.id_to_word,
cls.tokenize_fn,
cls.detokenize_fn,
) = create_vocab_and_tokenizer()
cls.vocab_size = len(cls.word_to_id)
cls.rouge = ROUGEScoreTorch(cls.vocab_size, cls.device)
def _text_to_logits(self, text, max_len=20):
"""Helper method to convert text to logits using the stored tokenizer."""
# Access the function from the class to avoid bound method issue
tokenize_func = type(self).tokenize_fn
return text_to_logits(
text, tokenize_func, self.vocab_size, self.device, max_len
)
def test_identical_strings(self):
"""Test Case 1: Identical strings should get perfect scores."""
candidate = "the cat sat on the mat"
reference = "the cat sat on the mat"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
rouge_1 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=1)
rouge_2 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=2)
rouge_l = self.rouge.rouge_l_batch(cand_logits, ref_logits)
# Identical strings should have perfect scores
self.assertAlmostEqual(rouge_1["precision"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_1["recall"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_1["f1"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_2["precision"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_2["recall"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_2["f1"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_l["precision"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_l["recall"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_l["f1"][0].item(), 1.0, places=3)
def test_completely_different_strings(self):
"""Test Case 2: Completely different strings should get zero scores."""
candidate = "the quick brown fox"
reference = "a big red car"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
rouge_1 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=1)
rouge_2 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=2)
rouge_l = self.rouge.rouge_l_batch(cand_logits, ref_logits)
# Different strings should have zero or very low scores
self.assertAlmostEqual(rouge_1["precision"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_1["recall"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_1["f1"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_2["precision"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_2["recall"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_2["f1"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_l["precision"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_l["recall"][0].item(), 0.0, places=3)
self.assertAlmostEqual(rouge_l["f1"][0].item(), 0.0, places=3)
def test_partial_overlap(self):
"""Test Case 3: Partial overlap - 3/5 words overlap (the, is, very)."""
candidate = "the cat is very good"
reference = "the dog is very bad"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
rouge_1 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=1)
rouge_2 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=2)
rouge_l = self.rouge.rouge_l_batch(cand_logits, ref_logits)
# Expected precision: 3/5 = 0.6, recall: 3/5 = 0.6
self.assertAlmostEqual(rouge_1["precision"][0].item(), 0.6, places=1)
self.assertAlmostEqual(rouge_1["recall"][0].item(), 0.6, places=1)
self.assertAlmostEqual(rouge_1["f1"][0].item(), 0.6, places=1)
# ROUGE-2 should have lower scores due to fewer bigram matches
self.assertLess(rouge_2["f1"][0].item(), rouge_1["f1"][0].item())
def test_word_reordering(self):
"""Test Case 4: Word reordering - ROUGE-L should be lower than ROUGE-1."""
candidate = "the cat sat on the mat"
reference = "mat the on sat cat the" # Same words, different order
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
rouge_1 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=1)
rouge_l = self.rouge.rouge_l_batch(cand_logits, ref_logits)
# ROUGE-1 should be 1.0 (same words)
self.assertAlmostEqual(rouge_1["precision"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_1["recall"][0].item(), 1.0, places=3)
self.assertAlmostEqual(rouge_1["f1"][0].item(), 1.0, places=3)
# ROUGE-L should be lower due to different word order
self.assertLess(rouge_l["f1"][0].item(), rouge_1["f1"][0].item())
def test_multiple_references(self):
"""Test Case 5: Multiple references - should take best match across references."""
candidate = "the cat is good"
ref1 = "the cat is very good"
ref2 = "a cat is good"
cand_logits = self._text_to_logits(candidate)
ref_logits = [
self._text_to_logits(ref1),
self._text_to_logits(ref2),
]
rouge_1 = self.rouge.rouge_n_batch(cand_logits, ref_logits, n=1)
rouge_l = self.rouge.rouge_l_batch(cand_logits, ref_logits)
# Should get reasonable scores by taking the best match
self.assertGreater(rouge_1["f1"][0].item(), 0.6)
self.assertGreater(rouge_l["f1"][0].item(), 0.6)
def test_batch_processing(self):
"""Test Case 6: Batch processing of multiple examples."""
candidates = ["the cat sat", "a dog ran", "the quick fox"]
references = ["the cat sat on mat", "the dog ran fast", "quick brown fox jumps"]
# Create batch tensors
max_len = 10
batch_cand_logits = []
batch_ref_logits = []
for cand, ref in zip(candidates, references):
cand_logits = self._text_to_logits(cand, max_len)
ref_logits = self._text_to_logits(ref, max_len)
batch_cand_logits.append(cand_logits)
batch_ref_logits.append(ref_logits)
batch_cand_logits = torch.cat(batch_cand_logits, dim=0)
batch_ref_logits = [torch.cat(batch_ref_logits, dim=0)]
rouge_1 = self.rouge.rouge_n_batch(batch_cand_logits, batch_ref_logits, n=1)
rouge_l = self.rouge.rouge_l_batch(batch_cand_logits, batch_ref_logits)
# Check that all examples have reasonable scores
self.assertEqual(len(rouge_1["f1"]), len(candidates))
self.assertEqual(len(rouge_l["f1"]), len(candidates))
for i in range(len(candidates)):
self.assertGreaterEqual(rouge_1["f1"][i].item(), 0.0)
self.assertLessEqual(rouge_1["f1"][i].item(), 1.0)
self.assertGreaterEqual(rouge_l["f1"][i].item(), 0.0)
self.assertLessEqual(rouge_l["f1"][i].item(), 1.0)
def test_loss_bounds(self):
"""Test that loss function has proper bounds."""
# Test case 1: Perfect match (should give loss = 0)
candidate = "the cat sat on the mat"
reference = "the cat sat on the mat"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
loss = self.rouge.compute_rouge_loss(cand_logits, ref_logits, reduction="mean")
self.assertAlmostEqual(
loss.item(), 0.0, places=5, msg="Perfect match should give loss = 0"
)
# Test case 2: No match (should give loss = 1)
candidate = "the quick brown fox"
reference = "a big red car"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
loss = self.rouge.compute_rouge_loss(cand_logits, ref_logits, reduction="mean")
self.assertAlmostEqual(
loss.item(),
2.0,
places=5,
msg="No match should give loss = 2 (ROUGE-1 + ROUGE-L)",
)
# Test case 3: Partial match (should give loss between 0 and 1)
candidate = "the cat is very good"
reference = "the dog is very bad" # 3/5 overlap
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
loss = self.rouge.compute_rouge_loss(cand_logits, ref_logits, reduction="mean")
self.assertGreater(loss.item(), 0.0)
self.assertLess(loss.item(), 2.0)
# With partial overlap, loss should be between 0 and 2
self.assertGreater(loss.item(), 0.5) # Should be greater than some threshold
self.assertLess(loss.item(), 1.5) # Should be less than max for partial match
def test_loss_reduction_modes(self):
"""Test different loss reduction modes."""
candidates = ["the cat sat", "a dog ran"]
references = ["the cat sat on mat", "the dog ran fast"]
# Create batch tensors
max_len = 10
batch_cand_logits = []
batch_ref_logits = []
for cand, ref in zip(candidates, references):
cand_logits = self._text_to_logits(cand, max_len)
ref_logits = self._text_to_logits(ref, max_len)
batch_cand_logits.append(cand_logits)
batch_ref_logits.append(ref_logits)
batch_cand_logits = torch.cat(batch_cand_logits, dim=0)
batch_ref_logits = [torch.cat(batch_ref_logits, dim=0)]
# Test different reduction modes
loss_mean = self.rouge.compute_rouge_loss(
batch_cand_logits, batch_ref_logits, reduction="mean"
)
loss_sum = self.rouge.compute_rouge_loss(
batch_cand_logits, batch_ref_logits, reduction="sum"
)
loss_none = self.rouge.compute_rouge_loss(
batch_cand_logits, batch_ref_logits, reduction="none"
)
# Check shapes and relationships
self.assertEqual(loss_mean.shape, ()) # scalar
self.assertEqual(loss_sum.shape, ()) # scalar
self.assertEqual(loss_none.shape, (2,)) # batch_size
# Check mathematical relationships
self.assertAlmostEqual(loss_mean.item(), loss_none.mean().item(), places=5)
self.assertAlmostEqual(loss_sum.item(), loss_none.sum().item(), places=5)
# All losses should be in [0, 2] (with default ROUGE-1 + ROUGE-L)
self.assertGreaterEqual(loss_mean.item(), 0.0)
self.assertLessEqual(loss_mean.item(), 2.0)
self.assertGreaterEqual(loss_sum.item(), 0.0)
self.assertLessEqual(
loss_sum.item(), len(candidates) * 2.0
) # sum can be up to batch_size * 2
for i in range(len(candidates)):
self.assertGreaterEqual(loss_none[i].item(), 0.0)
self.assertLessEqual(loss_none[i].item(), 2.0)
def test_single_rouge_type_loss_bounds(self):
"""Test that single ROUGE type loss has bounds [0, 1]."""
# Perfect match with single ROUGE type
candidate = "the cat sat on the mat"
reference = "the cat sat on the mat"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
loss_r1 = self.rouge.compute_rouge_loss(
cand_logits, ref_logits, rouge_types=["rouge_1"], reduction="mean"
)
loss_rl = self.rouge.compute_rouge_loss(
cand_logits, ref_logits, rouge_types=["rouge_l"], reduction="mean"
)
self.assertAlmostEqual(loss_r1.item(), 0.0, places=5)
self.assertAlmostEqual(loss_rl.item(), 0.0, places=5)
# No match with single ROUGE type
candidate = "the quick brown fox"
reference = "a big red car"
cand_logits = self._text_to_logits(candidate)
ref_logits = [self._text_to_logits(reference)]
loss_r1 = self.rouge.compute_rouge_loss(
cand_logits, ref_logits, rouge_types=["rouge_1"], reduction="mean"
)
loss_rl = self.rouge.compute_rouge_loss(
cand_logits, ref_logits, rouge_types=["rouge_l"], reduction="mean"
)
self.assertAlmostEqual(loss_r1.item(), 1.0, places=5)
self.assertAlmostEqual(loss_rl.item(), 1.0, places=5)
def test_overfit_convergence(self):
"""Test that a small model can overfit to near-zero ROUGE loss on a single batch.
This test validates that our ROUGE loss function implementation is correct
by training a simple model to convergence on one batch and verifying it
reaches near-zero loss.
"""
# Simple model for text generation
class SimpleTextModel(torch.nn.Module):
def __init__(self, vocab_size, seq_len, hidden_size=64):
super().__init__()
self.vocab_size = vocab_size
self.seq_len = seq_len
self.hidden_size = hidden_size
# Simple architecture: embedding -> LSTM -> linear
self.embedding = torch.nn.Embedding(vocab_size, hidden_size)
self.lstm = torch.nn.LSTM(hidden_size, hidden_size, batch_first=True)
self.output_proj = torch.nn.Linear(hidden_size, vocab_size)
def forward(self, input_ids):
# input_ids: (batch_size, seq_len)
batch_size = input_ids.size(0)
# Create embeddings
embeds = self.embedding(input_ids) # (batch_size, seq_len, hidden_size)
# LSTM
lstm_out, _ = self.lstm(embeds) # (batch_size, seq_len, hidden_size)
# Project to vocab
logits = self.output_proj(lstm_out) # (batch_size, seq_len, vocab_size)
return logits
# Set up training data - one batch with simple target
batch_size = 2
seq_len = 8
vocab_size = self.vocab_size
device = self.device
# Create simple input-target pairs that should be easy to memorize
# Input: "the cat sat" -> Target: "the cat sat on mat"
# Input: "a dog ran" -> Target: "a dog ran in park"
input_texts = ["the cat sat", "a dog ran"]
target_texts = ["the cat sat on mat", "a dog ran in park"]
# Convert to tensors
input_ids = []
target_logits = []
for inp_text, tgt_text in zip(input_texts, target_texts):
# Tokenize input (first 3 tokens)
tokenize_func = type(self).tokenize_fn
inp_tokens = tokenize_func(inp_text)[:3]
while len(inp_tokens) < seq_len:
inp_tokens.append(0) # PAD
input_ids.append(inp_tokens)
# Create target logits
tgt_logits = self._text_to_logits(tgt_text, seq_len)
target_logits.append(tgt_logits)
input_ids = torch.tensor(input_ids, device=device) # (batch_size, seq_len)
target_logits = [
torch.cat(target_logits, dim=0)
] # List of (batch_size, seq_len, vocab_size)
# Create model and optimizer
model = SimpleTextModel(vocab_size, seq_len).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
rouge_scorer = self.rouge
# Training loop - should converge to near zero loss
initial_loss = None
final_loss = None
losses = []
for epoch in range(200): # Should be enough for overfitting
optimizer.zero_grad()
# Forward pass
logits = model(input_ids) # (batch_size, seq_len, vocab_size)
# For differentiable training, we need a proxy loss
# We'll use cross-entropy between the predicted logits and target logits
# This is a differentiable approximation that should drive the model
# to produce logits that match the targets
# Convert target logits to soft targets (probabilities)
target_probs = torch.softmax(
target_logits[0], dim=-1
) # (batch_size, seq_len, vocab_size)
# Use KL divergence loss as a differentiable proxy
log_pred_probs = torch.log_softmax(logits, dim=-1)
kl_loss = torch.nn.functional.kl_div(
log_pred_probs, target_probs, reduction="batchmean"
)
# Also compute the actual ROUGE loss for monitoring (non-differentiable)
with torch.no_grad():
rouge_loss = rouge_scorer.compute_rouge_loss(
logits, target_logits, rouge_types=["rouge_1"], reduction="mean"
)
# Use KL loss for training, but monitor ROUGE loss
loss = kl_loss
losses.append(rouge_loss.item()) # Track ROUGE loss for validation
if epoch == 0:
initial_loss = rouge_loss.item()
# Backward pass
loss.backward()
optimizer.step()
# Check for convergence using ROUGE loss
current_rouge_loss = rouge_loss.item()
if current_rouge_loss < 0.01: # Very low ROUGE loss threshold
final_loss = current_rouge_loss
break
# Print progress occasionally
if epoch % 50 == 0:
print(
f"Epoch {epoch}: KL Loss = {kl_loss.item():.6f}, ROUGE Loss = {current_rouge_loss:.6f}"
)
final_loss = losses[-1] if final_loss is None else losses[-1]
# Assertions to validate overfitting behavior
self.assertIsNotNone(initial_loss, "Should have recorded initial loss")
self.assertGreater(initial_loss, 0.5, "Initial loss should be high")
self.assertLess(
final_loss, 0.1, "Final loss should be very low after overfitting"
)
self.assertLess(
final_loss, initial_loss * 0.1, "Loss should decrease by at least 90%"
)
# Additional validation: check that the model actually learned something
with torch.no_grad():
final_logits = model(input_ids)
final_rouge_scores = rouge_scorer.rouge_n_batch(
final_logits, target_logits, n=1
)
# F1 scores should be high (close to 1) after overfitting
mean_f1 = final_rouge_scores["f1"].mean().item()
self.assertGreater(
mean_f1, 0.8, "Model should achieve high F1 scores after overfitting"
)
print(f"Overfit test completed:")
print(f" Initial ROUGE loss: {initial_loss:.6f}")
print(f" Final ROUGE loss: {final_loss:.6f}")
print(f" ROUGE loss reduction: {(1 - final_loss/initial_loss)*100:.1f}%")
print(f" Final F1 score: {mean_f1:.3f}")
print(f" Converged in {len(losses)} epochs")
# Test passes if we successfully overfit to low loss
class TestROUGEPerformance(unittest.TestCase):
"""Performance tests for ROUGEScoreTorch class."""
@classmethod
def setUpClass(cls):
"""Set up test fixtures for performance tests."""
cls.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cls.vocab_size = 1000
cls.rouge = ROUGEScoreTorch(cls.vocab_size, cls.device)
def test_small_batch_short_sequences(self):
"""Test performance with small batch and short sequences."""
batch_size, seq_len = 4, 20
self._run_performance_test(batch_size, seq_len)
def test_medium_batch_medium_sequences(self):
"""Test performance with medium batch and medium sequences."""
batch_size, seq_len = 8, 50
self._run_performance_test(batch_size, seq_len)
def test_large_batch_short_sequences(self):
"""Test performance with large batch and short sequences."""
batch_size, seq_len = 16, 30
self._run_performance_test(batch_size, seq_len)
def test_small_batch_long_sequences(self):
"""Test performance with small batch and long sequences."""
batch_size, seq_len = 4, 100
self._run_performance_test(batch_size, seq_len)
def _run_performance_test(self, batch_size, seq_len):
"""Helper method to run performance test with given configuration."""
import time
# Create test data
candidate_logits = torch.randn(
batch_size, seq_len, self.vocab_size, device=self.device
)
reference_logits = [
torch.randn(batch_size, seq_len, self.vocab_size, device=self.device),
torch.randn(batch_size, seq_len, self.vocab_size, device=self.device),
]
# Warm up
_ = self.rouge.rouge_n_batch(candidate_logits, reference_logits, n=1)
# Benchmark
start = time.time()
rouge_1_scores = self.rouge.rouge_n_batch(
candidate_logits, reference_logits, n=1
)
rouge_2_scores = self.rouge.rouge_n_batch(
candidate_logits, reference_logits, n=2
)
rouge_l_scores = self.rouge.rouge_l_batch(candidate_logits, reference_logits)
loss = self.rouge.compute_rouge_loss(candidate_logits, reference_logits)
end = time.time()
# Check that computations complete within reasonable time
elapsed_time = end - start
self.assertLess(elapsed_time, 10.0) # Should complete within 10 seconds
# Check that scores are valid tensors with correct shapes
self.assertEqual(rouge_1_scores["f1"].shape, (batch_size,))
self.assertEqual(rouge_2_scores["f1"].shape, (batch_size,))
self.assertEqual(rouge_l_scores["f1"].shape, (batch_size,))
self.assertTrue(torch.isfinite(loss))
# Check that all scores are in valid range [0, 1]
self.assertTrue(
torch.all(rouge_1_scores["f1"] >= 0)
and torch.all(rouge_1_scores["f1"] <= 1)
)
self.assertTrue(
torch.all(rouge_2_scores["f1"] >= 0)
and torch.all(rouge_2_scores["f1"] <= 1)
)
self.assertTrue(
torch.all(rouge_l_scores["f1"] >= 0)
and torch.all(rouge_l_scores["f1"] <= 1)
)
# Check that loss is in valid range [0, 2] with 0 being best and 2 being worst (ROUGE-1 + ROUGE-L)
self.assertGreaterEqual(loss.item(), 0.0)
self.assertLessEqual(loss.item(), 2.0)
if __name__ == "__main__":
unittest.main()