Move own-solver/neurogolf_solver/profiler.py to own-solver/
Browse files
own-solver/neurogolf_solver/profiler.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Static profiling for ONNX models.
|
| 3 |
+
|
| 4 |
+
Uses neurogolf_utils.score_network() (onnx_tool) when available — this is
|
| 5 |
+
the ONLY scoring that matches Kaggle. The static fallback is approximate
|
| 6 |
+
and prints a WARNING. If onnx_tool returns (None, None, None), the model
|
| 7 |
+
is REJECTED — do not submit it.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import onnx
|
| 11 |
+
from onnx import numpy_helper
|
| 12 |
+
from .constants import BANNED_OPS, GH, GW
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
from neurogolf_utils import score_network as _score_network_official
|
| 16 |
+
HAS_ONNX_TOOL = True
|
| 17 |
+
except ImportError:
|
| 18 |
+
HAS_ONNX_TOOL = False
|
| 19 |
+
|
| 20 |
+
_WARNED_NO_ONNX_TOOL = False
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def score_network(path):
|
| 24 |
+
"""Score network. Returns (macs, memory, params) or (None, None, None).
|
| 25 |
+
|
| 26 |
+
If onnx_tool is available: uses official scorer. (None,None,None) = REJECTED.
|
| 27 |
+
If onnx_tool is NOT available: uses static fallback with WARNING.
|
| 28 |
+
"""
|
| 29 |
+
global _WARNED_NO_ONNX_TOOL
|
| 30 |
+
if HAS_ONNX_TOOL:
|
| 31 |
+
# Official scorer — trust its result. Do NOT catch exceptions silently.
|
| 32 |
+
try:
|
| 33 |
+
result = _score_network_official(path)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"WARNING: onnx_tool score_network failed on {path}: {e}")
|
| 36 |
+
return None, None, None
|
| 37 |
+
return result
|
| 38 |
+
else:
|
| 39 |
+
if not _WARNED_NO_ONNX_TOOL:
|
| 40 |
+
print("WARNING: onnx_tool not installed. Scores are APPROXIMATE and may not match Kaggle.")
|
| 41 |
+
print("WARNING: Models that fail onnx_tool profiling will be REJECTED on Kaggle.")
|
| 42 |
+
print("WARNING: Run neurogolf_utils.verify_network() in a Kaggle notebook before submitting.")
|
| 43 |
+
_WARNED_NO_ONNX_TOOL = True
|
| 44 |
+
return _static_profile(path)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _static_profile(path):
|
| 48 |
+
"""Static profiling fallback. APPROXIMATE — does not match Kaggle scoring.
|
| 49 |
+
Only used when onnx_tool is not installed."""
|
| 50 |
+
try:
|
| 51 |
+
model = onnx.load(path)
|
| 52 |
+
except:
|
| 53 |
+
return None, None, None
|
| 54 |
+
tensors = {}
|
| 55 |
+
params = 0
|
| 56 |
+
nbytes = 0
|
| 57 |
+
macs = 0
|
| 58 |
+
for init in model.graph.initializer:
|
| 59 |
+
a = numpy_helper.to_array(init)
|
| 60 |
+
tensors[init.name] = a
|
| 61 |
+
params += a.size
|
| 62 |
+
nbytes += a.nbytes
|
| 63 |
+
for nd in model.graph.node:
|
| 64 |
+
if nd.op_type == 'Constant':
|
| 65 |
+
for attr in nd.attribute:
|
| 66 |
+
if attr.t and attr.t.ByteSize() > 0:
|
| 67 |
+
try:
|
| 68 |
+
a = numpy_helper.to_array(attr.t)
|
| 69 |
+
if nd.output:
|
| 70 |
+
tensors[nd.output[0]] = a
|
| 71 |
+
params += a.size
|
| 72 |
+
nbytes += a.nbytes
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
+
# Banned op check — UPPERCASE to match Kaggle
|
| 76 |
+
if nd.op_type.upper() in {op.upper() for op in BANNED_OPS}:
|
| 77 |
+
print(f"WARNING: Banned op '{nd.op_type}' found in {path}")
|
| 78 |
+
return None, None, None
|
| 79 |
+
if nd.op_type == 'Conv' and len(nd.input) >= 2 and nd.input[1] in tensors:
|
| 80 |
+
w = tensors[nd.input[1]]
|
| 81 |
+
if w.ndim == 4:
|
| 82 |
+
co, ci, kh, kw = w.shape
|
| 83 |
+
macs += co * ci * kh * kw * GH * GW
|
| 84 |
+
return int(macs), int(nbytes), int(params)
|