File size: 15,818 Bytes
fef00d1 |
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 |
"""
Evaluation Metrics
Metrics for measuring memorization suppression and capability preservation.
Based on: "From Memorization to Reasoning in the Spectrum of Loss Curvature"
"""
import torch
import torch.nn as nn
from torch import Tensor
from typing import Optional
from dataclasses import dataclass
from tqdm import tqdm
import numpy as np
def levenshtein_distance(seq1: list, seq2: list) -> int:
"""
Compute the Levenshtein (edit) distance between two sequences.
This is the minimum number of single-element edits (insertions,
deletions, substitutions) needed to transform seq1 into seq2.
"""
# Try to use fast C implementation if available
try:
import Levenshtein
# Convert to strings for the library
s1 = " ".join(map(str, seq1))
s2 = " ".join(map(str, seq2))
return Levenshtein.distance(s1, s2)
except ImportError:
pass
# Pure Python implementation
m, n = len(seq1), len(seq2)
# Create distance matrix
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Initialize base cases
for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j
# Fill the matrix
for i in range(1, m + 1):
for j in range(1, n + 1):
if seq1[i - 1] == seq2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(
dp[i - 1][j], # deletion
dp[i][j - 1], # insertion
dp[i - 1][j - 1] # substitution
)
return dp[m][n]
def token_level_levenshtein(generated_ids: list[int], target_ids: list[int]) -> int:
"""Compute Levenshtein distance at the token level."""
return levenshtein_distance(generated_ids, target_ids)
@torch.no_grad()
def generate_greedy(
model: nn.Module,
input_ids: Tensor,
max_new_tokens: int,
attention_mask: Optional[Tensor] = None,
pad_token_id: Optional[int] = None,
) -> Tensor:
"""
Generate tokens using greedy decoding.
Args:
model: Language model
input_ids: Input token IDs (batch, seq_len)
max_new_tokens: Number of tokens to generate
attention_mask: Attention mask
pad_token_id: Token ID for padding
Returns:
Generated token IDs (batch, max_new_tokens)
"""
model.eval()
device = next(model.parameters()).device
input_ids = input_ids.to(device)
if attention_mask is not None:
attention_mask = attention_mask.to(device)
batch_size = input_ids.shape[0]
generated = []
# Use KV cache for efficiency
past_key_values = None
current_input = input_ids
for _ in range(max_new_tokens):
outputs = model(
input_ids=current_input,
attention_mask=attention_mask,
past_key_values=past_key_values,
use_cache=True,
)
# Get logits for last position
logits = outputs.logits[:, -1, :]
# Greedy selection
next_token = logits.argmax(dim=-1, keepdim=True)
generated.append(next_token)
# Update for next iteration
current_input = next_token
past_key_values = outputs.past_key_values
# Update attention mask if provided
if attention_mask is not None:
attention_mask = torch.cat([
attention_mask,
torch.ones((batch_size, 1), device=device, dtype=attention_mask.dtype)
], dim=1)
return torch.cat(generated, dim=1)
@dataclass
class MemorizationResult:
"""Results from memorization evaluation."""
# Metrics
strict_accuracy: float # Exact match rate
loose_accuracy: float # >=threshold match rate
avg_levenshtein: float # Average normalized Levenshtein distance
# Counts
n_samples: int
n_strict_match: int
n_loose_match: int
# Details (optional)
per_sample_results: Optional[list[dict]] = None
def strict_accuracy(
model: nn.Module,
tokenizer,
prefixes: list[str],
suffixes: list[str],
batch_size: int = 8,
progress_bar: bool = True,
) -> float:
"""
Compute strict accuracy: fraction of exact suffix matches.
Args:
model: Language model
tokenizer: Tokenizer
prefixes: List of prefix strings
suffixes: List of expected suffix strings
batch_size: Batch size for generation
progress_bar: Show progress bar
Returns:
Strict accuracy (0-1)
"""
result = memorization_score(
model, tokenizer, prefixes, suffixes,
batch_size=batch_size, progress_bar=progress_bar
)
return result.strict_accuracy
def loose_accuracy(
model: nn.Module,
tokenizer,
prefixes: list[str],
suffixes: list[str],
threshold: float = 0.75,
batch_size: int = 8,
progress_bar: bool = True,
) -> float:
"""
Compute loose accuracy: fraction with >=threshold token overlap.
Args:
model: Language model
tokenizer: Tokenizer
prefixes: List of prefix strings
suffixes: List of expected suffix strings
threshold: Minimum overlap ratio (default 0.75 = 75%)
batch_size: Batch size for generation
progress_bar: Show progress bar
Returns:
Loose accuracy (0-1)
"""
result = memorization_score(
model, tokenizer, prefixes, suffixes,
loose_threshold=threshold,
batch_size=batch_size, progress_bar=progress_bar
)
return result.loose_accuracy
def memorization_score(
model: nn.Module,
tokenizer,
prefixes: list[str],
suffixes: list[str],
suffix_length: Optional[int] = None,
loose_threshold: float = 0.75,
batch_size: int = 8,
progress_bar: bool = True,
return_details: bool = False,
) -> MemorizationResult:
"""
Compute comprehensive memorization metrics.
For each (prefix, suffix) pair:
1. Generate suffix_length tokens given the prefix
2. Compare generated tokens to expected suffix
3. Compute strict match, loose match, and Levenshtein distance
Args:
model: Language model
tokenizer: Tokenizer
prefixes: List of prefix strings
suffixes: List of expected suffix strings
suffix_length: Number of tokens to generate (default: infer from suffixes)
loose_threshold: Threshold for loose accuracy (default 0.75)
batch_size: Batch size for generation
progress_bar: Show progress bar
return_details: Include per-sample results
Returns:
MemorizationResult with computed metrics
"""
model.eval()
device = next(model.parameters()).device
assert len(prefixes) == len(suffixes), "Prefixes and suffixes must have same length"
n_samples = len(prefixes)
# Tokenize suffixes to get target IDs and determine generation length
suffix_ids_list = []
for suffix in suffixes:
ids = tokenizer.encode(suffix, add_special_tokens=False)
suffix_ids_list.append(ids)
if suffix_length is None:
# Use max suffix length
suffix_length = max(len(ids) for ids in suffix_ids_list)
# Process in batches
n_strict = 0
n_loose = 0
total_lev_normalized = 0.0
per_sample = [] if return_details else None
iterator = range(0, n_samples, batch_size)
if progress_bar:
iterator = tqdm(iterator, desc="Evaluating memorization")
for batch_start in iterator:
batch_end = min(batch_start + batch_size, n_samples)
batch_prefixes = prefixes[batch_start:batch_end]
batch_suffix_ids = suffix_ids_list[batch_start:batch_end]
# Tokenize prefixes
encoded = tokenizer(
batch_prefixes,
return_tensors="pt",
padding=True,
truncation=True,
)
input_ids = encoded["input_ids"].to(device)
attention_mask = encoded["attention_mask"].to(device)
# Generate
generated = generate_greedy(
model, input_ids, suffix_length,
attention_mask=attention_mask,
pad_token_id=tokenizer.pad_token_id,
)
# Compare each sample
for i, (gen_ids, target_ids) in enumerate(zip(generated, batch_suffix_ids)):
gen_list = gen_ids.tolist()
target_list = target_ids[:suffix_length] # Truncate target to generation length
# Pad target if shorter
if len(target_list) < len(gen_list):
target_list = target_list + [tokenizer.pad_token_id] * (len(gen_list) - len(target_list))
# Strict match
is_strict = gen_list == target_list
if is_strict:
n_strict += 1
# Levenshtein distance
lev_dist = token_level_levenshtein(gen_list, target_list)
lev_normalized = lev_dist / max(len(gen_list), len(target_list), 1)
total_lev_normalized += lev_normalized
# Loose match: 1 - normalized_distance >= threshold
overlap = 1 - lev_normalized
is_loose = overlap >= loose_threshold
if is_loose:
n_loose += 1
if return_details:
per_sample.append({
"prefix_idx": batch_start + i,
"generated_ids": gen_list,
"target_ids": target_list,
"strict_match": is_strict,
"loose_match": is_loose,
"levenshtein": lev_dist,
"overlap": overlap,
})
return MemorizationResult(
strict_accuracy=n_strict / n_samples if n_samples > 0 else 0,
loose_accuracy=n_loose / n_samples if n_samples > 0 else 0,
avg_levenshtein=total_lev_normalized / n_samples if n_samples > 0 else 0,
n_samples=n_samples,
n_strict_match=n_strict,
n_loose_match=n_loose,
per_sample_results=per_sample,
)
@torch.no_grad()
def perplexity(
model: nn.Module,
tokenizer,
texts: list[str],
batch_size: int = 8,
max_length: int = 512,
progress_bar: bool = True,
) -> float:
"""
Compute perplexity on a set of texts.
Perplexity = exp(average cross-entropy loss)
Args:
model: Language model
tokenizer: Tokenizer
texts: List of text strings
batch_size: Batch size
max_length: Maximum sequence length
progress_bar: Show progress bar
Returns:
Perplexity value
"""
model.eval()
device = next(model.parameters()).device
total_loss = 0.0
total_tokens = 0
iterator = range(0, len(texts), batch_size)
if progress_bar:
iterator = tqdm(iterator, desc="Computing perplexity")
for batch_start in iterator:
batch_end = min(batch_start + batch_size, len(texts))
batch_texts = texts[batch_start:batch_end]
# Tokenize
encoded = tokenizer(
batch_texts,
return_tensors="pt",
padding=True,
truncation=True,
max_length=max_length,
)
input_ids = encoded["input_ids"].to(device)
attention_mask = encoded["attention_mask"].to(device)
# Create labels: set padding positions to -100 so they're ignored in loss
labels = input_ids.clone()
labels[attention_mask == 0] = -100
# Forward pass
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
labels=labels,
)
# Get loss (already averaged over non-padding tokens by the model)
# We need to weight by number of tokens
# Count non-padding tokens (excluding first position since no loss there)
n_tokens = attention_mask[:, 1:].sum().item()
# Accumulate
total_loss += outputs.loss.item() * n_tokens
total_tokens += n_tokens
# Compute perplexity
avg_loss = total_loss / total_tokens if total_tokens > 0 else float('inf')
ppl = np.exp(avg_loss)
return ppl
def perplexity_from_dataset(
model: nn.Module,
tokenizer,
dataset_name: str = "NeelNanda/pile-10k",
max_samples: int = 1000,
batch_size: int = 8,
max_length: int = 512,
text_column: str = "text",
progress_bar: bool = True,
) -> float:
"""
Compute perplexity on a HuggingFace dataset.
Args:
model: Language model
tokenizer: Tokenizer
dataset_name: HuggingFace dataset name
max_samples: Maximum number of samples to use
batch_size: Batch size
max_length: Maximum sequence length
text_column: Name of the text column in the dataset
progress_bar: Show progress bar
Returns:
Perplexity value
"""
from datasets import load_dataset
# Load dataset
ds = load_dataset(dataset_name, split="train")
# Sample if needed
if max_samples and len(ds) > max_samples:
ds = ds.shuffle(seed=42).select(range(max_samples))
# Extract texts
texts = [ex[text_column] for ex in ds]
return perplexity(
model, tokenizer, texts,
batch_size=batch_size,
max_length=max_length,
progress_bar=progress_bar,
)
def evaluate_all(
model: nn.Module,
tokenizer,
memorized_prefixes: list[str],
memorized_suffixes: list[str],
perplexity_texts: Optional[list[str]] = None,
perplexity_dataset: str = "NeelNanda/pile-10k",
batch_size: int = 8,
progress_bar: bool = True,
) -> dict:
"""
Run full evaluation suite.
Args:
model: Language model
tokenizer: Tokenizer
memorized_prefixes: Prefixes for memorization test
memorized_suffixes: Expected suffixes for memorization test
perplexity_texts: Texts for perplexity (if None, uses dataset)
perplexity_dataset: Dataset for perplexity if texts not provided
batch_size: Batch size
progress_bar: Show progress bar
Returns:
Dictionary with all metrics
"""
results = {}
# Memorization metrics
print("Evaluating memorization...")
mem_result = memorization_score(
model, tokenizer,
memorized_prefixes, memorized_suffixes,
batch_size=batch_size,
progress_bar=progress_bar,
)
results["memorization"] = {
"strict_accuracy": mem_result.strict_accuracy,
"loose_accuracy": mem_result.loose_accuracy,
"avg_levenshtein": mem_result.avg_levenshtein,
"n_samples": mem_result.n_samples,
}
# Perplexity
print("Computing perplexity...")
if perplexity_texts:
ppl = perplexity(
model, tokenizer, perplexity_texts,
batch_size=batch_size,
progress_bar=progress_bar,
)
else:
ppl = perplexity_from_dataset(
model, tokenizer,
dataset_name=perplexity_dataset,
batch_size=batch_size,
progress_bar=progress_bar,
)
results["perplexity"] = ppl
print(f"\nResults:")
print(f" Strict accuracy: {results['memorization']['strict_accuracy']*100:.1f}%")
print(f" Loose accuracy: {results['memorization']['loose_accuracy']*100:.1f}%")
print(f" Avg Levenshtein: {results['memorization']['avg_levenshtein']:.3f}")
print(f" Perplexity: {results['perplexity']:.2f}")
return results
|