-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprepare_self_play_data.py
More file actions
258 lines (199 loc) · 8.74 KB
/
prepare_self_play_data.py
File metadata and controls
258 lines (199 loc) · 8.74 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
import json
import re
from tqdm import tqdm
import random
import argparse
from utils import load_test_problems, fix_qwq_completion, get_after_think
from eval.qwen_math import extract_answer, strip_string
from env_config import load_env, env_str
if __name__ == "__main__":
load_env()
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str, default=env_str("PREPARE_SELF_PLAY_DATA_DATA_PATH") or env_str("DATA_PATH", default=""))
parser.add_argument("--output_path", type=str, default=env_str("PREPARE_SELF_PLAY_DATA_OUTPUT_PATH") or env_str("OUTPUT_PATH", default=""))
args = parser.parse_args()
# Chinese (Han) ranges + Compatibility + most extensions (BMP + SMP)
_CHINESE_RE = re.compile(r"[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\U00020000-\U0002EBEF]")
# Korean (Hangul) ranges: Jamo, Compatibility Jamo, Extended-A/B, Syllables
_KOREAN_RE = re.compile(r"[\u1100-\u11FF\u3130-\u318F\uA960-\uA97F\uAC00-\uD7AF\uD7B0-\uD7FF]")
def contains_chinese_or_korean(seq) -> bool:
"""Return True if the given sequence (string or iterable of strings/chars)
contains any Chinese (Han) or Korean (Hangul) characters."""
if isinstance(seq, str):
text = seq
else:
try:
text = "".join(seq)
except TypeError:
text = str(seq)
return bool(_CHINESE_RE.search(text) or _KOREAN_RE.search(text))
def extract_code(text: str) -> str:
outputlines = text.split("\n")
indexlines = [i for i, line in enumerate(outputlines) if "```" in line]
if len(indexlines) < 2:
return ""
return "\n".join(outputlines[indexlines[-2] + 1:indexlines[-1]])
def is_valid(text, source="PromptCoT-Math"):
pattern = r'^<think>(.*?)</think>(.+)$'
match = re.match(pattern, text, re.DOTALL)
if not match:
return False
# Extract the content inside and after the think tags
inside_content = match.group(1)
after_content = match.group(2)
# Verify inside content is not empty
if not inside_content.strip():
return False
# Verify neither part contains additional think tags
if '<think>' in inside_content or '</think>' in inside_content:
return False
if '<think>' in after_content or '</think>' in after_content:
return False
text = text.split("</think>")[-1]
if contains_chinese_or_korean(text):
return False
if source == "PromptCoT-Math" or source == "OpenSource-Math":
prediction = extract_answer(text)
prediction = strip_string(prediction)
if prediction is None or not prediction:
return False
elif source == "PromptCoT-Code" or source == "OpenSource-Code":
prediction = extract_code(text)
if prediction is None or not prediction:
return False
else:
raise ValueError
return True
def qwq_prompt(problem):
template = (
f"<|im_start|>user\n{problem}<|im_end|>\n"
"<|im_start|>assistant\n"
)
return template
def extract_problem(problem):
if problem.endswith("\nPlease reason step by step, and put your final answer within \\boxed{}."):
problem = problem[:-len("\nPlease reason step by step, and put your final answer within \\boxed{}.")]
return problem.strip()
def no_solution(answer):
if "none" in answer.lower():
return True
if "no" in answer.lower():
return True
return False
def trivial_solution(answer):
if answer in ["0", "1"]:
return True
return False
test_problems = load_test_problems()
results = []
promptcot_count = 0
opensource_count = 0
with open(args.data_path, encoding="utf-8") as f:
for line in tqdm(f.readlines()):
item = json.loads(line)
source = item["source"]
prompt = item["prompt"]
completions = item["completions"]
test_results = item["test_results"]
if source == "PromptCoT-Math":
if extract_problem(item["prompt"]) in test_problems:
print("promptcot hit!")
continue
if len(test_results) == 0:
print("No answers.")
continue
if "the following conditions:\nPlease reason step by step" in prompt:
print("No solutions.")
continue
if "the following properties:\nPlease reason step by step" in prompt:
print("No solutions.")
continue
if "solution to\nPlease reason step by step" in prompt:
print("No solutions.")
continue
if "the following properties:\nFind" in prompt:
print("No solutions.")
continue
if "prove that" in prompt or "Prove that" in prompt:
print("No solutions.")
continue
# answer = None
# for completion, res in zip(completions, test_results):
# if res == "pass":
# answer = extract_answer(get_after_think(completion))
# break
if item["answer"] is None or item["answer"].strip() == "":
continue
if no_solution(item["answer"]):
print("No solutions.")
continue
if trivial_solution(item["answer"]):
print("Trivial solutions.")
continue
max_count = 10000 ### Note: if the answer is got through majority voting, max_count should be 2 or 4
elif source == "OpenSource-Math":
if extract_problem(item["prompt"]) in test_problems:
print("opensource hit!")
continue
if len(test_results) == 0:
print("No answers.")
continue
if item["answer"] is None or item["answer"].strip() == "":
continue
max_count = 10000
elif source == "PromptCoT-Code":
if extract_problem(item["prompt"]) in test_problems:
print("promptcot hit!")
continue
if len(test_results) == 0:
print("No answers.")
continue
max_count = 10000
elif source == "OpenSource-Code":
if extract_problem(item["prompt"]) in test_problems:
print("opensource hit!")
continue
if len(test_results) == 0:
print("No answers.")
continue
max_count = 10000
else:
raise ValueError
if sum([res == "pass" for res in test_results]) / len(test_results) > 1 / 2:
continue
if sum([res == "pass" for res in test_results]) == 0:
continue
chosen, rejected = set(), set()
for completion, test_res in zip(completions, test_results):
completion = fix_qwq_completion(completion)
if test_res == "pass":
if is_valid(completion, source):
chosen.add(completion)
else:
if is_valid(completion, source):
rejected.add(completion)
chosen, rejected = list(chosen), list(rejected)
# if len(chosen) == 1 and source == "PromptCoT-Math":
# print("No majority voted answers.")
# continue
if len(chosen) == 0 or len(rejected) == 0:
print("No valid completions.")
continue
n_pairs = min(max_count, min(len(chosen), len(rejected)))
for i in range(n_pairs):
if source == "PromptCoT-Math" or source == "PromptCoT-Code":
promptcot_count += 1
else:
opensource_count += 1
results.append({
"source": source,
"prompt": qwq_prompt(prompt).strip(),
"chosen": "\n" + chosen[i],
"rejected": "\n" + rejected[i]
})
print("PromptCoT2:", promptcot_count)
print("OpenSource: ", opensource_count)
random.shuffle(results)
with open(args.output_path, "w", encoding="utf-8") as f:
for item in results:
f.write(json.dumps(item) + "\n")