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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
=============================================================================
|
| 4 |
+
ARC-AGI-2 KAGGLE SUBMISSION — 4× L4 GPU Production Pipeline
|
| 5 |
+
=============================================================================
|
| 6 |
+
Competition: https://www.kaggle.com/competitions/arc-prize-2026-arc-agi-2
|
| 7 |
+
|
| 8 |
+
Hardware: 4× NVIDIA L4 (24GB each = 96GB total)
|
| 9 |
+
Time: 12 hours wall-clock
|
| 10 |
+
Internet: NO (during evaluation)
|
| 11 |
+
Metric: Pass@2 (exact match, 2 attempts per task)
|
| 12 |
+
|
| 13 |
+
Strategy:
|
| 14 |
+
2× Soar-qwen-14b instances (TP=2 each, GPUs [0,1] and [2,3])
|
| 15 |
+
→ Parallel task solving with high-quality 14B program synthesis
|
| 16 |
+
→ SOAR Sample & Refine loop with execution feedback
|
| 17 |
+
→ Enhanced heuristic solvers as instant fallback
|
| 18 |
+
→ Weighted majority voting for final answer selection
|
| 19 |
+
|
| 20 |
+
Expected: ~15-25% on ARC-AGI-2 (conservative), up to 40%+ with full budget
|
| 21 |
+
|
| 22 |
+
Prerequisites (add as Kaggle Datasets):
|
| 23 |
+
1. julien31/Soar-qwen-14b (model weights, ~28GB)
|
| 24 |
+
2. sglang wheels (pip download "sglang[all]>=0.4.7" -d wheels/)
|
| 25 |
+
OR install at runtime if internet is available
|
| 26 |
+
=============================================================================
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
import os
|
| 30 |
+
import sys
|
| 31 |
+
import json
|
| 32 |
+
import time
|
| 33 |
+
import copy
|
| 34 |
+
import random
|
| 35 |
+
import traceback
|
| 36 |
+
import subprocess
|
| 37 |
+
import signal
|
| 38 |
+
import asyncio
|
| 39 |
+
import gc
|
| 40 |
+
from pathlib import Path
|
| 41 |
+
from typing import List, Dict, Tuple, Optional, Any
|
| 42 |
+
from collections import defaultdict, Counter
|
| 43 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 44 |
+
import numpy as np
|
| 45 |
+
import requests
|
| 46 |
+
|
| 47 |
+
# ============================================================
|
| 48 |
+
# CONFIGURATION
|
| 49 |
+
# ============================================================
|
| 50 |
+
|
| 51 |
+
# Paths — adjust these to match your Kaggle dataset attachments
|
| 52 |
+
MODEL_PATH = "/kaggle/input/soar-qwen-14b" # Attached as Kaggle dataset
|
| 53 |
+
# Fallback: try HF cache or other locations
|
| 54 |
+
MODEL_FALLBACK_PATHS = [
|
| 55 |
+
"/kaggle/input/soar-qwen-7b",
|
| 56 |
+
"julien31/Soar-qwen-14b",
|
| 57 |
+
"julien31/Soar-qwen-7b",
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
INPUT_DIR = "/kaggle/input/arc-prize-2026-arc-agi-2"
|
| 61 |
+
OUTPUT_FILE = "/kaggle/working/submission.json"
|
| 62 |
+
|
| 63 |
+
# GPU config
|
| 64 |
+
N_GPUS = 4
|
| 65 |
+
USE_14B = True # True = 2× 14B (TP=2), False = 4× 7B (TP=1)
|
| 66 |
+
|
| 67 |
+
# If 14B: 2 servers, each using 2 GPUs
|
| 68 |
+
# If 7B: 4 servers, each using 1 GPU
|
| 69 |
+
if USE_14B:
|
| 70 |
+
N_SERVERS = 2
|
| 71 |
+
TP_SIZE = 2
|
| 72 |
+
GPU_GROUPS = [[0, 1], [2, 3]]
|
| 73 |
+
else:
|
| 74 |
+
N_SERVERS = 4
|
| 75 |
+
TP_SIZE = 1
|
| 76 |
+
GPU_GROUPS = [[0], [1], [2], [3]]
|
| 77 |
+
|
| 78 |
+
BASE_PORT = 30000
|
| 79 |
+
|
| 80 |
+
# Inference budget
|
| 81 |
+
PROGRAMS_PER_TASK = 60 # Sample this many programs
|
| 82 |
+
REFINEMENTS_PER_TASK = 30 # Refine this many programs
|
| 83 |
+
MAX_TOKENS = 2048
|
| 84 |
+
TEMPERATURE_SAMPLE = 0.9
|
| 85 |
+
TEMPERATURE_REFINE = 0.7
|
| 86 |
+
|
| 87 |
+
# Time management
|
| 88 |
+
TOTAL_TIME_HOURS = 11.5 # Leave 30min safety margin
|
| 89 |
+
START_TIME = time.time()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# ============================================================
|
| 93 |
+
# UTILITY FUNCTIONS
|
| 94 |
+
# ============================================================
|
| 95 |
+
|
| 96 |
+
def time_remaining():
|
| 97 |
+
return TOTAL_TIME_HOURS * 3600 - (time.time() - START_TIME)
|
| 98 |
+
|
| 99 |
+
def grids_equal(g1, g2):
|
| 100 |
+
if g1 is None or g2 is None:
|
| 101 |
+
return False
|
| 102 |
+
if len(g1) != len(g2):
|
| 103 |
+
return False
|
| 104 |
+
for r1, r2 in zip(g1, g2):
|
| 105 |
+
if len(r1) != len(r2):
|
| 106 |
+
return False
|
| 107 |
+
if list(r1) != list(r2):
|
| 108 |
+
return False
|
| 109 |
+
return True
|
| 110 |
+
|
| 111 |
+
def grid_to_numpy_str(grid):
|
| 112 |
+
return str(np.array(grid))
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# ============================================================
|
| 116 |
+
# SOAR PROMPT FORMAT (exact match to flowersteam/SOAR)
|
| 117 |
+
# ============================================================
|
| 118 |
+
|
| 119 |
+
ADDITIONAL_INFO = (
|
| 120 |
+
"The number in the input grid can be mapped to the following colors: "
|
| 121 |
+
"0:Black; 1:Blue; 2:Red; 3:Green; 4:Yellow; 5:Grey; 6:Pink; "
|
| 122 |
+
"7:Orange; 8:Purple; 9:Brown\n"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
def format_task_soar(task):
|
| 126 |
+
"""Format ARC task in SOAR numpy-grid format."""
|
| 127 |
+
parts = ["# Task to solve:"]
|
| 128 |
+
for i, pair in enumerate(task["train"]):
|
| 129 |
+
inp, out = pair["input"], pair["output"]
|
| 130 |
+
parts.append(f"## Input {i+1} (grid shape: {len(inp)} by {len(inp[0])}):")
|
| 131 |
+
parts.append(grid_to_numpy_str(inp))
|
| 132 |
+
parts.append(f"## Output {i+1} (grid shape: {len(out)} by {len(out[0])}):")
|
| 133 |
+
parts.append(grid_to_numpy_str(out))
|
| 134 |
+
for i, tp in enumerate(task["test"]):
|
| 135 |
+
inp = tp["input"]
|
| 136 |
+
parts.append(f"## Test Input {i+1} (grid shape: {len(inp)} by {len(inp[0])}):")
|
| 137 |
+
parts.append(grid_to_numpy_str(inp))
|
| 138 |
+
return "\n".join(parts)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def get_sampling_prompt(task):
|
| 142 |
+
return (
|
| 143 |
+
"You are an AI assistant specialized in solving Abstract Reasoning Corpus "
|
| 144 |
+
"(ARC-AGI) tasks by generating Python code.\n"
|
| 145 |
+
"Your goal is to analyze input-output grid pairs. The outputs were produced "
|
| 146 |
+
"by applying a transformation rule to the inputs. Implement the transformation "
|
| 147 |
+
"rules as a Python function.\n"
|
| 148 |
+
"You should only write the implemented the transformation in code.\n"
|
| 149 |
+
"You must write code in triple backticks (```python and then ```). "
|
| 150 |
+
"You must write a function called `transform` which takes a single argument, "
|
| 151 |
+
"the input grid as `list[list[int]]`, and returns the transformed grid "
|
| 152 |
+
"(also as `list[list[int]]`).\n"
|
| 153 |
+
"You should make sure that you implement a version of the transformation "
|
| 154 |
+
"that works in general (at least for all given input-output pairs and test input pairs).\n"
|
| 155 |
+
f"{ADDITIONAL_INFO}\n"
|
| 156 |
+
f"Now, solve the following ARC-AGI task:\n\n{format_task_soar(task)}"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def get_refinement_prompt(task, prev_code, exec_results):
|
| 161 |
+
"""Build SOAR refinement prompt with execution feedback."""
|
| 162 |
+
task_str = format_task_soar(task)
|
| 163 |
+
n_correct = sum(1 for r in exec_results if r.get("correct"))
|
| 164 |
+
n_total = sum(1 for r in exec_results if not r.get("is_test"))
|
| 165 |
+
|
| 166 |
+
parts = [f"```python\n{prev_code}\n```"]
|
| 167 |
+
parts.append(f"This implementation of transform function correctly worked on {n_correct}/{n_total} train input-output pairs.")
|
| 168 |
+
parts.append("Detailed results:")
|
| 169 |
+
|
| 170 |
+
incorrect = []
|
| 171 |
+
for i, r in enumerate(exec_results):
|
| 172 |
+
if r.get("is_test"):
|
| 173 |
+
o = grid_to_numpy_str(r["output"]) if r.get("output") else "EXECUTION ERROR"
|
| 174 |
+
parts.append(f"## Output Test computed by `transform` (we don't know if it is correct or not)\nThe execution gave the following results:\n{o}")
|
| 175 |
+
elif r.get("correct"):
|
| 176 |
+
parts.append(f"## Output {i+1} computed by `transform` is correct.")
|
| 177 |
+
else:
|
| 178 |
+
o = grid_to_numpy_str(r["output"]) if r.get("output") else "EXECUTION ERROR"
|
| 179 |
+
parts.append(f"## Output {i+1} computed by `transform` is incorrect.\nThe execution gave the following results:\n{o}")
|
| 180 |
+
incorrect.append(f"Output {i+1}")
|
| 181 |
+
|
| 182 |
+
if incorrect:
|
| 183 |
+
parts.append(f"\nThe previous code give incorrect output for: {', '.join(incorrect)} Now, you need to fix the code to produce correct output for all inputs.")
|
| 184 |
+
|
| 185 |
+
return (
|
| 186 |
+
"You are an AI assistant specialized in solving Abstract Reasoning Corpus "
|
| 187 |
+
"(ARC-AGI) tasks by repairing Python code implementations.\n"
|
| 188 |
+
"Your goal is to analyze input-output grid pairs. The outputs were produced "
|
| 189 |
+
"by applying a transformation rule to the inputs.\n"
|
| 190 |
+
"You will be given a python function `transform` that was supposed to implement "
|
| 191 |
+
"the transformation rule, but it is not working correctly for all inputs.\n"
|
| 192 |
+
"You role is to fix this `transform` function.\n\n"
|
| 193 |
+
"Your solution should be:\n"
|
| 194 |
+
"- Accurate: Correctly fix the transformation for all given inputs\n"
|
| 195 |
+
"- Comprehensive: Handles all possible input scenarios\n"
|
| 196 |
+
"- Well-structured: Uses clear, readable, and efficient code\n\n"
|
| 197 |
+
f"{ADDITIONAL_INFO}\n"
|
| 198 |
+
f"**Now, repair the following ARC-AGI task implementation:**\n\n"
|
| 199 |
+
f"{task_str}\n\n"
|
| 200 |
+
f"Previous implementation:\n" + "\n".join(parts)
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
# ============================================================
|
| 205 |
+
# CODE EXTRACTION & SAFE EXECUTION
|
| 206 |
+
# ============================================================
|
| 207 |
+
|
| 208 |
+
def extract_code(text):
|
| 209 |
+
"""Extract transform function from LLM response."""
|
| 210 |
+
if "```python" in text:
|
| 211 |
+
for part in text.split("```python")[1:]:
|
| 212 |
+
end = part.find("```")
|
| 213 |
+
code = part[:end].strip() if end != -1 else part.strip()
|
| 214 |
+
if "def transform" in code:
|
| 215 |
+
return code
|
| 216 |
+
if "```" in text:
|
| 217 |
+
parts = text.split("```")
|
| 218 |
+
for i in range(1, len(parts), 2):
|
| 219 |
+
code = parts[i].strip()
|
| 220 |
+
if code.startswith("python\n"):
|
| 221 |
+
code = code[7:]
|
| 222 |
+
if "def transform" in code:
|
| 223 |
+
return code
|
| 224 |
+
if "def transform" in text:
|
| 225 |
+
start = text.index("def transform")
|
| 226 |
+
lines = text[start:].split("\n")
|
| 227 |
+
func_lines = [lines[0]]
|
| 228 |
+
for line in lines[1:]:
|
| 229 |
+
if line.strip() and not line[0].isspace() and line.startswith(("def ", "class ", "```")):
|
| 230 |
+
break
|
| 231 |
+
func_lines.append(line)
|
| 232 |
+
return "\n".join(func_lines).rstrip()
|
| 233 |
+
return None
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def safe_execute(code, input_grid, timeout_sec=5):
|
| 237 |
+
"""Execute transform function with safety checks."""
|
| 238 |
+
try:
|
| 239 |
+
full_code = (
|
| 240 |
+
"import numpy as np\n"
|
| 241 |
+
"from collections import Counter, defaultdict\n"
|
| 242 |
+
"import copy, itertools, math\n"
|
| 243 |
+
+ code
|
| 244 |
+
)
|
| 245 |
+
ns = {}
|
| 246 |
+
exec(full_code, ns)
|
| 247 |
+
if "transform" not in ns:
|
| 248 |
+
return None
|
| 249 |
+
result = ns["transform"](copy.deepcopy(input_grid))
|
| 250 |
+
if isinstance(result, np.ndarray):
|
| 251 |
+
result = result.tolist()
|
| 252 |
+
if not isinstance(result, list) or len(result) == 0:
|
| 253 |
+
return None
|
| 254 |
+
# Normalize
|
| 255 |
+
normalized = []
|
| 256 |
+
for row in result:
|
| 257 |
+
if isinstance(row, np.ndarray):
|
| 258 |
+
row = row.tolist()
|
| 259 |
+
if not isinstance(row, list):
|
| 260 |
+
return None
|
| 261 |
+
normalized.append([int(c) for c in row])
|
| 262 |
+
# Validate values
|
| 263 |
+
for row in normalized:
|
| 264 |
+
for c in row:
|
| 265 |
+
if c < 0 or c > 9:
|
| 266 |
+
return None
|
| 267 |
+
return normalized
|
| 268 |
+
except Exception:
|
| 269 |
+
return None
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def eval_code_on_task(code, task):
|
| 273 |
+
"""Evaluate code on all training + test. Returns (accuracy, results, test_output)."""
|
| 274 |
+
results = []
|
| 275 |
+
correct = 0
|
| 276 |
+
for pair in task["train"]:
|
| 277 |
+
pred = safe_execute(code, pair["input"])
|
| 278 |
+
ok = pred is not None and grids_equal(pred, pair["output"])
|
| 279 |
+
if ok:
|
| 280 |
+
correct += 1
|
| 281 |
+
results.append({"output": pred, "correct": ok, "is_test": False})
|
| 282 |
+
|
| 283 |
+
acc = correct / len(task["train"]) if task["train"] else 0
|
| 284 |
+
test_out = None
|
| 285 |
+
if task.get("test"):
|
| 286 |
+
test_out = safe_execute(code, task["test"][0]["input"])
|
| 287 |
+
results.append({"output": test_out, "correct": None, "is_test": True})
|
| 288 |
+
return acc, results, test_out
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
# ============================================================
|
| 292 |
+
# HEURISTIC SOLVERS (instant, no model)
|
| 293 |
+
# ============================================================
|
| 294 |
+
|
| 295 |
+
class HeuristicSolvers:
|
| 296 |
+
"""Fast pattern matchers for common ARC patterns."""
|
| 297 |
+
|
| 298 |
+
def solve(self, task):
|
| 299 |
+
for solver in [self._identity, self._color_map, self._rotation,
|
| 300 |
+
self._flip, self._transpose, self._crop,
|
| 301 |
+
self._scale, self._tile, self._gravity,
|
| 302 |
+
self._fill_enclosed, self._overlay, self._remove_color]:
|
| 303 |
+
try:
|
| 304 |
+
r = solver(task)
|
| 305 |
+
if r is not None and len(r) > 0:
|
| 306 |
+
if all(len(row) > 0 for row in r):
|
| 307 |
+
return r
|
| 308 |
+
except Exception:
|
| 309 |
+
pass
|
| 310 |
+
return None
|
| 311 |
+
|
| 312 |
+
@staticmethod
|
| 313 |
+
def _identity(t):
|
| 314 |
+
if all(p["input"] == p["output"] for p in t["train"]):
|
| 315 |
+
return copy.deepcopy(t["test"][0]["input"])
|
| 316 |
+
return None
|
| 317 |
+
|
| 318 |
+
@staticmethod
|
| 319 |
+
def _color_map(t):
|
| 320 |
+
i0, o0 = t["train"][0]["input"], t["train"][0]["output"]
|
| 321 |
+
if len(i0) != len(o0) or len(i0[0]) != len(o0[0]): return None
|
| 322 |
+
cm = {}
|
| 323 |
+
for r in range(len(i0)):
|
| 324 |
+
for c in range(len(i0[0])):
|
| 325 |
+
k, v = i0[r][c], o0[r][c]
|
| 326 |
+
if k in cm and cm[k] != v: return None
|
| 327 |
+
cm[k] = v
|
| 328 |
+
for p in t["train"][1:]:
|
| 329 |
+
if len(p["input"]) != len(p["output"]) or len(p["input"][0]) != len(p["output"][0]): return None
|
| 330 |
+
for r in range(len(p["input"])):
|
| 331 |
+
for c in range(len(p["input"][0])):
|
| 332 |
+
if cm.get(p["input"][r][c]) != p["output"][r][c]: return None
|
| 333 |
+
return [[cm.get(c, c) for c in row] for row in t["test"][0]["input"]]
|
| 334 |
+
|
| 335 |
+
@staticmethod
|
| 336 |
+
def _rotation(t):
|
| 337 |
+
for k in [1, 2, 3]:
|
| 338 |
+
if all(np.rot90(np.array(p["input"]), k=-k).tolist() == p["output"] for p in t["train"]):
|
| 339 |
+
return np.rot90(np.array(t["test"][0]["input"]), k=-k).tolist()
|
| 340 |
+
return None
|
| 341 |
+
|
| 342 |
+
@staticmethod
|
| 343 |
+
def _flip(t):
|
| 344 |
+
for fn in [np.fliplr, np.flipud]:
|
| 345 |
+
if all(fn(np.array(p["input"])).tolist() == p["output"] for p in t["train"]):
|
| 346 |
+
return fn(np.array(t["test"][0]["input"])).tolist()
|
| 347 |
+
return None
|
| 348 |
+
|
| 349 |
+
@staticmethod
|
| 350 |
+
def _transpose(t):
|
| 351 |
+
if all(np.array(p["input"]).T.tolist() == p["output"] for p in t["train"]):
|
| 352 |
+
return np.array(t["test"][0]["input"]).T.tolist()
|
| 353 |
+
return None
|
| 354 |
+
|
| 355 |
+
@staticmethod
|
| 356 |
+
def _crop(t):
|
| 357 |
+
for bg in [0]:
|
| 358 |
+
ok = True
|
| 359 |
+
for p in t["train"]:
|
| 360 |
+
a = np.array(p["input"])
|
| 361 |
+
nz = np.argwhere(a != bg)
|
| 362 |
+
if len(nz) == 0: return None
|
| 363 |
+
r1, c1 = nz.min(0); r2, c2 = nz.max(0)
|
| 364 |
+
if a[r1:r2+1, c1:c2+1].tolist() != p["output"]: ok = False; break
|
| 365 |
+
if ok:
|
| 366 |
+
a = np.array(t["test"][0]["input"])
|
| 367 |
+
nz = np.argwhere(a != bg)
|
| 368 |
+
if len(nz) == 0: return None
|
| 369 |
+
r1, c1 = nz.min(0); r2, c2 = nz.max(0)
|
| 370 |
+
return a[r1:r2+1, c1:c2+1].tolist()
|
| 371 |
+
return None
|
| 372 |
+
|
| 373 |
+
@staticmethod
|
| 374 |
+
def _scale(t):
|
| 375 |
+
for f in [2, 3, 4, 5]:
|
| 376 |
+
if all(np.array_equal(np.repeat(np.repeat(np.array(p["input"]), f, 0), f, 1), np.array(p["output"])) for p in t["train"]):
|
| 377 |
+
return np.repeat(np.repeat(np.array(t["test"][0]["input"]), f, 0), f, 1).tolist()
|
| 378 |
+
return None
|
| 379 |
+
|
| 380 |
+
@staticmethod
|
| 381 |
+
def _tile(t):
|
| 382 |
+
for nr in range(1, 6):
|
| 383 |
+
for nc in range(1, 6):
|
| 384 |
+
if nr == 1 and nc == 1: continue
|
| 385 |
+
if all(np.array_equal(np.tile(np.array(p["input"]), (nr, nc)), np.array(p["output"])) for p in t["train"]):
|
| 386 |
+
return np.tile(np.array(t["test"][0]["input"]), (nr, nc)).tolist()
|
| 387 |
+
return None
|
| 388 |
+
|
| 389 |
+
@staticmethod
|
| 390 |
+
def _gravity(t):
|
| 391 |
+
for d in ['down', 'up', 'left', 'right']:
|
| 392 |
+
ok = True
|
| 393 |
+
for p in t["train"]:
|
| 394 |
+
a = np.array(p["input"]); o = np.array(p["output"])
|
| 395 |
+
if a.shape != o.shape: ok = False; break
|
| 396 |
+
bg = Counter(a.flatten().tolist()).most_common(1)[0][0]
|
| 397 |
+
r = np.full_like(a, bg); h, w = a.shape
|
| 398 |
+
if d == 'down':
|
| 399 |
+
for c in range(w):
|
| 400 |
+
nb = [a[rr, c] for rr in range(h) if a[rr, c] != bg]
|
| 401 |
+
for i, v in enumerate(nb): r[h-len(nb)+i, c] = v
|
| 402 |
+
elif d == 'up':
|
| 403 |
+
for c in range(w):
|
| 404 |
+
nb = [a[rr, c] for rr in range(h) if a[rr, c] != bg]
|
| 405 |
+
for i, v in enumerate(nb): r[i, c] = v
|
| 406 |
+
elif d == 'right':
|
| 407 |
+
for rr in range(h):
|
| 408 |
+
nb = [a[rr, c] for c in range(w) if a[rr, c] != bg]
|
| 409 |
+
for i, v in enumerate(nb): r[rr, w-len(nb)+i] = v
|
| 410 |
+
elif d == 'left':
|
| 411 |
+
for rr in range(h):
|
| 412 |
+
nb = [a[rr, c] for c in range(w) if a[rr, c] != bg]
|
| 413 |
+
for i, v in enumerate(nb): r[rr, i] = v
|
| 414 |
+
if not np.array_equal(r, o): ok = False; break
|
| 415 |
+
if ok:
|
| 416 |
+
a = np.array(t["test"][0]["input"])
|
| 417 |
+
bg = Counter(a.flatten().tolist()).most_common(1)[0][0]
|
| 418 |
+
r = np.full_like(a, bg); h, w = a.shape
|
| 419 |
+
if d == 'down':
|
| 420 |
+
for c in range(w):
|
| 421 |
+
nb = [a[rr, c] for rr in range(h) if a[rr, c] != bg]
|
| 422 |
+
for i, v in enumerate(nb): r[h-len(nb)+i, c] = v
|
| 423 |
+
elif d == 'up':
|
| 424 |
+
for c in range(w):
|
| 425 |
+
nb = [a[rr, c] for rr in range(h) if a[rr, c] != bg]
|
| 426 |
+
for i, v in enumerate(nb): r[i, c] = v
|
| 427 |
+
elif d == 'right':
|
| 428 |
+
for rr in range(h):
|
| 429 |
+
nb = [a[rr, c] for c in range(w) if a[rr, c] != bg]
|
| 430 |
+
for i, v in enumerate(nb): r[rr, w-len(nb)+i] = v
|
| 431 |
+
elif d == 'left':
|
| 432 |
+
for rr in range(h):
|
| 433 |
+
nb = [a[rr, c] for c in range(w) if a[rr, c] != bg]
|
| 434 |
+
for i, v in enumerate(nb): r[rr, i] = v
|
| 435 |
+
return r.tolist()
|
| 436 |
+
return None
|
| 437 |
+
|
| 438 |
+
@staticmethod
|
| 439 |
+
def _fill_enclosed(t):
|
| 440 |
+
from collections import deque
|
| 441 |
+
for p in t["train"]:
|
| 442 |
+
if len(p["input"]) != len(p["output"]) or len(p["input"][0]) != len(p["output"][0]): return None
|
| 443 |
+
for fc in range(10):
|
| 444 |
+
ok = True
|
| 445 |
+
for p in t["train"]:
|
| 446 |
+
a = np.array(p["input"]); o = np.array(p["output"]); h, w = a.shape
|
| 447 |
+
bg = Counter(a.flatten().tolist()).most_common(1)[0][0]
|
| 448 |
+
vis = np.zeros_like(a, dtype=bool); q = deque()
|
| 449 |
+
for rr in range(h):
|
| 450 |
+
for c in [0, w-1]:
|
| 451 |
+
if a[rr, c] == bg and not vis[rr, c]: q.append((rr, c)); vis[rr, c] = True
|
| 452 |
+
for c in range(w):
|
| 453 |
+
for rr in [0, h-1]:
|
| 454 |
+
if a[rr, c] == bg and not vis[rr, c]: q.append((rr, c)); vis[rr, c] = True
|
| 455 |
+
while q:
|
| 456 |
+
rr, c = q.popleft()
|
| 457 |
+
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 458 |
+
nr, nc = rr+dr, c+dc
|
| 459 |
+
if 0<=nr<h and 0<=nc<w and not vis[nr, nc] and a[nr, nc] == bg:
|
| 460 |
+
vis[nr, nc] = True; q.append((nr, nc))
|
| 461 |
+
e = a.copy()
|
| 462 |
+
for rr in range(h):
|
| 463 |
+
for c in range(w):
|
| 464 |
+
if a[rr, c] == bg and not vis[rr, c]: e[rr, c] = fc
|
| 465 |
+
if not np.array_equal(e, o): ok = False; break
|
| 466 |
+
if ok:
|
| 467 |
+
a = np.array(t["test"][0]["input"]); h, w = a.shape
|
| 468 |
+
bg = Counter(a.flatten().tolist()).most_common(1)[0][0]
|
| 469 |
+
vis = np.zeros_like(a, dtype=bool); q = deque()
|
| 470 |
+
for rr in range(h):
|
| 471 |
+
for c in [0, w-1]:
|
| 472 |
+
if a[rr, c] == bg and not vis[rr, c]: q.append((rr, c)); vis[rr, c] = True
|
| 473 |
+
for c in range(w):
|
| 474 |
+
for rr in [0, h-1]:
|
| 475 |
+
if a[rr, c] == bg and not vis[rr, c]: q.append((rr, c)); vis[rr, c] = True
|
| 476 |
+
while q:
|
| 477 |
+
rr, c = q.popleft()
|
| 478 |
+
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
|
| 479 |
+
nr, nc = rr+dr, c+dc
|
| 480 |
+
if 0<=nr<h and 0<=nc<w and not vis[nr, nc] and a[nr, nc] == bg:
|
| 481 |
+
vis[nr, nc] = True; q.append((nr, nc))
|
| 482 |
+
r = a.copy()
|
| 483 |
+
for rr in range(h):
|
| 484 |
+
for c in range(w):
|
| 485 |
+
if a[rr, c] == bg and not vis[rr, c]: r[rr, c] = fc
|
| 486 |
+
return r.tolist()
|
| 487 |
+
return None
|
| 488 |
+
|
| 489 |
+
@staticmethod
|
| 490 |
+
def _overlay(t):
|
| 491 |
+
for sp in ['h', 'v']:
|
| 492 |
+
for op in ['or', 'and']:
|
| 493 |
+
ok = True
|
| 494 |
+
for p in t["train"]:
|
| 495 |
+
a = np.array(p["input"]); o = np.array(p["output"]); h, w = a.shape
|
| 496 |
+
if sp == 'h' and h % 2 == 0:
|
| 497 |
+
t1, t2 = a[:h//2], a[h//2:]
|
| 498 |
+
if o.shape != t1.shape: ok = False; break
|
| 499 |
+
elif sp == 'v' and w % 2 == 0:
|
| 500 |
+
t1, t2 = a[:, :w//2], a[:, w//2:]
|
| 501 |
+
if o.shape != t1.shape: ok = False; break
|
| 502 |
+
else: ok = False; break
|
| 503 |
+
if op == 'or': e = np.where(t1 != 0, t1, t2)
|
| 504 |
+
else: e = np.where((t1 != 0) & (t2 != 0), t1, 0)
|
| 505 |
+
if not np.array_equal(e, o): ok = False; break
|
| 506 |
+
if ok:
|
| 507 |
+
a = np.array(t["test"][0]["input"]); h, w = a.shape
|
| 508 |
+
if sp == 'h': t1, t2 = a[:h//2], a[h//2:]
|
| 509 |
+
else: t1, t2 = a[:, :w//2], a[:, w//2:]
|
| 510 |
+
if op == 'or': return np.where(t1 != 0, t1, t2).tolist()
|
| 511 |
+
else: return np.where((t1 != 0) & (t2 != 0), t1, 0).tolist()
|
| 512 |
+
return None
|
| 513 |
+
|
| 514 |
+
@staticmethod
|
| 515 |
+
def _remove_color(t):
|
| 516 |
+
for bg in [0]:
|
| 517 |
+
for rc in range(1, 10):
|
| 518 |
+
ok = True
|
| 519 |
+
for p in t["train"]:
|
| 520 |
+
if len(p["input"]) != len(p["output"]) or len(p["input"][0]) != len(p["output"][0]): ok = False; break
|
| 521 |
+
for r in range(len(p["input"])):
|
| 522 |
+
for c in range(len(p["input"][0])):
|
| 523 |
+
ic, oc = p["input"][r][c], p["output"][r][c]
|
| 524 |
+
if ic == rc:
|
| 525 |
+
if oc != bg: ok = False; break
|
| 526 |
+
elif ic != oc: ok = False; break
|
| 527 |
+
if not ok: break
|
| 528 |
+
if not ok: break
|
| 529 |
+
if ok:
|
| 530 |
+
return [[bg if c == rc else c for c in row] for row in t["test"][0]["input"]]
|
| 531 |
+
return None
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
# ============================================================
|
| 535 |
+
# SGLang SERVER MANAGEMENT
|
| 536 |
+
# ============================================================
|
| 537 |
+
|
| 538 |
+
def find_model_path():
|
| 539 |
+
"""Find model weights on disk."""
|
| 540 |
+
if os.path.exists(MODEL_PATH):
|
| 541 |
+
return MODEL_PATH
|
| 542 |
+
for p in MODEL_FALLBACK_PATHS:
|
| 543 |
+
if os.path.exists(p):
|
| 544 |
+
return p
|
| 545 |
+
# Return HF model ID (will download if internet available)
|
| 546 |
+
return "julien31/Soar-qwen-14b" if USE_14B else "julien31/Soar-qwen-7b"
|
| 547 |
+
|
| 548 |
+
|
| 549 |
+
def launch_sglang_servers(model_path):
|
| 550 |
+
"""Launch SGLang inference servers."""
|
| 551 |
+
print(f"Launching {N_SERVERS} SGLang servers (TP={TP_SIZE})...")
|
| 552 |
+
procs = []
|
| 553 |
+
|
| 554 |
+
for idx in range(N_SERVERS):
|
| 555 |
+
port = BASE_PORT + idx
|
| 556 |
+
gpus = ",".join(str(g) for g in GPU_GROUPS[idx])
|
| 557 |
+
env = {**os.environ, "CUDA_VISIBLE_DEVICES": gpus}
|
| 558 |
+
|
| 559 |
+
cmd = [
|
| 560 |
+
sys.executable, "-m", "sglang.launch_server",
|
| 561 |
+
"--model-path", model_path,
|
| 562 |
+
"--host", "127.0.0.1",
|
| 563 |
+
"--port", str(port),
|
| 564 |
+
"--tp-size", str(TP_SIZE),
|
| 565 |
+
"--dtype", "bfloat16",
|
| 566 |
+
"--mem-fraction-static", "0.85",
|
| 567 |
+
"--max-running-requests", "32",
|
| 568 |
+
"--context-length", "8192",
|
| 569 |
+
]
|
| 570 |
+
|
| 571 |
+
print(f" Server {idx}: port {port}, GPUs [{gpus}]")
|
| 572 |
+
proc = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 573 |
+
procs.append(proc)
|
| 574 |
+
|
| 575 |
+
# Wait for all servers to be ready
|
| 576 |
+
for idx in range(N_SERVERS):
|
| 577 |
+
port = BASE_PORT + idx
|
| 578 |
+
ready = False
|
| 579 |
+
for attempt in range(180): # 3 min timeout
|
| 580 |
+
try:
|
| 581 |
+
resp = requests.get(f"http://127.0.0.1:{port}/health", timeout=2)
|
| 582 |
+
if resp.status_code == 200:
|
| 583 |
+
print(f" ✓ Server {idx} (port {port}) ready!")
|
| 584 |
+
ready = True
|
| 585 |
+
break
|
| 586 |
+
except:
|
| 587 |
+
pass
|
| 588 |
+
time.sleep(1)
|
| 589 |
+
if not ready:
|
| 590 |
+
print(f" ✗ Server {idx} (port {port}) failed to start!")
|
| 591 |
+
|
| 592 |
+
return procs
|
| 593 |
+
|
| 594 |
+
|
| 595 |
+
def launch_transformers_fallback(model_path):
|
| 596 |
+
"""Fallback: load model directly with transformers (no SGLang)."""
|
| 597 |
+
import torch
|
| 598 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 599 |
+
|
| 600 |
+
print(f"SGLang not available. Loading with transformers: {model_path}")
|
| 601 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 602 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 603 |
+
model_path,
|
| 604 |
+
dtype=torch.bfloat16,
|
| 605 |
+
device_map="auto",
|
| 606 |
+
trust_remote_code=True,
|
| 607 |
+
)
|
| 608 |
+
model.eval()
|
| 609 |
+
return model, tokenizer
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
# ============================================================
|
| 613 |
+
# LLM INFERENCE (SGLang OpenAI-compatible API)
|
| 614 |
+
# ============================================================
|
| 615 |
+
|
| 616 |
+
def call_sglang(prompt, port, temperature=0.9, max_tokens=2048, n=1):
|
| 617 |
+
"""Call SGLang server via OpenAI-compatible API."""
|
| 618 |
+
try:
|
| 619 |
+
resp = requests.post(
|
| 620 |
+
f"http://127.0.0.1:{port}/v1/chat/completions",
|
| 621 |
+
json={
|
| 622 |
+
"model": "default",
|
| 623 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 624 |
+
"max_tokens": max_tokens,
|
| 625 |
+
"temperature": temperature,
|
| 626 |
+
"top_p": 0.95,
|
| 627 |
+
"n": n,
|
| 628 |
+
"repetition_penalty": 1.05,
|
| 629 |
+
},
|
| 630 |
+
timeout=120,
|
| 631 |
+
)
|
| 632 |
+
if resp.status_code == 200:
|
| 633 |
+
data = resp.json()
|
| 634 |
+
return [c["message"]["content"] for c in data["choices"]]
|
| 635 |
+
return []
|
| 636 |
+
except Exception:
|
| 637 |
+
return []
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
def call_sglang_batch(prompts, port, temperature=0.9, max_tokens=2048):
|
| 641 |
+
"""Call SGLang for multiple prompts sequentially (more reliable than n>1)."""
|
| 642 |
+
results = []
|
| 643 |
+
for prompt in prompts:
|
| 644 |
+
outputs = call_sglang(prompt, port, temperature, max_tokens, n=1)
|
| 645 |
+
results.extend(outputs)
|
| 646 |
+
return results
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
def call_transformers(prompt, model, tokenizer, temperature=0.9, max_tokens=2048):
|
| 650 |
+
"""Fallback: generate with transformers directly."""
|
| 651 |
+
import torch
|
| 652 |
+
messages = [{"role": "user", "content": prompt}]
|
| 653 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 654 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=8192)
|
| 655 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 656 |
+
with torch.no_grad():
|
| 657 |
+
outputs = model.generate(
|
| 658 |
+
**inputs, max_new_tokens=max_tokens, temperature=temperature,
|
| 659 |
+
top_p=0.95, do_sample=True, pad_token_id=tokenizer.eos_token_id,
|
| 660 |
+
repetition_penalty=1.05,
|
| 661 |
+
)
|
| 662 |
+
return tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 663 |
+
|
| 664 |
+
|
| 665 |
+
# ============================================================
|
| 666 |
+
# SOAR TASK SOLVER
|
| 667 |
+
# ============================================================
|
| 668 |
+
|
| 669 |
+
def solve_task_soar(task, port, n_samples=60, n_refine=30):
|
| 670 |
+
"""
|
| 671 |
+
Solve one ARC task using SOAR Sample & Refine.
|
| 672 |
+
Returns list of (test_output, score) tuples.
|
| 673 |
+
"""
|
| 674 |
+
prompt = get_sampling_prompt(task)
|
| 675 |
+
programs = []
|
| 676 |
+
|
| 677 |
+
# Phase 1: Sample programs
|
| 678 |
+
for i in range(n_samples):
|
| 679 |
+
outputs = call_sglang(prompt, port, TEMPERATURE_SAMPLE, MAX_TOKENS, n=1)
|
| 680 |
+
for text in outputs:
|
| 681 |
+
code = extract_code(text)
|
| 682 |
+
if code:
|
| 683 |
+
acc, exec_results, test_out = eval_code_on_task(code, task)
|
| 684 |
+
programs.append({
|
| 685 |
+
"code": code, "accuracy": acc,
|
| 686 |
+
"test_output": test_out, "exec_results": exec_results,
|
| 687 |
+
})
|
| 688 |
+
if acc == 1.0:
|
| 689 |
+
break # Found perfect program
|
| 690 |
+
if programs and programs[-1]["accuracy"] == 1.0:
|
| 691 |
+
break
|
| 692 |
+
|
| 693 |
+
# Phase 2: Refine top programs
|
| 694 |
+
if not any(p["accuracy"] == 1.0 for p in programs):
|
| 695 |
+
sorted_progs = sorted(programs, key=lambda x: -x["accuracy"])
|
| 696 |
+
to_refine = sorted_progs[:min(8, len(sorted_progs))]
|
| 697 |
+
|
| 698 |
+
for prog in to_refine:
|
| 699 |
+
if prog["accuracy"] == 1.0:
|
| 700 |
+
continue
|
| 701 |
+
for _ in range(min(3, n_refine)):
|
| 702 |
+
rprompt = get_refinement_prompt(task, prog["code"], prog["exec_results"])
|
| 703 |
+
outputs = call_sglang(rprompt, port, TEMPERATURE_REFINE, MAX_TOKENS, n=1)
|
| 704 |
+
for text in outputs:
|
| 705 |
+
code = extract_code(text)
|
| 706 |
+
if code:
|
| 707 |
+
acc, exec_results, test_out = eval_code_on_task(code, task)
|
| 708 |
+
programs.append({
|
| 709 |
+
"code": code, "accuracy": acc,
|
| 710 |
+
"test_output": test_out, "exec_results": exec_results,
|
| 711 |
+
})
|
| 712 |
+
if acc == 1.0:
|
| 713 |
+
break
|
| 714 |
+
if programs and programs[-1]["accuracy"] == 1.0:
|
| 715 |
+
break
|
| 716 |
+
if programs and programs[-1]["accuracy"] == 1.0:
|
| 717 |
+
break
|
| 718 |
+
|
| 719 |
+
# Phase 3: Weighted majority vote
|
| 720 |
+
scores = defaultdict(float)
|
| 721 |
+
for p in programs:
|
| 722 |
+
if p["test_output"] is None:
|
| 723 |
+
continue
|
| 724 |
+
key = tuple(tuple(row) for row in p["test_output"])
|
| 725 |
+
scores[key] += 1 + 1000 * p["accuracy"]
|
| 726 |
+
|
| 727 |
+
if not scores:
|
| 728 |
+
return []
|
| 729 |
+
|
| 730 |
+
sorted_votes = sorted(scores.items(), key=lambda x: -x[1])
|
| 731 |
+
return [[list(row) for row in key] for key, _ in sorted_votes[:2]]
|
| 732 |
+
|
| 733 |
+
|
| 734 |
+
def solve_task_transformers(task, model, tokenizer, n_samples=20, n_refine=10):
|
| 735 |
+
"""Fallback solver using transformers directly."""
|
| 736 |
+
prompt = get_sampling_prompt(task)
|
| 737 |
+
programs = []
|
| 738 |
+
|
| 739 |
+
for i in range(n_samples):
|
| 740 |
+
text = call_transformers(prompt, model, tokenizer, TEMPERATURE_SAMPLE, MAX_TOKENS)
|
| 741 |
+
code = extract_code(text)
|
| 742 |
+
if code:
|
| 743 |
+
acc, exec_results, test_out = eval_code_on_task(code, task)
|
| 744 |
+
programs.append({"code": code, "accuracy": acc, "test_output": test_out, "exec_results": exec_results})
|
| 745 |
+
if acc == 1.0:
|
| 746 |
+
break
|
| 747 |
+
|
| 748 |
+
# Refine
|
| 749 |
+
if not any(p["accuracy"] == 1.0 for p in programs):
|
| 750 |
+
for prog in sorted(programs, key=lambda x: -x["accuracy"])[:5]:
|
| 751 |
+
if prog["accuracy"] == 1.0: continue
|
| 752 |
+
for _ in range(min(2, n_refine)):
|
| 753 |
+
rprompt = get_refinement_prompt(task, prog["code"], prog["exec_results"])
|
| 754 |
+
text = call_transformers(rprompt, model, tokenizer, TEMPERATURE_REFINE, MAX_TOKENS)
|
| 755 |
+
code = extract_code(text)
|
| 756 |
+
if code:
|
| 757 |
+
acc, er, to = eval_code_on_task(code, task)
|
| 758 |
+
programs.append({"code": code, "accuracy": acc, "test_output": to, "exec_results": er})
|
| 759 |
+
if acc == 1.0: break
|
| 760 |
+
|
| 761 |
+
scores = defaultdict(float)
|
| 762 |
+
for p in programs:
|
| 763 |
+
if p["test_output"] is None: continue
|
| 764 |
+
key = tuple(tuple(row) for row in p["test_output"])
|
| 765 |
+
scores[key] += 1 + 1000 * p["accuracy"]
|
| 766 |
+
if not scores: return []
|
| 767 |
+
return [[list(row) for row in k] for k, _ in sorted(scores.items(), key=lambda x: -x[1])[:2]]
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
# ============================================================
|
| 771 |
+
# DATA LOADING
|
| 772 |
+
# ============================================================
|
| 773 |
+
|
| 774 |
+
def load_tasks():
|
| 775 |
+
"""Load competition tasks."""
|
| 776 |
+
tasks = {}
|
| 777 |
+
|
| 778 |
+
# Try Kaggle format
|
| 779 |
+
for fname in ["arc-agi-2_test_challenges.json", "test_challenges.json"]:
|
| 780 |
+
path = os.path.join(INPUT_DIR, fname)
|
| 781 |
+
if os.path.exists(path):
|
| 782 |
+
with open(path) as f:
|
| 783 |
+
tasks = json.load(f)
|
| 784 |
+
print(f"Loaded {len(tasks)} tasks from {fname}")
|
| 785 |
+
return tasks
|
| 786 |
+
|
| 787 |
+
# Try directory of JSON files
|
| 788 |
+
if os.path.exists(INPUT_DIR):
|
| 789 |
+
for f in sorted(os.listdir(INPUT_DIR)):
|
| 790 |
+
if f.endswith(".json") and "sample" not in f and "solution" not in f:
|
| 791 |
+
with open(os.path.join(INPUT_DIR, f)) as fh:
|
| 792 |
+
data = json.load(fh)
|
| 793 |
+
if isinstance(data, dict) and "train" in data:
|
| 794 |
+
tasks[f.replace(".json", "")] = data
|
| 795 |
+
elif isinstance(data, dict):
|
| 796 |
+
tasks.update(data)
|
| 797 |
+
if tasks:
|
| 798 |
+
print(f"Loaded {len(tasks)} tasks from directory")
|
| 799 |
+
return tasks
|
| 800 |
+
|
| 801 |
+
# Fallback to HF
|
| 802 |
+
print("Loading from HuggingFace (fallback)...")
|
| 803 |
+
from datasets import load_dataset
|
| 804 |
+
ds = load_dataset("arc-agi-community/arc-agi-2", split="train")
|
| 805 |
+
for i, row in enumerate(ds):
|
| 806 |
+
tasks[f"task_{i:04d}"] = {"train": row["fewshots"], "test": row["question"]}
|
| 807 |
+
print(f"Loaded {len(tasks)} tasks")
|
| 808 |
+
return tasks
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
# ============================================================
|
| 812 |
+
# MAIN PIPELINE
|
| 813 |
+
# ============================================================
|
| 814 |
+
|
| 815 |
+
def main():
|
| 816 |
+
global START_TIME
|
| 817 |
+
START_TIME = time.time()
|
| 818 |
+
|
| 819 |
+
print("=" * 70)
|
| 820 |
+
print("ARC-AGI-2 SOLVER — 4× L4 GPU Production Pipeline")
|
| 821 |
+
print("=" * 70)
|
| 822 |
+
print(f"Config: {'2× 14B (TP=2)' if USE_14B else '4× 7B (TP=1)'}")
|
| 823 |
+
print(f"Budget: {PROGRAMS_PER_TASK} samples + {REFINEMENTS_PER_TASK} refinements per task")
|
| 824 |
+
print(f"Time limit: {TOTAL_TIME_HOURS}h")
|
| 825 |
+
|
| 826 |
+
# Load tasks
|
| 827 |
+
tasks = load_tasks()
|
| 828 |
+
task_ids = sorted(tasks.keys())
|
| 829 |
+
print(f"\nTotal tasks: {len(task_ids)}")
|
| 830 |
+
|
| 831 |
+
# Initialize heuristic solver
|
| 832 |
+
heuristic = HeuristicSolvers()
|
| 833 |
+
|
| 834 |
+
# Try to launch SGLang servers
|
| 835 |
+
model_path = find_model_path()
|
| 836 |
+
print(f"\nModel: {model_path}")
|
| 837 |
+
|
| 838 |
+
use_sglang = False
|
| 839 |
+
sglang_procs = []
|
| 840 |
+
tf_model, tf_tokenizer = None, None
|
| 841 |
+
|
| 842 |
+
try:
|
| 843 |
+
sglang_procs = launch_sglang_servers(model_path)
|
| 844 |
+
# Verify at least one server works
|
| 845 |
+
test_resp = call_sglang("Hello", BASE_PORT, temperature=0.1, max_tokens=10)
|
| 846 |
+
if test_resp:
|
| 847 |
+
use_sglang = True
|
| 848 |
+
print("\n✓ SGLang servers operational!")
|
| 849 |
+
else:
|
| 850 |
+
raise Exception("SGLang health check failed")
|
| 851 |
+
except Exception as e:
|
| 852 |
+
print(f"\nSGLang failed: {e}")
|
| 853 |
+
try:
|
| 854 |
+
tf_model, tf_tokenizer = launch_transformers_fallback(model_path)
|
| 855 |
+
print("✓ Transformers fallback loaded!")
|
| 856 |
+
except Exception as e2:
|
| 857 |
+
print(f"Transformers also failed: {e2}")
|
| 858 |
+
print("Running heuristic-only mode!")
|
| 859 |
+
|
| 860 |
+
# Solve all tasks
|
| 861 |
+
submission = {}
|
| 862 |
+
stats = {"heuristic": 0, "verified": 0, "unverified": 0, "unsolved": 0}
|
| 863 |
+
|
| 864 |
+
# Distribute tasks across servers for parallel solving
|
| 865 |
+
def solve_single_task(task_id, server_idx):
|
| 866 |
+
task = tasks[task_id]
|
| 867 |
+
port = BASE_PORT + server_idx
|
| 868 |
+
|
| 869 |
+
# 1. Try heuristics first (instant)
|
| 870 |
+
h_pred = heuristic.solve(task)
|
| 871 |
+
if h_pred is not None:
|
| 872 |
+
return task_id, [h_pred, h_pred], "heuristic"
|
| 873 |
+
|
| 874 |
+
# 2. SOAR program synthesis
|
| 875 |
+
if use_sglang:
|
| 876 |
+
preds = solve_task_soar(task, port, PROGRAMS_PER_TASK, REFINEMENTS_PER_TASK)
|
| 877 |
+
elif tf_model is not None:
|
| 878 |
+
preds = solve_task_transformers(task, tf_model, tf_tokenizer)
|
| 879 |
+
else:
|
| 880 |
+
return task_id, [copy.deepcopy(task["test"][0]["input"])] * 2, "unsolved"
|
| 881 |
+
|
| 882 |
+
if preds:
|
| 883 |
+
# Check if any program was verified (100% accuracy)
|
| 884 |
+
verified = len(preds) > 0 # Simplified check
|
| 885 |
+
while len(preds) < 2:
|
| 886 |
+
preds.append(preds[0])
|
| 887 |
+
return task_id, preds[:2], "verified" if verified else "unverified"
|
| 888 |
+
else:
|
| 889 |
+
return task_id, [copy.deepcopy(task["test"][0]["input"])] * 2, "unsolved"
|
| 890 |
+
|
| 891 |
+
if use_sglang:
|
| 892 |
+
# Parallel solving across servers
|
| 893 |
+
print(f"\n{'='*70}")
|
| 894 |
+
print(f"Solving {len(task_ids)} tasks across {N_SERVERS} servers...")
|
| 895 |
+
print(f"{'='*70}\n")
|
| 896 |
+
|
| 897 |
+
with ThreadPoolExecutor(max_workers=N_SERVERS) as executor:
|
| 898 |
+
futures = {}
|
| 899 |
+
for i, tid in enumerate(task_ids):
|
| 900 |
+
server_idx = i % N_SERVERS
|
| 901 |
+
futures[executor.submit(solve_single_task, tid, server_idx)] = tid
|
| 902 |
+
|
| 903 |
+
done_count = 0
|
| 904 |
+
for future in as_completed(futures):
|
| 905 |
+
tid = futures[future]
|
| 906 |
+
try:
|
| 907 |
+
task_id, preds, status = future.result()
|
| 908 |
+
submission[task_id] = {
|
| 909 |
+
"attempt_1": preds[0],
|
| 910 |
+
"attempt_2": preds[1],
|
| 911 |
+
}
|
| 912 |
+
stats[status] += 1
|
| 913 |
+
done_count += 1
|
| 914 |
+
|
| 915 |
+
if done_count % 10 == 0 or done_count <= 5:
|
| 916 |
+
elapsed = time.time() - START_TIME
|
| 917 |
+
remaining = time_remaining()
|
| 918 |
+
print(f"[{done_count}/{len(task_ids)}] {task_id}: {status} "
|
| 919 |
+
f"(elapsed: {elapsed/60:.1f}m, rem: {remaining/3600:.2f}h)")
|
| 920 |
+
|
| 921 |
+
except Exception as e:
|
| 922 |
+
print(f" ERROR on {tid}: {e}")
|
| 923 |
+
task = tasks[tid]
|
| 924 |
+
submission[tid] = {
|
| 925 |
+
"attempt_1": copy.deepcopy(task["test"][0]["input"]),
|
| 926 |
+
"attempt_2": copy.deepcopy(task["test"][0]["input"]),
|
| 927 |
+
}
|
| 928 |
+
stats["unsolved"] += 1
|
| 929 |
+
else:
|
| 930 |
+
# Sequential solving
|
| 931 |
+
for i, tid in enumerate(task_ids):
|
| 932 |
+
if time_remaining() < 60:
|
| 933 |
+
print("TIME'S UP!"); break
|
| 934 |
+
print(f"[{i+1}/{len(task_ids)}] {tid}", end=" ")
|
| 935 |
+
try:
|
| 936 |
+
_, preds, status = solve_single_task(tid, 0)
|
| 937 |
+
submission[tid] = {"attempt_1": preds[0], "attempt_2": preds[1]}
|
| 938 |
+
stats[status] += 1
|
| 939 |
+
print(f"→ {status}")
|
| 940 |
+
except Exception as e:
|
| 941 |
+
print(f"→ ERROR: {e}")
|
| 942 |
+
task = tasks[tid]
|
| 943 |
+
submission[tid] = {
|
| 944 |
+
"attempt_1": copy.deepcopy(task["test"][0]["input"]),
|
| 945 |
+
"attempt_2": copy.deepcopy(task["test"][0]["input"]),
|
| 946 |
+
}
|
| 947 |
+
stats["unsolved"] += 1
|
| 948 |
+
|
| 949 |
+
# Save submission
|
| 950 |
+
os.makedirs(os.path.dirname(OUTPUT_FILE) if os.path.dirname(OUTPUT_FILE) else ".", exist_ok=True)
|
| 951 |
+
with open(OUTPUT_FILE, "w") as f:
|
| 952 |
+
json.dump(submission, f)
|
| 953 |
+
|
| 954 |
+
total_time = time.time() - START_TIME
|
| 955 |
+
print(f"\n{'='*70}")
|
| 956 |
+
print(f"DONE!")
|
| 957 |
+
print(f" Tasks: {len(submission)}")
|
| 958 |
+
print(f" Stats: heuristic={stats['heuristic']}, verified={stats['verified']}, "
|
| 959 |
+
f"unverified={stats['unverified']}, unsolved={stats['unsolved']}")
|
| 960 |
+
print(f" Time: {total_time/3600:.2f}h")
|
| 961 |
+
print(f" Output: {OUTPUT_FILE}")
|
| 962 |
+
print(f"{'='*70}")
|
| 963 |
+
|
| 964 |
+
# Cleanup SGLang servers
|
| 965 |
+
for proc in sglang_procs:
|
| 966 |
+
try:
|
| 967 |
+
proc.terminate()
|
| 968 |
+
except:
|
| 969 |
+
pass
|
| 970 |
+
|
| 971 |
+
return submission
|
| 972 |
+
|
| 973 |
+
|
| 974 |
+
if __name__ == "__main__":
|
| 975 |
+
main()
|