-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline_run.py
205 lines (159 loc) · 6.2 KB
/
baseline_run.py
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
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
# Define the checkpoint for the Mathstral model
checkpoint = "meta-llama/Llama-3.2-1B-Instruct"
# Set device to GPU (CUDA) if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the tokenizer and model with proper settings for device and dtype
# tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device) # Explicitly move to GPU
zero_shot_prompt = [
{
"role": "system",
"content": (
"You are a math expert. When you respond, respond only with the Solution of the final Problem, thinking "
"step by step. At the end of the Solution, when you give your final answer, write it in the form "
"'Final Answer: The final answer is $answer$. I hope it is correct.'"
)
}
]
problem_prompt = [
{
"role": "user",
"content": (
"Simplify the following expression:\n"
"1/5 ⋅ 2/7 ÷ 12/20."
)
}
]
# Combine prompts into a single input for the model
def run(system_prompt, user_prompt):
full_prompt = system_prompt + user_prompt
pipe = pipeline(
"text-generation",
model= "meta-llama/Llama-3.2-1B-Instruct",
torch_dtype=torch.bfloat16,
device_map="auto",
)
out = pipe(
full_prompt,
max_new_tokens=2048,
)
text = out[0]["generated_text"][-1]['content']
# print('what',text)
# del out
# gc.collect()
# torch.cuda.empty_cache()
# print_memory_stats()
return text
# !tar -xvf MATH.tar
import re
def extract_answer(text):
"""
Extracts the content inside 'boxed{...}' from a given string.
Args:
text (str): The input string containing 'boxed{...}'.
Returns:
str: The extracted content inside 'boxed{...}', or None if no match is found.
"""
# Regular expression to match the content inside 'boxed{...}'
pattern = r'boxed{(.*?)}'
# Use re.search to find the pattern
match = re.search(pattern, text)
# Extract the answer if a match is found
if match:
return match.group(1) # The first capturing group
else:
return None
# Example usage:
text = "adasbda boxed{answer} asdasda"
answer = extract_answer(text)
if answer:
print(f"Extracted answer: {answer}")
else:
print("No match found")
import os
import json
import gc
import torch
gc.collect() # These commands help you when you face CUDA OOM error
torch.cuda.empty_cache()
# Define the path to the directory containing JSON files
directory_path = "MATH/train/algebra"
# File where answers will be stored
output_file = "answers_unique.json"
# Initialize counters and answer lists
count = 0
correct1 = 0
correct2 = 0
def print_memory_stats():
allocated_memory = torch.cuda.memory_allocated() / (1024 ** 3) # Convert to GB
reserved_memory = torch.cuda.memory_reserved() / (1024 ** 3) # Convert to GB
free_memory = reserved_memory - allocated_memory
print(f"Allocated Memory: {allocated_memory:.2f} GB")
print(f"Reserved Memory: {reserved_memory:.2f} GB")
print(f"Free Memory: {free_memory:.2f} GB")
# Function to save lists to a JSON file
def save_answers_to_file(oracle_answers, first_answers, second_answers, output_file):
if os.path.exists(output_file):
with open(output_file, 'r') as f:
data = json.load(f)
else:
data = {
"oracle_answers": [],
"first_answers": [],
"second_answers": []
}
# Append new data to existing lists
data["oracle_answers"].extend([oracle_answers])
data["first_answers"].extend([first_answers])
data["second_answers"].extend([second_answers])
# Write the updated data back to the file
with open(output_file, 'w') as f:
json.dump(data, f, indent=4)
# Iterate over all files in the directory
for i, filename in enumerate(os.listdir(directory_path)):
print(i)
if i<679:
continue
# gc.collect() # These commands help you when you face CUDA OOM error
# torch.cuda.empty_cache()
if filename.endswith(".json"): # Check if the file is a JSON file
file_path = os.path.join(directory_path, filename)
# Open and load the JSON file into a dictionary
with open(file_path, 'r') as f:
json_data = json.load(f)
problem_prompt = [
{
"role": "user",
"content": (json_data['problem'])
}
]
oracle_answer = json_data['solution']
oracle_single_answer = extract_answer(oracle_answer)
model_answer = run(zero_shot_prompt, problem_prompt)
model_single_answer = extract_answer(model_answer)
# Append the answers
first_answer = model_answer
count += 1
if oracle_single_answer == model_single_answer:
correct1 += 1
print(f"Accuracy1: {correct1/count:.2f}")
# Self-correction process
self_correction_text = 'There might be an error in the solution above because of lack of understanding of the question. Please correct the error, if any, and rewrite the solution. Only output the final solution! At the end of the Solution, when you give your final answer, write it in the form "Final Answer: The final answer is $answer$. I hope it is correct."'
model_answer_prompt = [
{
"role": "assistant",
"content": (model_answer)
}
]
self_correction_prompt = [
{
"role": "user",
"content": (self_correction_text)
}
]
model_self_corrected_answer = run(zero_shot_prompt+problem_prompt+model_answer_prompt, self_correction_prompt)
second_answer = model_self_corrected_answer
# Save the answers to the file after processing each JSON file
save_answers_to_file(oracle_answer, first_answer, second_answer, output_file)