chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import collections
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
The output file should be processed by post_processors.openai_api_callback.MathScaleCallBack
|
||||
"""
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list), preds
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--maj_ks", type=str, default="8,16,32,64,128")
|
||||
parser.add_argument("--id_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in sorted(glob(args.input_file)):
|
||||
if "metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
print(len(data))
|
||||
data = merge_seed_sampled_data(data)
|
||||
print(len(data))
|
||||
|
||||
id_data = json.load(open(args.id_file))
|
||||
all_ids = set([item["id"] for item in id_data])
|
||||
data = [item for item in data if item["id"] in all_ids]
|
||||
|
||||
maj_ks = list(map(int, args.maj_ks.split(",")))
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
maj_at_k = {k: 0 for k in maj_ks}
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
avg_solution_num = 0
|
||||
for item in tqdm(data):
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
|
||||
if "data_topic" in item:
|
||||
acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
cnt_data_topic[item["data_topic"]] += 1
|
||||
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
|
||||
if isinstance(item["pred"], str):
|
||||
item["pred"] = [item["pred"]]
|
||||
|
||||
pred2res = {pred: res[j] for j, pred in enumerate(item["pred"])}
|
||||
|
||||
avg_solution_num += len(item["pred"])
|
||||
|
||||
for _k in maj_ks:
|
||||
k_preds = item["pred"][:_k]
|
||||
k_sc_pred = majority_voting_frequency(k_preds)[0][0]
|
||||
k_sc_res = pred2res[k_sc_pred]
|
||||
if k_sc_res:
|
||||
maj_at_k[_k] += 1
|
||||
|
||||
sc_pred = majority_voting_frequency(item["pred"])[0][0]
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_res"] = pred2res[sc_pred]
|
||||
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(data, open(args.output_file, "w"))
|
||||
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for k, v in acc_data_topic.items():
|
||||
metrics[f"acc_{k}"] = v / cnt_data_topic[k]
|
||||
metrics[f"total_{k}"] = cnt_data_topic[k]
|
||||
metrics["avg_solution_num"] = avg_solution_num / len(data)
|
||||
for _k in maj_ks:
|
||||
metrics[f"maj@{_k}"] = maj_at_k[_k] / len(data)
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(len(data))
|
||||
print(metrics)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,73 @@
|
||||
import collections
|
||||
import json
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import argparse
|
||||
from typing import Dict, List
|
||||
|
||||
|
||||
def draw_line_plot(data, file_path, baseline, baseline_label):
|
||||
all_colors = ["#AFEEEE", "#4682B4", "#6A5ACD", "#BA55D3", "#3CB371"]
|
||||
|
||||
# Compute histogram data
|
||||
x_real = np.array([4, 8, 16, 32, 64, 128])
|
||||
x = np.arange(6)
|
||||
|
||||
# Create the line plot
|
||||
plt.figure(figsize=(4, 4))
|
||||
# plt.plot(x, data, color='#658873', marker='o', linestyle='-')
|
||||
for i, (label, values) in enumerate(data.items()):
|
||||
plt.plot(x, values, color=all_colors[i], marker='.', linestyle='-', label=label)
|
||||
|
||||
baseline = np.ones_like(list(data.values())[0]) * baseline
|
||||
plt.plot(x, baseline, linestyle='--', color=all_colors[-1], label=baseline_label)
|
||||
|
||||
# Change the size of the font in the legent
|
||||
plt.legend(prop={"size": 15})
|
||||
plt.xticks(x, x_real)
|
||||
plt.tick_params(axis='x', labelsize=15) # Change the font size of the x-axis scale
|
||||
plt.tick_params(axis='y', labelsize=15) # Change the font size of the y-axis scale
|
||||
plt.grid(True)
|
||||
|
||||
# Add labels and title
|
||||
plt.xlabel('Sample@K', fontsize=16)
|
||||
plt.ylabel('Accuracy', fontsize=16)
|
||||
plt.legend(loc='center right')
|
||||
|
||||
plt.savefig(file_path, bbox_inches='tight', dpi=800, format='png')
|
||||
|
||||
|
||||
def main():
|
||||
# data = {
|
||||
# "self-consistency": [65.96, 69.30, 71.14, 72.30, 72.28, 72.52],
|
||||
# "BoN weighted": [66.98, 69.50, 71.32, 72.42, 72.54, 72.66],
|
||||
# }
|
||||
# greedy_decoding = 65.50
|
||||
#
|
||||
# draw_line_plot(data, "mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42.png", greedy_decoding, "greedy decoding")
|
||||
|
||||
# data = {
|
||||
# "self-consistency": [67.86, 70.26, 71.54, 72.38, 72.58, 72.88],
|
||||
# "BoN weighted": [68.84, 70.84, 71.66, 72.24, 72.70, 72.96],
|
||||
# }
|
||||
# greedy_decoding = 66.72
|
||||
#
|
||||
# draw_line_plot(data, "mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42.png", greedy_decoding, "greedy decoding")
|
||||
|
||||
# data = {
|
||||
# "self-consistency": [68.76, 70.62, 71.72, 72.40, 72.50, 72.58],
|
||||
# "BoN weighted": [69.22, 70.90, 72.00, 72.36, 72.56, 72.66],
|
||||
# }
|
||||
# greedy_decoding = 67.50
|
||||
|
||||
data = {
|
||||
"self-consistency": [60.34, 65.92, 68.40, 69.66, 70.52, 71.02],
|
||||
"BoN weighted": [62.32, 66.70, 68.48, 69.68, 70.36, 71.20],
|
||||
}
|
||||
greedy_decoding = 61.42
|
||||
|
||||
draw_line_plot(data, "mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42.png", greedy_decoding, "greedy decoding")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,149 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import collections
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
The output file should be processed by post_processors.openai_api_callback.MathScaleCallBack
|
||||
"""
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list), preds
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--maj_k", type=int, default=16)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in sorted(glob(args.input_file)):
|
||||
if "metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
print(len(data))
|
||||
data = merge_seed_sampled_data(data)
|
||||
print(len(data))
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
maj_at_k = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
avg_solution_num = 0
|
||||
outputs = []
|
||||
for item in tqdm(data):
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
|
||||
if "data_topic" in item:
|
||||
acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
cnt_data_topic[item["data_topic"]] += 1
|
||||
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
|
||||
if isinstance(item["pred"], str):
|
||||
item["pred"] = [item["pred"]]
|
||||
|
||||
avg_solution_num += len(item["pred"])
|
||||
|
||||
k_preds = item["pred"][:args.maj_k]
|
||||
k_sc_pred = majority_voting_frequency(k_preds)[0][0]
|
||||
k_sc_res = mathscale_is_equiv(k_sc_pred, item["label"])[0]
|
||||
if k_sc_res:
|
||||
maj_at_k += 1
|
||||
else:
|
||||
outputs.append(item)
|
||||
|
||||
item[f"sc_pred@{args.maj_k}"] = k_sc_pred
|
||||
item[f"sc_res@{args.maj_k}"] = k_sc_res
|
||||
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(outputs, open(args.output_file, "w"))
|
||||
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for k, v in acc_data_topic.items():
|
||||
metrics[f"acc_{k}"] = v / cnt_data_topic[k]
|
||||
metrics[f"total_{k}"] = cnt_data_topic[k]
|
||||
metrics["avg_solution_num"] = avg_solution_num / len(data)
|
||||
metrics[f"maj@{args.maj_k}"] = maj_at_k / len(data)
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(len(data))
|
||||
print(len(outputs))
|
||||
print(metrics)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,110 @@
|
||||
import collections
|
||||
import json
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
|
||||
def plot_bar_chart(data, file_path):
|
||||
indices = list(range(len(data)))
|
||||
|
||||
# Create the bar chart
|
||||
plt.figure(figsize=(4, 4))
|
||||
bars = plt.bar(indices, data)
|
||||
|
||||
# Customize the chart
|
||||
plt.title('Bar Chart of Array Values')
|
||||
plt.xlabel('Frequency', fontsize=16)
|
||||
plt.ylabel('Amount Difference between Correct and Incorrect Prediction', fontsize=12)
|
||||
|
||||
# Add value labels on top of each bar
|
||||
# for i, bar in enumerate(bars):
|
||||
# height = bar.get_height()
|
||||
# plt.text(bar.get_x() + bar.get_width() / 2., height,
|
||||
# f'{data[i]}',
|
||||
# ha='center', va='bottom',
|
||||
# fontsize=12)
|
||||
|
||||
# Color positive and negative bars differently
|
||||
for i, value in enumerate(data):
|
||||
if value >= 0:
|
||||
bars[i].set_color('blue')
|
||||
else:
|
||||
bars[i].set_color('red')
|
||||
|
||||
plt.tick_params(axis='x', labelsize=15) # Change the font size of the x-axis scale
|
||||
plt.tick_params(axis='y', labelsize=15) # Change the font size of the y-axis scale
|
||||
|
||||
plt.savefig(file_path, bbox_inches='tight', dpi=800, format='png')
|
||||
|
||||
|
||||
def draw_line_plot(data, file_path):
|
||||
# Compute histogram data
|
||||
x = np.arange(0.1, 1.1, 0.1)
|
||||
|
||||
# Create the line plot
|
||||
plt.figure(figsize=(4, 4))
|
||||
plt.plot(x, data, color='#658873', marker='o', linestyle='-')
|
||||
|
||||
# Change the size of the font in the legent
|
||||
# plt.legend(prop={"size": 15})
|
||||
plt.tick_params(axis='x', labelsize=15) # Change the font size of the x-axis scale
|
||||
plt.tick_params(axis='y', labelsize=15) # Change the font size of the y-axis scale
|
||||
plt.grid(True)
|
||||
|
||||
# Add labels and title
|
||||
# plt.xlabel('Top-1 Averaged Frequency', fontsize=16)
|
||||
plt.ylabel('Ratio of Correct Predictions', fontsize=16)
|
||||
# plt.legend(loc='upper left')
|
||||
|
||||
plt.savefig(file_path, bbox_inches='tight', dpi=800, format='png')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--n", type=int, default=128)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = json.load(open(args.input_file))
|
||||
|
||||
freq_pos = collections.Counter()
|
||||
freq_neg = collections.Counter()
|
||||
freq = collections.Counter()
|
||||
for item in data:
|
||||
if item["sc_res"]:
|
||||
freq_pos[item["sc_freq"]] += 1
|
||||
else:
|
||||
freq_neg[item["sc_freq"]] += 1
|
||||
freq[item["sc_freq"]] += 1
|
||||
|
||||
diffs = []
|
||||
for i in range(args.n + 1):
|
||||
tmp = 0
|
||||
if i in freq_pos:
|
||||
tmp += freq_pos[i]
|
||||
if i in freq_neg:
|
||||
tmp -= freq_neg[i]
|
||||
diffs.append(tmp)
|
||||
|
||||
plot_bar_chart(diffs, args.output_file)
|
||||
|
||||
ps = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
|
||||
reversed_acc_pos = collections.Counter()
|
||||
reversed_acc = collections.Counter()
|
||||
for p in ps:
|
||||
for key, value in freq_pos.items():
|
||||
if key / args.n >= p:
|
||||
reversed_acc_pos[p] += value
|
||||
for key, value in freq.items():
|
||||
if key / args.n >= p:
|
||||
reversed_acc[p] += value
|
||||
|
||||
p_ratio = {p: reversed_acc_pos[p] / reversed_acc[p] for p in ps}
|
||||
|
||||
draw_line_plot(list(p_ratio.values()), args.output_file.replace(".png", "_line.png"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,149 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from glob import glob
|
||||
from functools import partial
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list), preds
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def worker(item, n: int):
|
||||
sc_pred, sc_freq = majority_voting_frequency(item["pred"][:n])[0]
|
||||
sc_res = mathscale_is_equiv(sc_pred, item["label"])[0]
|
||||
return {
|
||||
"sc_res": sc_res,
|
||||
"sc_pred": sc_pred,
|
||||
"sc_freq": sc_freq,
|
||||
"id": item["id"],
|
||||
}
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--ps", type=str,
|
||||
help="The probabilities of top-1 frequency over total samples, split by comma")
|
||||
parser.add_argument("--num_workers", type=int, default=8)
|
||||
parser.add_argument("--n", type=int, default=128)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in sorted(glob(args.input_file)):
|
||||
print(file)
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
data = merge_seed_sampled_data(data)
|
||||
|
||||
pbar = tqdm(data)
|
||||
outputs = []
|
||||
sc = 0
|
||||
num_pred = 0
|
||||
freq_pos = collections.Counter()
|
||||
freq = collections.Counter()
|
||||
with ThreadPoolExecutor(max_workers=args.num_workers) as executor:
|
||||
futures = []
|
||||
_annotate = partial(worker, n=args.n)
|
||||
for _input in pbar:
|
||||
future = executor.submit(_annotate, _input)
|
||||
num_pred += len(_input["response"])
|
||||
futures.append(future)
|
||||
pbar.update()
|
||||
|
||||
for future in tqdm(as_completed(futures), total=len(futures), desc="Collecting results"):
|
||||
result = future.result()
|
||||
outputs.append(result)
|
||||
|
||||
if result["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
if result["sc_res"]:
|
||||
freq_pos[result["sc_freq"]] += 1
|
||||
freq[result["sc_freq"]] += 1
|
||||
|
||||
ps = list(map(float, args.ps.split(",")))
|
||||
reversed_acc_pos = collections.Counter()
|
||||
reversed_acc = collections.Counter()
|
||||
for p in ps:
|
||||
for key, value in freq_pos.items():
|
||||
if key / args.n >= p:
|
||||
reversed_acc_pos[p] += value
|
||||
for key, value in freq.items():
|
||||
if key / args.n >= p:
|
||||
reversed_acc[p] += value
|
||||
|
||||
p_ratio = {p: reversed_acc_pos[p] / reversed_acc[p] for p in ps}
|
||||
|
||||
print(f"Reversed Acc Pos Ratio: {p_ratio}")
|
||||
|
||||
print(f"SC: {sc}/{len(data)} = {sc / len(data)}")
|
||||
print(f"Num Pred: {num_pred}/{len(data)} = {num_pred / len(data)}")
|
||||
|
||||
json.dump(outputs, open(args.output_file, "w"), ensure_ascii=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,71 @@
|
||||
python scripts/math_scale/analyze/extract_hard_questions.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.json \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json \
|
||||
--maj_k 16
|
||||
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
|
||||
python scripts/math_scale/qwen25math_style_eval_math.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json \
|
||||
--num_workers 1
|
||||
#{
|
||||
# "acc": 0.6292,
|
||||
# "pass@k": 0.6292,
|
||||
# "maj@k": 0.6292,
|
||||
# "correct": 3146,
|
||||
# "total": 5000
|
||||
#}
|
||||
# Compared with original: 61.4
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.json \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
|
||||
python scripts/math_scale/qwen25math_style_eval_math.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json \
|
||||
--num_workers 1
|
||||
#{
|
||||
# "acc": 0.6702,
|
||||
# "pass@k": 0.6702,
|
||||
# "maj@k": 0.6702,
|
||||
# "correct": 3351,
|
||||
# "total": 5000
|
||||
#}
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.json \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
|
||||
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
python scripts/math_scale/qwen25math_style_eval_math.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json \
|
||||
--num_workers 1
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.json \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
|
||||
# mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
python scripts/math_scale/qwen25math_style_eval_math.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json \
|
||||
--num_workers 1
|
||||
python scripts/math_scale/analyze/compute_acc_by_id.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.json \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/math.test.v1.1.0shot.n1.tem0.0.p1.0.sympy_eval.sft-k16-fail.json \
|
||||
--id_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/math/math.test.v1.1.0shot.n128.tem1.0.p1.0.k16-fail.json
|
||||
@@ -0,0 +1,24 @@
|
||||
#exp_dir=llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/
|
||||
#exp_dir=llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/
|
||||
exp_dir=llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/
|
||||
|
||||
|
||||
#python scripts/math_scale/analyze/get_output_frequency.py \
|
||||
# --input_file "../msranlpintern/reward_modeling/experiments/${exp_dir}/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*[0-9].json" \
|
||||
# --output_file ../msranlpintern/reward_modeling/experiments/${exp_dir}/math/math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.json \
|
||||
# --n 128 --ps "0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0"
|
||||
|
||||
|
||||
#python scripts/math_scale/analyze/freq2image.py \
|
||||
# --input_file ../msranlpintern/reward_modeling/experiments/${exp_dir}/math/math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.json \
|
||||
# --output_file ./math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.iter0.png
|
||||
|
||||
#exp_dir=llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/
|
||||
#python scripts/math_scale/analyze/freq2image.py \
|
||||
# --input_file ../msranlpintern/reward_modeling/experiments/${exp_dir}/math/math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.json \
|
||||
# --output_file ./math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.iter1.png
|
||||
#
|
||||
#exp_dir=llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/
|
||||
python scripts/math_scale/analyze/freq2image.py \
|
||||
--input_file ../msranlpintern/reward_modeling/experiments/${exp_dir}/math/math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.json \
|
||||
--output_file ./math.test.v1.1.0shot.n64.tem1.0.p1.0.analyze.output_freq.iter2.png
|
||||
@@ -0,0 +1,26 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import sys
|
||||
|
||||
sys.set_int_max_str_digits(0)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str, required=True)
|
||||
parser.add_argument("--output_file", type=str, required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
print(file)
|
||||
data.extend(json.load(open(file)))
|
||||
|
||||
print(len(data))
|
||||
print(data[0].keys())
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,401 @@
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from glob import glob
|
||||
|
||||
|
||||
# sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
#
|
||||
# from data.math_util import is_equiv
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file, recursive=True):
|
||||
print(file)
|
||||
# data += json.load(open(file))
|
||||
f = open(file, "r")
|
||||
try:
|
||||
data += json.load(f)
|
||||
except:
|
||||
print(f"Error in file {file}")
|
||||
new_file = file.replace(".json", ".jsonl")
|
||||
lines = open(new_file, "r").readlines()
|
||||
for line in lines:
|
||||
try:
|
||||
data.append(json.loads(line))
|
||||
except:
|
||||
print(f"Error in line: {line}")
|
||||
|
||||
data = merge_seed_sampled_data(data)
|
||||
|
||||
outputs = []
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
num_pairs = 0
|
||||
pos_missing = 0
|
||||
neg_missing = 0
|
||||
full_positive_samples = []
|
||||
full_negative_samples = []
|
||||
for item in data:
|
||||
if "res" not in item:
|
||||
raise RuntimeError("Use `fix_answer_extract_and_verify.py` to add `res` field to the input file")
|
||||
# if isinstance(item["pred"], list):
|
||||
# preds = item["pred"]
|
||||
# else:
|
||||
# preds = [item["pred"]]
|
||||
#
|
||||
# res = [is_equiv(p, str(item["label"])) for p in preds]
|
||||
# if isinstance(item["pred"], str):
|
||||
# res = res[0]
|
||||
# item["res"] = res
|
||||
|
||||
if not item["res"]:
|
||||
continue
|
||||
|
||||
if item["res"][0]:
|
||||
cnt += 1
|
||||
if any(item["res"]):
|
||||
pass_at_k += 1
|
||||
|
||||
pos = []
|
||||
neg = []
|
||||
for resp, r in zip(item["response"], item["res"]):
|
||||
if r:
|
||||
pos.append(resp)
|
||||
else:
|
||||
neg.append(resp)
|
||||
|
||||
if len(pos) == 0:
|
||||
full_negative_samples.append(item)
|
||||
pos_missing += 1
|
||||
if len(neg) == 0:
|
||||
neg_missing += 1
|
||||
full_positive_samples.append(item)
|
||||
if len(pos) == 0 or len(neg) == 0:
|
||||
continue
|
||||
|
||||
item["pos"] = pos
|
||||
item["neg"] = neg
|
||||
num_pairs += len(pos) * len(neg)
|
||||
outputs.append(item)
|
||||
|
||||
print(f"Total number of items: {len(data)}")
|
||||
print(f"Acc: {cnt / len(data)}")
|
||||
print(f"Pass at k: {pass_at_k / len(data)}")
|
||||
print(f"No positive solutions: {pos_missing} / {len(data)}")
|
||||
print(f"No negative solutions: {neg_missing} / {len(data)}")
|
||||
print(f"Num pairs: {num_pairs}")
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
json.dump(full_positive_samples[:100], open(args.output_file.replace(".json", ".pos.sample.json"), "w"), indent=2)
|
||||
json.dump(full_negative_samples[:100], open(args.output_file.replace(".json", ".neg.sample.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
"""
|
||||
>>> python scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.0-of-8.json" \
|
||||
--output_file ../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.json
|
||||
|
||||
Total number of items: 1250
|
||||
Acc: 0.8544
|
||||
Pass at k: 0.9696
|
||||
Missing: 825 / 1250
|
||||
Num pairs: 2022
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.fix_predict.json" \
|
||||
--output_file ../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.fix_predict.dpo.json
|
||||
|
||||
Total number of items: 10000
|
||||
Acc: 0.7141
|
||||
Pass at k: 0.8515
|
||||
Missing: 6383 / 10000
|
||||
Num pairs: 17630
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair.py --input_file "../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.*-of-16.json" --output_file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.1-of-30.v1.2.0shot.n10.tem1.0.p0.9.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.0-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.1-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.10-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.11-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.12-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.13-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.14-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.15-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.2-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.3-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.4-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.5-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.6-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.7-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.8-of-16.json
|
||||
Error in file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/train.v60.300k.2-of-30.v1.2.0shot.n10.tem1.0.p0.9.9-of-16.json
|
||||
Total number of items: 10000
|
||||
Acc: 0.6983
|
||||
Pass at k: 0.8525
|
||||
Missing: 5670 / 10000
|
||||
Num pairs: 66254
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/all_splits/train.v60.300k.all.v1.2.0shot.n10.tem1.0.p0.9.*-of-100.json" \
|
||||
--output_file ../msranlpintern/share/models/mathstral-7B-v0.1/mathscale/all_splits/train.v60.300k.all.v1.2.0shot.n10.tem1.0.p0.9.dpo_v1.0.json
|
||||
|
||||
Total number of items: 300000
|
||||
Acc: 0.4003566666666667
|
||||
Pass at k: 0.5258733333333333
|
||||
No positive solutions: 142238 / 300000
|
||||
No negative solutions: 71338 / 300000
|
||||
Num pairs: 1361840
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "./500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.?-of-8.json" \
|
||||
--output_file train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-0-of-20/train.500k.de_con.boxed.v1.0.0-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-1-of-20/train.500k.de_con.boxed.v1.0.1-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-10-of-20/train.500k.de_con.boxed.v1.0.10-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-11-of-20/train.500k.de_con.boxed.v1.0.11-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-12-of-20/train.500k.de_con.boxed.v1.0.12-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-13-of-20/train.500k.de_con.boxed.v1.0.13-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-14-of-20/train.500k.de_con.boxed.v1.0.14-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-15-of-20/train.500k.de_con.boxed.v1.0.15-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-16-of-20/train.500k.de_con.boxed.v1.0.16-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-17-of-20/train.500k.de_con.boxed.v1.0.17-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-18-of-20/train.500k.de_con.boxed.v1.0.18-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-19-of-20/train.500k.de_con.boxed.v1.0.19-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-2-of-20/train.500k.de_con.boxed.v1.0.2-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-3-of-20/train.500k.de_con.boxed.v1.0.3-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-4-of-20/train.500k.de_con.boxed.v1.0.4-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-5-of-20/train.500k.de_con.boxed.v1.0.5-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-6-of-20/train.500k.de_con.boxed.v1.0.6-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-7-of-20/train.500k.de_con.boxed.v1.0.7-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-8-of-20/train.500k.de_con.boxed.v1.0.8-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.0-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.1-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.2-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.3-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.4-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.5-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.6-of-8.json
|
||||
./500k-split-9-of-20/train.500k.de_con.boxed.v1.0.9-of-20.0shot.n10.tem1.0.p0.9.7-of-8.json
|
||||
Total number of items: 491733
|
||||
Acc: 0.6720944089577067
|
||||
Pass at k: 0.8531032084484873
|
||||
No positive solutions: 72234 / 491733
|
||||
No negative solutions: 187738 / 491733
|
||||
Num pairs: 3873158
|
||||
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "./500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-32.json" \
|
||||
--output_file train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.json
|
||||
|
||||
Total number of items: 491733
|
||||
Acc: 0.681286389158344
|
||||
Pass at k: 0.8782998090427122
|
||||
No positive solutions: 59844 / 491733
|
||||
No negative solutions: 211316 / 491733
|
||||
Num pairs: 3693524
|
||||
|
||||
|
||||
########################################## ITERATION 1 ###########################################################
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.json
|
||||
|
||||
Total number of items: 491733
|
||||
Acc: 0.7095415601556129
|
||||
Pass at k: 0.8631289744637842
|
||||
No positive solutions: 67304 / 491733
|
||||
No negative solutions: 250765 / 491733
|
||||
Num pairs: 2844227
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.json
|
||||
Total number of items: 491733
|
||||
Acc: 0.6936853943095135
|
||||
Pass at k: 0.8353761085792493
|
||||
No positive solutions: 80951 / 491733
|
||||
No negative solutions: 255617 / 491733
|
||||
Num pairs: 2550984
|
||||
|
||||
|
||||
"""
|
||||
@@ -0,0 +1,339 @@
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from glob import glob
|
||||
import collections
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
|
||||
def evaluate_by_sc(item, external_sc: str = None):
|
||||
if not item["response"]:
|
||||
return []
|
||||
|
||||
if isinstance(item["response"], list):
|
||||
preds = item["pred"]
|
||||
else:
|
||||
preds = [item["pred"]]
|
||||
|
||||
assert len(preds) > 1, f"Self-consistency requires at least 2 predictions, but got {len(preds)}."
|
||||
|
||||
if external_sc:
|
||||
sc_pred = external_sc
|
||||
else:
|
||||
sc_pred = item["sc_pred"]
|
||||
res = [mathscale_is_equiv(p, sc_pred)[0] for p in preds]
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def get_pred_frequency(preds, target_pred):
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
if isinstance(target_pred, list):
|
||||
target_pred = str(sorted(target_pred))
|
||||
return tmp.get(target_pred, 0) / len(preds)
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def load_data(file_path):
|
||||
if os.path.exists(file_path):
|
||||
data = json.load(open(file_path))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(file_path, recursive=True):
|
||||
if ".metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
f = open(file, "r")
|
||||
try:
|
||||
data += json.load(f)
|
||||
except:
|
||||
print(f"Error in file {file}")
|
||||
new_file = file.replace(".json", ".jsonl")
|
||||
lines = open(new_file, "r").readlines()
|
||||
for line in lines:
|
||||
try:
|
||||
data.append(json.loads(line))
|
||||
except:
|
||||
print(f"Error in line: {line}")
|
||||
return data
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--external_file_for_sc", type=str, default=None)
|
||||
parser.add_argument("--top_p", type=float, default=0.0)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = load_data(args.input_file)
|
||||
data = merge_seed_sampled_data(data)
|
||||
|
||||
filtered = 0
|
||||
if args.external_file_for_sc:
|
||||
external_data = load_data(args.external_file_for_sc)
|
||||
# id2external_sc = {item["id"]: item["sc_pred"] for item in external_data}
|
||||
id2sc = {}
|
||||
for item in tqdm(external_data):
|
||||
sc_pred = item["sc_pred"]
|
||||
|
||||
if item["pred"] == "":
|
||||
continue
|
||||
|
||||
if item["pred"] == "failed extracting answer from completion":
|
||||
continue
|
||||
|
||||
freq = get_pred_frequency(item["pred"], sc_pred)
|
||||
if freq > args.top_p:
|
||||
id2sc[item["id"]] = sc_pred
|
||||
else:
|
||||
filtered += 1
|
||||
else:
|
||||
id2sc = {}
|
||||
for item in tqdm(data):
|
||||
sc_pred = item["sc_pred"]
|
||||
|
||||
if item["pred"] == "":
|
||||
continue
|
||||
|
||||
if item["pred"] == "failed extracting answer from completion":
|
||||
continue
|
||||
|
||||
freq = get_pred_frequency(item["pred"], sc_pred)
|
||||
if freq > args.top_p:
|
||||
id2sc[item["id"]] = sc_pred
|
||||
else:
|
||||
filtered += 1
|
||||
|
||||
print(f"Filtered {filtered} samples.")
|
||||
|
||||
outputs = []
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
num_pairs = 0
|
||||
pos_missing = 0
|
||||
neg_missing = 0
|
||||
full_positive_samples = []
|
||||
full_negative_samples = []
|
||||
for item in data:
|
||||
if item["id"] in id2sc:
|
||||
res_by_sc = evaluate_by_sc(item, id2sc[item["id"]])
|
||||
else:
|
||||
continue
|
||||
|
||||
if len(res_by_sc) == 0:
|
||||
continue
|
||||
|
||||
if res_by_sc[0]:
|
||||
cnt += 1
|
||||
if any(res_by_sc):
|
||||
pass_at_k += 1
|
||||
|
||||
pos = []
|
||||
neg = []
|
||||
for resp, r in zip(item["response"], res_by_sc):
|
||||
if r:
|
||||
pos.append(resp)
|
||||
else:
|
||||
neg.append(resp)
|
||||
|
||||
if len(pos) == 0:
|
||||
full_negative_samples.append(item)
|
||||
pos_missing += 1
|
||||
if len(neg) == 0:
|
||||
neg_missing += 1
|
||||
full_positive_samples.append(item)
|
||||
if len(pos) == 0 or len(neg) == 0:
|
||||
continue
|
||||
|
||||
item["pos"] = pos
|
||||
item["neg"] = neg
|
||||
num_pairs += len(pos) * len(neg)
|
||||
outputs.append(item)
|
||||
|
||||
print(f"Total number of items: {len(data)}")
|
||||
print(f"Acc: {cnt / (len(data) - filtered)}")
|
||||
print(f"Pass at k: {pass_at_k / len(data)}")
|
||||
print(f"No positive solutions: {pos_missing} / {len(data)}")
|
||||
print(f"No negative solutions: {neg_missing} / {len(data)}")
|
||||
print(f"Num pairs: {num_pairs}")
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
json.dump(full_positive_samples[:100], open(args.output_file.replace(".json", ".pos.sample.json"), "w"), indent=2)
|
||||
json.dump(full_negative_samples[:100], open(args.output_file.replace(".json", ".neg.sample.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
"""
|
||||
########################################## ITERATION 1 ###########################################################
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.json
|
||||
|
||||
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.by_sc.json
|
||||
# Total number of items: 491733
|
||||
# Acc: 0.6936853943095135
|
||||
# Pass at k: 0.8353761085792493
|
||||
# No positive solutions: 80951 / 491733
|
||||
# No negative solutions: 255617 / 491733
|
||||
# Num pairs: 2550984
|
||||
Total number of items: 491733
|
||||
Acc: 0.85128718227168
|
||||
Pass at k: 1.0
|
||||
No positive solutions: 0 / 491733
|
||||
No negative solutions: 275526 / 491733
|
||||
Num pairs: 3958764
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.*-of-8.s0.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.s0.prefer_pair.by_sc.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.0-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.1-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.2-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.3-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.4-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.5-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.6-of-8.s0.json
|
||||
../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.7-of-8.s0.json
|
||||
Total number of items: 82642
|
||||
Acc: 0.706493066479514
|
||||
Pass at k: 1.0
|
||||
No positive solutions: 0 / 82642
|
||||
No negative solutions: 18718 / 82642
|
||||
Num pairs: 786166
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PATH_PREFIX}/experiments//mathstral.mathscale4o.sc-prm.raft.iter1.H100.dp8.v1.0.s42/checkpoint-800/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.s[01].json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments//mathstral.mathscale4o.sc-prm.raft.iter1.H100.dp8.v1.0.s42/checkpoint-800/mathscale4o/train.500k.de_con.boxed.v1.0.n20.tem1.0.p0.9.v0.1.prefer_pair.by_sc.json
|
||||
|
||||
Total number of items: 491733
|
||||
Acc: 0.8624497440684273
|
||||
Pass at k: 1.0
|
||||
No positive solutions: 0 / 491733
|
||||
No negative solutions: 264522 / 491733
|
||||
Num pairs: 14647115
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-8.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.by_sc_p0.6.json
|
||||
--top_p 0.6
|
||||
|
||||
Filtered 123792 samples.
|
||||
Total number of items: 491733
|
||||
Acc: 0.7133363024242831
|
||||
Pass at k: 0.7482536254430758
|
||||
No positive solutions: 0 / 491733
|
||||
No negative solutions: 272161 / 491733
|
||||
Num pairs: 1350704
|
||||
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n90.tem1.0.p0.9.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.prefer_pair.by_sc_p0.6.json \
|
||||
--top_p 0.6
|
||||
|
||||
Filtered 0 samples.
|
||||
Total number of items: 491733
|
||||
Acc: 0.8171100983663899
|
||||
Pass at k: 1.0
|
||||
No positive solutions: 0 / 491733
|
||||
No negative solutions: 199144 / 491733
|
||||
Num pairs: 333282827
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.*json" \
|
||||
--output_file ${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.by_sc_p0.0.json
|
||||
|
||||
Filtered 0 samples.
|
||||
Total number of items: 491337
|
||||
Acc: 0.7944180877890328
|
||||
Pass at k: 1.0
|
||||
No positive solutions: 0 / 491337
|
||||
No negative solutions: 160871 / 491337
|
||||
Num pairs: 5961085
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.*json" \
|
||||
--output_file ${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.prefer_pair.by_sc_p0.0.json --top_p 0.5
|
||||
|
||||
Filtered 164514 samples.
|
||||
Total number of items: 491337
|
||||
Acc: 0.6043285972764111
|
||||
Pass at k: 0.6651707483865453
|
||||
No positive solutions: 0 / 491337
|
||||
No negative solutions: 159681 / 491337
|
||||
Num pairs: 2672191
|
||||
|
||||
"""
|
||||
@@ -0,0 +1,35 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
p=$1
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
# --input_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/split-1024/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file $OUTPUT_PREFIX_PATH/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.gd.azure.json \
|
||||
# --num_workers 128
|
||||
|
||||
#= ================== mscale-300k
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
#
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# ===================== 4o again
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/split-1024/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file $OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "$OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.s42.json" \
|
||||
# --response_id_field id --num_workers 128 --top_p $p
|
||||
python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/split-1024/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
--output_file $OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.gd.azure.json \
|
||||
--num_workers 128
|
||||
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import sys
|
||||
import collections
|
||||
from multiprocessing import Pool
|
||||
from tqdm import tqdm
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
|
||||
def counting_partial_response_value(preds, label):
|
||||
assert isinstance(preds, list), preds
|
||||
v = 0
|
||||
for pred in preds:
|
||||
res = mathscale_is_equiv(pred, label)[0]
|
||||
if res:
|
||||
v += 1
|
||||
return v
|
||||
|
||||
|
||||
def parse_value(v, binary: bool):
|
||||
if binary:
|
||||
return 1 if v > 0 else 0
|
||||
return v
|
||||
|
||||
|
||||
def _process_response_worker(item):
|
||||
item_id, resp_id, prefix_id = item["prefix_id"].split("_")
|
||||
prefix = item["prefix"]
|
||||
if "mscale-v0.1" in item_id:
|
||||
pass
|
||||
elif "numina" not in item_id:
|
||||
item_id = int(item_id)
|
||||
if not item["label"]:
|
||||
return item_id, {}
|
||||
|
||||
if item["pred"] == "":
|
||||
return item_id, {}
|
||||
|
||||
if item["pred"] == "failed extracting answer from completion":
|
||||
return item_id, {}
|
||||
|
||||
v = len([1 for res in item["res"] if res])
|
||||
return item_id, {"v": v, "prefix": prefix}
|
||||
|
||||
|
||||
def _process_trajectories_worker(item, binary: bool):
|
||||
item_id, trajectories = item
|
||||
outputs = {
|
||||
"idx": item_id
|
||||
}
|
||||
|
||||
trajectory_pairs = [(traj["v"], traj["prefix"]) for traj in trajectories]
|
||||
prefix_vis = set()
|
||||
|
||||
values = []
|
||||
prefixes = []
|
||||
for v, prefix in trajectory_pairs:
|
||||
if prefix in prefix_vis:
|
||||
continue
|
||||
values.append(parse_value(v, binary))
|
||||
prefixes.append(prefix)
|
||||
prefix_vis.add(prefix)
|
||||
outputs[f"value"] = values
|
||||
outputs[f"prefix"] = prefixes
|
||||
return outputs
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data, id_field="id"):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item[id_field] not in id2data:
|
||||
id2data[item[id_field]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item[id_field]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item[id_field]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--binary", default=False, action="store_true")
|
||||
parser.add_argument("--num_workers", type=int, default=8)
|
||||
args = parser.parse_args()
|
||||
|
||||
print("Collecting data...")
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in sorted(glob(args.input_file)):
|
||||
if ".metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
try:
|
||||
sub_data = json.load(open(file))
|
||||
except:
|
||||
print(f"Warning: {file}l")
|
||||
sub_data = [json.loads(line) for line in open(f"{file}l").readlines()]
|
||||
data += sub_data
|
||||
|
||||
data = merge_seed_sampled_data(data)
|
||||
|
||||
item_id2partial_trajectories = collections.defaultdict(list)
|
||||
missing = 0
|
||||
with Pool(args.num_workers) as pool:
|
||||
inputs = []
|
||||
for item in data:
|
||||
inputs.append(item)
|
||||
for item_id, result in tqdm(pool.imap_unordered(_process_response_worker, inputs), total=len(inputs)):
|
||||
if len(result) == 0:
|
||||
missing += 1
|
||||
continue
|
||||
item_id2partial_trajectories[item_id].append(result)
|
||||
|
||||
shoot_cnt = collections.Counter()
|
||||
for item_id, trajectories in item_id2partial_trajectories.items():
|
||||
for traj in trajectories:
|
||||
shoot_cnt[traj["v"]] += 1
|
||||
print(shoot_cnt)
|
||||
|
||||
print(f"Missing {missing} items in the response data.")
|
||||
|
||||
outputs = []
|
||||
cnt = collections.Counter()
|
||||
with Pool(args.num_workers) as pool:
|
||||
inputs = [(item_id, trajectories) for item_id, trajectories in item_id2partial_trajectories.items()]
|
||||
_annotate = partial(_process_trajectories_worker, binary=args.binary)
|
||||
for result in tqdm(pool.imap_unordered(_annotate, inputs), total=len(inputs)):
|
||||
outputs.append(result)
|
||||
cnt.update(result["value"])
|
||||
|
||||
print(cnt)
|
||||
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.binary.local.json \
|
||||
--binary --num_workers 24
|
||||
|
||||
Counter({0: 1243140, 3: 916473, 2: 442815, 1: 435687})
|
||||
Missing 74 items in the response data.
|
||||
Counter({1: 1793986, 0: 1242775})
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/share/models/llama3.1_8b_mathscale4o/model_lr1e-5_batch512_epochs3_gpus8_linearSchedule/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/share/models/llama3.1_8b_mathscale4o/model_lr1e-5_batch512_epochs3_gpus8_linearSchedule/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.binary.local.json \
|
||||
--binary --num_workers 24
|
||||
|
||||
Counter({0: 1103040, 3: 908168, 2: 435088, 1: 427143})
|
||||
Missing 0 items in the response data.
|
||||
Counter({1: 1769112, 0: 1102585})
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.local.json \
|
||||
--num_workers 24
|
||||
|
||||
Counter({0: 1243140, 3: 916473, 2: 442815, 1: 435687})
|
||||
Missing 74 items in the response data.
|
||||
Counter({0: 1242779, 3: 915927, 2: 442573, 1: 435482})
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/share/models/llama3.1_8b_mathscale4o/model_lr1e-5_batch512_epochs3_gpus8_linearSchedule/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/share/models/llama3.1_8b_mathscale4o/model_lr1e-5_batch512_epochs3_gpus8_linearSchedule/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.local.json \
|
||||
--num_workers 24
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.local.json \
|
||||
--num_workers 24
|
||||
|
||||
Counter({0: 1013210, 3: 660151, 2: 306372, 1: 305458})
|
||||
Missing 0 items in the response data.
|
||||
Counter({0: 1012382, 3: 659126, 2: 305949, 1: 305153})
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_gd.local.json \
|
||||
--num_workers 24
|
||||
|
||||
Counter({0: 950765, 3: 748413, 2: 353782, 1: 336295})
|
||||
Missing 0 items in the response data.
|
||||
Counter({0: 950541, 3: 747952, 2: 353608, 1: 336155})
|
||||
|
||||
"""
|
||||
@@ -0,0 +1,277 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import sys
|
||||
import collections
|
||||
from multiprocessing import Pool
|
||||
from tqdm import tqdm
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def extract_sc_mul_labels(response_data, id_field: str):
|
||||
id2sc_preds = {}
|
||||
for item in response_data:
|
||||
if not item["response"]:
|
||||
continue
|
||||
|
||||
if isinstance(item["response"], list):
|
||||
preds = item["pred"]
|
||||
else:
|
||||
preds = [item["pred"]]
|
||||
|
||||
assert len(preds) > 1, f"Self-consistency requires at least 2 predictions, but got {len(preds)}."
|
||||
|
||||
sorted_preds = majority_voting_frequency(preds)
|
||||
item_id = item[id_field] if isinstance(item[id_field], str) else str(item[id_field])
|
||||
id2sc_preds[item_id] = sorted_preds
|
||||
|
||||
return id2sc_preds
|
||||
|
||||
|
||||
def counting_partial_response_value(preds, label):
|
||||
assert isinstance(preds, list), preds
|
||||
v = 0
|
||||
for pred in preds:
|
||||
res = mathscale_is_equiv(pred, label)[0]
|
||||
if res:
|
||||
v += 1
|
||||
return v
|
||||
|
||||
|
||||
def parse_value(v, binary: bool):
|
||||
if binary:
|
||||
return 1 if v > 0 else 0
|
||||
return v
|
||||
|
||||
|
||||
def _process_response_init(id2sc_preds):
|
||||
global _id2sc_preds
|
||||
_id2sc_preds = id2sc_preds
|
||||
|
||||
|
||||
def _process_response_worker(item, top_p: float = 0.0):
|
||||
item_id, resp_id, prefix_id = item["prefix_id"].split("_")
|
||||
prefix = item["prefix"]
|
||||
# item_id = int(item_id)
|
||||
if item_id not in _id2sc_preds:
|
||||
return item_id, {}
|
||||
|
||||
if item["pred"] == "":
|
||||
return item_id, {}
|
||||
|
||||
if item["pred"] == "failed extracting answer from completion":
|
||||
return item_id, {}
|
||||
|
||||
sc_label, sc_freq = _id2sc_preds[item_id][0]
|
||||
tot_num = sum([fre for _, fre in _id2sc_preds[item_id]])
|
||||
if sc_freq / tot_num < top_p:
|
||||
return item_id, {}
|
||||
|
||||
v = counting_partial_response_value(item["pred"], sc_label)
|
||||
return item_id, {"v": v, "prefix": prefix}
|
||||
|
||||
|
||||
def _process_trajectories_worker(item, binary: bool):
|
||||
item_id, trajectories = item
|
||||
outputs = {
|
||||
"idx": item_id
|
||||
}
|
||||
|
||||
trajectory_pairs = [(traj["v"], traj["prefix"]) for traj in trajectories]
|
||||
prefix_set = set()
|
||||
|
||||
values = []
|
||||
prefixes = []
|
||||
for v, prefix in trajectory_pairs:
|
||||
if prefix in prefix_set:
|
||||
continue
|
||||
values.append(parse_value(v, binary))
|
||||
prefixes.append(prefix)
|
||||
prefix_set.add(prefix)
|
||||
outputs["value"] = values
|
||||
outputs["prefix"] = prefixes
|
||||
return outputs
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data, id_field="id"):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item[id_field] not in id2data:
|
||||
id2data[item[id_field]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item[id_field]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item[id_field]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--response_file_for_sc", type=str)
|
||||
parser.add_argument("--response_id_field", type=str, default="idx")
|
||||
parser.add_argument("--binary", default=False, action="store_true")
|
||||
parser.add_argument("--num_workers", type=int, default=8)
|
||||
parser.add_argument("--top_p", type=float, default=0.0)
|
||||
args = parser.parse_args()
|
||||
|
||||
print("Collecting data...")
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
if ".metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
data += json.load(open(file))
|
||||
|
||||
print("Collecting response data...")
|
||||
if os.path.exists(args.response_file_for_sc):
|
||||
response_data = json.load(open(args.response_file_for_sc))
|
||||
else:
|
||||
response_data = []
|
||||
for file in glob(args.response_file_for_sc):
|
||||
if ".metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
response_data += json.load(open(file))
|
||||
response_data = merge_seed_sampled_data(response_data, args.response_id_field)
|
||||
|
||||
last_round_id2sc_preds = extract_sc_mul_labels(response_data, args.response_id_field)
|
||||
num_candidate_cnt = collections.Counter()
|
||||
for item_id, preds in last_round_id2sc_preds.items():
|
||||
num_candidate_cnt[len(preds)] += 1
|
||||
print(num_candidate_cnt)
|
||||
del response_data
|
||||
|
||||
item_id2partial_trajectories = collections.defaultdict(list)
|
||||
missing = 0
|
||||
with Pool(args.num_workers, initializer=_process_response_init, initargs=(last_round_id2sc_preds,)) as pool:
|
||||
inputs = []
|
||||
for item in data:
|
||||
inputs.append(item)
|
||||
_annotate = partial(_process_response_worker, top_p=args.top_p)
|
||||
for item_id, result in tqdm(pool.imap_unordered(_annotate, inputs), total=len(inputs)):
|
||||
if len(result) == 0:
|
||||
missing += 1
|
||||
continue
|
||||
item_id2partial_trajectories[item_id].append(result)
|
||||
|
||||
shoot_cnt = collections.Counter()
|
||||
for item_id, trajectories in item_id2partial_trajectories.items():
|
||||
for traj in trajectories:
|
||||
shoot_cnt[traj["v"]] += 1
|
||||
print(shoot_cnt)
|
||||
|
||||
print(f"Missing {missing} items in the response data.")
|
||||
|
||||
outputs = []
|
||||
cnt = collections.Counter()
|
||||
with Pool(args.num_workers) as pool:
|
||||
inputs = [(item_id, trajectories) for item_id, trajectories in item_id2partial_trajectories.items()]
|
||||
_annotate = partial(_process_trajectories_worker, binary=args.binary)
|
||||
for result in tqdm(pool.imap_unordered(_annotate, inputs), total=len(inputs)):
|
||||
outputs.append(result)
|
||||
cnt.update(result["value"])
|
||||
|
||||
print(cnt)
|
||||
parent_dir = os.path.dirname(args.output_file)
|
||||
if not os.path.exists(parent_dir):
|
||||
os.makedirs(parent_dir, exist_ok=True)
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm_by_sc.azure.json \
|
||||
--response_file_for_sc "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.?-of-8.json" \
|
||||
--response_id_field id --num_workers 128
|
||||
|
||||
Counter({3: 889966, 0: 537841, 2: 459487, 1: 397897})
|
||||
Missing 0 items in the response data.
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm.iter0-pdpo-sc.azure.json \
|
||||
--response_file_for_sc "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n90.tem1.0.p0.9.json" \
|
||||
--response_id_field id --num_workers 128
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/split-512/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm.iter0-pdpo-sc-p0.6.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n90.tem1.0.p0.9.json" \
|
||||
--response_id_field id --num_workers 128 --top_p 0.6
|
||||
|
||||
Counter({3: 945267, 2: 408413, 0: 308107, 1: 279042})
|
||||
Missing 1097360 items in the response data.
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/split-256/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample16.filter_same.*-of-4.prefix_completion.n3.tem1.0.p0.9.*-of-256.json" \
|
||||
--output_file ${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample16.filter_same.prefix_completion.n3.tem1.0.p0.9.process_rm.sc-10-p0.0.azure.json \
|
||||
--response_file_for_sc "${MODEL_PREFIX_PATH}/mathstral-7B-v0.1/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.*json" --response_id_field id --num_workers 128
|
||||
|
||||
Counter({3: 1869170, 0: 1528083, 2: 859527, 1: 787261})
|
||||
Missing 24189 items in the response data.
|
||||
Counter({3: 1862945, 0: 1521858, 2: 855962, 1: 783862})
|
||||
|
||||
|
||||
>>> python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/completion-split-128/cot.de_con.n8.tem1.0.p1.0.s0.upper0.7.r0.3.sample32.filter_same.{split_id}-of-4.n3.tem1.0.p1.0.*-of-128.s0.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/cot.de_con.n8.tem1.0.p1.0.s0.upper0.7.r0.3.sample32.filter_same.process_rm.iter0-pdpo-sc-p0.5.v0.0.{split_id}-of-4.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-*-of-10/cot.de_con.*-of-10.n8.tem1.0.p1.0.*-of-*.s*.json" --response_id_field id --num_workers 128 --top_p 0.5
|
||||
Counter({3: 803168, 2: 454386, 1: 260676, 0: 159454})
|
||||
Missing 2783152 items in the response data.
|
||||
Counter({3: 772040, 2: 436348, 1: 250464, 0: 153477})
|
||||
"""
|
||||
@@ -0,0 +1,14 @@
|
||||
import json
|
||||
import argparse
|
||||
import os.path
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--orig_data_file", type=str)
|
||||
parser.add_argument("--orig_id_filed", type=str, default="id")
|
||||
parser.add_argument("--train_data_file", type=str)
|
||||
parser.add_argument("--train_id_field", type=str, default="idx")
|
||||
args = parser.parse_args()
|
||||
|
||||
pass
|
||||
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
outputs = []
|
||||
|
||||
for i, item in tqdm(enumerate(data)):
|
||||
prompt = item["prompt"]
|
||||
|
||||
_s = prompt.index("### Instruction:") + len("### Instruction:")
|
||||
_e = prompt.index("### Response:")
|
||||
question = prompt[_s:_e].strip()
|
||||
|
||||
response = item["completion"]
|
||||
|
||||
outputs.append({
|
||||
"id": i,
|
||||
"question": question,
|
||||
"response": response,
|
||||
})
|
||||
|
||||
json.dump(outputs, open(args.output_file, "w"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,46 @@
|
||||
import json
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))
|
||||
|
||||
from data.mathscale.util import mathscale_extract_answer_v2
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--split", type=int, default=0)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
outputs = []
|
||||
for i, item in tqdm(enumerate(data)):
|
||||
item["id"] = f"mscale-v0.1-300k-{item['uuid']}"
|
||||
if "\\boxed{" not in item["completion"]:
|
||||
continue
|
||||
label = mathscale_extract_answer_v2(item["completion"])
|
||||
if label != "":
|
||||
item["label"] = label
|
||||
completion = item.pop("completion")
|
||||
item["box_solution"] = completion
|
||||
item["solution"] = completion
|
||||
item.pop("prompt")
|
||||
outputs.append(item)
|
||||
|
||||
print(len(outputs))
|
||||
if args.split <= 0:
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
else:
|
||||
split_size = (len(outputs) + args.split - 1) // args.split
|
||||
for i in range(args.split):
|
||||
json.dump(outputs[i * split_size:(i + 1) * split_size],
|
||||
open(args.output_file.replace(".json", f".{i}-of-{args.split}.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,42 @@
|
||||
import json
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))
|
||||
|
||||
from data.mathscale.util import mathscale_extract_answer_v2
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--split", type=int, default=0)
|
||||
args = parser.parse_args()
|
||||
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
outputs = []
|
||||
for i, item in tqdm(enumerate(data)):
|
||||
item["id"] = f"numina-{i}"
|
||||
if "\\boxed{" not in item["completion"]:
|
||||
continue
|
||||
label = mathscale_extract_answer_v2(item["completion"])
|
||||
if label != "":
|
||||
item["label"] = label
|
||||
outputs.append(item)
|
||||
|
||||
print(len(outputs))
|
||||
if args.split <= 0:
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
else:
|
||||
split_size = (len(outputs) + args.split - 1) // args.split
|
||||
for i in range(args.split):
|
||||
json.dump(outputs[i * split_size:(i + 1) * split_size],
|
||||
open(args.output_file.replace(".json", f".{i}-of-{args.split}.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,99 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv_proxy, is_correct as mathscale_is_correct, mathscale_extract_answer
|
||||
from data.math import number_answer_extractor
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
|
||||
"""
|
||||
This file is used to fix the incorrect answer extraction and verification in the previous version of the data pipeline, which has used the GSM8K's utils.
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
mathscale_fn = mathscale_extract_answer()
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
inconsistent = 0
|
||||
missing = 0
|
||||
for item in data:
|
||||
if not item["label"]:
|
||||
tmp = mathscale_fn(item["completion"])
|
||||
if not tmp:
|
||||
missing += 1
|
||||
continue
|
||||
item["label"] = tmp
|
||||
|
||||
if isinstance(item["label"], int):
|
||||
item["label"] = str(item["label"])
|
||||
|
||||
res = []
|
||||
pred_clean = []
|
||||
for resp in item["response"]:
|
||||
tmp_res, tmp_pred_clean, _ = mathscale_is_correct(resp, item["label"])
|
||||
res.append(tmp_res)
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
pred2res = {pred: r for pred, r in zip(pred_clean, res)}
|
||||
sc_pred = majority_voting_predict(pred_clean)
|
||||
sc_res = pred2res[sc_pred]
|
||||
|
||||
tmp = 0
|
||||
for a, b in zip(pred_clean, item["pred"]):
|
||||
if a != b:
|
||||
tmp += 1
|
||||
inconsistent += tmp / len(pred_clean)
|
||||
|
||||
item["pred"] = pred_clean
|
||||
item["res"] = res
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_res"] = sc_res
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
print(inconsistent)
|
||||
print(missing)
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data), "correct": cnt, "total": len(data)}
|
||||
print(metrics)
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
>>> python scripts/math_scale/fix_answer_extract_and_verify.py \
|
||||
--input_file "../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.?-of-8.json" \
|
||||
--output_file ../msranlpintern/share/models/mathscale-mistral/mathscale/train.v60.300k.1-of-30.v1.0.0shot.n5.tem1.0.p0.9.fix_predict.json
|
||||
|
||||
1808.000000000016
|
||||
0
|
||||
{'acc': 0.7141, 'pass@k': 0.8515, 'maj@k': 0.763, 'correct': 7141, 'total': 10000}
|
||||
"""
|
||||
@@ -0,0 +1,87 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv_proxy, is_correct as mathscale_is_correct, mathscale_extract_answer, mathscale_extract_answer_fn_v4
|
||||
from data.math import number_answer_extractor
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
|
||||
"""
|
||||
This file is used to fix the incorrect answer extraction and verification in the previous version of `mathscale_extract_answer_v2`.
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
mathscale_fn = mathscale_extract_answer_fn_v4(completion_field="completion")
|
||||
data = mathscale_fn(data)
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
inconsistent = 0
|
||||
missing = 0
|
||||
for item in data:
|
||||
if isinstance(item["label"], int):
|
||||
item["label"] = str(item["label"])
|
||||
|
||||
res = []
|
||||
pred_clean = []
|
||||
for resp in item["response"]:
|
||||
tmp_res, tmp_pred_clean, _ = mathscale_is_correct(resp, item["label"])
|
||||
res.append(tmp_res)
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
pred2res = {pred: r for pred, r in zip(pred_clean, res)}
|
||||
sc_pred = majority_voting_predict(pred_clean)
|
||||
sc_res = pred2res[sc_pred]
|
||||
|
||||
tmp = 0
|
||||
for a, b in zip(pred_clean, item["pred"]):
|
||||
if a != b:
|
||||
tmp += 1
|
||||
inconsistent += tmp / len(pred_clean)
|
||||
|
||||
item["pred"] = pred_clean
|
||||
item["res"] = res
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_res"] = sc_res
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
print(inconsistent)
|
||||
print(missing)
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data), "correct": cnt, "total": len(data)}
|
||||
print(metrics)
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
>>>
|
||||
"""
|
||||
@@ -0,0 +1,59 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
p=$1
|
||||
|
||||
echo "Constructing process_rm sample for split 0"
|
||||
echo "p" $p
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[01]-of-10/cot.de_con.[01]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# =================================================================================================
|
||||
|
||||
# Use the co-part model's responses as sc labels.
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split23.A100.dp16.v1.0.s42/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split23.A100.dp16.v1.0.s42/checkpoint-100/numina/830k-split-[01]-of-10/cot.de_con.[01]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
|
||||
# Iterative DPO: split-01 -> split-23
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-1024.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# Iterative DPO: split-01 -> split-23 -> split-45
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/cot.de_con.n16.tem1.0.p1.0.split45.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-1024.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/cot.de_con.n16.tem1.0.p1.0.split45.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/830k-split-[45]-of-10/cot.de_con.[45]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# Iterative DPO: split-01 -> split-23 -> split-45 -> split-67
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split67.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-1024.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split67.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[67]-of-10/cot.de_con.[67]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# Iterative DPO: split-01 -> split-23 -> split-45 -> split-89
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split89.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-1024.s0.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split89.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# Iterative DPO: split-01 -> split-23 -> split-45 -> split-67 -> split-89
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.2.iter4.split01-23-45-67.H100.dp8.v1.5.s42/checkpoint-500/numina/cot.de_con.n16.tem1.2.p1.0.split89.upper0.8.r0.3.sample32.filter_same.n3.tem1.0.p1.0.*-of-1024.s0.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.2.iter4.split01-23-45-67.H100.dp8.v1.5.s42/checkpoint-500/numina/cot.de_con.n16.tem1.2.p1.0.split89.upper0.8.r0.3.sample32.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.2.iter4.split01-23-45-67.H100.dp8.v1.5.s42/checkpoint-500/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.2.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
@@ -0,0 +1,144 @@
|
||||
export OUTPUT_PREFIX_PATH=../msranlpintern/reward_modeling/
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[01]-of-10/cot.de_con.[01]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split01.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[01]-of-10/cot.de_con.[01]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
# p=0.0
|
||||
# Counter({0: 941033, 1: 550298, 3: 422913, 2: 412766})
|
||||
# Missing 0 items in the response data.
|
||||
# Counter({0: 910219, 1: 532186, 3: 408604, 2: 398571})
|
||||
# p=0.5
|
||||
# Counter({3: 337436, 2: 215444, 1: 152720, 0: 122527})
|
||||
# Missing 1498883 items in the response data.
|
||||
# Counter({3: 325974, 2: 208001, 1: 147657, 0: 118407})
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.n3.tem1.0.p1.0.*-of-512.s0.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.mathscale4o.process-dpo.iter0.A100.dp8.v2.2.s42/checkpoint-1200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" --response_id_field id --num_workers 128 --top_p $p
|
||||
# p=0.0
|
||||
# Counter({0: 1019074, 1: 593140, 3: 451050, 2: 444011})
|
||||
# Missing 0 items in the response data.
|
||||
# Counter({0: 916032, 1: 532522, 3: 405042, 2: 398580})
|
||||
# p=0.5
|
||||
# Counter({3: 360314, 2: 233018, 1: 164644, 0: 132445})
|
||||
# Missing 1616854 items in the response data.
|
||||
# Counter({3: 323596, 2: 209373, 1: 148006, 0: 119201})
|
||||
|
||||
|
||||
# Sample from the dpo-trained co- models, and re-computing the process-rm labels.
|
||||
|
||||
|
||||
# Change to vanilla iterative DPO training.
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/cot.de_con.n16.tem1.0.p1.0.split23.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
# Vanilla outcome DPO
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/830k-split-[23]-of-10/cot.de_con.[23]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter1.split01.H100.dp8.v1.0.s42/checkpoint-200/numina/cot.de_con.n16.tem1.0.p1.0.prefer_pair.by_sc_p0.5.json \
|
||||
--top_p 0.5
|
||||
#Filtered 95299 samples.
|
||||
#Total number of items: 165284
|
||||
#Acc: 0.8421661784668143
|
||||
#Pass at k: 0.42342271484233196
|
||||
#No positive solutions: 0 / 165284
|
||||
#No negative solutions: 22851 / 165284
|
||||
#Num pairs: 1994151
|
||||
|
||||
# ====================================== Split 45; Start from split 01 -> 23 ======================================
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/830k-split-[45]-of-10/cot.de_con.[45]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/cot.de_con.n16.tem1.0.p1.0.split45.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/830k-split-[45]-of-10/cot.de_con.[45]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter2.split01-23.H100.dp8.v1.4.s42/checkpoint-100/numina/cot.de_con.n16.tem1.0.p1.0.split45.prefer_pair.by_sc_p0.5.json \
|
||||
--top_p 0.5
|
||||
#Filtered 76704 samples.
|
||||
#Total number of items: 165284
|
||||
#Acc: 0.8534996613230977
|
||||
#Pass at k: 0.5359260424481499
|
||||
#No positive solutions: 0 / 165284
|
||||
#No negative solutions: 31129 / 165284
|
||||
#Num pairs: 2403757
|
||||
|
||||
|
||||
# ====================================== Split 67; Start from split 01 -> 23 -> 45 ======================================
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[67]-of-10/cot.de_con.[67]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split67.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
#Number of prefixes: 1997762
|
||||
#249773
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[67]-of-10/cot.de_con.[67]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split45.prefer_pair.by_sc_p0.0.json \
|
||||
--top_p 0.0
|
||||
#Filtered 0 samples.
|
||||
#Total number of items: 165284
|
||||
#Acc: 0.7186297524261271
|
||||
#Pass at k: 1.0
|
||||
#No positive solutions: 0 / 165284
|
||||
#No negative solutions: 32968 / 165284
|
||||
#Num pairs: 6000694
|
||||
|
||||
# Split 89
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split89.upper0.8.r0.3.sample8.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 8
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split89.prefer_pair.by_sc_p0.0.json \
|
||||
--top_p 0.0
|
||||
#Filtered 0 samples.
|
||||
#Total number of items: 165282
|
||||
#Acc: 0.7201631151607556
|
||||
#Pass at k: 1.0
|
||||
#No positive solutions: 0 / 165282
|
||||
#No negative solutions: 33060 / 165282
|
||||
#Num pairs: 5986473
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.0.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.5.iter3.split01-23-45.H100.dp8.v1.4.s42/checkpoint-400/numina/cot.de_con.n16.tem1.0.p1.0.split89.prefer_pair.by_sc_p0.2.json \
|
||||
--top_p 0.2
|
||||
#Filtered 26351 samples.
|
||||
#Total number of items: 165282
|
||||
#Acc: 0.7397916951580281
|
||||
#Pass at k: 0.8405694509988989
|
||||
#No positive solutions: 0 / 165282
|
||||
#No negative solutions: 33058 / 165282
|
||||
#Num pairs: 5182543
|
||||
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.2.iter4.split01-23-45-67.H100.dp8.v1.5.s42/checkpoint-500/numina/830k-split-[89]-of-10/cot.de_con.[89]-of-10.n8.tem1.2.p1.0.*-of-64.s[01].json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/llama3.1.8b.numina.process-dpo-sc-p0.2.iter4.split01-23-45-67.H100.dp8.v1.5.s42/checkpoint-500/numina/cot.de_con.n16.tem1.2.p1.0.split89.upper0.8.r0.3.sample32.filter_same.json \
|
||||
--upper_step_ratio 0.8 --sample_ratio 0.3 --filter_all_same --sample_over_p 32
|
||||
#Number of prefixes: 8049954
|
||||
#254713
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv_proxy, is_correct as mathscale_is_correct
|
||||
|
||||
|
||||
def majority_voting_predict(preds):
|
||||
if isinstance(preds, str):
|
||||
return preds
|
||||
|
||||
preds = [pred for pred in preds if pred]
|
||||
if len(preds) == 0:
|
||||
return ""
|
||||
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
pred = collections.Counter(tmp).most_common(1)[0][0]
|
||||
pred = eval(pred)
|
||||
elif isinstance(preds[0], str):
|
||||
pred = collections.Counter(preds).most_common(1)[0][0]
|
||||
else:
|
||||
# raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
print(f"Unknown type {type(preds[0])}")
|
||||
pred = ""
|
||||
return pred
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file")
|
||||
parser.add_argument("--output_file")
|
||||
parser.add_argument("--label_field", type=str, default="answer")
|
||||
args = parser.parse_args()
|
||||
|
||||
data = [json.loads(line) for line in open(args.input_file)]
|
||||
for item in data:
|
||||
response = item["completion"]
|
||||
if isinstance(response, str):
|
||||
res, pred_clean, _ = mathscale_is_correct(response, item[args.label_field])
|
||||
if pred_clean is None:
|
||||
pred_clean = ""
|
||||
sc_pred = pred_clean
|
||||
sc_res = res
|
||||
elif isinstance(response, list):
|
||||
res = []
|
||||
pred_clean = []
|
||||
for resp in response:
|
||||
tmp_res, tmp_pred_clean, _ = mathscale_is_correct(resp, item[args.label_field])
|
||||
if tmp_pred_clean is None:
|
||||
tmp_pred_clean = ""
|
||||
res.append(tmp_res)
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
pred2res = {pred: r for pred, r in zip(pred_clean, res)}
|
||||
sc_pred = majority_voting_predict(pred_clean)
|
||||
sc_res = pred2res[sc_pred]
|
||||
else:
|
||||
raise ValueError(f"Unknown type of response: {type(response)}")
|
||||
|
||||
item["pred"] = pred_clean
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_res"] = sc_res
|
||||
item["res"] = res
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
for item in data:
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
if "data_topic" in item:
|
||||
if "." in item["data_topic"]:
|
||||
item["data_topic"] = item["data_topic"].split(".")[0]
|
||||
acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
cnt_data_topic[item["data_topic"]] += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
|
||||
if len(data) == 0:
|
||||
metrics = {"acc": 0, "pass@k": 0, "maj@k": 0, "correct": 0, "total": 0}
|
||||
else:
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data),
|
||||
"correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for key in acc_data_topic:
|
||||
metrics[f"acc_{key}"] = acc_data_topic[key] / cnt_data_topic[key]
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(json.dumps(metrics, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,12 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
#p=$1
|
||||
half_id=$1
|
||||
|
||||
echo "Constructing process_rm sample for split $half_id"
|
||||
#echo "p" $p
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/split-512/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.gd.azure.json \
|
||||
--num_workers 128
|
||||
@@ -0,0 +1,12 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
p=$1
|
||||
half_id=$2
|
||||
|
||||
echo "Constructing process_rm sample for split $half_id"
|
||||
echo "p" $p
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/split-512/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.*-of-256.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
@@ -0,0 +1,38 @@
|
||||
export OUTPUT_PREFIX_PATH=../msranlpintern/reward_modeling/
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-0.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-0-of-2/train.500k-0-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.*-of-256.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-0.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-0-of-2/train.500k-0-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-1.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-1-of-2/train.500k-1-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.*-of-256.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-1.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-1-of-2/train.500k-1-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/split-512/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.*-of-256.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/split-512/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-512.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.co-sft.half-${half_id}.V100.tp2dp64.v1.0.s42/checkpoint-400/mathscale4o/500k-half-${half_id}-of-2/train.500k-${half_id}-of-2.de_con.boxed.v1.0.n10.tem1.0.p0.9.*-of-256.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
|
||||
# half=1 p=0.0
|
||||
#Counter({3: 555266, 0: 429788, 2: 288700, 1: 262752})
|
||||
#Missing 9618 items in the response data.
|
||||
#Counter({3: 552136, 0: 426893, 2: 286803, 1: 261009})
|
||||
# half=1 p=0.5
|
||||
#Counter({3: 511946, 2: 218546, 1: 140566, 0: 122720})
|
||||
#Missing 552346 items in the response data.
|
||||
#Counter({3: 509128, 2: 217127, 1: 139676, 0: 121953})
|
||||
# half=0 p=0.5
|
||||
#Counter({3: 538985, 2: 229498, 1: 146411, 0: 128951})
|
||||
#Missing 579971 items in the response data.
|
||||
#Counter({3: 521894, 2: 223010, 1: 142343, 0: 125221})
|
||||
# half=0 p=0.0
|
||||
# Counter({3: 584337, 0: 451324, 2: 303606, 1: 274166})
|
||||
# Missing 10383 items in the response data.
|
||||
# Counter({3: 566162, 0: 440860, 2: 295316, 1: 267302})
|
||||
@@ -0,0 +1,61 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
import collections
|
||||
|
||||
"""
|
||||
The output file should be processed by post_processors.openai_api_callback.MathScaleCallBack
|
||||
"""
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
for item in data:
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
|
||||
if "data_topic" in item:
|
||||
acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
cnt_data_topic[item["data_topic"]] += 1
|
||||
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data), "correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for k, v in acc_data_topic.items():
|
||||
metrics[f"acc_{k}"] = v / cnt_data_topic[k]
|
||||
metrics[f"total_{k}"] = cnt_data_topic[k]
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(len(data))
|
||||
print(json.dumps(metrics, indent=2))
|
||||
@@ -0,0 +1,16 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.0.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json"
|
||||
cat "$path/mwpbench/checkpoint-$step/fresh_gaokao_math_2023.v1.0.0shot.n1.tem0.0.p1.0.0-of-1.metrics.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,155 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import collections
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
The output file should be processed by post_processors.openai_api_callback.MathScaleCallBack
|
||||
"""
|
||||
|
||||
|
||||
def majority_voting_frequency(preds):
|
||||
assert isinstance(preds, list), preds
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
tmp = collections.Counter(tmp)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(eval(pred), fre) for pred, fre in tmp]
|
||||
elif isinstance(preds[0], str):
|
||||
tmp = collections.Counter(preds)
|
||||
tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
|
||||
sorted_preds = [(pred, fre) for pred, fre in tmp]
|
||||
else:
|
||||
raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
return sorted_preds
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
parser.add_argument("--maj_ks", type=str, default="8,16,32,64,128")
|
||||
parser.add_argument("--seed", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in sorted(glob(args.input_file)):
|
||||
if "metrics" in file:
|
||||
continue
|
||||
print(file)
|
||||
data += json.load(open(file))
|
||||
if len(data) == 0:
|
||||
raise ValueError(f"No data found in {args.input_file}")
|
||||
|
||||
print(len(data))
|
||||
data = merge_seed_sampled_data(data)
|
||||
print(len(data))
|
||||
|
||||
maj_ks = list(map(int, args.maj_ks.split(",")))
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
maj_at_k = {k: 0 for k in maj_ks}
|
||||
pass_at_k = {k: 0 for k in maj_ks}
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
avg_solution_num = 0
|
||||
for item in tqdm(data):
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
|
||||
if "data_topic" in item:
|
||||
acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
cnt_data_topic[item["data_topic"]] += 1
|
||||
|
||||
# if any(res):
|
||||
# pass_at_k += 1
|
||||
|
||||
if isinstance(item["pred"], str):
|
||||
item["pred"] = [item["pred"]]
|
||||
|
||||
avg_solution_num += len(item["pred"])
|
||||
|
||||
for _k in maj_ks:
|
||||
k_preds = item["pred"][:_k]
|
||||
k_sc_pred = majority_voting_frequency(k_preds)[0][0]
|
||||
if mathscale_is_equiv(k_sc_pred, item["label"].lower())[0]:
|
||||
maj_at_k[_k] += 1
|
||||
|
||||
if any(res[:_k]):
|
||||
pass_at_k[_k] += 1
|
||||
|
||||
sc_pred = majority_voting_frequency(item["pred"])[0][0]
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_res"] = mathscale_is_equiv(sc_pred, item["label"])[0]
|
||||
|
||||
# assert pass_at_k <= len(data)
|
||||
json.dump(data, open(args.output_file, "w"))
|
||||
|
||||
metrics = {"acc": cnt / len(data), "correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for k, v in acc_data_topic.items():
|
||||
metrics[f"acc_{k}"] = v / cnt_data_topic[k]
|
||||
metrics[f"total_{k}"] = cnt_data_topic[k]
|
||||
metrics["avg_solution_num"] = avg_solution_num / len(data)
|
||||
for _k in maj_ks:
|
||||
metrics[f"maj@{_k}"] = maj_at_k[_k] / len(data)
|
||||
metrics[f"pass@{_k}"] = pass_at_k[_k] / len(data)
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(len(data))
|
||||
print(metrics)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,5 @@
|
||||
for i in {0..19}; do
|
||||
python scripts/math_scale/merge_dp_seed_predictions.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-${i}-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.[0-8]-of-8.s[0-8].json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-${i}-of-20/train.500k.de_con.boxed.v1.0.${i}-of-20.0shot.n90.tem1.0.p0.9.json
|
||||
done
|
||||
@@ -0,0 +1,16 @@
|
||||
path=$1
|
||||
prefix=$2
|
||||
version=$3
|
||||
|
||||
split_size=1
|
||||
|
||||
shift 3 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/math500/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.?-of-${split_size}.json" \
|
||||
# --output_file "$path/math500/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.json"
|
||||
cat "$path/math500/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.0-of-${split_size}.metrics.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,20 @@
|
||||
path=$1
|
||||
prefix=$2
|
||||
version=$3
|
||||
|
||||
split_size=1
|
||||
|
||||
shift 3 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.?-of-${split_size}.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.json"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
# --output_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.json"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
# --output_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json"
|
||||
# cat "$path/mwpbench/checkpoint-$step/fresh_gaokao_math_2023.v1.0.0shot.n1.tem0.0.p1.0.0-of-1.metrics.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,21 @@
|
||||
path=$1
|
||||
prefix=$2
|
||||
version=$3
|
||||
|
||||
split_size=1
|
||||
|
||||
shift 3 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.sympy_eval.json" \
|
||||
# --output_file "$path/mwpbench/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.json"
|
||||
cat "$path/mwpbench/checkpoint-$step/$prefix.test.v$version.0shot.n1.tem0.0.p1.0.0-of-1.sympy_eval.metrics.json"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
# --output_file "$path/mwpbench/checkpoint-$step/full_test.v1.0.0shot.n1.tem0.0.p1.0.json"
|
||||
# python scripts/math_scale/merge_dp_predictions.py --input_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
# --output_file "$path/checkpoint-$step/math/math.test.v1.1.0shot.n1.tem0.0.p1.0.json"
|
||||
# cat "$path/mwpbench/checkpoint-$step/fresh_gaokao_math_2023.v1.0.0shot.n1.tem0.0.p1.0.0-of-1.metrics.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_math.2k.v0.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_math.2k.v0.0.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v0.0.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v0.0.0shot.n1.tem0.0.p1.0.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_math.2k.v1.3.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_math.2k.v1.3.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.3.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v1.3.0shot.n1.tem0.0.p1.0.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.1.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.1.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.1.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v1.1.0shot.n1.tem0.0.p1.0.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.2.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.2.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.2.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v1.2.0shot.n1.tem0.0.p1.0.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
path=$1
|
||||
|
||||
shift 1 # Shift the first 5 arguments, so $1 now refers to the 6th argument
|
||||
|
||||
for step in "$@"; do
|
||||
echo "Merging predictions for step $step"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.3.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/train_wo_gsm.2k.v1.3.0shot.n1.tem0.0.p1.0.json"
|
||||
python scripts/math_scale/merge_dp_predictions.py --input_file "$path/mwpbench/checkpoint-$step/full_test.v1.3.0shot.n1.tem0.0.p1.0.?-of-8.json" \
|
||||
--output_file "$path/mwpbench/checkpoint-$step/full_test.v1.3.0shot.n1.tem0.0.p1.0.json"
|
||||
echo "Done merging predictions for step $step"
|
||||
echo
|
||||
done
|
||||
@@ -0,0 +1,15 @@
|
||||
python scripts/math_scale/construct_prefer_pair.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.4o.json"
|
||||
#Total number of items: 298275
|
||||
#Acc: 0.7078333752409689 Pass at k: 0.8356214902355209
|
||||
#No positive solutions: 49030 / 298275 No negative solutions: 148203 / 298275
|
||||
#Num pairs: 1650384
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.4o.json \
|
||||
--num_workers 24
|
||||
#Counter({0: 648447, 3: 465424, 2: 184121, 1: 178847, 6: 3645, 4: 1485})
|
||||
#Missing 308 items in the response data.
|
||||
#Counter({0: 648147, 3: 465044, 2: 183998, 1: 178757, 6: 3642, 4: 1484})
|
||||
@@ -0,0 +1,39 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
p=$1
|
||||
|
||||
echo "Constructing process_rm sample for split 0"
|
||||
echo "p" $p
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" --response_id_field id --num_workers 128 --top_p $p#
|
||||
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
#p=0.5
|
||||
#Counter({3: 493281, 2: 165717, 1: 102586, 0: 86441})
|
||||
#Missing 298249 items in the response data.
|
||||
#Counter({3: 487316, 2: 163641, 1: 101298, 0: 85343})
|
||||
#p=0
|
||||
#Counter({3: 522458, 0: 241172, 2: 209424, 1: 172472})
|
||||
#Missing 748 items in the response data.
|
||||
#Counter({3: 516169, 0: 238263, 2: 206891, 1: 170353})
|
||||
|
||||
#python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
# --input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
# --output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
# --response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
#Counter({3: 470992, 0: 195492, 2: 178005, 1: 144163})
|
||||
#Missing 391 items in the response data.
|
||||
#Counter({3: 470540, 0: 195452, 2: 177903, 1: 144094})
|
||||
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/split-1024/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.process_rm.sc-p$p.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n8.tem1.0.p1.0.*-of-512.s*.json" --response_id_field id --num_workers 128 --top_p $p
|
||||
@@ -0,0 +1,78 @@
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
# Vanilla outcome DPO
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.by_sc_p0.5.json \
|
||||
--top_p 0.5
|
||||
#Filtered 63546 samples.
|
||||
#Total number of items: 298275
|
||||
#Acc: 0.9530181613690681
|
||||
#Pass at k: 0.7869549911993965
|
||||
#No positive solutions: 0 / 298275
|
||||
#No negative solutions: 156892 / 298275
|
||||
#Num pairs: 1233371
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.by_sc_p0.0.json \
|
||||
--top_p 0.0
|
||||
#Filtered 0 samples.
|
||||
#Total number of items: 298275
|
||||
#Acc: 0.8771335177269298
|
||||
#Pass at k: 1.0
|
||||
#No positive solutions: 0 / 298275
|
||||
#No negative solutions: 157701 / 298275
|
||||
#Num pairs: 2538931
|
||||
|
||||
|
||||
# Iter - 2
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.by_sc_p0.5.json \
|
||||
--top_p 0.5
|
||||
|
||||
#Filtered 43373 samples.
|
||||
#Total number of items: 298275
|
||||
#Acc: 0.9614400828553719
|
||||
#Pass at k: 0.8545872097896237
|
||||
#No positive solutions: 0 / 298275
|
||||
#No negative solutions: 192622 / 298275
|
||||
#Num pairs: 986657
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.5.iter1.V100.tp4dp32.v3.1.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.by_sc_p0.json \
|
||||
--top_p 0.0
|
||||
#Filtered 0 samples.
|
||||
#Total number of items: 298275
|
||||
#Acc: 0.9059827340541446
|
||||
#Pass at k: 1.0
|
||||
#No positive solutions: 0 / 298275
|
||||
#No negative solutions: 193482 / 298275
|
||||
#Num pairs: 1897089
|
||||
|
||||
|
||||
# Iter - 3
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ../msranlpintern/reward_modeling/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.p0.0.iter2.H100.dp16.v1.3.s42/checkpoint-500/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.prefer_pair.by_sc_p0.0.json \
|
||||
--top_p 0.0
|
||||
@@ -0,0 +1,15 @@
|
||||
export OUTPUT_PREFIX_PATH=/mnt/fangkai_blob/reward_modeling/
|
||||
|
||||
target_dir=$1
|
||||
|
||||
#python scripts/math_scale/rerank_w_prm_math.py \
|
||||
# --response_file "$OUTPUT_PREFIX_PATH/experiments/$target_dir/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*[0-9].json" \
|
||||
# --reward_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-rm-sc.p0.0.iter2.V100.dp256.v1.0.s42/$target_dir/-*/test-checkpoint-500/eval_predictions.json" \
|
||||
# --reduction min --num_workers 128 --re_index
|
||||
|
||||
python scripts/math_scale/rerank_w_prm_math.py \
|
||||
--response_file "$OUTPUT_PREFIX_PATH/experiments/$target_dir/math/math.test.v1.1.0shot.n1.tem1.0.p1.0.0-of-1.s*[0-9].json" \
|
||||
--reward_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mscale-v0.1-300k.process-rm-sc.p0.0.iter3.V100.dp256.v1.0.s42/$target_dir/-*/test-checkpoint-400/eval_predictions.json" \
|
||||
--reduction min --num_workers 128 --re_index
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
export OUTPUT_PATH_PREFIX=../msranlpintern/reward_modeling
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "$OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.s42.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_gd.py \
|
||||
--input_file "$OUTPUT_PREFIX_PATH/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/split-1024/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.prefix_completion.n3.tem1.0.p0.9.*-of-1024.json" \
|
||||
--output_file $OUTPUT_PREFIX_PATH/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.process_rm.gd.azure.json \
|
||||
--num_workers 128
|
||||
|
||||
|
||||
# ========== mscale
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "$OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo-sc.iter0.A100.dp8.v2.1.s42/checkpoint-300/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "$OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.*-of-512.s42.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.sft.V100.tp2dp8.v2.0.s42/checkpoint-800/mathscale4o/mscale-v0.1-300k/mscale.v0.1.300k.v1.0.n10.tem1.0.p1.0.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
|
||||
|
||||
python scripts/math/deepseek_math_sample_steps.py \
|
||||
--input_file "$OUTPUT_PATH_PREFIX/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/500k-split-*-of-20/train.500k.de_con.boxed.v1.0.*-of-20.0shot.n10.tem1.0.p0.9.*-of-64.s42.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mscale-v0.1-300k.process-dpo-sc.iter0.H100.dp8.v1.3.s42/checkpoint-1200/mathscale4o/train.500k.de_con.boxed.v1.0.n10.tem1.0.p0.9.upper0.7.r0.3.sample10.filter_same.json \
|
||||
--upper_step_ratio 0.7 --sample_ratio 0.3 --filter_all_same --sample_over_p 10
|
||||
#Number of prefixes: 2226672
|
||||
#222705
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import json
|
||||
import argparse
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file")
|
||||
parser.add_argument("--output_file")
|
||||
args = parser.parse_args()
|
||||
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
for item in data:
|
||||
item["prompt"] = f"{item['question']}\n\nPlease put your final answer within " + "\\boxed{}."
|
||||
|
||||
with open(args.output_file, "w") as f:
|
||||
for item in tqdm(data):
|
||||
f.write(json.dumps(item) + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,40 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def remove_suffix(solution: str):
|
||||
return solution.split("The answer is")[0].strip()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
data.extend([json.loads(line) for line in open(file).readlines()])
|
||||
|
||||
q_set = set()
|
||||
outputs = []
|
||||
for i, item in tqdm(enumerate(data)):
|
||||
q_set.add(item["question"] + item["completion"])
|
||||
item["solution"] = item["completion"]
|
||||
item["solution_wo_suffix"] = remove_suffix(item["completion"])
|
||||
item["id"] = i
|
||||
outputs.append(item)
|
||||
|
||||
print(len(q_set))
|
||||
print(len(outputs))
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,64 @@
|
||||
import json
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def extract_qa(completion: str, remove_suffix: bool = False):
|
||||
q_s = completion.find("Created Question:")
|
||||
if q_s == -1:
|
||||
return "", ""
|
||||
|
||||
s_s = completion.find("Solution to the Created Question:")
|
||||
if s_s == -1:
|
||||
return "", ""
|
||||
|
||||
question = completion[q_s + len("Created Question:"):s_s].strip()
|
||||
solution = completion[s_s + len("Solution to the Created Question:"):].strip()
|
||||
|
||||
if remove_suffix:
|
||||
solution = solution.split("The answer is")[0].strip()
|
||||
|
||||
return question, solution
|
||||
|
||||
|
||||
def remove_suffix(solution: str):
|
||||
return solution.split("The answer is")[0].strip()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
data.extend([json.loads(line) for line in open(file).readlines()])
|
||||
|
||||
q_set = set()
|
||||
cnt = 0
|
||||
outputs = []
|
||||
for item in tqdm(data):
|
||||
question, solution = extract_qa(item["completion"])
|
||||
if question == "" or solution == "":
|
||||
continue
|
||||
if question + solution in q_set:
|
||||
cnt += 1
|
||||
continue
|
||||
q_set.add(question + solution)
|
||||
item["question"] = question
|
||||
item["solution"] = solution
|
||||
item["solution_wo_suffix"] = remove_suffix(solution)
|
||||
outputs.append(item)
|
||||
|
||||
print(len(outputs))
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,59 @@
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from glob import glob
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.input_file):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = []
|
||||
for file in glob(args.input_file):
|
||||
print(file)
|
||||
data.extend(json.load(open(file)))
|
||||
|
||||
print(len(data))
|
||||
|
||||
outputs = []
|
||||
for item in tqdm(data):
|
||||
item.pop("text")
|
||||
if "sc_res" in item:
|
||||
item.pop("sc_res")
|
||||
if "sc_pred" in item:
|
||||
item.pop("sc_pred")
|
||||
# item.pop("id")
|
||||
# assert "uuid" in item
|
||||
if "uuid" in item:
|
||||
item.pop("id")
|
||||
if "\\boxed{" not in item["response"]:
|
||||
continue
|
||||
if not item["pred"]:
|
||||
continue
|
||||
item["label"] = item.pop("pred")
|
||||
response = item.pop("response")
|
||||
item["box_solution"] = item["solution_wo_suffix"] + response
|
||||
outputs.append(item)
|
||||
|
||||
print(len(outputs))
|
||||
json.dump(outputs, open(args.output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
>>> python ~/gpt-chat-examples/scripts/math_scale/process_raw_4o_labeling.py \
|
||||
--input_file "4o.500k.de_con.v1.0.0shot.n1.tem0.0.p1.0.[0-9]*-of-100.json" --output_file ../train.500k.de_con.v1.0.boxed.json
|
||||
|
||||
511309
|
||||
491733
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
import os
|
||||
from pebble import ProcessPool
|
||||
from functools import partial
|
||||
from multiprocessing.pool import Pool
|
||||
import re
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.qwen25math.grader import math_equal
|
||||
from data.qwen25math.parser import extract_answer, strip_string, STRIP_EXCEPTIONS
|
||||
|
||||
|
||||
def extract_content_from_tag(pred: str):
|
||||
# Regular expression pattern to match the content between <answer> and </answer>
|
||||
pattern = r'<answer>(.*?)</answer>'
|
||||
# Use re.DOTALL to allow matching newlines within the tags
|
||||
match = re.search(pattern, pred, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1).strip() # Strip removes extra spaces or newlines
|
||||
return pred
|
||||
|
||||
|
||||
def majority_voting_predict(preds):
|
||||
if isinstance(preds, str):
|
||||
return preds
|
||||
|
||||
preds = [pred for pred in preds if pred]
|
||||
if len(preds) == 0:
|
||||
return "", 0
|
||||
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
pred, freq = collections.Counter(tmp).most_common(1)[0]
|
||||
pred = eval(pred)
|
||||
elif isinstance(preds[0], str):
|
||||
pred, freq = collections.Counter(preds).most_common(1)[0]
|
||||
else:
|
||||
# raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
print(f"Unknown type {type(preds[0])}")
|
||||
pred = ""
|
||||
freq = 0
|
||||
|
||||
freq = freq / len(preds)
|
||||
|
||||
return pred, freq
|
||||
|
||||
|
||||
def _annotate(param):
|
||||
return param[0], math_equal(param[-2], param[-1])
|
||||
|
||||
|
||||
def preprocess_item(item, args):
|
||||
response = item[args.response_field]
|
||||
if isinstance(response, str):
|
||||
response = extract_content_from_tag(response)
|
||||
pred_clean = extract_answer(response, data_name="math")
|
||||
pred_clean = strip_string(pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if pred_clean is None:
|
||||
pred_clean = ""
|
||||
sc_pred = pred_clean
|
||||
sc_freq = 1.0
|
||||
elif isinstance(response, list):
|
||||
pred_clean = []
|
||||
for resp in response:
|
||||
resp = extract_content_from_tag(resp)
|
||||
tmp_pred_clean = extract_answer(resp, data_name="math")
|
||||
tmp_pred_clean = strip_string(tmp_pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if tmp_pred_clean is None:
|
||||
tmp_pred_clean = ""
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
sc_pred, sc_freq = majority_voting_predict(pred_clean)
|
||||
else:
|
||||
raise ValueError(f"Unknown type of response: {type(response)}")
|
||||
item["pred"] = pred_clean
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_freq"] = sc_freq
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
|
||||
if "college_math" in item[args.source_field]:
|
||||
item[args.label_field] = item[args.label_field].replace("$", "").strip()
|
||||
|
||||
data_name = item[args.source_field].split(".")[0]
|
||||
if data_name not in STRIP_EXCEPTIONS:
|
||||
item[args.label_field] = strip_string(item[args.label_field], skip_unit=data_name == "carp_en")
|
||||
else:
|
||||
# gt_ans = (
|
||||
# gt_ans.replace("\\neq", "\\ne")
|
||||
# .replace("\\leq", "\\le")
|
||||
# .replace("\\geq", "\\ge")
|
||||
# )
|
||||
raise NotImplementedError()
|
||||
|
||||
return item
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--num_workers", type=int, default=16)
|
||||
parser.add_argument("--sub_category", type=str, default=None)
|
||||
parser.add_argument("--label_field", type=str, default="label")
|
||||
parser.add_argument("--response_field", type=str, default="response")
|
||||
parser.add_argument("--source_field", type=str, default="data_topic")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.input_file.endswith(".json"):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
if args.sub_category is not None:
|
||||
print(args.sub_category)
|
||||
sub_categories = set(list(args.sub_category.split(",")))
|
||||
data = [item for item in data if any([sub_category in item[args.source_field] for sub_category in sub_categories])]
|
||||
|
||||
_mp_inputs = []
|
||||
with Pool(args.num_workers) as p:
|
||||
results = list(tqdm(p.imap(partial(preprocess_item, args=args), data), total=len(data), desc="Preprocess data"))
|
||||
|
||||
for i, item in tqdm(enumerate(results), total=len(results), desc="Preprocess data"):
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
for j, pred in enumerate(preds):
|
||||
_mp_inputs.append(((i, j), pred, str(item[args.label_field])))
|
||||
|
||||
pbar = tqdm(_mp_inputs, total=len(_mp_inputs), desc="Submitting eval task", dynamic_ncols=True)
|
||||
|
||||
outputs = collections.defaultdict(dict)
|
||||
timeout_cnt = 0
|
||||
|
||||
with ProcessPool(max_workers=1) as pool:
|
||||
future = pool.map(_annotate, pbar, timeout=3)
|
||||
iterator = future.result()
|
||||
with tqdm(total=len(_mp_inputs), desc="Evaluate") as progress_bar:
|
||||
while True:
|
||||
try:
|
||||
idx, result = next(iterator)
|
||||
# scores.append(result)
|
||||
outputs[idx[0]][idx[1]] = result
|
||||
except StopIteration:
|
||||
break
|
||||
except TimeoutError as error:
|
||||
print(error)
|
||||
# outputs[idx[0]][idx[1]] = False
|
||||
timeout_cnt += 1
|
||||
except Exception as error:
|
||||
print(error)
|
||||
# exit()
|
||||
progress_bar.update(1)
|
||||
|
||||
for i, item in enumerate(data):
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
if i not in outputs:
|
||||
all_res = [False] * len(preds)
|
||||
else:
|
||||
all_res = outputs[i]
|
||||
|
||||
for j, pred in enumerate(preds):
|
||||
if j not in all_res:
|
||||
all_res[j] = False
|
||||
|
||||
assert len(all_res) == len(preds)
|
||||
pred2res = {pred: all_res[j] for j, pred in enumerate(preds)}
|
||||
sc_res = pred2res[item["sc_pred"]]
|
||||
|
||||
item["res"] = [pred2res[pred] for pred in preds]
|
||||
item["sc_res"] = sc_res
|
||||
assert "sc_freq" in item
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
assert len(item["res"]) == 1
|
||||
item["res"] = item["res"][0]
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
for item in data:
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
if args.source_field in item:
|
||||
if "." in item[args.source_field]:
|
||||
item[args.source_field] = item[args.source_field].split(".")[0]
|
||||
acc_data_topic[item[args.source_field]] += int(res[0])
|
||||
cnt_data_topic[item[args.source_field]] += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
output_file = args.input_file.replace(".json", ".sympy_eval.json")
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(data, open(output_file, "w"), indent=2)
|
||||
|
||||
if len(data) == 0:
|
||||
metrics = {"acc": 0, "pass@k": 0, "maj@k": 0, "correct": 0, "total": 0}
|
||||
else:
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data),
|
||||
"correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for key in acc_data_topic:
|
||||
metrics[f"acc_{key}"] = acc_data_topic[key] / cnt_data_topic[key]
|
||||
json.dump(metrics, open(output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(json.dumps(metrics, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,51 @@
|
||||
split_id=$1
|
||||
|
||||
#exp_name=qwen2.5-math.7b.base.math.long_cot.sft.MI300x.dp16.v5.0.1.s42
|
||||
exp_name=qwen2.5-math.7b.base.numina-level5-refine.long_cot.pattern-dpo.mi300x.dp32.iter0.v1.8.s42
|
||||
|
||||
#bash scripts/math_scale/merge_mwpbench_predictions.sh /mnt/fangkai_blob/reward_modeling/experiments/${exp_name} train.wo.math.gsm.2k 2.0 "${split_id}"
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/mwpbench/checkpoint-${split_id}/train.wo.math.gsm.2k.test.v2.0.0shot.n1.tem0.0.p1.0.0-of-1.json" \
|
||||
# --num_workers 1
|
||||
python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
--input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/math500/checkpoint-${split_id}/math.test.v2.0.0shot.n1.tem0.0.p1.0.0-of-1.json" \
|
||||
--num_workers 1 --source_field subject
|
||||
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-128.s0.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/eurus-2-rl-data/train.numina.no_box_inst/cot.n4.tem1.0.p0.7.v2.0.${split_id}-of-128.s1.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/eurus-2-rl-data/train.numina.no_box_inst/cot.n8.tem1.0.p0.7.v2.0.s0.sympy_eval.r1_all_wrong.n8.tem1.0.p0.8.v2.0.${split_id}-of-128.s3.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-128.s0.json" \
|
||||
# --num_workers 64 --source_field source \
|
||||
# --label_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-72B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.s0.sympy_preprocess.refined.freq0.0.v1.0.json" \
|
||||
# --output_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-128.s0.refined.qwen25-math-72b-maj8-freq0.0.v1.0.sympy_eval.json"
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/reward_modeling/experiments/${exp_name}/checkpoint-1900/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.s0.refined.qwen25-math-72b-maj8-freq0.0.v1.0.sympy_eval.alter-iter1.n3.tem1.0.p0.7.v1.1.${split_id}-of-128.s0.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-72B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.s0.sympy_preprocess.refined.freq0.0.v1.0.${split_id}-of-64.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-7B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-64.s0.json" \
|
||||
# --num_workers 64 --source_field source
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-7B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-64.s0.json" \
|
||||
# --num_workers 64 --source_field source \
|
||||
# --label_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-72B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.s0.sympy_preprocess.refined.freq0.0.v1.0.json" \
|
||||
# --output_file "/mnt/fangkai_blob/share/models/Qwen2.5-Math-7B-Instruct/numina.aops_aime_oly.level_5_v1.0/cot.de_con.n8.tem1.0.p0.7.v2.0.${split_id}-of-64.s0.refined.freq0.0.v1.0.sympy_eval.json"
|
||||
|
||||
#python scripts/math_scale/qwen25math_style_eval_v2.0.py \
|
||||
# --input_file "/mnt/fangkai_blob/share/models/Llama-3.3-70B-Instruct/numina/math-difficulty/math.cot.de_con.16k.n4.tem1.0.p0.85.v5.0.s0.sympy_eval.iter1-alter.16k.n1.tem1.0.p0.85.${split_id}-of-16.v1.0.s0.json" \
|
||||
# --num_workers 64 --source_field data_topic
|
||||
@@ -0,0 +1,212 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
import os
|
||||
from pebble import ProcessPool
|
||||
import re
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.qwen25math.grader import math_equal
|
||||
from data.qwen25math.parser import extract_answer, strip_string, STRIP_EXCEPTIONS
|
||||
|
||||
|
||||
def extract_content_from_tag(pred: str):
|
||||
# Regular expression pattern to match the content between <answer> and </answer>
|
||||
pattern = r'<answer>(.*?)</answer>'
|
||||
# Use re.DOTALL to allow matching newlines within the tags
|
||||
match = re.search(pattern, pred, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1).strip() # Strip removes extra spaces or newlines
|
||||
return pred
|
||||
|
||||
|
||||
def majority_voting_predict(preds):
|
||||
if isinstance(preds, str):
|
||||
return preds
|
||||
|
||||
preds = [pred for pred in preds if pred]
|
||||
if len(preds) == 0:
|
||||
return ""
|
||||
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
pred = collections.Counter(tmp).most_common(1)[0][0]
|
||||
pred = eval(pred)
|
||||
elif isinstance(preds[0], str):
|
||||
pred = collections.Counter(preds).most_common(1)[0][0]
|
||||
else:
|
||||
# raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
print(f"Unknown type {type(preds[0])}")
|
||||
pred = ""
|
||||
return pred
|
||||
|
||||
|
||||
def _annotate(param):
|
||||
return param[0], math_equal(param[-2], param[-1])
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--num_workers", type=int, default=16)
|
||||
parser.add_argument("--sub_category", type=str, default=None)
|
||||
parser.add_argument("--label_field", type=str, default="label")
|
||||
parser.add_argument("--response_field", type=str, default="response")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.input_file.endswith(".json"):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
if args.sub_category is not None:
|
||||
print(args.sub_category)
|
||||
sub_categories = set(list(args.sub_category.split(",")))
|
||||
data = [item for item in data if any([sub_category in item["data_topic"] for sub_category in sub_categories])]
|
||||
|
||||
_mp_inputs = []
|
||||
for i, item in enumerate(data):
|
||||
response = item[args.response_field]
|
||||
if isinstance(response, str):
|
||||
response = extract_content_from_tag(response)
|
||||
pred_clean = extract_answer(response, data_name="math")
|
||||
pred_clean = strip_string(pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if pred_clean is None:
|
||||
pred_clean = ""
|
||||
sc_pred = pred_clean
|
||||
elif isinstance(response, list):
|
||||
pred_clean = []
|
||||
for resp in response:
|
||||
resp = extract_content_from_tag(resp)
|
||||
tmp_pred_clean = extract_answer(resp, data_name="math")
|
||||
tmp_pred_clean = strip_string(tmp_pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if tmp_pred_clean is None:
|
||||
tmp_pred_clean = ""
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
sc_pred = majority_voting_predict(pred_clean)
|
||||
else:
|
||||
raise ValueError(f"Unknown type of response: {type(response)}")
|
||||
item["pred"] = pred_clean
|
||||
item["sc_pred"] = sc_pred
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
|
||||
# if "college_math" in item["data_topic"]:
|
||||
# item[args.label_field] = item[args.label_field].replace("$", "").strip()
|
||||
#
|
||||
# data_name = item["data_topic"].split(".")[0]
|
||||
# if data_name not in STRIP_EXCEPTIONS:
|
||||
# item[args.label_field] = strip_string(item[args.label_field], skip_unit=data_name == "carp_en")
|
||||
# else:
|
||||
# # gt_ans = (
|
||||
# # gt_ans.replace("\\neq", "\\ne")
|
||||
# # .replace("\\leq", "\\le")
|
||||
# # .replace("\\geq", "\\ge")
|
||||
# # )
|
||||
# raise NotImplementedError()
|
||||
item[args.label_field] = strip_string(item[args.label_field], skip_unit=False)
|
||||
|
||||
for j, pred in enumerate(preds):
|
||||
_mp_inputs.append(((i, j), pred, str(item[args.label_field])))
|
||||
pbar = tqdm(_mp_inputs, total=len(_mp_inputs), desc="Submitting eval task", dynamic_ncols=True)
|
||||
|
||||
outputs = collections.defaultdict(dict)
|
||||
timeout_cnt = 0
|
||||
|
||||
with ProcessPool(max_workers=1) as pool:
|
||||
future = pool.map(_annotate, pbar, timeout=3)
|
||||
iterator = future.result()
|
||||
with tqdm(total=len(_mp_inputs), desc="Evaluate") as progress_bar:
|
||||
while True:
|
||||
try:
|
||||
idx, result = next(iterator)
|
||||
# scores.append(result)
|
||||
outputs[idx[0]][idx[1]] = result
|
||||
except StopIteration:
|
||||
break
|
||||
except TimeoutError as error:
|
||||
print(error)
|
||||
# outputs[idx[0]][idx[1]] = False
|
||||
timeout_cnt += 1
|
||||
except Exception as error:
|
||||
print(error)
|
||||
# exit()
|
||||
progress_bar.update(1)
|
||||
|
||||
for i, item in enumerate(data):
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
if i not in outputs:
|
||||
all_res = [False] * len(preds)
|
||||
else:
|
||||
all_res = outputs[i]
|
||||
|
||||
for j, pred in enumerate(preds):
|
||||
if j not in all_res:
|
||||
all_res[j] = False
|
||||
|
||||
assert len(all_res) == len(preds)
|
||||
pred2res = {pred: all_res[j] for j, pred in enumerate(preds)}
|
||||
sc_res = pred2res[item["sc_pred"]]
|
||||
|
||||
item["res"] = [pred2res[pred] for pred in preds]
|
||||
item["sc_res"] = sc_res
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
assert len(item["res"]) == 1
|
||||
item["res"] = item["res"][0]
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
for item in data:
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
# if "data_topic" in item:
|
||||
# if "." in item["data_topic"]:
|
||||
# item["data_topic"] = item["data_topic"].split(".")[0]
|
||||
# acc_data_topic[item["data_topic"]] += int(res[0])
|
||||
# cnt_data_topic[item["data_topic"]] += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
output_file = args.input_file.replace(".json", ".sympy_eval.json")
|
||||
assert pass_at_k <= len(data)
|
||||
json.dump(data, open(output_file, "w"), indent=2)
|
||||
|
||||
if len(data) == 0:
|
||||
metrics = {"acc": 0, "pass@k": 0, "maj@k": 0, "correct": 0, "total": 0}
|
||||
else:
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data),
|
||||
"correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for key in acc_data_topic:
|
||||
metrics[f"acc_{key}"] = acc_data_topic[key] / cnt_data_topic[key]
|
||||
json.dump(metrics, open(output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(json.dumps(metrics, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,258 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
import os
|
||||
from pebble import ProcessPool
|
||||
from functools import partial
|
||||
from multiprocessing.pool import Pool
|
||||
import re
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.qwen25math.grader import math_equal
|
||||
from data.qwen25math.parser import extract_answer, strip_string, STRIP_EXCEPTIONS
|
||||
|
||||
|
||||
def extract_content_from_tag(pred: str):
|
||||
# Regular expression pattern to match the content between <answer> and </answer>
|
||||
pattern = r'<answer>(.*?)</answer>'
|
||||
# Use re.DOTALL to allow matching newlines within the tags
|
||||
match = re.search(pattern, pred, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1).strip() # Strip removes extra spaces or newlines
|
||||
return pred
|
||||
|
||||
|
||||
def majority_voting_predict(preds):
|
||||
if isinstance(preds, str):
|
||||
return preds
|
||||
|
||||
preds = [pred for pred in preds if pred]
|
||||
if len(preds) == 0:
|
||||
return "", 0
|
||||
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
pred, freq = collections.Counter(tmp).most_common(1)[0]
|
||||
pred = eval(pred)
|
||||
elif isinstance(preds[0], str):
|
||||
pred, freq = collections.Counter(preds).most_common(1)[0]
|
||||
else:
|
||||
# raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
print(f"Unknown type {type(preds[0])}")
|
||||
pred = ""
|
||||
freq = 0
|
||||
|
||||
freq = freq / len(preds)
|
||||
|
||||
return pred, freq
|
||||
|
||||
|
||||
def _annotate(param):
|
||||
return param[0], math_equal(param[-2], param[-1])
|
||||
|
||||
|
||||
def preprocess_item(item, args):
|
||||
response = item[args.response_field]
|
||||
if isinstance(response, str):
|
||||
response = extract_content_from_tag(response)
|
||||
pred_clean = extract_answer(response, data_name="math")
|
||||
pred_clean = strip_string(pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if pred_clean is None:
|
||||
pred_clean = ""
|
||||
sc_pred = pred_clean
|
||||
sc_freq = 1.0
|
||||
elif isinstance(response, list):
|
||||
pred_clean = []
|
||||
for resp in response:
|
||||
resp = extract_content_from_tag(resp)
|
||||
tmp_pred_clean = extract_answer(resp, data_name="math")
|
||||
tmp_pred_clean = strip_string(tmp_pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if tmp_pred_clean is None:
|
||||
tmp_pred_clean = ""
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
sc_pred, sc_freq = majority_voting_predict(pred_clean)
|
||||
else:
|
||||
raise ValueError(f"Unknown type of response: {type(response)}")
|
||||
item["pred"] = pred_clean
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_freq"] = sc_freq
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
|
||||
if "college_math" in item[args.source_field]:
|
||||
item[args.label_field] = item[args.label_field].replace("$", "").strip()
|
||||
|
||||
data_name = item[args.source_field].split(".")[0]
|
||||
if data_name not in STRIP_EXCEPTIONS:
|
||||
item[args.label_field] = strip_string(item[args.label_field], skip_unit=data_name == "carp_en")
|
||||
else:
|
||||
# gt_ans = (
|
||||
# gt_ans.replace("\\neq", "\\ne")
|
||||
# .replace("\\leq", "\\le")
|
||||
# .replace("\\geq", "\\ge")
|
||||
# )
|
||||
raise NotImplementedError()
|
||||
|
||||
return item
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--label_file", type=str, default=None)
|
||||
parser.add_argument("--output_file", type=str, default=None)
|
||||
parser.add_argument("--num_workers", type=int, default=16)
|
||||
parser.add_argument("--sub_category", type=str, default=None)
|
||||
parser.add_argument("--label_field", type=str, default="label")
|
||||
parser.add_argument("--response_field", type=str, default="response")
|
||||
parser.add_argument("--source_field", type=str, default="data_topic")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.input_file.endswith(".json"):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
if args.sub_category is not None:
|
||||
print(args.sub_category)
|
||||
sub_categories = set(list(args.sub_category.split(",")))
|
||||
data = [item for item in data if any([sub_category in item[args.source_field] for sub_category in sub_categories])]
|
||||
|
||||
if args.label_file is not None:
|
||||
label_data = json.load(open(args.label_file))
|
||||
label_data = {item["id"]: item for item in label_data}
|
||||
new_data = []
|
||||
_labeling_missing = 0
|
||||
for item in data:
|
||||
if item["id"] in label_data:
|
||||
item["label"] = label_data[item["id"]]["label"]
|
||||
new_data.append(item)
|
||||
else:
|
||||
_labeling_missing += 1
|
||||
|
||||
print(f"Labeling missing: {_labeling_missing}")
|
||||
data = new_data
|
||||
|
||||
_mp_inputs = []
|
||||
with Pool(args.num_workers) as p:
|
||||
results = list(tqdm(p.imap(partial(preprocess_item, args=args), data), total=len(data), desc="Preprocess data"))
|
||||
|
||||
for i, item in tqdm(enumerate(results), total=len(results), desc="Preprocess data"):
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
for j, pred in enumerate(preds):
|
||||
_mp_inputs.append(((i, j), pred, str(item[args.label_field])))
|
||||
data = results
|
||||
|
||||
pbar = tqdm(_mp_inputs, total=len(_mp_inputs), desc="Submitting eval task", dynamic_ncols=True)
|
||||
|
||||
outputs = collections.defaultdict(dict)
|
||||
timeout_cnt = 0
|
||||
|
||||
with ProcessPool(max_workers=1) as pool:
|
||||
future = pool.map(_annotate, pbar, timeout=3)
|
||||
iterator = future.result()
|
||||
with tqdm(total=len(_mp_inputs), desc="Evaluate") as progress_bar:
|
||||
while True:
|
||||
try:
|
||||
idx, result = next(iterator)
|
||||
# scores.append(result)
|
||||
outputs[idx[0]][idx[1]] = result
|
||||
except StopIteration:
|
||||
break
|
||||
except TimeoutError as error:
|
||||
print(error)
|
||||
# outputs[idx[0]][idx[1]] = False
|
||||
timeout_cnt += 1
|
||||
except Exception as error:
|
||||
print(error)
|
||||
# exit()
|
||||
progress_bar.update(1)
|
||||
|
||||
for i, item in enumerate(data):
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
if i not in outputs:
|
||||
all_res = [False] * len(preds)
|
||||
else:
|
||||
all_res = outputs[i]
|
||||
|
||||
for j, pred in enumerate(preds):
|
||||
if j not in all_res:
|
||||
all_res[j] = False
|
||||
|
||||
assert len(all_res) == len(preds)
|
||||
pred2res = {pred: all_res[j] for j, pred in enumerate(preds)}
|
||||
sc_res = pred2res[item["sc_pred"]]
|
||||
|
||||
item["res"] = [pred2res[pred] for pred in preds]
|
||||
item["sc_res"] = sc_res
|
||||
assert "sc_freq" in item
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
assert len(item["res"]) == 1
|
||||
item["res"] = item["res"][0]
|
||||
|
||||
cnt = 0
|
||||
pass_at_k = 0
|
||||
sc = 0
|
||||
acc_data_topic = collections.Counter()
|
||||
cnt_data_topic = collections.Counter()
|
||||
for item in data:
|
||||
if not isinstance(item["res"], list):
|
||||
res = [item["res"]]
|
||||
else:
|
||||
res = item["res"]
|
||||
if res[0]:
|
||||
cnt += 1
|
||||
if args.source_field in item:
|
||||
if "." in item[args.source_field]:
|
||||
item[args.source_field] = item[args.source_field].split(".")[0]
|
||||
acc_data_topic[item[args.source_field]] += int(res[0])
|
||||
cnt_data_topic[item[args.source_field]] += 1
|
||||
if any(res):
|
||||
pass_at_k += 1
|
||||
if item["sc_res"]:
|
||||
sc += 1
|
||||
|
||||
output_file = args.input_file.replace(".json", ".sympy_eval.json")
|
||||
assert pass_at_k <= len(data)
|
||||
|
||||
if len(data) == 0:
|
||||
metrics = {"acc": 0, "pass@k": 0, "maj@k": 0, "correct": 0, "total": 0}
|
||||
else:
|
||||
metrics = {"acc": cnt / len(data), "pass@k": pass_at_k / len(data), "maj@k": sc / len(data),
|
||||
"correct": cnt, "total": len(data)}
|
||||
if len(acc_data_topic) > 0:
|
||||
for key in acc_data_topic:
|
||||
metrics[f"acc_{key}"] = acc_data_topic[key] / cnt_data_topic[key]
|
||||
|
||||
if args.output_file is None:
|
||||
json.dump(data, open(output_file, "w"), indent=2)
|
||||
|
||||
json.dump(metrics, open(output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
else:
|
||||
json.dump(data, open(args.output_file, "w"), indent=2)
|
||||
|
||||
json.dump(metrics, open(args.output_file.replace(".json", ".metrics.json"), "w"), indent=2)
|
||||
|
||||
print(json.dumps(metrics, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,137 @@
|
||||
import argparse
|
||||
import collections
|
||||
import json
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
import os
|
||||
from pebble import ProcessPool
|
||||
from functools import partial
|
||||
from multiprocessing.pool import Pool
|
||||
import re
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from data.qwen25math.grader import math_equal
|
||||
from data.qwen25math.parser import extract_answer, strip_string, STRIP_EXCEPTIONS
|
||||
|
||||
|
||||
def extract_content_from_tag(pred: str):
|
||||
# Regular expression pattern to match the content between <answer> and </answer>
|
||||
pattern = r'<answer>(.*?)</answer>'
|
||||
# Use re.DOTALL to allow matching newlines within the tags
|
||||
match = re.search(pattern, pred, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1).strip() # Strip removes extra spaces or newlines
|
||||
return pred
|
||||
|
||||
|
||||
def majority_voting_predict(preds):
|
||||
if isinstance(preds, str):
|
||||
return preds
|
||||
|
||||
preds = [pred for pred in preds if pred]
|
||||
if len(preds) == 0:
|
||||
return "", 0
|
||||
|
||||
assert isinstance(preds, list)
|
||||
if isinstance(preds[0], list):
|
||||
tmp = []
|
||||
for pred in preds:
|
||||
tmp.append(str(sorted(pred)))
|
||||
pred, freq = collections.Counter(tmp).most_common(1)[0]
|
||||
pred = eval(pred)
|
||||
elif isinstance(preds[0], str):
|
||||
pred, freq = collections.Counter(preds).most_common(1)[0]
|
||||
else:
|
||||
# raise ValueError(f"Unknown type {type(preds[0])}")
|
||||
print(f"Unknown type {type(preds[0])}")
|
||||
pred = ""
|
||||
freq = 0
|
||||
|
||||
freq = freq / len(preds)
|
||||
|
||||
return pred, freq
|
||||
|
||||
|
||||
def _annotate(param):
|
||||
return param[0], math_equal(param[-2], param[-1])
|
||||
|
||||
|
||||
def preprocess_item(item, args):
|
||||
response = item[args.response_field]
|
||||
if isinstance(response, str):
|
||||
response = extract_content_from_tag(response)
|
||||
pred_clean = extract_answer(response, data_name="math")
|
||||
pred_clean = strip_string(pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if pred_clean is None:
|
||||
pred_clean = ""
|
||||
sc_pred = pred_clean
|
||||
sc_freq = 1.0
|
||||
elif isinstance(response, list):
|
||||
pred_clean = []
|
||||
for resp in response:
|
||||
resp = extract_content_from_tag(resp)
|
||||
tmp_pred_clean = extract_answer(resp, data_name="math")
|
||||
tmp_pred_clean = strip_string(tmp_pred_clean, skip_unit="math" in STRIP_EXCEPTIONS)
|
||||
if tmp_pred_clean is None:
|
||||
tmp_pred_clean = ""
|
||||
pred_clean.append(tmp_pred_clean)
|
||||
sc_pred, sc_freq = majority_voting_predict(pred_clean)
|
||||
else:
|
||||
raise ValueError(f"Unknown type of response: {type(response)}")
|
||||
item["pred"] = pred_clean
|
||||
item["sc_pred"] = sc_pred
|
||||
item["sc_freq"] = sc_freq
|
||||
|
||||
if not isinstance(item["pred"], list):
|
||||
preds = [item["pred"]]
|
||||
else:
|
||||
preds = item["pred"]
|
||||
|
||||
if "college_math" in item[args.source_field]:
|
||||
item[args.label_field] = item[args.label_field].replace("$", "").strip()
|
||||
|
||||
data_name = item[args.source_field].split(".")[0]
|
||||
if data_name not in STRIP_EXCEPTIONS:
|
||||
item[args.label_field] = strip_string(item[args.label_field], skip_unit=data_name == "carp_en")
|
||||
else:
|
||||
# gt_ans = (
|
||||
# gt_ans.replace("\\neq", "\\ne")
|
||||
# .replace("\\leq", "\\le")
|
||||
# .replace("\\geq", "\\ge")
|
||||
# )
|
||||
raise NotImplementedError()
|
||||
|
||||
return item
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--input_file", type=str)
|
||||
parser.add_argument("--num_workers", type=int, default=16)
|
||||
parser.add_argument("--label_field", type=str, default="label")
|
||||
parser.add_argument("--response_field", type=str, default="response")
|
||||
parser.add_argument("--source_field", type=str, default="data_topic")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.input_file.endswith(".json"):
|
||||
data = json.load(open(args.input_file))
|
||||
else:
|
||||
data = [json.loads(line) for line in open(args.input_file).readlines()]
|
||||
|
||||
_mp_inputs = []
|
||||
with Pool(args.num_workers) as p:
|
||||
results = list(tqdm(p.imap(partial(preprocess_item, args=args), data), total=len(data), desc="Preprocess data"))
|
||||
|
||||
data = results
|
||||
for i, item in enumerate(data):
|
||||
assert "sc_freq" in item
|
||||
|
||||
output_file = args.input_file.replace(".json", ".sympy_preprocess.json")
|
||||
json.dump(data, open(output_file, "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,33 @@
|
||||
# Iter - 1: PRM reranking
|
||||
export OUTPUT_PATH_PREFIX=../msranlpintern/reward_modeling
|
||||
|
||||
for global_split_id in {0..19}; do
|
||||
python scripts/math_scale/rerank_w_prm_math_scale_save.py \
|
||||
--response_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/500k-split-${global_split_id}-of-20/train.500k.de_con.boxed.v1.0.${global_split_id}-of-20.0shot.n90.tem1.0.p0.9.json" \
|
||||
--reward_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-rm-sc.iter1.h100.dp8.v1.0.s42/mathstral.mathscale4o.pdpo.iter0.v2.2.s42.ckpt-600.mathscale4o.500k.global-${global_split_id}-of-20-local-*-of-30/test-checkpoint-2000/eval_predictions.json" \
|
||||
--reduction "min" --num_workers 64 --top_k 4 --sc_type "bon" --output_file ${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.sc-iter1-prm-top4.min.bon.${global_split_id}-of-20. json
|
||||
done
|
||||
|
||||
# Concat dataset
|
||||
python scripts/math_scale/concat_data.py \
|
||||
--input_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.sc-iter1-prm-top4.min.bon.w_incorrect.glo-*-of-20.json" \
|
||||
--output_file ${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.sc-iter1-prm-top4.min.bon.w_incorrect.json
|
||||
|
||||
|
||||
python scripts/math_scale/concat_data.py \
|
||||
--input_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.sc-iter1-prm-top4.min.bon.glo-*-of-20.json" \
|
||||
--output_file ${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/mathscale4o/train.500k.de_con.boxed.v1.0.n90.tem1.0.p0.9.sc-iter1-prm-top4.min.bon.json
|
||||
|
||||
|
||||
# Iter - 1: DPO - NuminaMath (new questions)
|
||||
python scripts/math_scale/construct_prefer_pair_sc.py \
|
||||
--input_file "${OUTPUT_PATH_PREFIX}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.*-of-8.s0.json" \
|
||||
--output_file $OUTPUT_PATH_PREFIX/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-0-of-10/cot.de_con.0-of-10.n8.tem1.0.p1.0.s0.prefer_pair.by_sc.json
|
||||
|
||||
python scripts/math_scale/construct_process_rm_sample_sc.py \
|
||||
--input_file "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/completion-split-128/cot.de_con.n8.tem1.0.p1.0.s0.upper0.7.r0.3.sample32.filter_same.{split_id}-of-4.n3.tem1.0.p1.0.*-of-128.s0.json" \
|
||||
--output_file ${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/cot.de_con.n8.tem1.0.p1.0.s0.upper0.7.r0.3.sample32.filter_same.process_rm.iter0-pdpo-sc-p0.5.v0.0.{split_id}-of-4.azure.json \
|
||||
--response_file_for_sc "${OUTPUT_PREFIX_PATH}/experiments/mathstral.mathscale4o.process-dpo.iter0.V100.tp8dp48.v2.2.fix.s42/checkpoint-600/numina/830k-split-*-of-10/cot.de_con.*-of-10.n8.tem1.0.p1.0.*-of-*.s*.json" --response_id_field id --num_workers 128 --top_p 0.5
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
import json
|
||||
import argparse
|
||||
import os.path
|
||||
from glob import glob
|
||||
from tqdm import tqdm
|
||||
from multiprocessing import Pool
|
||||
import numpy as np
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
|
||||
def load_rewards(reward_file, re_index):
|
||||
if os.path.exists(reward_file):
|
||||
rewards = json.load(open(reward_file))
|
||||
else:
|
||||
rewards = []
|
||||
for i, file in enumerate(sorted(glob(reward_file))):
|
||||
print(file)
|
||||
sub_rewards = json.load(open(file))
|
||||
if re_index:
|
||||
for item in sub_rewards:
|
||||
item["index"] = f"{item['index']}_{i}"
|
||||
rewards.extend(sub_rewards)
|
||||
|
||||
return rewards
|
||||
|
||||
|
||||
def softmax(x):
|
||||
exp = np.exp(x - np.max(x, axis=-1, keepdims=True))
|
||||
return exp / exp.sum(axis=-1, keepdims=True)
|
||||
|
||||
|
||||
def reward_reduction(ending_logits, reduction: str = "min", norm: bool = True):
|
||||
if norm:
|
||||
# ending_logits = torch.tensor(ending_logits)
|
||||
# ending_probs = torch.softmax(ending_logits, dim=-1).tolist()
|
||||
ending_probs = softmax(ending_logits)
|
||||
step_rewards = [step[1] for step in ending_probs]
|
||||
else:
|
||||
step_rewards = [step[1] for step in ending_logits]
|
||||
if reduction == "min":
|
||||
reward = min(step_rewards)
|
||||
elif reduction == "product":
|
||||
reward = 1
|
||||
for prob in step_rewards:
|
||||
reward *= prob
|
||||
elif reduction == "sum":
|
||||
reward = sum(step_rewards)
|
||||
else:
|
||||
raise ValueError(f"Invalid reduction method: {reduction}")
|
||||
return reward
|
||||
|
||||
|
||||
def weighted_majority_voting_predict(preds, weights):
|
||||
pred2weight = {}
|
||||
for pred, weight in zip(preds, weights):
|
||||
if pred not in pred2weight:
|
||||
pred2weight[pred] = 0
|
||||
pred2weight[pred] += weight
|
||||
return max(pred2weight, key=pred2weight.get)
|
||||
|
||||
|
||||
def _init(id2reward):
|
||||
global _id2reward
|
||||
_id2reward = id2reward
|
||||
|
||||
|
||||
def _worker(item, reduction, norm, sc_top_k=None):
|
||||
if not item["response"] or not item["pred"] or not item["res"]:
|
||||
return {
|
||||
"missing": 1,
|
||||
"reward_missing": 0,
|
||||
"pred_missing": 0,
|
||||
"seq_too_long": 0,
|
||||
"sorted_results": [],
|
||||
"sc_res": False
|
||||
}
|
||||
unsorted_results = []
|
||||
reward_missing = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
for i, (resp, pred, r) in enumerate(zip(item["response"], item["pred"], item["res"])):
|
||||
resp_id = f"{item['id']}_{i}"
|
||||
if resp_id not in _id2reward:
|
||||
reward_missing += 1
|
||||
continue
|
||||
if not pred:
|
||||
pred_missing += 1
|
||||
continue
|
||||
|
||||
process_rewards = _id2reward[resp_id]
|
||||
if len(process_rewards["ending_logits"]) == 0:
|
||||
seq_too_long += 1
|
||||
continue
|
||||
|
||||
assert resp == process_rewards["response"], f"{resp} \n\n {process_rewards['response']} \n\n ========="
|
||||
|
||||
reward = reward_reduction(process_rewards["ending_logits"], reduction, norm)
|
||||
unsorted_results.append((resp, pred, r, reward))
|
||||
sorted_results = sorted(unsorted_results, key=lambda x: x[-1], reverse=True)
|
||||
|
||||
sc_top_k_res = {}
|
||||
if sc_top_k and sorted_results:
|
||||
for k in sc_top_k:
|
||||
preds = [r[1] for r in sorted_results[:k]]
|
||||
sc_pred = majority_voting_predict(preds)
|
||||
if sc_pred != "":
|
||||
sc_res = mathscale_is_equiv(sc_pred, item["label"])[0]
|
||||
else:
|
||||
sc_res = False
|
||||
sc_top_k_res[k] = sc_res
|
||||
|
||||
sc_k_res = {}
|
||||
if sc_top_k and unsorted_results:
|
||||
for k in sc_top_k:
|
||||
preds = [r[1] for r in unsorted_results[:k]]
|
||||
sc_pred = majority_voting_predict(preds)
|
||||
if sc_pred != "":
|
||||
sc_res = mathscale_is_equiv(sc_pred, item["label"])[0]
|
||||
else:
|
||||
sc_res = False
|
||||
sc_k_res[k] = sc_res
|
||||
|
||||
weighted_best_of_k = {}
|
||||
if sc_top_k and unsorted_results:
|
||||
for k in sc_top_k:
|
||||
preds = [r[1] for r in unsorted_results[:k]]
|
||||
# weights = torch.softmax(torch.tensor([r[-1] for r in unsorted_results]), dim=0).tolist()
|
||||
weights = softmax(np.array([r[-1] for r in unsorted_results])).tolist()
|
||||
best_of_k_pred = weighted_majority_voting_predict(preds, weights)
|
||||
if best_of_k_pred != "":
|
||||
best_of_k_res = mathscale_is_equiv(best_of_k_pred, item["label"])[0]
|
||||
else:
|
||||
best_of_k_res = False
|
||||
weighted_best_of_k[k] = best_of_k_res
|
||||
|
||||
return {
|
||||
"missing": 0,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"sorted_results": sorted_results,
|
||||
"sc_res": item["sc_res"],
|
||||
"sc_top_k_res": sc_top_k_res,
|
||||
"weighted_best_of_k": weighted_best_of_k,
|
||||
"sc_k_res": sc_k_res,
|
||||
}
|
||||
|
||||
|
||||
def merge_key(item, value):
|
||||
assert isinstance(item, list)
|
||||
if isinstance(value, list):
|
||||
item = item + value
|
||||
else:
|
||||
item.append(value)
|
||||
return item
|
||||
|
||||
|
||||
def merge_seed_sampled_data(data):
|
||||
id2data = {}
|
||||
for item in data:
|
||||
if item["id"] not in id2data:
|
||||
id2data[item["id"]] = item
|
||||
continue
|
||||
|
||||
tmp = id2data[item["id"]]
|
||||
if isinstance(tmp["response"], str):
|
||||
tmp["response"] = [tmp["response"]]
|
||||
if not isinstance(tmp["res"], list):
|
||||
tmp["res"] = [tmp["res"]]
|
||||
if not isinstance(tmp["pred"], list):
|
||||
tmp["pred"] = [tmp["pred"]]
|
||||
|
||||
tmp["response"] = merge_key(tmp["response"], item["response"])
|
||||
tmp["res"] = merge_key(tmp["res"], item["res"])
|
||||
tmp["pred"] = merge_key(tmp["pred"], item["pred"])
|
||||
assert isinstance(tmp["pred"], list), tmp["pred"]
|
||||
id2data[item["id"]] = tmp
|
||||
|
||||
return list(id2data.values())
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--response_file", type=str, required=True)
|
||||
parser.add_argument("--reward_file", type=str, required=True)
|
||||
parser.add_argument("--reduction", type=str, default="min")
|
||||
parser.add_argument("--raw_logits", action="store_true", default=False)
|
||||
parser.add_argument("--num_workers", type=int, default=4)
|
||||
parser.add_argument("--re_index", action="store_true", default=False)
|
||||
parser.add_argument("--keep_top_k", type=str, default="")
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.response_file):
|
||||
responses = json.load(open(args.response_file))
|
||||
else:
|
||||
responses = []
|
||||
for file in glob(args.response_file):
|
||||
print(file)
|
||||
responses.extend(json.load(open(file)))
|
||||
|
||||
responses = merge_seed_sampled_data(responses)
|
||||
|
||||
print(len(responses[0]["response"]), len(responses[0]["pred"]), len(responses[0]["res"]))
|
||||
print(responses[0]["id"])
|
||||
|
||||
rewards = load_rewards(args.reward_file, args.re_index)
|
||||
print(rewards[0]['index'])
|
||||
id2reward = {item["index"]: item for item in rewards}
|
||||
|
||||
# _k = [1, 3, 5]
|
||||
_k = [3, 5, 4, 8, 16, 32, 64, 128]
|
||||
prm_pass_at_k = {k: 0 for k in _k}
|
||||
missing = 0
|
||||
missing_reward = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
sc_cnt = 0
|
||||
ultimate_results = []
|
||||
with Pool(args.num_workers, initializer=_init, initargs=(id2reward,)) as pool:
|
||||
annotate = partial(_worker, reduction=args.reduction, norm=(not args.raw_logits), sc_top_k=_k)
|
||||
results = list(tqdm(pool.imap_unordered(annotate, responses), total=len(responses)))
|
||||
|
||||
sc_top_k = {k: 0 for k in _k}
|
||||
weighted_best_of_k = {k: 0 for k in _k}
|
||||
sc_k = {k: 0 for k in _k}
|
||||
outputs = []
|
||||
for item in results:
|
||||
missing += item["missing"]
|
||||
missing_reward += item["reward_missing"]
|
||||
pred_missing += item["pred_missing"]
|
||||
seq_too_long += item["seq_too_long"]
|
||||
sorted_results = item["sorted_results"]
|
||||
|
||||
if item["sc_res"]:
|
||||
sc_cnt += 1
|
||||
|
||||
if not sorted_results:
|
||||
continue
|
||||
|
||||
ultimate_results.append(sorted_results)
|
||||
for k in _k:
|
||||
if any([r[2] for r in sorted_results[:k]]):
|
||||
prm_pass_at_k[k] += 1
|
||||
|
||||
for k, v in item["sc_top_k_res"].items():
|
||||
if v:
|
||||
sc_top_k[k] += 1
|
||||
|
||||
for k, v in item["weighted_best_of_k"].items():
|
||||
if v:
|
||||
weighted_best_of_k[k] += 1
|
||||
|
||||
for k, v in item["sc_k_res"].items():
|
||||
if v:
|
||||
sc_k[k] += 1
|
||||
|
||||
print(f"Total: {len(responses)}")
|
||||
print(f"Missing: {missing}")
|
||||
print(f"Missing reward: {missing_reward}")
|
||||
print(f"Missing pred: {pred_missing}")
|
||||
print(f"Seq too long: {seq_too_long}")
|
||||
print(f"SC: {sc_cnt}")
|
||||
for k, v in prm_pass_at_k.items():
|
||||
print(f"PRM pass at {k}: {v}")
|
||||
print(f"PRM pass at {k} rate: {v / len(responses) * 100:.2f}%")
|
||||
for k, v in sc_top_k.items():
|
||||
print(f"SC pass at {k}: {v}")
|
||||
print(f"SC pass at {k} rate: {v / len(responses) * 100:.2f}%")
|
||||
print(f"SC rate: {sc_cnt / len(responses) * 100:.2f}%")
|
||||
for k, v in weighted_best_of_k.items():
|
||||
print(f"Weighted best of k pass at {k}: {v}")
|
||||
print(f"Weighted best of k pass at {k} rate: {v / len(responses) * 100:.2f}%")
|
||||
for k, v in sc_k.items():
|
||||
print(f"SC k pass at {k}: {v}")
|
||||
print(f"SC k pass at {k} rate {v / len(responses) * 100:.2f}%")
|
||||
|
||||
json.dump(ultimate_results[:100], open("debug.json", "w"), indent=2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,236 @@
|
||||
import json
|
||||
import argparse
|
||||
import os.path
|
||||
from glob import glob
|
||||
from tqdm import tqdm
|
||||
from multiprocessing import Pool
|
||||
import numpy as np
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
This script is used to rerank according to different measure of rewards and save the sorted responses for rejection sampling.
|
||||
"""
|
||||
|
||||
|
||||
def load_rewards(reward_file, re_index):
|
||||
if os.path.exists(reward_file):
|
||||
print(reward_file)
|
||||
rewards = json.load(open(reward_file))
|
||||
else:
|
||||
rewards = []
|
||||
for i, file in enumerate(sorted(glob(reward_file))):
|
||||
print(file)
|
||||
try:
|
||||
sub_rewards = json.load(open(file))
|
||||
except Exception as e:
|
||||
print(f"Error in {file}: {e}")
|
||||
continue
|
||||
if re_index:
|
||||
for item in sub_rewards:
|
||||
item["index"] = f"{item['index']}_{i}"
|
||||
rewards.extend(sub_rewards)
|
||||
|
||||
return rewards
|
||||
|
||||
|
||||
def softmax(x):
|
||||
exp = np.exp(x - np.max(x, axis=-1, keepdims=True))
|
||||
return exp / exp.sum(axis=-1, keepdims=True)
|
||||
|
||||
|
||||
def reward_reduction(ending_logits, reduction: str = "min", norm: bool = True):
|
||||
if norm:
|
||||
# ending_logits = torch.tensor(ending_logits)
|
||||
# ending_probs = torch.softmax(ending_logits, dim=-1).tolist()
|
||||
ending_probs = softmax(ending_logits).tolist()
|
||||
step_rewards = [step[1] for step in ending_probs]
|
||||
else:
|
||||
step_rewards = [step[1] for step in ending_logits]
|
||||
if reduction == "min":
|
||||
reward = min(step_rewards)
|
||||
elif reduction == "product":
|
||||
reward = 1
|
||||
for prob in step_rewards:
|
||||
reward *= prob
|
||||
elif reduction == "sum":
|
||||
reward = sum(step_rewards)
|
||||
else:
|
||||
raise ValueError(f"Invalid reduction method: {reduction}")
|
||||
return reward
|
||||
|
||||
|
||||
def weighted_majority_voting_predict(preds, weights):
|
||||
pred2weight = {}
|
||||
for pred, weight in zip(preds, weights):
|
||||
if pred not in pred2weight:
|
||||
pred2weight[pred] = 0
|
||||
pred2weight[pred] += weight
|
||||
return max(pred2weight, key=pred2weight.get)
|
||||
|
||||
|
||||
def _init(id2reward):
|
||||
global _id2reward
|
||||
_id2reward = id2reward
|
||||
|
||||
|
||||
def _worker(item, reduction, norm, sc_type: str, include_incorrect: bool, top_k: int):
|
||||
if not item["response"] or not item["pred"] or not item["res"]:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": 0,
|
||||
"pred_missing": 0,
|
||||
"seq_too_long": 0,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
unsorted_results = []
|
||||
reward_missing = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
for i, (resp, pred, r) in enumerate(zip(item["response"], item["pred"], item["res"])):
|
||||
resp_id = f"{item['id']}_{i}"
|
||||
if resp_id not in _id2reward:
|
||||
reward_missing += 1
|
||||
continue
|
||||
if not pred:
|
||||
pred_missing += 1
|
||||
continue
|
||||
|
||||
process_rewards = _id2reward[resp_id]
|
||||
if len(process_rewards["ending_logits"]) == 0:
|
||||
seq_too_long += 1
|
||||
continue
|
||||
|
||||
assert resp == process_rewards["response"], f"{resp} \n\n {process_rewards['response']} \n\n ========="
|
||||
|
||||
reward = reward_reduction(process_rewards["ending_logits"], reduction, norm)
|
||||
unsorted_results.append((resp, pred, r, reward))
|
||||
|
||||
if len(unsorted_results) == 0:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
|
||||
# Decide the self-consistency-based pseudo label
|
||||
if sc_type == "majority":
|
||||
sc_pred = majority_voting_predict(item["pred"])
|
||||
elif sc_type == "bon":
|
||||
preds = [r[1] for r in unsorted_results]
|
||||
# weights = torch.softmax(torch.tensor([r[-1] for r in unsorted_results]), dim=0).tolist()
|
||||
weights = softmax([r[-1] for r in unsorted_results]).tolist()
|
||||
assert len(preds) == len(weights)
|
||||
sc_pred = weighted_majority_voting_predict(preds, weights)
|
||||
else:
|
||||
raise ValueError(f"Invalid self-consistency type: {sc_type}")
|
||||
|
||||
if not include_incorrect:
|
||||
unsorted_results = [r for r in unsorted_results if mathscale_is_equiv(r[1], sc_pred)[0]]
|
||||
|
||||
sorted_results = sorted(unsorted_results, key=lambda x: x[-1], reverse=True)
|
||||
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 0,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": unsorted_results,
|
||||
"top_k_sorted_results": sorted_results[:top_k],
|
||||
"sc_pred": sc_pred,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--response_file", type=str, required=True)
|
||||
parser.add_argument("--reward_file", type=str, required=True)
|
||||
parser.add_argument("--reduction", type=str, default="min")
|
||||
parser.add_argument("--raw_logits", action="store_true", default=False)
|
||||
parser.add_argument("--num_workers", type=int, default=4)
|
||||
parser.add_argument("--re_index", action="store_true", default=False)
|
||||
parser.add_argument("--include_incorrect", action="store_true", default=False)
|
||||
parser.add_argument("--top_k", type=int, default=3)
|
||||
parser.add_argument("--sc_type", type=str, choices=["majority", "bon"])
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
print(os.cpu_count())
|
||||
|
||||
if os.path.exists(args.response_file):
|
||||
print(args.response_file)
|
||||
responses = json.load(open(args.response_file))
|
||||
else:
|
||||
responses = []
|
||||
for file in glob(args.response_file):
|
||||
print(file)
|
||||
responses.extend(json.load(open(file)))
|
||||
|
||||
print(len(responses[0]["response"]), len(responses[0]["pred"]), len(responses[0]["res"]))
|
||||
print(responses[0]["id"])
|
||||
print(len(responses))
|
||||
|
||||
rewards = load_rewards(args.reward_file, args.re_index)
|
||||
print(rewards[0]['index'])
|
||||
print(len(rewards))
|
||||
id2reward = {item["index"]: item for item in rewards}
|
||||
|
||||
id2responses = {item["id"]: item for item in responses}
|
||||
|
||||
missing = 0
|
||||
missing_reward = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
with Pool(args.num_workers, initializer=_init, initargs=(id2reward,)) as pool:
|
||||
annotate = partial(_worker, reduction=args.reduction, norm=(not args.raw_logits), sc_type=args.sc_type, include_incorrect=args.include_incorrect,
|
||||
top_k=args.top_k)
|
||||
results = list(tqdm(pool.imap_unordered(annotate, responses), total=len(responses)))
|
||||
|
||||
outputs = []
|
||||
for item in results:
|
||||
missing += item["missing"]
|
||||
missing_reward += item["reward_missing"]
|
||||
pred_missing += item["pred_missing"]
|
||||
seq_too_long += item["seq_too_long"]
|
||||
|
||||
sorted_results = item["top_k_sorted_results"]
|
||||
|
||||
if not sorted_results:
|
||||
continue
|
||||
|
||||
orig_item = id2responses[item["id"]]
|
||||
orig_item.pop("response")
|
||||
orig_item.pop("pred")
|
||||
if "sc_res" in orig_item:
|
||||
orig_item.pop("sc_res")
|
||||
if "sc_pred" in orig_item:
|
||||
orig_item.pop("sc_pred")
|
||||
orig_item.pop("res")
|
||||
|
||||
orig_item["top_k_response"] = [r[0] for r in sorted_results]
|
||||
orig_item["top_k_pred"] = [r[1] for r in sorted_results]
|
||||
orig_item["top_k_reward"] = [r[3] for r in sorted_results]
|
||||
outputs.append(orig_item)
|
||||
|
||||
print(f"Total: {len(responses)}")
|
||||
print(f"Missing: {missing}")
|
||||
print(f"Missing reward: {missing_reward}")
|
||||
print(f"Missing pred: {pred_missing}")
|
||||
print(f"Seq too long: {seq_too_long}")
|
||||
json.dump(outputs, open(args.output_file, "w"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,250 @@
|
||||
import json
|
||||
import argparse
|
||||
import os.path
|
||||
from glob import glob
|
||||
from tqdm import tqdm
|
||||
from multiprocessing import Pool
|
||||
import numpy as np
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
This script is used to rerank according to different measure of rewards and save the sorted responses for rejection sampling.
|
||||
"""
|
||||
|
||||
|
||||
def load_rewards(reward_file, re_index):
|
||||
if os.path.exists(reward_file):
|
||||
print(reward_file)
|
||||
rewards = json.load(open(reward_file))
|
||||
else:
|
||||
rewards = []
|
||||
for i, file in enumerate(sorted(glob(reward_file))):
|
||||
print(file)
|
||||
try:
|
||||
sub_rewards = json.load(open(file))
|
||||
except Exception as e:
|
||||
print(f"Error in {file}: {e}")
|
||||
continue
|
||||
if re_index:
|
||||
for item in sub_rewards:
|
||||
item["index"] = f"{item['index']}_{i}"
|
||||
rewards.extend(sub_rewards)
|
||||
|
||||
return rewards
|
||||
|
||||
|
||||
def softmax(x):
|
||||
exp = np.exp(x - np.max(x, axis=-1, keepdims=True))
|
||||
return exp / exp.sum(axis=-1, keepdims=True)
|
||||
|
||||
|
||||
def reward_reduction(ending_logits, reduction: str = "min", norm: bool = True):
|
||||
if norm:
|
||||
# ending_logits = torch.tensor(ending_logits)
|
||||
# ending_probs = torch.softmax(ending_logits, dim=-1).tolist()
|
||||
ending_probs = softmax(ending_logits).tolist()
|
||||
step_rewards = [step[1] for step in ending_probs]
|
||||
else:
|
||||
step_rewards = [step[1] for step in ending_logits]
|
||||
if reduction == "min":
|
||||
reward = min(step_rewards)
|
||||
elif reduction == "product":
|
||||
reward = 1
|
||||
for prob in step_rewards:
|
||||
reward *= prob
|
||||
elif reduction == "sum":
|
||||
reward = sum(step_rewards)
|
||||
else:
|
||||
raise ValueError(f"Invalid reduction method: {reduction}")
|
||||
return reward
|
||||
|
||||
|
||||
def weighted_majority_voting_predict(preds, weights):
|
||||
pred2weight = {}
|
||||
for pred, weight in zip(preds, weights):
|
||||
if pred not in pred2weight:
|
||||
pred2weight[pred] = 0
|
||||
pred2weight[pred] += weight
|
||||
return max(pred2weight, key=pred2weight.get)
|
||||
|
||||
|
||||
def _init(id2reward):
|
||||
global _id2reward
|
||||
_id2reward = id2reward
|
||||
|
||||
|
||||
def _worker(item, reduction, norm, sc_type: str, include_incorrect: bool, top_k: int):
|
||||
if not item["response"] or not item["pred"] or not item["res"]:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": 0,
|
||||
"pred_missing": 0,
|
||||
"seq_too_long": 0,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
unsorted_results = []
|
||||
reward_missing = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
for i, (resp, pred, r) in enumerate(zip(item["response"], item["pred"], item["res"])):
|
||||
resp_id = f"{item['id']}_{i}"
|
||||
if resp_id not in _id2reward:
|
||||
reward_missing += 1
|
||||
continue
|
||||
if not pred:
|
||||
pred_missing += 1
|
||||
continue
|
||||
|
||||
process_rewards = _id2reward[resp_id]
|
||||
if len(process_rewards["ending_logits"]) == 0:
|
||||
seq_too_long += 1
|
||||
continue
|
||||
|
||||
assert resp == process_rewards["response"], f"{resp} \n\n {process_rewards['response']} \n\n ========="
|
||||
|
||||
reward = reward_reduction(process_rewards["ending_logits"], reduction, norm)
|
||||
unsorted_results.append((resp, pred, r, reward))
|
||||
|
||||
if len(unsorted_results) == 0:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
|
||||
# Decide the self-consistency-based pseudo label
|
||||
if sc_type == "majority":
|
||||
sc_pred = majority_voting_predict(item["pred"])
|
||||
elif sc_type == "bon":
|
||||
preds = [r[1] for r in unsorted_results]
|
||||
# weights = torch.softmax(torch.tensor([r[-1] for r in unsorted_results]), dim=0).tolist()
|
||||
weights = softmax([r[-1] for r in unsorted_results]).tolist()
|
||||
assert len(preds) == len(weights)
|
||||
sc_pred = weighted_majority_voting_predict(preds, weights)
|
||||
else:
|
||||
raise ValueError(f"Invalid self-consistency type: {sc_type}")
|
||||
|
||||
if not include_incorrect:
|
||||
unsorted_results = [r for r in unsorted_results if mathscale_is_equiv(r[1], sc_pred)[0]]
|
||||
|
||||
sorted_results = sorted(unsorted_results, key=lambda x: x[-1], reverse=True)
|
||||
|
||||
pairs = []
|
||||
for i in range(top_k):
|
||||
if i >= len(sorted_results) or (len(sorted_results) - i - 1) <= i:
|
||||
break
|
||||
chosen = sorted_results[i]
|
||||
reject = sorted_results[-(i + 1)]
|
||||
pairs.append((chosen[0], reject[0]))
|
||||
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 0,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": unsorted_results,
|
||||
"top_k_sorted_results": sorted_results[:top_k],
|
||||
"sc_pred": sc_pred,
|
||||
"top_k_chosen": [r[0] for r in pairs],
|
||||
"top_k_reject": [r[1] for r in pairs],
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--response_file", type=str, required=True)
|
||||
parser.add_argument("--reward_file", type=str, required=True)
|
||||
parser.add_argument("--reduction", type=str, default="min")
|
||||
parser.add_argument("--raw_logits", action="store_true", default=False)
|
||||
parser.add_argument("--num_workers", type=int, default=4)
|
||||
parser.add_argument("--re_index", action="store_true", default=False)
|
||||
parser.add_argument("--include_incorrect", action="store_true", default=False)
|
||||
parser.add_argument("--top_k", type=int, default=3)
|
||||
parser.add_argument("--sc_type", type=str, choices=["majority", "bon"])
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
print(os.cpu_count())
|
||||
|
||||
if os.path.exists(args.response_file):
|
||||
print(args.response_file)
|
||||
responses = json.load(open(args.response_file))
|
||||
else:
|
||||
responses = []
|
||||
for file in glob(args.response_file):
|
||||
print(file)
|
||||
responses.extend(json.load(open(file)))
|
||||
|
||||
print(len(responses[0]["response"]), len(responses[0]["pred"]), len(responses[0]["res"]))
|
||||
print(responses[0]["id"])
|
||||
print(len(responses))
|
||||
|
||||
rewards = load_rewards(args.reward_file, args.re_index)
|
||||
print(rewards[0]['index'])
|
||||
print(len(rewards))
|
||||
id2reward = {item["index"]: item for item in rewards}
|
||||
|
||||
id2responses = {item["id"]: item for item in responses}
|
||||
|
||||
missing = 0
|
||||
missing_reward = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
with Pool(args.num_workers, initializer=_init, initargs=(id2reward,)) as pool:
|
||||
annotate = partial(_worker, reduction=args.reduction, norm=(not args.raw_logits), sc_type=args.sc_type, include_incorrect=args.include_incorrect,
|
||||
top_k=args.top_k)
|
||||
results = list(tqdm(pool.imap_unordered(annotate, responses), total=len(responses)))
|
||||
|
||||
outputs = []
|
||||
for item in results:
|
||||
missing += item["missing"]
|
||||
missing_reward += item["reward_missing"]
|
||||
pred_missing += item["pred_missing"]
|
||||
seq_too_long += item["seq_too_long"]
|
||||
|
||||
sorted_results = item["top_k_sorted_results"]
|
||||
|
||||
if not sorted_results:
|
||||
continue
|
||||
|
||||
orig_item = id2responses[item["id"]]
|
||||
orig_item.pop("response")
|
||||
orig_item.pop("pred")
|
||||
if "sc_res" in orig_item:
|
||||
orig_item.pop("sc_res")
|
||||
if "sc_pred" in orig_item:
|
||||
orig_item.pop("sc_pred")
|
||||
orig_item.pop("res")
|
||||
|
||||
orig_item["top_k_response"] = [r[0] for r in sorted_results]
|
||||
orig_item["top_k_pred"] = [r[1] for r in sorted_results]
|
||||
orig_item["top_k_reward"] = [r[3] for r in sorted_results]
|
||||
|
||||
orig_item["top_k_chosen"] = item["top_k_chosen"]
|
||||
orig_item["top_k_reject"] = item["top_k_reject"]
|
||||
|
||||
outputs.append(orig_item)
|
||||
|
||||
print(f"Total: {len(responses)}")
|
||||
print(f"Missing: {missing}")
|
||||
print(f"Missing reward: {missing_reward}")
|
||||
print(f"Missing pred: {pred_missing}")
|
||||
print(f"Seq too long: {seq_too_long}")
|
||||
json.dump(outputs, open(args.output_file, "w"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,259 @@
|
||||
import json
|
||||
import argparse
|
||||
import os.path
|
||||
from glob import glob
|
||||
from tqdm import tqdm
|
||||
from multiprocessing import Pool
|
||||
import numpy as np
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from post_processors.openai_api_callback import majority_voting_predict
|
||||
from data.mathscale.util import mathscale_is_equiv
|
||||
|
||||
"""
|
||||
This script is used to rerank according to different measure of rewards and save the sorted responses for rejection sampling.
|
||||
"""
|
||||
|
||||
|
||||
def load_rewards(reward_file, re_index):
|
||||
if os.path.exists(reward_file):
|
||||
print(reward_file)
|
||||
rewards = json.load(open(reward_file))
|
||||
else:
|
||||
rewards = []
|
||||
for i, file in enumerate(sorted(glob(reward_file))):
|
||||
print(file)
|
||||
try:
|
||||
sub_rewards = json.load(open(file))
|
||||
except Exception as e:
|
||||
print(f"Error in {file}: {e}")
|
||||
continue
|
||||
if re_index:
|
||||
for item in sub_rewards:
|
||||
item["index"] = f"{item['index']}_{i}"
|
||||
rewards.extend(sub_rewards)
|
||||
|
||||
return rewards
|
||||
|
||||
|
||||
def softmax(x):
|
||||
exp = np.exp(x - np.max(x, axis=-1, keepdims=True))
|
||||
return exp / exp.sum(axis=-1, keepdims=True)
|
||||
|
||||
|
||||
def reward_reduction(ending_logits, reduction: str = "min", norm: bool = True):
|
||||
if norm:
|
||||
ending_probs = softmax(ending_logits).tolist()
|
||||
step_rewards = [step[1] for step in ending_probs]
|
||||
else:
|
||||
step_rewards = [step[1] for step in ending_logits]
|
||||
if reduction == "min":
|
||||
reward = min(step_rewards)
|
||||
elif reduction == "product":
|
||||
reward = 1
|
||||
for prob in step_rewards:
|
||||
reward *= prob
|
||||
elif reduction == "sum":
|
||||
reward = sum(step_rewards)
|
||||
else:
|
||||
raise ValueError(f"Invalid reduction method: {reduction}")
|
||||
return reward
|
||||
|
||||
|
||||
def weighted_majority_voting_predict(preds, weights):
|
||||
pred2weight = {}
|
||||
for pred, weight in zip(preds, weights):
|
||||
if pred not in pred2weight:
|
||||
pred2weight[pred] = 0
|
||||
pred2weight[pred] += weight
|
||||
return max(pred2weight, key=pred2weight.get)
|
||||
|
||||
|
||||
def _init(id2reward):
|
||||
global _id2reward
|
||||
_id2reward = id2reward
|
||||
|
||||
|
||||
def _worker(item, reduction, norm, sc_type: str, include_incorrect: bool, top_k: int, margin: float = 0.2):
|
||||
if not item["response"] or not item["pred"] or not item["res"]:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": 0,
|
||||
"pred_missing": 0,
|
||||
"seq_too_long": 0,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
unsorted_results = []
|
||||
reward_missing = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
for i, (resp, pred, r) in enumerate(zip(item["response"], item["pred"], item["res"])):
|
||||
resp_id = f"{item['id']}_{i}"
|
||||
if resp_id not in _id2reward:
|
||||
reward_missing += 1
|
||||
continue
|
||||
if not pred:
|
||||
pred_missing += 1
|
||||
continue
|
||||
|
||||
process_rewards = _id2reward[resp_id]
|
||||
if len(process_rewards["ending_logits"]) == 0:
|
||||
seq_too_long += 1
|
||||
continue
|
||||
|
||||
assert resp == process_rewards["response"], f"{resp} \n\n {process_rewards['response']} \n\n ========="
|
||||
|
||||
reward = reward_reduction(process_rewards["ending_logits"], reduction, norm)
|
||||
unsorted_results.append((resp, pred, r, reward))
|
||||
|
||||
if len(unsorted_results) == 0:
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 1,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": [],
|
||||
"top_k_sorted_results": [],
|
||||
}
|
||||
|
||||
# Decide the self-consistency-based pseudo label
|
||||
if sc_type == "majority":
|
||||
sc_pred = majority_voting_predict(item["pred"])
|
||||
elif sc_type == "bon":
|
||||
preds = [r[1] for r in unsorted_results]
|
||||
# weights = torch.softmax(torch.tensor([r[-1] for r in unsorted_results]), dim=0).tolist()
|
||||
weights = softmax([r[-1] for r in unsorted_results]).tolist()
|
||||
assert len(preds) == len(weights)
|
||||
sc_pred = weighted_majority_voting_predict(preds, weights)
|
||||
else:
|
||||
raise ValueError(f"Invalid self-consistency type: {sc_type}")
|
||||
|
||||
sorted_results = sorted(unsorted_results, key=lambda x: x[-1], reverse=True)
|
||||
|
||||
chosen = []
|
||||
reject = []
|
||||
for i in range(len(sorted_results)):
|
||||
if not include_incorrect and not mathscale_is_equiv(sorted_results[i][1], sc_pred)[0]:
|
||||
continue
|
||||
if len(chosen) >= top_k:
|
||||
break
|
||||
pos = sorted_results[i]
|
||||
neg_list = []
|
||||
for j in range(i + 1, len(sorted_results)):
|
||||
neg = sorted_results[j]
|
||||
if pos[-1] - neg[-1] >= margin:
|
||||
neg_list.append(neg[0])
|
||||
|
||||
if neg_list:
|
||||
chosen.append(pos[0])
|
||||
reject.append(neg_list)
|
||||
|
||||
return {
|
||||
"id": item["id"],
|
||||
"missing": 0,
|
||||
"reward_missing": reward_missing,
|
||||
"pred_missing": pred_missing,
|
||||
"seq_too_long": seq_too_long,
|
||||
"unsorted_results": unsorted_results,
|
||||
"top_k_sorted_results": sorted_results[:top_k],
|
||||
"sc_pred": sc_pred,
|
||||
"top_k_chosen": chosen,
|
||||
"top_k_reject": reject,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--response_file", type=str, required=True)
|
||||
parser.add_argument("--reward_file", type=str, required=True)
|
||||
parser.add_argument("--reduction", type=str, default="min")
|
||||
parser.add_argument("--raw_logits", action="store_true", default=False)
|
||||
parser.add_argument("--num_workers", type=int, default=4)
|
||||
parser.add_argument("--re_index", action="store_true", default=False)
|
||||
parser.add_argument("--include_incorrect", action="store_true", default=False)
|
||||
parser.add_argument("--top_k", type=int, default=3)
|
||||
parser.add_argument("--sc_type", type=str, choices=["majority", "bon"])
|
||||
parser.add_argument("--margin", type=float, default=0.2)
|
||||
parser.add_argument("--output_file", type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
print(os.cpu_count())
|
||||
|
||||
if os.path.exists(args.response_file):
|
||||
print(args.response_file)
|
||||
responses = json.load(open(args.response_file))
|
||||
else:
|
||||
responses = []
|
||||
for file in glob(args.response_file):
|
||||
print(file)
|
||||
responses.extend(json.load(open(file)))
|
||||
|
||||
print(len(responses[0]["response"]), len(responses[0]["pred"]), len(responses[0]["res"]))
|
||||
print(responses[0]["id"])
|
||||
print(len(responses))
|
||||
|
||||
rewards = load_rewards(args.reward_file, args.re_index)
|
||||
print(rewards[0]['index'])
|
||||
print(len(rewards))
|
||||
id2reward = {item["index"]: item for item in rewards}
|
||||
|
||||
id2responses = {item["id"]: item for item in responses}
|
||||
|
||||
missing = 0
|
||||
missing_reward = 0
|
||||
pred_missing = 0
|
||||
seq_too_long = 0
|
||||
with Pool(args.num_workers, initializer=_init, initargs=(id2reward,)) as pool:
|
||||
annotate = partial(_worker, reduction=args.reduction, norm=(not args.raw_logits), sc_type=args.sc_type, include_incorrect=args.include_incorrect,
|
||||
top_k=args.top_k, margin=args.margin)
|
||||
results = list(tqdm(pool.imap_unordered(annotate, responses), total=len(responses)))
|
||||
|
||||
outputs = []
|
||||
num_pairs = 0
|
||||
for item in results:
|
||||
missing += item["missing"]
|
||||
missing_reward += item["reward_missing"]
|
||||
pred_missing += item["pred_missing"]
|
||||
seq_too_long += item["seq_too_long"]
|
||||
|
||||
sorted_results = item["top_k_sorted_results"]
|
||||
|
||||
if not sorted_results:
|
||||
continue
|
||||
|
||||
orig_item = id2responses[item["id"]]
|
||||
orig_item.pop("response")
|
||||
orig_item.pop("pred")
|
||||
if "sc_res" in orig_item:
|
||||
orig_item.pop("sc_res")
|
||||
if "sc_pred" in orig_item:
|
||||
orig_item.pop("sc_pred")
|
||||
orig_item.pop("res")
|
||||
|
||||
orig_item["top_k_response"] = [r[0] for r in sorted_results]
|
||||
orig_item["top_k_pred"] = [r[1] for r in sorted_results]
|
||||
orig_item["top_k_reward"] = [r[3] for r in sorted_results]
|
||||
|
||||
orig_item["top_k_chosen"] = item["top_k_chosen"]
|
||||
orig_item["top_k_reject"] = item["top_k_reject"]
|
||||
num_pairs += len(item["top_k_chosen"])
|
||||
|
||||
outputs.append(orig_item)
|
||||
|
||||
print(f"Total: {len(responses)}")
|
||||
print(f"Missing: {missing}")
|
||||
print(f"Missing reward: {missing_reward}")
|
||||
print(f"Missing pred: {pred_missing}")
|
||||
print(f"Seq too long: {seq_too_long}")
|
||||
print(f"Num pairs: {num_pairs}")
|
||||
json.dump(outputs, open(args.output_file, "w"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,23 @@
|
||||
import json
|
||||
import argparse
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Process some inputs.")
|
||||
parser.add_argument("--input_file", type=str, help="input file path")
|
||||
parser.add_argument("--split", type=int, help="split size")
|
||||
args = parser.parse_args()
|
||||
|
||||
data = json.load(open(args.input_file, encoding='utf-8'))
|
||||
|
||||
split_size = args.split
|
||||
bsz = (len(data) + split_size - 1) // split_size
|
||||
for i in range(split_size):
|
||||
with open(args.input_file.replace(".json", f".{i}-of-{split_size}.json"), "w", encoding="utf-8") as f:
|
||||
json.dump(data[i * bsz:(i + 1) * bsz], f)
|
||||
|
||||
print(f"Split data into {split_size} parts.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user