Update neurogolf_utils.py to May 14 2026 version (MACs removed from scoring, ORT profiler memory, sanitize_model)
Browse files- own-solver/neurogolf_utils.py +225 -25
own-solver/neurogolf_utils.py
CHANGED
|
@@ -12,7 +12,68 @@
|
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
|
| 15 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
import itertools
|
| 18 |
import json
|
|
@@ -48,7 +109,7 @@ _COLORS = [
|
|
| 48 |
(146, 117, 86)
|
| 49 |
]
|
| 50 |
_DATA_TYPE = onnx.TensorProto.FLOAT
|
| 51 |
-
_EXCLUDED_OP_TYPES = ["LOOP", "SCAN", "NONZERO", "UNIQUE", "SCRIPT", "FUNCTION"]
|
| 52 |
_FILESIZE_LIMIT_IN_BYTES = 1.44 * 1024 * 1024
|
| 53 |
_GRID_SHAPE = [_BATCH_SIZE, _CHANNELS, _HEIGHT, _WIDTH]
|
| 54 |
_IR_VERSION, _OPSET_IMPORTS = 10, [onnx.helper.make_opsetid("", 10)]
|
|
@@ -134,6 +195,81 @@ _TASK_ZERO = {
|
|
| 134 |
}
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def check_network(filename):
|
| 138 |
file_path = pathlib.Path(filename)
|
| 139 |
if not file_path.is_file():
|
|
@@ -174,23 +310,78 @@ def convert_from_numpy(benchmark):
|
|
| 174 |
return example
|
| 175 |
|
| 176 |
|
| 177 |
-
def
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
|
| 196 |
def load_examples(task_num):
|
|
@@ -298,7 +489,14 @@ def verify_network(network, task_num, examples):
|
|
| 298 |
onnx.save(network, filename)
|
| 299 |
if not check_network(filename): return
|
| 300 |
try:
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
except onnxruntime.ONNXRuntimeError as e:
|
| 303 |
print(f"Error: Unable to load ONNX model: {e}")
|
| 304 |
return
|
|
@@ -307,17 +505,19 @@ def verify_network(network, task_num, examples):
|
|
| 307 |
print(f"Results on ARC-AGI examples: {arc_agi_right} pass, {arc_agi_wrong} fail")
|
| 308 |
print(f"Results on ARC-GEN examples: {arc_gen_right} pass, {arc_gen_wrong} fail")
|
| 309 |
print()
|
| 310 |
-
|
| 311 |
-
if
|
|
|
|
|
|
|
| 312 |
print("Error: Your network performance could not be measured")
|
| 313 |
elif arc_agi_wrong + arc_gen_wrong == 0:
|
| 314 |
print("Your network IS READY for submission!")
|
| 315 |
print()
|
| 316 |
-
print("Performance stats:")
|
| 317 |
onnx_tool.model_profile(filename)
|
| 318 |
-
points = max(1.0, 25.0 - math.log(
|
| 319 |
print()
|
| 320 |
-
print(f"It appears to require {
|
| 321 |
print()
|
| 322 |
print("Next steps:")
|
| 323 |
print(f" * Click the link below to download {filename} onto your local machine.")
|
|
|
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
|
| 15 |
+
"""
|
| 16 |
+
Module containing utilities for the IJCAI-ECAI 2026 NeuroGolf Championship.
|
| 17 |
+
|
| 18 |
+
Version History:
|
| 19 |
+
* 2026-05-14:
|
| 20 |
+
* Puts back the _EXCLUDED_OP_TYPES check (thank you @cdeotte and @pavelsavchenkov!)
|
| 21 |
+
* Applies stronger sanitization to tensor & node names (thank you @linkinpony!)
|
| 22 |
+
* Rejects duplicate graph.value_info entries with the same tensor name (thank you @pavelsavchenkov!)
|
| 23 |
+
* 2026-05-06:
|
| 24 |
+
* Scalar parameters are now penalized with unit cost.
|
| 25 |
+
* Each tensor's memory footprint is set to the maximum size across all runs.
|
| 26 |
+
* Duplicate node names no longer create parameter undercount.
|
| 27 |
+
* Tensor names containing ONNX's special "kernel_time" string are disallowed.
|
| 28 |
+
* Runtime trace file prefixes are specified to prevent profile clobbering.
|
| 29 |
+
* Multi-input / multi-output graphs disallowed.
|
| 30 |
+
* 2026-05-04:
|
| 31 |
+
* Sequences and nonpositive tensor dimensions are disallowed.
|
| 32 |
+
* Accurate shape information derived from the ONNX Runtime Profiler.
|
| 33 |
+
* MACs no longer contribute to the objective criterion.
|
| 34 |
+
* 2026-05-04:
|
| 35 |
+
* Sequences and nonpositive tensor dimensions are disallowed.
|
| 36 |
+
* Accurate shape information derived from the ONNX Runtime Profiler.
|
| 37 |
+
* MACs no longer contribute to the objective criterion.
|
| 38 |
+
* 2026-04-30:
|
| 39 |
+
* Compress operators have been banned.
|
| 40 |
+
* Name collision between tensors and initializers are disallowed.
|
| 41 |
+
* Functions / custom domains / subgraphs are disallowed.
|
| 42 |
+
* Zero-cost networks now yield a full 25 points.
|
| 43 |
+
* 2026-04-28:
|
| 44 |
+
* Constant folding enabled to address the undercounting of parameters.
|
| 45 |
+
* Our "statically-defined shapes" constaint is now strictly enforced.
|
| 46 |
+
* Memory footprint calculation is now a sum of static shape sizes.
|
| 47 |
+
* Nodes with negative parameter counts or MACs are disallowed.
|
| 48 |
+
* 2026-04-21:
|
| 49 |
+
* Tests with grids larger than 30x30 are ignored.
|
| 50 |
+
* Nodes with negative memory values are disallowed.
|
| 51 |
+
* 2026-04-15:
|
| 52 |
+
* Initial version.
|
| 53 |
+
|
| 54 |
+
Contributors from the Kaggle Community:
|
| 55 |
+
* @anglolodorf
|
| 56 |
+
* @arc144
|
| 57 |
+
* @asalhi
|
| 58 |
+
* @calibrator
|
| 59 |
+
* @cdeotte
|
| 60 |
+
* @hengck23
|
| 61 |
+
* @jazivxt
|
| 62 |
+
* @jiweiliu
|
| 63 |
+
* @kameronkilchrist
|
| 64 |
+
* @kevinyuluo
|
| 65 |
+
* @kosirowada
|
| 66 |
+
* @linkinpony
|
| 67 |
+
* @maxjeblick
|
| 68 |
+
* @mukundan314
|
| 69 |
+
* @pavelsavchenkov
|
| 70 |
+
* @prokaj
|
| 71 |
+
* @robga
|
| 72 |
+
* @shinh0
|
| 73 |
+
* @tonylica
|
| 74 |
+
* @yeoyunsianggeremie
|
| 75 |
+
* @yiheng
|
| 76 |
+
"""
|
| 77 |
|
| 78 |
import itertools
|
| 79 |
import json
|
|
|
|
| 109 |
(146, 117, 86)
|
| 110 |
]
|
| 111 |
_DATA_TYPE = onnx.TensorProto.FLOAT
|
| 112 |
+
_EXCLUDED_OP_TYPES = ["LOOP", "SCAN", "NONZERO", "UNIQUE", "SCRIPT", "FUNCTION", "COMPRESS"]
|
| 113 |
_FILESIZE_LIMIT_IN_BYTES = 1.44 * 1024 * 1024
|
| 114 |
_GRID_SHAPE = [_BATCH_SIZE, _CHANNELS, _HEIGHT, _WIDTH]
|
| 115 |
_IR_VERSION, _OPSET_IMPORTS = 10, [onnx.helper.make_opsetid("", 10)]
|
|
|
|
| 195 |
}
|
| 196 |
|
| 197 |
|
| 198 |
+
def calculate_memory(model, trace_path):
|
| 199 |
+
onnx.checker.check_model(model, full_check=True)
|
| 200 |
+
graph = onnx.shape_inference.infer_shapes(model, strict_mode=True).graph
|
| 201 |
+
if len(graph.input) > 1 or len(graph.output) > 1: return None
|
| 202 |
+
init_names = {init.name for init in graph.initializer}
|
| 203 |
+
init_names.update(init.name for init in graph.sparse_initializer)
|
| 204 |
+
io_names = {t.name for t in list(graph.input) + list(graph.output)}
|
| 205 |
+
if io_names.intersection(init_names): return None
|
| 206 |
+
if model.functions: return None
|
| 207 |
+
for opset in model.opset_import:
|
| 208 |
+
if opset.domain not in {"", "ai.onnx"}: return None
|
| 209 |
+
node_outputs = {}
|
| 210 |
+
tensor_names = set()
|
| 211 |
+
for node in graph.node:
|
| 212 |
+
for attr in node.attribute:
|
| 213 |
+
if attr.type in [onnx.AttributeProto.GRAPH,
|
| 214 |
+
onnx.AttributeProto.GRAPHS]:
|
| 215 |
+
return None
|
| 216 |
+
node_outputs[node.name] = list(node.output)
|
| 217 |
+
for output_name in node.output:
|
| 218 |
+
if output_name: tensor_names.add(output_name)
|
| 219 |
+
tensor_memory = {}
|
| 220 |
+
tensor_dtypes = {}
|
| 221 |
+
tensor_map = {
|
| 222 |
+
t.name: t for t in list(graph.input) + list(graph.value_info) + list(graph.output)
|
| 223 |
+
}
|
| 224 |
+
tensor_names.update(tensor_map.keys())
|
| 225 |
+
for tensor_name in tensor_names:
|
| 226 |
+
item = tensor_map.get(tensor_name)
|
| 227 |
+
if not item: return None
|
| 228 |
+
if item.type.HasField("sequence_type"): return None
|
| 229 |
+
if not item.type.HasField("tensor_type"): continue
|
| 230 |
+
tensor_type = item.type.tensor_type
|
| 231 |
+
if not tensor_type.HasField("shape"): return None
|
| 232 |
+
num_elements = 1
|
| 233 |
+
for dim in tensor_type.shape.dim:
|
| 234 |
+
if dim.HasField("dim_param"): return None
|
| 235 |
+
if not dim.HasField("dim_value"): return None
|
| 236 |
+
if dim.dim_value <= 0: return None
|
| 237 |
+
num_elements *= dim.dim_value
|
| 238 |
+
if tensor_name in ['input', 'output']: continue
|
| 239 |
+
np_dtype = onnx.helper.tensor_dtype_to_np_dtype(tensor_type.elem_type)
|
| 240 |
+
tensor_memory[tensor_name] = num_elements * np.dtype(np_dtype).itemsize
|
| 241 |
+
tensor_dtypes[tensor_name] = np_dtype
|
| 242 |
+
|
| 243 |
+
# Defensive check to verify uniqueness.
|
| 244 |
+
seen = set()
|
| 245 |
+
for item in list(graph.input) + list(graph.value_info) + list(graph.output):
|
| 246 |
+
if item.name in seen: return None
|
| 247 |
+
seen.add(item.name)
|
| 248 |
+
for node in graph.node:
|
| 249 |
+
for output_name in node.output:
|
| 250 |
+
if output_name and output_name != "output":
|
| 251 |
+
item = tensor_map.get(output_name)
|
| 252 |
+
if item is None or not item.type.HasField("tensor_type"):
|
| 253 |
+
return None
|
| 254 |
+
|
| 255 |
+
# Retrieve actual tensor shapes via the ONNX Runtime Profiler's JSON Trace.
|
| 256 |
+
with open(trace_path, 'r') as f:
|
| 257 |
+
trace_data = json.load(f)
|
| 258 |
+
for event in trace_data:
|
| 259 |
+
if event.get("cat") != "Node" or "args" not in event: continue
|
| 260 |
+
if "output_type_shape" not in event["args"]: continue
|
| 261 |
+
node_name = event.get("name").replace("_kernel_time", "")
|
| 262 |
+
if node_name not in node_outputs: continue
|
| 263 |
+
for i, shape_dict in enumerate(event["args"]["output_type_shape"]):
|
| 264 |
+
if i >= len(node_outputs[node_name]): continue
|
| 265 |
+
output_name = node_outputs[node_name][i]
|
| 266 |
+
if output_name not in tensor_dtypes: continue
|
| 267 |
+
itemsize = np.dtype(tensor_dtypes[output_name]).itemsize
|
| 268 |
+
mem = itemsize * sum(math.prod(dims) for dims in shape_dict.values())
|
| 269 |
+
tensor_memory[output_name] = max(tensor_memory[output_name], mem)
|
| 270 |
+
return sum(tensor_memory.values())
|
| 271 |
+
|
| 272 |
+
|
| 273 |
def check_network(filename):
|
| 274 |
file_path = pathlib.Path(filename)
|
| 275 |
if not file_path.is_file():
|
|
|
|
| 310 |
return example
|
| 311 |
|
| 312 |
|
| 313 |
+
def calculate_params(model):
|
| 314 |
+
params = 0
|
| 315 |
+
for init in model.graph.initializer:
|
| 316 |
+
if any(d <= 0 for d in init.dims): return None
|
| 317 |
+
params += math.prod(init.dims)
|
| 318 |
+
for sparse_init in model.graph.sparse_initializer:
|
| 319 |
+
if any(d <= 0 for d in sparse_init.values.dims): return None
|
| 320 |
+
params += math.prod(sparse_init.values.dims)
|
| 321 |
+
for node in model.graph.node:
|
| 322 |
+
if node.op_type != 'Constant': continue
|
| 323 |
+
for attr in node.attribute:
|
| 324 |
+
if attr.name == 'value':
|
| 325 |
+
if any(d <= 0 for d in attr.t.dims): return None
|
| 326 |
+
params += math.prod(attr.t.dims)
|
| 327 |
+
elif attr.name == 'sparse_value':
|
| 328 |
+
if any(d <= 0 for d in attr.sparse_tensor.values.dims): return None
|
| 329 |
+
params += math.prod(attr.sparse_tensor.values.dims)
|
| 330 |
+
elif attr.name == 'value_floats':
|
| 331 |
+
params += len(attr.floats)
|
| 332 |
+
elif attr.name == 'value_ints':
|
| 333 |
+
params += len(attr.ints)
|
| 334 |
+
elif attr.name == 'value_strings':
|
| 335 |
+
params += len(attr.strings)
|
| 336 |
+
return params
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
def score_network(sanitized, trace_path):
|
| 340 |
+
for node in sanitized.graph.node:
|
| 341 |
+
if node.op_type.upper() in _EXCLUDED_OP_TYPES:
|
| 342 |
+
print(f"Error: Op type {node.op_type} is not permitted.")
|
| 343 |
+
return None, None
|
| 344 |
+
if "Sequence" in node.op_type:
|
| 345 |
+
print(f"Error: Op type {node.op_type} is not permitted.")
|
| 346 |
+
return None, None
|
| 347 |
+
return calculate_memory(sanitized, trace_path), calculate_params(sanitized)
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
def sanitize_model(model):
|
| 351 |
+
for node in model.graph.node:
|
| 352 |
+
node.name = node.output[0]
|
| 353 |
+
if "kernel_time" in node.output[0]: return None
|
| 354 |
+
|
| 355 |
+
name_map, counter = {}, 0
|
| 356 |
+
|
| 357 |
+
def get_safe_name(old_name):
|
| 358 |
+
nonlocal counter
|
| 359 |
+
if not old_name or old_name in ["input", "output"]: return old_name
|
| 360 |
+
if old_name not in name_map:
|
| 361 |
+
name_map[old_name] = f"safe_name_{counter}"
|
| 362 |
+
counter += 1
|
| 363 |
+
return name_map[old_name]
|
| 364 |
+
|
| 365 |
+
for inp in model.graph.input:
|
| 366 |
+
inp.name = get_safe_name(inp.name)
|
| 367 |
+
for init in model.graph.initializer:
|
| 368 |
+
init.name = get_safe_name(init.name)
|
| 369 |
+
|
| 370 |
+
for node in model.graph.node:
|
| 371 |
+
for i in range(len(node.input)):
|
| 372 |
+
node.input[i] = get_safe_name(node.input[i])
|
| 373 |
+
for i in range(len(node.output)):
|
| 374 |
+
node.output[i] = get_safe_name(node.output[i])
|
| 375 |
+
if len(node.output) > 0 and node.output[0]:
|
| 376 |
+
node.name = node.output[0]
|
| 377 |
+
|
| 378 |
+
for out in model.graph.output:
|
| 379 |
+
out.name = get_safe_name(out.name)
|
| 380 |
+
for vi in model.graph.value_info:
|
| 381 |
+
vi.name = get_safe_name(vi.name)
|
| 382 |
+
for node in model.graph.node:
|
| 383 |
+
node.name = node.output[0]
|
| 384 |
+
return model
|
| 385 |
|
| 386 |
|
| 387 |
def load_examples(task_num):
|
|
|
|
| 489 |
onnx.save(network, filename)
|
| 490 |
if not check_network(filename): return
|
| 491 |
try:
|
| 492 |
+
# Load the model, sanitize node names, and enable profiling.
|
| 493 |
+
sanitized = sanitize_model(onnx.load(filename))
|
| 494 |
+
if not sanitized: return
|
| 495 |
+
options = onnxruntime.SessionOptions()
|
| 496 |
+
options.enable_profiling = True
|
| 497 |
+
options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_DISABLE_ALL
|
| 498 |
+
options.profile_file_prefix = f"{task_num:03}"
|
| 499 |
+
session = onnxruntime.InferenceSession(sanitized.SerializeToString(), options)
|
| 500 |
except onnxruntime.ONNXRuntimeError as e:
|
| 501 |
print(f"Error: Unable to load ONNX model: {e}")
|
| 502 |
return
|
|
|
|
| 505 |
print(f"Results on ARC-AGI examples: {arc_agi_right} pass, {arc_agi_wrong} fail")
|
| 506 |
print(f"Results on ARC-GEN examples: {arc_gen_right} pass, {arc_gen_wrong} fail")
|
| 507 |
print()
|
| 508 |
+
memory, params = score_network(sanitized, session.end_profiling())
|
| 509 |
+
if memory is None or params is None:
|
| 510 |
+
print("Error: Your network performance could not be measured")
|
| 511 |
+
if memory < 0 or params < 0:
|
| 512 |
print("Error: Your network performance could not be measured")
|
| 513 |
elif arc_agi_wrong + arc_gen_wrong == 0:
|
| 514 |
print("Your network IS READY for submission!")
|
| 515 |
print()
|
| 516 |
+
print("Performance stats (memory values reported here are approximate):")
|
| 517 |
onnx_tool.model_profile(filename)
|
| 518 |
+
points = max(1.0, 25.0 - math.log(max(1.0, memory + params)))
|
| 519 |
print()
|
| 520 |
+
print(f"It appears to require {memory} bytes + {params} params, yielding {points:.3f} points.")
|
| 521 |
print()
|
| 522 |
print("Next steps:")
|
| 523 |
print(f" * Click the link below to download {filename} onto your local machine.")
|