File size: 10,434 Bytes
24c2665 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
#!/usr/bin/env python3
"""
๋ชจ๋ธ๋ณ ํน์ ๋ฌธ์ ์๋ต ๋น๊ต ๋๊ตฌ (์ ์ฒด ์๋ต ์ถ๋ ฅ ๋ฒ์ )
์ฌ์ฉ๋ฒ: python compare_models.py --benchmark aime24 --problem_idx 0 --models qwen25_7b_base azr_coder_7b azr_base_7b
์ฃผ์ ๋ณ๊ฒฝ์ฌํญ:
- ๋ฌธ์ ํ
์คํธ์ ์ถ๋ก ๊ณผ์ ์ ์ ์ฒด ์ถ๋ ฅ (๊ธธ์ด ์ ํ ์ ๊ฑฐ)
- ๋ ์์ธํ ๋ถ์์ ์ํ ์์ ํ ์๋ต ํ์ธ ๊ฐ๋ฅ
"""
import json
import argparse
import os
import glob
import sys
from typing import List, Dict, Any
import re
def extract_answer_from_response(response: str) -> str:
"""์๋ต์์ ์ต์ข
๋ต์ ์ถ์ถ"""
# \boxed{...} ํจํด ์ฐพ๊ธฐ
boxed_pattern = r'\\boxed\{([^}]+)\}'
matches = re.findall(boxed_pattern, response)
if matches:
return matches[-1] # ๋ง์ง๋ง boxed ๋ต์ ์ฌ์ฉ
# ์ซ์ ํจํด ์ฐพ๊ธฐ (fallback)
number_pattern = r'\b\d+(?:\.\d+)?\b'
numbers = re.findall(number_pattern, response)
if numbers:
return numbers[-1]
return "๋ต์์ ์ฐพ์ ์ ์์"
def load_problem_result(model_dir: str, benchmark: str, problem_idx: int) -> Dict[str, Any]:
"""ํน์ ๋ชจ๋ธ์ ํน์ ๋ฌธ์ ๊ฒฐ๊ณผ ๋ก๋"""
# ๊ฒฐ๊ณผ ํ์ผ ์ฐพ๊ธฐ
result_files = glob.glob(f"{model_dir}/{benchmark}/*.jsonl")
if not result_files:
return {"error": f"๋ฒค์น๋งํฌ '{benchmark}' ๊ฒฐ๊ณผ ํ์ผ์ ์ฐพ์ ์ ์์"}
result_file = result_files[0] # ์ฒซ ๋ฒ์งธ ํ์ผ ์ฌ์ฉ
try:
with open(result_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
if problem_idx >= len(lines):
return {"error": f"๋ฌธ์ ์ธ๋ฑ์ค {problem_idx}๊ฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋จ (์ด {len(lines)}๊ฐ ๋ฌธ์ )"}
problem_data = json.loads(lines[problem_idx])
return problem_data
except Exception as e:
return {"error": f"ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}"}
def format_response_text(text: str, max_length: int = None) -> str:
"""์๋ต ํ
์คํธ ํฌ๋งทํ
"""
if max_length is not None and len(text) > max_length:
return text[:max_length] + "... (truncated)"
return text
def compare_models_on_problem(
benchmark: str,
problem_idx: int,
model_dirs: List[str],
results_base_dir: str = "/home/ubuntu/RLVR/Absolute-Zero-Reasoner/evaluation/math_eval/EVAL/results"
):
"""ํน์ ๋ฌธ์ ์ ๋ํ ๋ชจ๋ธ๋ณ ๋น๊ต"""
print("=" * 100)
print(f"๐ ๋ฒค์น๋งํฌ: {benchmark.upper()}")
print(f"๐ข ๋ฌธ์ ๋ฒํธ: {problem_idx}")
print("=" * 100)
# ๊ฐ ๋ชจ๋ธ์ ๊ฒฐ๊ณผ ๋ก๋
model_results = {}
problem_text = None
correct_answer = None
for model_name in model_dirs:
model_path = os.path.join(results_base_dir, model_name)
result = load_problem_result(model_path, benchmark, problem_idx)
model_results[model_name] = result
# ๋ฌธ์ ํ
์คํธ์ ์ ๋ต์ ์ฒซ ๋ฒ์งธ ์ฑ๊ณตํ ๋ชจ๋ธ์์ ๊ฐ์ ธ์ค๊ธฐ
if problem_text is None and "error" not in result:
problem_text = result.get("question", "๋ฌธ์ ๋ฅผ ์ฐพ์ ์ ์์")
correct_answer = result.get("gt", result.get("answer", "์ ๋ต์ ์ฐพ์ ์ ์์"))
# ๋ฌธ์ ์ ๋ณด ์ถ๋ ฅ
print(f"๐ ๋ฌธ์ :")
print("-" * 80)
print(format_response_text(problem_text))
print()
print(f"โ
์ ๋ต: {correct_answer}")
print()
# ๋ชจ๋ธ ์ถ๋ ฅ ์์ ์ ์ (์ ํธํ๋ ์์)
preferred_order = [
"qwen25_7b_base",
"azr_base_7b",
"qwen25_7b_coder",
"azr_coder_7b"
]
# ์
๋ ฅ๋ ๋ชจ๋ธ๋ค์ ์ ํธ ์์๋๋ก ์ ๋ ฌ
ordered_models = []
# ๋จผ์ ์ ํธ ์์์ ์๋ ๋ชจ๋ธ๋ค์ ์์๋๋ก ์ถ๊ฐ
for preferred_model in preferred_order:
if preferred_model in model_dirs:
ordered_models.append(preferred_model)
# ์ ํธ ์์์ ์๋ ๋ชจ๋ธ๋ค์ ๋๋จธ์ง์ ์ถ๊ฐ
for model in model_dirs:
if model not in ordered_models:
ordered_models.append(model)
# ๋ชจ๋ธ๋ณ ๊ฒฐ๊ณผ ๋น๊ต
print("๐ค ๋ชจ๋ธ๋ณ ์๋ต ๋น๊ต:")
print("=" * 100)
for model_name in ordered_models:
result = model_results[model_name]
print(f"\n๐ธ {model_name.upper()}")
print("-" * 60)
if "error" in result:
print(f"โ ์ค๋ฅ: {result['error']}")
continue
# ๋ชจ๋ธ ์์ธก๊ณผ ์ ๋ต ์ฌ๋ถ
model_pred = result.get("pred", [""])[0] if result.get("pred") else ""
is_correct = result.get("score", [False])[0] if result.get("score") else False
# ์ ์ฒด ์๋ต์์ ๋ต์ ์ถ์ถ
full_response = result.get("code", [""])[0] if result.get("code") else ""
extracted_answer = extract_answer_from_response(full_response)
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
status = "โ
์ ๋ต" if is_correct else "โ ์ค๋ต"
print(f"๊ฒฐ๊ณผ: {status}")
print(f"๋ชจ๋ธ ๋ต์: {model_pred}")
print(f"์ถ์ถ๋ ๋ต์: {extracted_answer}")
print()
# ์ถ๋ก ๊ณผ์ (์ ์ฒด)
if full_response:
print("๐ง ์ถ๋ก ๊ณผ์ (์ ์ฒด):")
print(format_response_text(full_response))
else:
print("์ถ๋ก ๊ณผ์ ์ ์ฐพ์ ์ ์์")
print("-" * 60)
# ์์ฝ ํต๊ณ
print(f"\n๐ ์์ฝ:")
print("-" * 40)
correct_models = []
wrong_models = []
error_models = []
for model_name in ordered_models:
result = model_results[model_name]
if "error" in result:
error_models.append(model_name)
elif result.get("score", [False])[0]:
correct_models.append(model_name)
else:
wrong_models.append(model_name)
if correct_models:
print(f"โ
์ ๋ต ๋ชจ๋ธ: {', '.join(correct_models)}")
if wrong_models:
print(f"โ ์ค๋ต ๋ชจ๋ธ: {', '.join(wrong_models)}")
if error_models:
print(f"โ ๏ธ ์ค๋ฅ ๋ชจ๋ธ: {', '.join(error_models)}")
print(f"์ ๋ต๋ฅ : {len(correct_models)}/{len(model_dirs) - len(error_models)} ({len(correct_models)/(len(model_dirs) - len(error_models))*100:.1f}%)")
def list_available_benchmarks(results_base_dir: str, model_name: str) -> List[str]:
"""์ฌ์ฉ ๊ฐ๋ฅํ ๋ฒค์น๋งํฌ ๋ชฉ๋ก ๋ฐํ"""
model_path = os.path.join(results_base_dir, model_name)
if not os.path.exists(model_path):
return []
benchmarks = []
for item in os.listdir(model_path):
item_path = os.path.join(model_path, item)
if os.path.isdir(item_path):
benchmarks.append(item)
return sorted(benchmarks)
def get_problem_count(results_base_dir: str, model_name: str, benchmark: str) -> int:
"""ํน์ ๋ฒค์น๋งํฌ์ ๋ฌธ์ ์ ๋ฐํ"""
result_files = glob.glob(f"{results_base_dir}/{model_name}/{benchmark}/*.jsonl")
if not result_files:
return 0
try:
with open(result_files[0], 'r', encoding='utf-8') as f:
return len(f.readlines())
except:
return 0
def main():
parser = argparse.ArgumentParser(description="๋ชจ๋ธ๋ณ ํน์ ๋ฌธ์ ์๋ต ๋น๊ต")
parser.add_argument("--benchmark", "-b", type=str, required=True,
help="๋ฒค์น๋งํฌ ์ด๋ฆ (์: aime24, aime25, amc23, math500)")
parser.add_argument("--problem_idx", "-p", type=int, required=True,
help="๋ฌธ์ ์ธ๋ฑ์ค (0๋ถํฐ ์์)")
parser.add_argument("--models", "-m", nargs="+", required=True,
help="๋น๊ตํ ๋ชจ๋ธ ๋๋ ํ ๋ฆฌ ์ด๋ฆ๋ค")
parser.add_argument("--results_dir", "-r", type=str,
default="/home/ubuntu/RLVR/Absolute-Zero-Reasoner/evaluation/math_eval/EVAL/results",
help="๊ฒฐ๊ณผ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก")
parser.add_argument("--list", "-l", action="store_true",
help="์ฌ์ฉ ๊ฐ๋ฅํ ๋ฒค์น๋งํฌ์ ๋ฌธ์ ์ ๋์ด")
parser.add_argument("--full", "-f", action="store_true",
help="์ ์ฒด ์๋ต ์ถ๋ ฅ (๊ธฐ๋ณธ๊ฐ: True, ์ด์ ํญ์ ์ ์ฒด ์ถ๋ ฅ)")
parser.add_argument("--output", "-o", type=str,
help="๊ฒฐ๊ณผ๋ฅผ ํ์ผ๋ก ์ ์ฅ (์: --output result.txt)")
args = parser.parse_args()
if args.list:
print("๐ ์ฌ์ฉ ๊ฐ๋ฅํ ์ ๋ณด:")
print("-" * 50)
# ์ฒซ ๋ฒ์งธ ๋ชจ๋ธ ๊ธฐ์ค์ผ๋ก ๋ฒค์น๋งํฌ ๋์ด
if args.models:
benchmarks = list_available_benchmarks(args.results_dir, args.models[0])
for benchmark in benchmarks:
problem_count = get_problem_count(args.results_dir, args.models[0], benchmark)
print(f"โข {benchmark}: {problem_count}๊ฐ ๋ฌธ์ (์ธ๋ฑ์ค 0-{problem_count-1})")
return
# ์ถ๋ ฅ ํ์ผ ์ค์
output_file = None
if args.output:
try:
output_file = open(args.output, 'w', encoding='utf-8')
# ํ์ค ์ถ๋ ฅ์ ํ์ผ๋ก ๋ฆฌ๋๋ ์
original_stdout = sys.stdout
sys.stdout = output_file
print(f"๊ฒฐ๊ณผ๋ฅผ {args.output} ํ์ผ์ ์ ์ฅํฉ๋๋ค...")
# ํ์ผ์ ์ ์ฅ๋ ๋ด์ฉ์์๋ ์ด ๋ฉ์์ง๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํด ๋ค์ ์๋ stdout์ผ๋ก ์ถ๋ ฅ
sys.stdout = original_stdout
print(f"๐ ๊ฒฐ๊ณผ๋ฅผ {args.output} ํ์ผ์ ์ ์ฅํฉ๋๋ค...")
sys.stdout = output_file
except Exception as e:
print(f"โ ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
return
try:
# ๋ชจ๋ธ ๋น๊ต ์คํ
compare_models_on_problem(
benchmark=args.benchmark,
problem_idx=args.problem_idx,
model_dirs=args.models,
results_base_dir=args.results_dir
)
finally:
# ํ์ผ ์ถ๋ ฅ์ธ ๊ฒฝ์ฐ ์๋ stdout ๋ณต์ ๋ฐ ํ์ผ ๋ซ๊ธฐ
if output_file:
sys.stdout = original_stdout
output_file.close()
print(f"โ
๊ฒฐ๊ณผ๊ฐ {args.output} ํ์ผ์ ์ ์ฅ๋์์ต๋๋ค.")
print(f"๐ ํ์ผ ๋ด์ฉ ํ์ธ: cat {args.output}")
print(f"๐ ํ์ผ ์ด๊ธฐ: less {args.output}")
print(f"๐ ํน์ ๋ชจ๋ธ ๊ฒ์: grep -A 10 'QWEN25_7B_BASE' {args.output}")
if __name__ == "__main__":
main() |