-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
162 lines (132 loc) · 5.1 KB
/
Copy pathutils.py
File metadata and controls
162 lines (132 loc) · 5.1 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
from tqdm import tqdm
import io
import json
import multiprocessing
import os
import sys
import torch
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(seed)
torch.manual_seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
SEED = 42
set_seed(SEED)
def read_jsonl(file_path):
data = []
with open(file_path, 'r') as f:
for line in f:
data.append(json.loads(line))
return data
def execute_code(code, output_queue):
"""Executes the given code and puts the result in the queue."""
stdout_capture = io.StringIO()
original_stdout = sys.stdout # Save original stdout
try:
sys.stdout = stdout_capture # Redirect stdout
exec(code) # Execute the code
output_queue.put((True, stdout_capture.getvalue()))
except Exception as e:
output_queue.put((False, str(e)))
finally:
sys.stdout = original_stdout # Always restore stdout
def get_python_code_output(code, timeout=3):
"""
Runs Python code and captures output, skipping code that waits for stdin.
Times out after the specified duration.
"""
output_queue = multiprocessing.Queue()
process = multiprocessing.Process(target=execute_code, args=(code, output_queue))
process.start()
process.join(timeout)
# If process is still alive after timeout, terminate it
if process.is_alive():
process.terminate() # Kill the process
process.join() # Ensure cleanup
return None # Skipping code due to timeout
# Retrieve results from the queue
if not output_queue.empty():
success, result = output_queue.get()
return result if success else None
return None
def extract_code_block(text, start_delimiter="```python", end_delimiter="```"):
"""Extracts text between start_delimiter and end_delimiter."""
try:
start_index = text.index(start_delimiter) + len(start_delimiter)
end_index = text.index(end_delimiter, start_index)
return text[start_index:end_index].strip()
except ValueError:
return None
def safe_parse_float(value):
try:
return float(value)
except (ValueError, TypeError):
return None
def calculate_conditional_perplexity(prefix, generation, model, tokenizer):
"""
Calculate the conditional perplexity of a generation given its prefix.
Args:
prefix (str): The prompt/prefix text used to generate the completion
generation (str): The generated completion text
model: The language model
tokenizer: The tokenizer associated with the model
Returns:
float: The conditional perplexity score
"""
# Tokenize the full sequence (prefix + generation)
full_text = prefix + generation
encodings = tokenizer(full_text, return_tensors="pt")
input_ids = encodings["input_ids"].to("cuda")
# Tokenize just the prefix to find its length
prefix_encodings = tokenizer(prefix, return_tensors="pt")
prefix_length = prefix_encodings["input_ids"].shape[1]
# Create labels: -100 for prefix tokens (ignored in loss calculation)
# and actual token ids for generation
labels = input_ids.clone()
labels[:, :prefix_length] = -100
# Calculate log-likelihood
with torch.no_grad():
outputs = model(input_ids=input_ids, labels=labels)
neg_log_likelihood = outputs.loss.item()
# Calculate perplexity only on the generated portion
ppl = torch.exp(torch.tensor(neg_log_likelihood)).item()
return ppl
def evaluate_generations(prefixes, generations, model, tokenizer):
"""
Calculate conditional perplexity for a list of prefix-generation pairs.
Args:
prefixes (list): List of prefixes
generations (list): List of generated texts
model: The language model
tokenizer: The tokenizer associated with the model
Returns:
dict: Dictionary containing mean perplexity and individual scores
"""
perplexities = []
total = min(len(prefixes), len(generations))
for prefix, gen in tqdm(zip(prefixes, generations), desc="Calculating perplexity", total=total):
try:
ppl = calculate_conditional_perplexity(prefix, gen, model, tokenizer)
perplexities.append(ppl)
except Exception as e:
print(f"Error calculating perplexity: {e}")
perplexities.append(None)
# Filter out None values for mean calculation
valid_perplexities = [p for p in perplexities if p is not None]
mean_ppl = sum(valid_perplexities) / len(valid_perplexities) if valid_perplexities else None
return {
"mean_perplexity": mean_ppl,
"individual_perplexities": perplexities
}
def save_results(results, file_path):
"""
Save results to a JSON file.
Args:
results (dict): Dictionary containing perplexity results
file_path (str): Json file path to save results
"""
with open(file_path, "w") as f:
json.dump(results, f, indent=4)