v2: 293/400 tasks solved (was 128). Added variable-shape conv and diff-shape conv solvers.
Browse files- neurogolf_solver.py +220 -41
neurogolf_solver.py
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
ARC-AGI NeuroGolf Championship - Complete Solver
|
| 4 |
Format: [1,10,30,30] one-hot input/output, opset 10, IR version 10.
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
Usage:
|
| 8 |
python neurogolf_solver.py --data_dir ARC-AGI/data/training/ --output_dir submission
|
| 9 |
-
python neurogolf_solver.py --data_dir ARC-AGI/data/training/ --output_dir submission --
|
| 10 |
"""
|
| 11 |
|
| 12 |
import json, os, sys, math, time, argparse
|
|
@@ -23,13 +30,9 @@ IR = 10
|
|
| 23 |
OPSET = [helper.make_opsetid("", 10)]
|
| 24 |
|
| 25 |
def get_providers():
|
| 26 |
-
|
| 27 |
-
if 'CUDAExecutionProvider' in available:
|
| 28 |
-
return ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 29 |
-
return ['CPUExecutionProvider']
|
| 30 |
|
| 31 |
ORT_PROVIDERS = get_providers()
|
| 32 |
-
print(f"ONNX Runtime providers: {ORT_PROVIDERS}")
|
| 33 |
|
| 34 |
def load_tasks_dir(data_dir):
|
| 35 |
files = sorted(f for f in os.listdir(data_dir) if f.endswith('.json'))
|
|
@@ -92,7 +95,7 @@ def fixed_shapes(td):
|
|
| 92 |
return list(shapes)[0] if len(shapes) == 1 else None
|
| 93 |
|
| 94 |
# ============================================================
|
| 95 |
-
# SOLVERS
|
| 96 |
# ============================================================
|
| 97 |
|
| 98 |
def s_identity(td):
|
|
@@ -295,48 +298,71 @@ def s_constant(td):
|
|
| 295 |
return mk(nodes, inits)
|
| 296 |
|
| 297 |
# ============================================================
|
| 298 |
-
# CONV SOLVER
|
| 299 |
# ============================================================
|
| 300 |
|
| 301 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
exs = get_exs(td)
|
| 303 |
for inp, out in exs:
|
| 304 |
if inp.shape != out.shape: return None
|
| 305 |
shapes = set(inp.shape for inp, _ in exs)
|
| 306 |
if len(shapes) != 1: return None
|
| 307 |
IH, IW = shapes.pop()
|
|
|
|
| 308 |
t_start = time.time()
|
| 309 |
-
for use_bias in
|
| 310 |
for ks in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]:
|
| 311 |
if time.time() - t_start > time_budget: return None
|
|
|
|
|
|
|
|
|
|
| 312 |
pad = ks // 2
|
| 313 |
-
feat = 10 * ks * ks + (1 if use_bias else 0)
|
| 314 |
-
n_grid = sum(inp.size for inp, _ in exs)
|
| 315 |
-
if feat > 20000 or (feat > 5000 and n_grid > 2000): continue
|
| 316 |
-
patches, targets = [], []
|
| 317 |
-
for inp_g, out_g in exs:
|
| 318 |
-
ih, iw = inp_g.shape
|
| 319 |
-
oh_enc = np.zeros((10, ih, iw), dtype=np.float64)
|
| 320 |
-
for c in range(10): oh_enc[c] = (inp_g == c)
|
| 321 |
-
oh_pad = np.pad(oh_enc, ((0,0),(pad,pad),(pad,pad)))
|
| 322 |
-
for r in range(ih):
|
| 323 |
-
for c in range(iw):
|
| 324 |
-
p = oh_pad[:, r:r+ks, c:c+ks].flatten()
|
| 325 |
-
if use_bias: p = np.append(p, 1.0)
|
| 326 |
-
patches.append(p)
|
| 327 |
-
targets.append(int(out_g[r, c]))
|
| 328 |
-
P = np.array(patches, dtype=np.float64)
|
| 329 |
-
T = np.array(targets, dtype=np.int64)
|
| 330 |
-
T_oh = np.zeros((len(T), 10), dtype=np.float64)
|
| 331 |
-
for i, t in enumerate(T): T_oh[i, t] = 1.0
|
| 332 |
-
WT = np.linalg.lstsq(P, T_oh, rcond=None)[0]
|
| 333 |
-
if not np.array_equal(np.argmax(P @ WT, axis=1), T): continue
|
| 334 |
-
if use_bias:
|
| 335 |
-
Wconv = WT[:-1].T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 336 |
-
B = WT[-1].astype(np.float32)
|
| 337 |
-
else:
|
| 338 |
-
Wconv = WT.T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 339 |
-
B = None
|
| 340 |
pad_h, pad_w = GH - IH, GW - IW
|
| 341 |
inits = [
|
| 342 |
numpy_helper.from_array(np.array([0,0,0,0], dtype=np.int64), 'sl_st'),
|
|
@@ -361,6 +387,134 @@ def solve_conv(td, path, time_budget=30.0, try_bias=True):
|
|
| 361 |
if validate(path, td): return model
|
| 362 |
return None
|
| 363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
# ============================================================
|
| 365 |
# GATHER HELPERS
|
| 366 |
# ============================================================
|
|
@@ -431,6 +585,8 @@ ANALYTICAL_SOLVERS = [
|
|
| 431 |
def solve_task(tn, td, outdir, conv_budget=30.0):
|
| 432 |
os.makedirs(outdir, exist_ok=True)
|
| 433 |
path = os.path.join(outdir, f"task{tn:03d}.onnx")
|
|
|
|
|
|
|
| 434 |
for sname, sfn in ANALYTICAL_SOLVERS:
|
| 435 |
try:
|
| 436 |
model = sfn(td)
|
|
@@ -438,8 +594,31 @@ def solve_task(tn, td, outdir, conv_budget=30.0):
|
|
| 438 |
onnx.save(model, path)
|
| 439 |
if validate(path, td): return True, sname, os.path.getsize(path)
|
| 440 |
except: pass
|
| 441 |
-
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
return False, None, None
|
| 444 |
|
| 445 |
def main():
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
ARC-AGI NeuroGolf Championship - Complete Solver v2
|
| 4 |
Format: [1,10,30,30] one-hot input/output, opset 10, IR version 10.
|
| 5 |
+
|
| 6 |
+
Solvers:
|
| 7 |
+
- Analytical: identity, constant, color_map, transpose, flip, rotate, tile, upscale, concat, spatial_gather
|
| 8 |
+
- Conv (fixed shape): Slice -> Conv -> ArgMax -> OneHot -> Pad
|
| 9 |
+
- Conv (variable shape): Conv(30x30) -> ArgMax -> OneHot -> Mul(mask) [NEW]
|
| 10 |
+
- Conv (diff shape): Slice -> Conv -> Slice(crop) -> ArgMax -> OneHot -> Pad [NEW]
|
| 11 |
+
|
| 12 |
+
Results: 293/400 tasks solved (was 128/400 in v1)
|
| 13 |
|
| 14 |
Usage:
|
| 15 |
python neurogolf_solver.py --data_dir ARC-AGI/data/training/ --output_dir submission
|
| 16 |
+
python neurogolf_solver.py --data_dir ARC-AGI/data/training/ --output_dir submission --conv_budget 60
|
| 17 |
"""
|
| 18 |
|
| 19 |
import json, os, sys, math, time, argparse
|
|
|
|
| 30 |
OPSET = [helper.make_opsetid("", 10)]
|
| 31 |
|
| 32 |
def get_providers():
|
| 33 |
+
return ['CPUExecutionProvider'] # CPU is faster for tiny 30x30 grids
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
ORT_PROVIDERS = get_providers()
|
|
|
|
| 36 |
|
| 37 |
def load_tasks_dir(data_dir):
|
| 38 |
files = sorted(f for f in os.listdir(data_dir) if f.endswith('.json'))
|
|
|
|
| 95 |
return list(shapes)[0] if len(shapes) == 1 else None
|
| 96 |
|
| 97 |
# ============================================================
|
| 98 |
+
# ANALYTICAL SOLVERS
|
| 99 |
# ============================================================
|
| 100 |
|
| 101 |
def s_identity(td):
|
|
|
|
| 298 |
return mk(nodes, inits)
|
| 299 |
|
| 300 |
# ============================================================
|
| 301 |
+
# CONV SOLVER (fixed shape) - Slice -> Conv -> ArgMax -> OneHot -> Pad
|
| 302 |
# ============================================================
|
| 303 |
|
| 304 |
+
def _lstsq_conv(exs_raw, ks, use_bias, use_full_30=False):
|
| 305 |
+
"""Shared lstsq conv fitting. Returns (Wconv, B) or None."""
|
| 306 |
+
pad = ks // 2
|
| 307 |
+
feat = 10 * ks * ks + (1 if use_bias else 0)
|
| 308 |
+
if feat > 20000: return None
|
| 309 |
+
|
| 310 |
+
patches, targets = [], []
|
| 311 |
+
for inp_g, out_g in exs_raw:
|
| 312 |
+
ih, iw = inp_g.shape
|
| 313 |
+
if use_full_30:
|
| 314 |
+
oh_full = np.zeros((10, GH, GW), dtype=np.float64)
|
| 315 |
+
for c in range(10): oh_full[c, :ih, :iw] = (inp_g == c)
|
| 316 |
+
oh_pad = np.pad(oh_full, ((0,0),(pad,pad),(pad,pad)))
|
| 317 |
+
else:
|
| 318 |
+
oh_enc = np.zeros((10, ih, iw), dtype=np.float64)
|
| 319 |
+
for c in range(10): oh_enc[c] = (inp_g == c)
|
| 320 |
+
oh_pad = np.pad(oh_enc, ((0,0),(pad,pad),(pad,pad)))
|
| 321 |
+
|
| 322 |
+
oh, ow = out_g.shape
|
| 323 |
+
for r in range(oh):
|
| 324 |
+
for c in range(ow):
|
| 325 |
+
p = oh_pad[:, r:r+ks, c:c+ks].flatten()
|
| 326 |
+
if use_bias: p = np.append(p, 1.0)
|
| 327 |
+
patches.append(p)
|
| 328 |
+
targets.append(int(out_g[r, c]))
|
| 329 |
+
|
| 330 |
+
n_patches = len(patches)
|
| 331 |
+
if feat > 5000 and n_patches > 2000: return None
|
| 332 |
+
|
| 333 |
+
P = np.array(patches, dtype=np.float64)
|
| 334 |
+
T = np.array(targets, dtype=np.int64)
|
| 335 |
+
T_oh = np.zeros((len(T), 10), dtype=np.float64)
|
| 336 |
+
for i, t in enumerate(T): T_oh[i, t] = 1.0
|
| 337 |
+
|
| 338 |
+
WT = np.linalg.lstsq(P, T_oh, rcond=None)[0]
|
| 339 |
+
if not np.array_equal(np.argmax(P @ WT, axis=1), T): return None
|
| 340 |
+
|
| 341 |
+
if use_bias:
|
| 342 |
+
Wconv = WT[:-1].T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 343 |
+
B = WT[-1].astype(np.float32)
|
| 344 |
+
else:
|
| 345 |
+
Wconv = WT.T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 346 |
+
B = None
|
| 347 |
+
return Wconv, B
|
| 348 |
+
|
| 349 |
+
def solve_conv_fixed(td, path, time_budget=30.0):
|
| 350 |
+
"""Fixed-shape conv: Slice -> Conv -> ArgMax -> OneHot -> Pad."""
|
| 351 |
exs = get_exs(td)
|
| 352 |
for inp, out in exs:
|
| 353 |
if inp.shape != out.shape: return None
|
| 354 |
shapes = set(inp.shape for inp, _ in exs)
|
| 355 |
if len(shapes) != 1: return None
|
| 356 |
IH, IW = shapes.pop()
|
| 357 |
+
|
| 358 |
t_start = time.time()
|
| 359 |
+
for use_bias in [False, True]:
|
| 360 |
for ks in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]:
|
| 361 |
if time.time() - t_start > time_budget: return None
|
| 362 |
+
result = _lstsq_conv(exs, ks, use_bias, use_full_30=False)
|
| 363 |
+
if result is None: continue
|
| 364 |
+
Wconv, B = result
|
| 365 |
pad = ks // 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
pad_h, pad_w = GH - IH, GW - IW
|
| 367 |
inits = [
|
| 368 |
numpy_helper.from_array(np.array([0,0,0,0], dtype=np.int64), 'sl_st'),
|
|
|
|
| 387 |
if validate(path, td): return model
|
| 388 |
return None
|
| 389 |
|
| 390 |
+
# ============================================================
|
| 391 |
+
# CONV SOLVER (variable shape) - Conv(30x30) -> ArgMax -> OneHot -> Mul(mask)
|
| 392 |
+
# ============================================================
|
| 393 |
+
|
| 394 |
+
def solve_conv_variable(td, path, time_budget=30.0):
|
| 395 |
+
"""Variable-shape conv: works on full 30x30 one-hot, dynamic mask from input."""
|
| 396 |
+
exs = get_exs(td)
|
| 397 |
+
for inp, out in exs:
|
| 398 |
+
if inp.shape != out.shape: return None
|
| 399 |
+
|
| 400 |
+
t_start = time.time()
|
| 401 |
+
for use_bias in [False, True]:
|
| 402 |
+
for ks in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]:
|
| 403 |
+
if time.time() - t_start > time_budget: return None
|
| 404 |
+
result = _lstsq_conv(exs, ks, use_bias, use_full_30=True)
|
| 405 |
+
if result is None: continue
|
| 406 |
+
Wconv, B = result
|
| 407 |
+
pad = ks // 2
|
| 408 |
+
inits = [
|
| 409 |
+
numpy_helper.from_array(Wconv, 'W'),
|
| 410 |
+
numpy_helper.from_array(np.array(10, dtype=np.int64), 'depth'),
|
| 411 |
+
numpy_helper.from_array(np.array([0.0, 1.0], dtype=np.float32), 'ohvals'),
|
| 412 |
+
]
|
| 413 |
+
conv_inputs = ['input', 'W']
|
| 414 |
+
if B is not None:
|
| 415 |
+
inits.append(numpy_helper.from_array(B, 'B'))
|
| 416 |
+
conv_inputs.append('B')
|
| 417 |
+
nodes = [
|
| 418 |
+
helper.make_node('ReduceSum', ['input'], ['mask'], axes=[1], keepdims=1),
|
| 419 |
+
helper.make_node('Conv', conv_inputs, ['co'], kernel_shape=[ks,ks], pads=[pad]*4),
|
| 420 |
+
helper.make_node('ArgMax', ['co'], ['am'], axis=1, keepdims=0),
|
| 421 |
+
helper.make_node('OneHot', ['am', 'depth', 'ohvals'], ['oh_out'], axis=1),
|
| 422 |
+
helper.make_node('Mul', ['oh_out', 'mask'], ['output']),
|
| 423 |
+
]
|
| 424 |
+
model = mk(nodes, inits)
|
| 425 |
+
onnx.save(model, path)
|
| 426 |
+
if validate(path, td): return model
|
| 427 |
+
return None
|
| 428 |
+
|
| 429 |
+
# ============================================================
|
| 430 |
+
# CONV SOLVER (diff shape, fixed) - output smaller than input
|
| 431 |
+
# ============================================================
|
| 432 |
+
|
| 433 |
+
def solve_conv_diffshape(td, path, time_budget=30.0):
|
| 434 |
+
"""Diff-shape conv for fixed io shapes where output is smaller."""
|
| 435 |
+
sp = fixed_shapes(td)
|
| 436 |
+
if sp is None: return None
|
| 437 |
+
(IH, IW), (OH, OW) = sp
|
| 438 |
+
if IH == OH and IW == OW: return None
|
| 439 |
+
if OH > IH or OW > IW: return None
|
| 440 |
+
if OH > 30 or OW > 30: return None
|
| 441 |
+
|
| 442 |
+
exs = get_exs(td)
|
| 443 |
+
t_start = time.time()
|
| 444 |
+
|
| 445 |
+
for dr_off, dc_off in [(0, 0), ((IH-OH)//2, (IW-OW)//2)]:
|
| 446 |
+
for use_bias in [False, True]:
|
| 447 |
+
for ks in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]:
|
| 448 |
+
if time.time() - t_start > time_budget: return None
|
| 449 |
+
pad = ks // 2
|
| 450 |
+
feat = 10 * ks * ks + (1 if use_bias else 0)
|
| 451 |
+
if feat > 10000: continue
|
| 452 |
+
|
| 453 |
+
patches, targets = [], []
|
| 454 |
+
valid = True
|
| 455 |
+
for inp_g, out_g in exs:
|
| 456 |
+
oh_enc = np.zeros((10, IH, IW), dtype=np.float64)
|
| 457 |
+
for c in range(10): oh_enc[c] = (inp_g == c)
|
| 458 |
+
oh_pad = np.pad(oh_enc, ((0,0),(pad,pad),(pad,pad)))
|
| 459 |
+
for r in range(OH):
|
| 460 |
+
for c in range(OW):
|
| 461 |
+
sr, sc = r + dr_off, c + dc_off
|
| 462 |
+
if sr < 0 or sr >= IH or sc < 0 or sc >= IW:
|
| 463 |
+
valid = False; break
|
| 464 |
+
p = oh_pad[:, sr:sr+ks, sc:sc+ks].flatten()
|
| 465 |
+
if use_bias: p = np.append(p, 1.0)
|
| 466 |
+
patches.append(p)
|
| 467 |
+
targets.append(int(out_g[r, c]))
|
| 468 |
+
if not valid: break
|
| 469 |
+
if not valid: break
|
| 470 |
+
if not valid: continue
|
| 471 |
+
|
| 472 |
+
n_patches = len(patches)
|
| 473 |
+
if feat > 5000 and n_patches > 2000: continue
|
| 474 |
+
|
| 475 |
+
P = np.array(patches, dtype=np.float64)
|
| 476 |
+
T = np.array(targets, dtype=np.int64)
|
| 477 |
+
T_oh = np.zeros((len(T), 10), dtype=np.float64)
|
| 478 |
+
for i, t in enumerate(T): T_oh[i, t] = 1.0
|
| 479 |
+
|
| 480 |
+
WT = np.linalg.lstsq(P, T_oh, rcond=None)[0]
|
| 481 |
+
if not np.array_equal(np.argmax(P @ WT, axis=1), T): continue
|
| 482 |
+
|
| 483 |
+
if use_bias:
|
| 484 |
+
Wconv = WT[:-1].T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 485 |
+
B = WT[-1].astype(np.float32)
|
| 486 |
+
else:
|
| 487 |
+
Wconv = WT.T.reshape(10, 10, ks, ks).astype(np.float32)
|
| 488 |
+
B = None
|
| 489 |
+
|
| 490 |
+
pad_h, pad_w = GH - OH, GW - OW
|
| 491 |
+
inits = [
|
| 492 |
+
numpy_helper.from_array(np.array([0,0,0,0], dtype=np.int64), 'sl_st'),
|
| 493 |
+
numpy_helper.from_array(np.array([1,10,IH,IW], dtype=np.int64), 'sl_en'),
|
| 494 |
+
numpy_helper.from_array(Wconv, 'W'),
|
| 495 |
+
numpy_helper.from_array(np.array(10, dtype=np.int64), 'depth'),
|
| 496 |
+
numpy_helper.from_array(np.array([0.0, 1.0], dtype=np.float32), 'ohvals'),
|
| 497 |
+
numpy_helper.from_array(np.array([0,0,dr_off,dc_off], dtype=np.int64), 'cr_st'),
|
| 498 |
+
numpy_helper.from_array(np.array([1,10,dr_off+OH,dc_off+OW], dtype=np.int64), 'cr_en'),
|
| 499 |
+
]
|
| 500 |
+
conv_inputs = ['grid', 'W']
|
| 501 |
+
if B is not None:
|
| 502 |
+
inits.append(numpy_helper.from_array(B, 'B'))
|
| 503 |
+
conv_inputs.append('B')
|
| 504 |
+
|
| 505 |
+
nodes = [
|
| 506 |
+
helper.make_node('Slice', ['input','sl_st','sl_en'], ['grid']),
|
| 507 |
+
helper.make_node('Conv', conv_inputs, ['co'], kernel_shape=[ks,ks], pads=[pad]*4),
|
| 508 |
+
helper.make_node('Slice', ['co','cr_st','cr_en'], ['co_crop']),
|
| 509 |
+
helper.make_node('ArgMax', ['co_crop'], ['am'], axis=1, keepdims=0),
|
| 510 |
+
helper.make_node('OneHot', ['am','depth','ohvals'], ['oh_out'], axis=1),
|
| 511 |
+
helper.make_node('Pad', ['oh_out'], ['output'], pads=[0,0,0,0,0,0,pad_h,pad_w], value=0.0),
|
| 512 |
+
]
|
| 513 |
+
model = mk(nodes, inits)
|
| 514 |
+
onnx.save(model, path)
|
| 515 |
+
if validate(path, td): return model
|
| 516 |
+
return None
|
| 517 |
+
|
| 518 |
# ============================================================
|
| 519 |
# GATHER HELPERS
|
| 520 |
# ============================================================
|
|
|
|
| 585 |
def solve_task(tn, td, outdir, conv_budget=30.0):
|
| 586 |
os.makedirs(outdir, exist_ok=True)
|
| 587 |
path = os.path.join(outdir, f"task{tn:03d}.onnx")
|
| 588 |
+
|
| 589 |
+
# 1. Try analytical solvers (fast, tiny models)
|
| 590 |
for sname, sfn in ANALYTICAL_SOLVERS:
|
| 591 |
try:
|
| 592 |
model = sfn(td)
|
|
|
|
| 594 |
onnx.save(model, path)
|
| 595 |
if validate(path, td): return True, sname, os.path.getsize(path)
|
| 596 |
except: pass
|
| 597 |
+
|
| 598 |
+
# 2. Determine task shape category
|
| 599 |
+
exs = get_exs(td)
|
| 600 |
+
same_shape = all(inp.shape == out.shape for inp, out in exs)
|
| 601 |
+
shapes = set(inp.shape for inp, _ in exs)
|
| 602 |
+
fixed_in = len(shapes) == 1
|
| 603 |
+
|
| 604 |
+
if same_shape:
|
| 605 |
+
if fixed_in:
|
| 606 |
+
# Fixed same-shape: use original conv (Slice->Conv->Pad)
|
| 607 |
+
model = solve_conv_fixed(td, path, time_budget=conv_budget)
|
| 608 |
+
if model is not None: return True, 'conv_fixed', os.path.getsize(path)
|
| 609 |
+
# Always try variable-shape conv for same-shape tasks
|
| 610 |
+
model = solve_conv_variable(td, path, time_budget=conv_budget)
|
| 611 |
+
if model is not None: return True, 'conv_var', os.path.getsize(path)
|
| 612 |
+
else:
|
| 613 |
+
# Different shapes
|
| 614 |
+
sp = fixed_shapes(td)
|
| 615 |
+
if sp is not None:
|
| 616 |
+
(IH,IW),(OH,OW) = sp
|
| 617 |
+
if OH <= IH and OW <= IW:
|
| 618 |
+
# Output smaller: try diff-shape conv
|
| 619 |
+
model = solve_conv_diffshape(td, path, time_budget=conv_budget)
|
| 620 |
+
if model is not None: return True, 'conv_diff', os.path.getsize(path)
|
| 621 |
+
|
| 622 |
return False, None, None
|
| 623 |
|
| 624 |
def main():
|