Repository navigation
Expand file tree
/
Copy pathperplexity.py
More file actions
52 lines (40 loc) · 1.92 KB
/
Copy pathperplexity.py
File metadata and controls
52 lines (40 loc) · 1.92 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
import math
from typing import List, Tuple
from buffdata.models.schemas import DatasetItem
class PerplexityFilter:
def __init__(self, model_id: str = "gpt2"):
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
self.model = AutoModelForCausalLM.from_pretrained(model_id).to(self.device)
self.model.eval()
def calculate_perplexity(self, text: str) -> float:
import torch
if not text.strip():
return float('inf')
inputs = self.tokenizer(text, return_tensors="pt").to(self.device)
max_length = self.model.config.n_positions
# If text is too long, truncate it
if inputs.input_ids.size(1) > max_length:
inputs.input_ids = inputs.input_ids[:, :max_length]
if "attention_mask" in inputs:
inputs.attention_mask = inputs.attention_mask[:, :max_length]
with torch.no_grad():
outputs = self.model(inputs.input_ids, labels=inputs.input_ids)
loss = outputs.loss
return math.exp(loss.item())
def filter_batch(self, items: List[DatasetItem], max_ppl: float) -> Tuple[List[DatasetItem], List[DatasetItem]]:
kept = []
dropped = []
for item in items:
d = item.to_dict()
text = d.get("output") or d.get("text") or d.get("instruction") or ""
ppl = self.calculate_perplexity(text)
item.metadata = item.metadata or {}
item.metadata["perplexity"] = ppl
if ppl <= max_ppl:
kept.append(item)
else:
dropped.append(item)
return kept, dropped